parser.c revision 132718
1/* C++ Parser.
2   Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3   Written by Mark Mitchell <mark@codesourcery.com>.
4
5   This file is part of GCC.
6
7   GCC is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2, or (at your option)
10   any later version.
11
12   GCC is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with GCC; see the file COPYING.  If not, write to the Free
19   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20   02111-1307, USA.  */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26#include "dyn-string.h"
27#include "varray.h"
28#include "cpplib.h"
29#include "tree.h"
30#include "cp-tree.h"
31#include "c-pragma.h"
32#include "decl.h"
33#include "flags.h"
34#include "diagnostic.h"
35#include "toplev.h"
36#include "output.h"
37
38
39/* The lexer.  */
40
41/* Overview
42   --------
43
44   A cp_lexer represents a stream of cp_tokens.  It allows arbitrary
45   look-ahead.
46
47   Methodology
48   -----------
49
50   We use a circular buffer to store incoming tokens.
51
52   Some artifacts of the C++ language (such as the
53   expression/declaration ambiguity) require arbitrary look-ahead.
54   The strategy we adopt for dealing with these problems is to attempt
55   to parse one construct (e.g., the declaration) and fall back to the
56   other (e.g., the expression) if that attempt does not succeed.
57   Therefore, we must sometimes store an arbitrary number of tokens.
58
59   The parser routinely peeks at the next token, and then consumes it
60   later.  That also requires a buffer in which to store the tokens.
61
62   In order to easily permit adding tokens to the end of the buffer,
63   while removing them from the beginning of the buffer, we use a
64   circular buffer.  */
65
66/* A C++ token.  */
67
68typedef struct cp_token GTY (())
69{
70  /* The kind of token.  */
71  ENUM_BITFIELD (cpp_ttype) type : 8;
72  /* If this token is a keyword, this value indicates which keyword.
73     Otherwise, this value is RID_MAX.  */
74  ENUM_BITFIELD (rid) keyword : 8;
75  /* Token flags.  */
76  unsigned char flags;
77  /* The value associated with this token, if any.  */
78  tree value;
79  /* The location at which this token was found.  */
80  location_t location;
81} cp_token;
82
83/* The number of tokens in a single token block.
84   Computed so that cp_token_block fits in a 512B allocation unit.  */
85
86#define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
87
88/* A group of tokens.  These groups are chained together to store
89   large numbers of tokens.  (For example, a token block is created
90   when the body of an inline member function is first encountered;
91   the tokens are processed later after the class definition is
92   complete.)
93
94   This somewhat ungainly data structure (as opposed to, say, a
95   variable-length array), is used due to constraints imposed by the
96   current garbage-collection methodology.  If it is made more
97   flexible, we could perhaps simplify the data structures involved.  */
98
99typedef struct cp_token_block GTY (())
100{
101  /* The tokens.  */
102  cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
103  /* The number of tokens in this block.  */
104  size_t num_tokens;
105  /* The next token block in the chain.  */
106  struct cp_token_block *next;
107  /* The previous block in the chain.  */
108  struct cp_token_block *prev;
109} cp_token_block;
110
111typedef struct cp_token_cache GTY (())
112{
113  /* The first block in the cache.  NULL if there are no tokens in the
114     cache.  */
115  cp_token_block *first;
116  /* The last block in the cache.  NULL If there are no tokens in the
117     cache.  */
118  cp_token_block *last;
119} cp_token_cache;
120
121/* Prototypes.  */
122
123static cp_token_cache *cp_token_cache_new
124  (void);
125static void cp_token_cache_push_token
126  (cp_token_cache *, cp_token *);
127
128/* Create a new cp_token_cache.  */
129
130static cp_token_cache *
131cp_token_cache_new (void)
132{
133  return ggc_alloc_cleared (sizeof (cp_token_cache));
134}
135
136/* Add *TOKEN to *CACHE.  */
137
138static void
139cp_token_cache_push_token (cp_token_cache *cache,
140			   cp_token *token)
141{
142  cp_token_block *b = cache->last;
143
144  /* See if we need to allocate a new token block.  */
145  if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
146    {
147      b = ggc_alloc_cleared (sizeof (cp_token_block));
148      b->prev = cache->last;
149      if (cache->last)
150	{
151	  cache->last->next = b;
152	  cache->last = b;
153	}
154      else
155	cache->first = cache->last = b;
156    }
157  /* Add this token to the current token block.  */
158  b->tokens[b->num_tokens++] = *token;
159}
160
161/* The cp_lexer structure represents the C++ lexer.  It is responsible
162   for managing the token stream from the preprocessor and supplying
163   it to the parser.  */
164
165typedef struct cp_lexer GTY (())
166{
167  /* The memory allocated for the buffer.  Never NULL.  */
168  cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
169  /* A pointer just past the end of the memory allocated for the buffer.  */
170  cp_token * GTY ((skip (""))) buffer_end;
171  /* The first valid token in the buffer, or NULL if none.  */
172  cp_token * GTY ((skip (""))) first_token;
173  /* The next available token.  If NEXT_TOKEN is NULL, then there are
174     no more available tokens.  */
175  cp_token * GTY ((skip (""))) next_token;
176  /* A pointer just past the last available token.  If FIRST_TOKEN is
177     NULL, however, there are no available tokens, and then this
178     location is simply the place in which the next token read will be
179     placed.  If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180     When the LAST_TOKEN == BUFFER, then the last token is at the
181     highest memory address in the BUFFER.  */
182  cp_token * GTY ((skip (""))) last_token;
183
184  /* A stack indicating positions at which cp_lexer_save_tokens was
185     called.  The top entry is the most recent position at which we
186     began saving tokens.  The entries are differences in token
187     position between FIRST_TOKEN and the first saved token.
188
189     If the stack is non-empty, we are saving tokens.  When a token is
190     consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191     pointer will not.  The token stream will be preserved so that it
192     can be reexamined later.
193
194     If the stack is empty, then we are not saving tokens.  Whenever a
195     token is consumed, the FIRST_TOKEN pointer will be moved, and the
196     consumed token will be gone forever.  */
197  varray_type saved_tokens;
198
199  /* The STRING_CST tokens encountered while processing the current
200     string literal.  */
201  varray_type string_tokens;
202
203  /* True if we should obtain more tokens from the preprocessor; false
204     if we are processing a saved token cache.  */
205  bool main_lexer_p;
206
207  /* True if we should output debugging information.  */
208  bool debugging_p;
209
210  /* The next lexer in a linked list of lexers.  */
211  struct cp_lexer *next;
212} cp_lexer;
213
214/* Prototypes.  */
215
216static cp_lexer *cp_lexer_new_main
217  (void);
218static cp_lexer *cp_lexer_new_from_tokens
219  (struct cp_token_cache *);
220static int cp_lexer_saving_tokens
221  (const cp_lexer *);
222static cp_token *cp_lexer_next_token
223  (cp_lexer *, cp_token *);
224static cp_token *cp_lexer_prev_token
225  (cp_lexer *, cp_token *);
226static ptrdiff_t cp_lexer_token_difference
227  (cp_lexer *, cp_token *, cp_token *);
228static cp_token *cp_lexer_read_token
229  (cp_lexer *);
230static void cp_lexer_maybe_grow_buffer
231  (cp_lexer *);
232static void cp_lexer_get_preprocessor_token
233  (cp_lexer *, cp_token *);
234static cp_token *cp_lexer_peek_token
235  (cp_lexer *);
236static cp_token *cp_lexer_peek_nth_token
237  (cp_lexer *, size_t);
238static inline bool cp_lexer_next_token_is
239  (cp_lexer *, enum cpp_ttype);
240static bool cp_lexer_next_token_is_not
241  (cp_lexer *, enum cpp_ttype);
242static bool cp_lexer_next_token_is_keyword
243  (cp_lexer *, enum rid);
244static cp_token *cp_lexer_consume_token
245  (cp_lexer *);
246static void cp_lexer_purge_token
247  (cp_lexer *);
248static void cp_lexer_purge_tokens_after
249  (cp_lexer *, cp_token *);
250static void cp_lexer_save_tokens
251  (cp_lexer *);
252static void cp_lexer_commit_tokens
253  (cp_lexer *);
254static void cp_lexer_rollback_tokens
255  (cp_lexer *);
256static inline void cp_lexer_set_source_position_from_token
257  (cp_lexer *, const cp_token *);
258static void cp_lexer_print_token
259  (FILE *, cp_token *);
260static inline bool cp_lexer_debugging_p
261  (cp_lexer *);
262static void cp_lexer_start_debugging
263  (cp_lexer *) ATTRIBUTE_UNUSED;
264static void cp_lexer_stop_debugging
265  (cp_lexer *) ATTRIBUTE_UNUSED;
266
267/* Manifest constants.  */
268
269#define CP_TOKEN_BUFFER_SIZE 5
270#define CP_SAVED_TOKENS_SIZE 5
271
272/* A token type for keywords, as opposed to ordinary identifiers.  */
273#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
274
275/* A token type for template-ids.  If a template-id is processed while
276   parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
277   the value of the CPP_TEMPLATE_ID is whatever was returned by
278   cp_parser_template_id.  */
279#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
280
281/* A token type for nested-name-specifiers.  If a
282   nested-name-specifier is processed while parsing tentatively, it is
283   replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
284   CPP_NESTED_NAME_SPECIFIER is whatever was returned by
285   cp_parser_nested_name_specifier_opt.  */
286#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
287
288/* A token type for tokens that are not tokens at all; these are used
289   to mark the end of a token block.  */
290#define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
291
292/* Variables.  */
293
294/* The stream to which debugging output should be written.  */
295static FILE *cp_lexer_debug_stream;
296
297/* Create a new main C++ lexer, the lexer that gets tokens from the
298   preprocessor.  */
299
300static cp_lexer *
301cp_lexer_new_main (void)
302{
303  cp_lexer *lexer;
304  cp_token first_token;
305
306  /* It's possible that lexing the first token will load a PCH file,
307     which is a GC collection point.  So we have to grab the first
308     token before allocating any memory.  */
309  cp_lexer_get_preprocessor_token (NULL, &first_token);
310  c_common_no_more_pch ();
311
312  /* Allocate the memory.  */
313  lexer = ggc_alloc_cleared (sizeof (cp_lexer));
314
315  /* Create the circular buffer.  */
316  lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
317  lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
318
319  /* There is one token in the buffer.  */
320  lexer->last_token = lexer->buffer + 1;
321  lexer->first_token = lexer->buffer;
322  lexer->next_token = lexer->buffer;
323  memcpy (lexer->buffer, &first_token, sizeof (cp_token));
324
325  /* This lexer obtains more tokens by calling c_lex.  */
326  lexer->main_lexer_p = true;
327
328  /* Create the SAVED_TOKENS stack.  */
329  VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
330
331  /* Create the STRINGS array.  */
332  VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
333
334  /* Assume we are not debugging.  */
335  lexer->debugging_p = false;
336
337  return lexer;
338}
339
340/* Create a new lexer whose token stream is primed with the TOKENS.
341   When these tokens are exhausted, no new tokens will be read.  */
342
343static cp_lexer *
344cp_lexer_new_from_tokens (cp_token_cache *tokens)
345{
346  cp_lexer *lexer;
347  cp_token *token;
348  cp_token_block *block;
349  ptrdiff_t num_tokens;
350
351  /* Allocate the memory.  */
352  lexer = ggc_alloc_cleared (sizeof (cp_lexer));
353
354  /* Create a new buffer, appropriately sized.  */
355  num_tokens = 0;
356  for (block = tokens->first; block != NULL; block = block->next)
357    num_tokens += block->num_tokens;
358  lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
359  lexer->buffer_end = lexer->buffer + num_tokens;
360
361  /* Install the tokens.  */
362  token = lexer->buffer;
363  for (block = tokens->first; block != NULL; block = block->next)
364    {
365      memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
366      token += block->num_tokens;
367    }
368
369  /* The FIRST_TOKEN is the beginning of the buffer.  */
370  lexer->first_token = lexer->buffer;
371  /* The next available token is also at the beginning of the buffer.  */
372  lexer->next_token = lexer->buffer;
373  /* The buffer is full.  */
374  lexer->last_token = lexer->first_token;
375
376  /* This lexer doesn't obtain more tokens.  */
377  lexer->main_lexer_p = false;
378
379  /* Create the SAVED_TOKENS stack.  */
380  VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
381
382  /* Create the STRINGS array.  */
383  VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
384
385  /* Assume we are not debugging.  */
386  lexer->debugging_p = false;
387
388  return lexer;
389}
390
391/* Returns nonzero if debugging information should be output.  */
392
393static inline bool
394cp_lexer_debugging_p (cp_lexer *lexer)
395{
396  return lexer->debugging_p;
397}
398
399/* Set the current source position from the information stored in
400   TOKEN.  */
401
402static inline void
403cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
404                                         const cp_token *token)
405{
406  /* Ideally, the source position information would not be a global
407     variable, but it is.  */
408
409  /* Update the line number.  */
410  if (token->type != CPP_EOF)
411    input_location = token->location;
412}
413
414/* TOKEN points into the circular token buffer.  Return a pointer to
415   the next token in the buffer.  */
416
417static inline cp_token *
418cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
419{
420  token++;
421  if (token == lexer->buffer_end)
422    token = lexer->buffer;
423  return token;
424}
425
426/* TOKEN points into the circular token buffer.  Return a pointer to
427   the previous token in the buffer.  */
428
429static inline cp_token *
430cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
431{
432  if (token == lexer->buffer)
433    token = lexer->buffer_end;
434  return token - 1;
435}
436
437/* nonzero if we are presently saving tokens.  */
438
439static int
440cp_lexer_saving_tokens (const cp_lexer* lexer)
441{
442  return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
443}
444
445/* Return a pointer to the token that is N tokens beyond TOKEN in the
446   buffer.  */
447
448static cp_token *
449cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
450{
451  token += n;
452  if (token >= lexer->buffer_end)
453    token = lexer->buffer + (token - lexer->buffer_end);
454  return token;
455}
456
457/* Returns the number of times that START would have to be incremented
458   to reach FINISH.  If START and FINISH are the same, returns zero.  */
459
460static ptrdiff_t
461cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
462{
463  if (finish >= start)
464    return finish - start;
465  else
466    return ((lexer->buffer_end - lexer->buffer)
467	    - (start - finish));
468}
469
470/* Obtain another token from the C preprocessor and add it to the
471   token buffer.  Returns the newly read token.  */
472
473static cp_token *
474cp_lexer_read_token (cp_lexer* lexer)
475{
476  cp_token *token;
477
478  /* Make sure there is room in the buffer.  */
479  cp_lexer_maybe_grow_buffer (lexer);
480
481  /* If there weren't any tokens, then this one will be the first.  */
482  if (!lexer->first_token)
483    lexer->first_token = lexer->last_token;
484  /* Similarly, if there were no available tokens, there is one now.  */
485  if (!lexer->next_token)
486    lexer->next_token = lexer->last_token;
487
488  /* Figure out where we're going to store the new token.  */
489  token = lexer->last_token;
490
491  /* Get a new token from the preprocessor.  */
492  cp_lexer_get_preprocessor_token (lexer, token);
493
494  /* Increment LAST_TOKEN.  */
495  lexer->last_token = cp_lexer_next_token (lexer, token);
496
497  /* Strings should have type `const char []'.  Right now, we will
498     have an ARRAY_TYPE that is constant rather than an array of
499     constant elements.
500     FIXME: Make fix_string_type get this right in the first place.  */
501  if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
502      && flag_const_strings)
503    {
504      tree type;
505
506      /* Get the current type.  It will be an ARRAY_TYPE.  */
507      type = TREE_TYPE (token->value);
508      /* Use build_cplus_array_type to rebuild the array, thereby
509	 getting the right type.  */
510      type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
511      /* Reset the type of the token.  */
512      TREE_TYPE (token->value) = type;
513    }
514
515  return token;
516}
517
518/* If the circular buffer is full, make it bigger.  */
519
520static void
521cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
522{
523  /* If the buffer is full, enlarge it.  */
524  if (lexer->last_token == lexer->first_token)
525    {
526      cp_token *new_buffer;
527      cp_token *old_buffer;
528      cp_token *new_first_token;
529      ptrdiff_t buffer_length;
530      size_t num_tokens_to_copy;
531
532      /* Remember the current buffer pointer.  It will become invalid,
533	 but we will need to do pointer arithmetic involving this
534	 value.  */
535      old_buffer = lexer->buffer;
536      /* Compute the current buffer size.  */
537      buffer_length = lexer->buffer_end - lexer->buffer;
538      /* Allocate a buffer twice as big.  */
539      new_buffer = ggc_realloc (lexer->buffer,
540				2 * buffer_length * sizeof (cp_token));
541
542      /* Because the buffer is circular, logically consecutive tokens
543	 are not necessarily placed consecutively in memory.
544	 Therefore, we must keep move the tokens that were before
545	 FIRST_TOKEN to the second half of the newly allocated
546	 buffer.  */
547      num_tokens_to_copy = (lexer->first_token - old_buffer);
548      memcpy (new_buffer + buffer_length,
549	      new_buffer,
550	      num_tokens_to_copy * sizeof (cp_token));
551      /* Clear the rest of the buffer.  We never look at this storage,
552	 but the garbage collector may.  */
553      memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
554	      (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
555
556      /* Now recompute all of the buffer pointers.  */
557      new_first_token
558	= new_buffer + (lexer->first_token - old_buffer);
559      if (lexer->next_token != NULL)
560	{
561	  ptrdiff_t next_token_delta;
562
563	  if (lexer->next_token > lexer->first_token)
564	    next_token_delta = lexer->next_token - lexer->first_token;
565	  else
566	    next_token_delta =
567	      buffer_length - (lexer->first_token - lexer->next_token);
568	  lexer->next_token = new_first_token + next_token_delta;
569	}
570      lexer->last_token = new_first_token + buffer_length;
571      lexer->buffer = new_buffer;
572      lexer->buffer_end = new_buffer + buffer_length * 2;
573      lexer->first_token = new_first_token;
574    }
575}
576
577/* Store the next token from the preprocessor in *TOKEN.  */
578
579static void
580cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
581                                 cp_token *token)
582{
583  bool done;
584
585  /* If this not the main lexer, return a terminating CPP_EOF token.  */
586  if (lexer != NULL && !lexer->main_lexer_p)
587    {
588      token->type = CPP_EOF;
589      token->location.line = 0;
590      token->location.file = NULL;
591      token->value = NULL_TREE;
592      token->keyword = RID_MAX;
593
594      return;
595    }
596
597  done = false;
598  /* Keep going until we get a token we like.  */
599  while (!done)
600    {
601      /* Get a new token from the preprocessor.  */
602      token->type = c_lex_with_flags (&token->value, &token->flags);
603      /* Issue messages about tokens we cannot process.  */
604      switch (token->type)
605	{
606	case CPP_ATSIGN:
607	case CPP_HASH:
608	case CPP_PASTE:
609	  error ("invalid token");
610	  break;
611
612	default:
613	  /* This is a good token, so we exit the loop.  */
614	  done = true;
615	  break;
616	}
617    }
618  /* Now we've got our token.  */
619  token->location = input_location;
620
621  /* Check to see if this token is a keyword.  */
622  if (token->type == CPP_NAME
623      && C_IS_RESERVED_WORD (token->value))
624    {
625      /* Mark this token as a keyword.  */
626      token->type = CPP_KEYWORD;
627      /* Record which keyword.  */
628      token->keyword = C_RID_CODE (token->value);
629      /* Update the value.  Some keywords are mapped to particular
630	 entities, rather than simply having the value of the
631	 corresponding IDENTIFIER_NODE.  For example, `__const' is
632	 mapped to `const'.  */
633      token->value = ridpointers[token->keyword];
634    }
635  else
636    token->keyword = RID_MAX;
637}
638
639/* Return a pointer to the next token in the token stream, but do not
640   consume it.  */
641
642static cp_token *
643cp_lexer_peek_token (cp_lexer* lexer)
644{
645  cp_token *token;
646
647  /* If there are no tokens, read one now.  */
648  if (!lexer->next_token)
649    cp_lexer_read_token (lexer);
650
651  /* Provide debugging output.  */
652  if (cp_lexer_debugging_p (lexer))
653    {
654      fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
655      cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
656      fprintf (cp_lexer_debug_stream, "\n");
657    }
658
659  token = lexer->next_token;
660  cp_lexer_set_source_position_from_token (lexer, token);
661  return token;
662}
663
664/* Return true if the next token has the indicated TYPE.  */
665
666static bool
667cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
668{
669  cp_token *token;
670
671  /* Peek at the next token.  */
672  token = cp_lexer_peek_token (lexer);
673  /* Check to see if it has the indicated TYPE.  */
674  return token->type == type;
675}
676
677/* Return true if the next token does not have the indicated TYPE.  */
678
679static bool
680cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
681{
682  return !cp_lexer_next_token_is (lexer, type);
683}
684
685/* Return true if the next token is the indicated KEYWORD.  */
686
687static bool
688cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
689{
690  cp_token *token;
691
692  /* Peek at the next token.  */
693  token = cp_lexer_peek_token (lexer);
694  /* Check to see if it is the indicated keyword.  */
695  return token->keyword == keyword;
696}
697
698/* Return a pointer to the Nth token in the token stream.  If N is 1,
699   then this is precisely equivalent to cp_lexer_peek_token.  */
700
701static cp_token *
702cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
703{
704  cp_token *token;
705
706  /* N is 1-based, not zero-based.  */
707  my_friendly_assert (n > 0, 20000224);
708
709  /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary.  */
710  token = lexer->next_token;
711  /* If there are no tokens in the buffer, get one now.  */
712  if (!token)
713    {
714      cp_lexer_read_token (lexer);
715      token = lexer->next_token;
716    }
717
718  /* Now, read tokens until we have enough.  */
719  while (--n > 0)
720    {
721      /* Advance to the next token.  */
722      token = cp_lexer_next_token (lexer, token);
723      /* If that's all the tokens we have, read a new one.  */
724      if (token == lexer->last_token)
725	token = cp_lexer_read_token (lexer);
726    }
727
728  return token;
729}
730
731/* Consume the next token.  The pointer returned is valid only until
732   another token is read.  Callers should preserve copy the token
733   explicitly if they will need its value for a longer period of
734   time.  */
735
736static cp_token *
737cp_lexer_consume_token (cp_lexer* lexer)
738{
739  cp_token *token;
740
741  /* If there are no tokens, read one now.  */
742  if (!lexer->next_token)
743    cp_lexer_read_token (lexer);
744
745  /* Remember the token we'll be returning.  */
746  token = lexer->next_token;
747
748  /* Increment NEXT_TOKEN.  */
749  lexer->next_token = cp_lexer_next_token (lexer,
750					   lexer->next_token);
751  /* Check to see if we're all out of tokens.  */
752  if (lexer->next_token == lexer->last_token)
753    lexer->next_token = NULL;
754
755  /* If we're not saving tokens, then move FIRST_TOKEN too.  */
756  if (!cp_lexer_saving_tokens (lexer))
757    {
758      /* If there are no tokens available, set FIRST_TOKEN to NULL.  */
759      if (!lexer->next_token)
760	lexer->first_token = NULL;
761      else
762	lexer->first_token = lexer->next_token;
763    }
764
765  /* Provide debugging output.  */
766  if (cp_lexer_debugging_p (lexer))
767    {
768      fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
769      cp_lexer_print_token (cp_lexer_debug_stream, token);
770      fprintf (cp_lexer_debug_stream, "\n");
771    }
772
773  return token;
774}
775
776/* Permanently remove the next token from the token stream.  There
777   must be a valid next token already; this token never reads
778   additional tokens from the preprocessor.  */
779
780static void
781cp_lexer_purge_token (cp_lexer *lexer)
782{
783  cp_token *token;
784  cp_token *next_token;
785
786  token = lexer->next_token;
787  while (true)
788    {
789      next_token = cp_lexer_next_token (lexer, token);
790      if (next_token == lexer->last_token)
791	break;
792      *token = *next_token;
793      token = next_token;
794    }
795
796  lexer->last_token = token;
797  /* The token purged may have been the only token remaining; if so,
798     clear NEXT_TOKEN.  */
799  if (lexer->next_token == token)
800    lexer->next_token = NULL;
801}
802
803/* Permanently remove all tokens after TOKEN, up to, but not
804   including, the token that will be returned next by
805   cp_lexer_peek_token.  */
806
807static void
808cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
809{
810  cp_token *peek;
811  cp_token *t1;
812  cp_token *t2;
813
814  if (lexer->next_token)
815    {
816      /* Copy the tokens that have not yet been read to the location
817	 immediately following TOKEN.  */
818      t1 = cp_lexer_next_token (lexer, token);
819      t2 = peek = cp_lexer_peek_token (lexer);
820      /* Move tokens into the vacant area between TOKEN and PEEK.  */
821      while (t2 != lexer->last_token)
822	{
823	  *t1 = *t2;
824	  t1 = cp_lexer_next_token (lexer, t1);
825	  t2 = cp_lexer_next_token (lexer, t2);
826	}
827      /* Now, the next available token is right after TOKEN.  */
828      lexer->next_token = cp_lexer_next_token (lexer, token);
829      /* And the last token is wherever we ended up.  */
830      lexer->last_token = t1;
831    }
832  else
833    {
834      /* There are no tokens in the buffer, so there is nothing to
835	 copy.  The last token in the buffer is TOKEN itself.  */
836      lexer->last_token = cp_lexer_next_token (lexer, token);
837    }
838}
839
840/* Begin saving tokens.  All tokens consumed after this point will be
841   preserved.  */
842
843static void
844cp_lexer_save_tokens (cp_lexer* lexer)
845{
846  /* Provide debugging output.  */
847  if (cp_lexer_debugging_p (lexer))
848    fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
849
850  /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
851     restore the tokens if required.  */
852  if (!lexer->next_token)
853    cp_lexer_read_token (lexer);
854
855  VARRAY_PUSH_INT (lexer->saved_tokens,
856		   cp_lexer_token_difference (lexer,
857					      lexer->first_token,
858					      lexer->next_token));
859}
860
861/* Commit to the portion of the token stream most recently saved.  */
862
863static void
864cp_lexer_commit_tokens (cp_lexer* lexer)
865{
866  /* Provide debugging output.  */
867  if (cp_lexer_debugging_p (lexer))
868    fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
869
870  VARRAY_POP (lexer->saved_tokens);
871}
872
873/* Return all tokens saved since the last call to cp_lexer_save_tokens
874   to the token stream.  Stop saving tokens.  */
875
876static void
877cp_lexer_rollback_tokens (cp_lexer* lexer)
878{
879  size_t delta;
880
881  /* Provide debugging output.  */
882  if (cp_lexer_debugging_p (lexer))
883    fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
884
885  /* Find the token that was the NEXT_TOKEN when we started saving
886     tokens.  */
887  delta = VARRAY_TOP_INT(lexer->saved_tokens);
888  /* Make it the next token again now.  */
889  lexer->next_token = cp_lexer_advance_token (lexer,
890					      lexer->first_token,
891					      delta);
892  /* It might be the case that there were no tokens when we started
893     saving tokens, but that there are some tokens now.  */
894  if (!lexer->next_token && lexer->first_token)
895    lexer->next_token = lexer->first_token;
896
897  /* Stop saving tokens.  */
898  VARRAY_POP (lexer->saved_tokens);
899}
900
901/* Print a representation of the TOKEN on the STREAM.  */
902
903static void
904cp_lexer_print_token (FILE * stream, cp_token* token)
905{
906  const char *token_type = NULL;
907
908  /* Figure out what kind of token this is.  */
909  switch (token->type)
910    {
911    case CPP_EQ:
912      token_type = "EQ";
913      break;
914
915    case CPP_COMMA:
916      token_type = "COMMA";
917      break;
918
919    case CPP_OPEN_PAREN:
920      token_type = "OPEN_PAREN";
921      break;
922
923    case CPP_CLOSE_PAREN:
924      token_type = "CLOSE_PAREN";
925      break;
926
927    case CPP_OPEN_BRACE:
928      token_type = "OPEN_BRACE";
929      break;
930
931    case CPP_CLOSE_BRACE:
932      token_type = "CLOSE_BRACE";
933      break;
934
935    case CPP_SEMICOLON:
936      token_type = "SEMICOLON";
937      break;
938
939    case CPP_NAME:
940      token_type = "NAME";
941      break;
942
943    case CPP_EOF:
944      token_type = "EOF";
945      break;
946
947    case CPP_KEYWORD:
948      token_type = "keyword";
949      break;
950
951      /* This is not a token that we know how to handle yet.  */
952    default:
953      break;
954    }
955
956  /* If we have a name for the token, print it out.  Otherwise, we
957     simply give the numeric code.  */
958  if (token_type)
959    fprintf (stream, "%s", token_type);
960  else
961    fprintf (stream, "%d", token->type);
962  /* And, for an identifier, print the identifier name.  */
963  if (token->type == CPP_NAME
964      /* Some keywords have a value that is not an IDENTIFIER_NODE.
965	 For example, `struct' is mapped to an INTEGER_CST.  */
966      || (token->type == CPP_KEYWORD
967	  && TREE_CODE (token->value) == IDENTIFIER_NODE))
968    fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
969}
970
971/* Start emitting debugging information.  */
972
973static void
974cp_lexer_start_debugging (cp_lexer* lexer)
975{
976  ++lexer->debugging_p;
977}
978
979/* Stop emitting debugging information.  */
980
981static void
982cp_lexer_stop_debugging (cp_lexer* lexer)
983{
984  --lexer->debugging_p;
985}
986
987
988/* The parser.  */
989
990/* Overview
991   --------
992
993   A cp_parser parses the token stream as specified by the C++
994   grammar.  Its job is purely parsing, not semantic analysis.  For
995   example, the parser breaks the token stream into declarators,
996   expressions, statements, and other similar syntactic constructs.
997   It does not check that the types of the expressions on either side
998   of an assignment-statement are compatible, or that a function is
999   not declared with a parameter of type `void'.
1000
1001   The parser invokes routines elsewhere in the compiler to perform
1002   semantic analysis and to build up the abstract syntax tree for the
1003   code processed.
1004
1005   The parser (and the template instantiation code, which is, in a
1006   way, a close relative of parsing) are the only parts of the
1007   compiler that should be calling push_scope and pop_scope, or
1008   related functions.  The parser (and template instantiation code)
1009   keeps track of what scope is presently active; everything else
1010   should simply honor that.  (The code that generates static
1011   initializers may also need to set the scope, in order to check
1012   access control correctly when emitting the initializers.)
1013
1014   Methodology
1015   -----------
1016
1017   The parser is of the standard recursive-descent variety.  Upcoming
1018   tokens in the token stream are examined in order to determine which
1019   production to use when parsing a non-terminal.  Some C++ constructs
1020   require arbitrary look ahead to disambiguate.  For example, it is
1021   impossible, in the general case, to tell whether a statement is an
1022   expression or declaration without scanning the entire statement.
1023   Therefore, the parser is capable of "parsing tentatively."  When the
1024   parser is not sure what construct comes next, it enters this mode.
1025   Then, while we attempt to parse the construct, the parser queues up
1026   error messages, rather than issuing them immediately, and saves the
1027   tokens it consumes.  If the construct is parsed successfully, the
1028   parser "commits", i.e., it issues any queued error messages and
1029   the tokens that were being preserved are permanently discarded.
1030   If, however, the construct is not parsed successfully, the parser
1031   rolls back its state completely so that it can resume parsing using
1032   a different alternative.
1033
1034   Future Improvements
1035   -------------------
1036
1037   The performance of the parser could probably be improved
1038   substantially.  Some possible improvements include:
1039
1040     - The expression parser recurses through the various levels of
1041       precedence as specified in the grammar, rather than using an
1042       operator-precedence technique.  Therefore, parsing a simple
1043       identifier requires multiple recursive calls.
1044
1045     - We could often eliminate the need to parse tentatively by
1046       looking ahead a little bit.  In some places, this approach
1047       might not entirely eliminate the need to parse tentatively, but
1048       it might still speed up the average case.  */
1049
1050/* Flags that are passed to some parsing functions.  These values can
1051   be bitwise-ored together.  */
1052
1053typedef enum cp_parser_flags
1054{
1055  /* No flags.  */
1056  CP_PARSER_FLAGS_NONE = 0x0,
1057  /* The construct is optional.  If it is not present, then no error
1058     should be issued.  */
1059  CP_PARSER_FLAGS_OPTIONAL = 0x1,
1060  /* When parsing a type-specifier, do not allow user-defined types.  */
1061  CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1062} cp_parser_flags;
1063
1064/* The different kinds of declarators we want to parse.  */
1065
1066typedef enum cp_parser_declarator_kind
1067{
1068  /* We want an abstract declartor.  */
1069  CP_PARSER_DECLARATOR_ABSTRACT,
1070  /* We want a named declarator.  */
1071  CP_PARSER_DECLARATOR_NAMED,
1072  /* We don't mind, but the name must be an unqualified-id.  */
1073  CP_PARSER_DECLARATOR_EITHER
1074} cp_parser_declarator_kind;
1075
1076/* A mapping from a token type to a corresponding tree node type.  */
1077
1078typedef struct cp_parser_token_tree_map_node
1079{
1080  /* The token type.  */
1081  ENUM_BITFIELD (cpp_ttype) token_type : 8;
1082  /* The corresponding tree code.  */
1083  ENUM_BITFIELD (tree_code) tree_type : 8;
1084} cp_parser_token_tree_map_node;
1085
1086/* A complete map consists of several ordinary entries, followed by a
1087   terminator.  The terminating entry has a token_type of CPP_EOF.  */
1088
1089typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1090
1091/* The status of a tentative parse.  */
1092
1093typedef enum cp_parser_status_kind
1094{
1095  /* No errors have occurred.  */
1096  CP_PARSER_STATUS_KIND_NO_ERROR,
1097  /* An error has occurred.  */
1098  CP_PARSER_STATUS_KIND_ERROR,
1099  /* We are committed to this tentative parse, whether or not an error
1100     has occurred.  */
1101  CP_PARSER_STATUS_KIND_COMMITTED
1102} cp_parser_status_kind;
1103
1104/* Context that is saved and restored when parsing tentatively.  */
1105
1106typedef struct cp_parser_context GTY (())
1107{
1108  /* If this is a tentative parsing context, the status of the
1109     tentative parse.  */
1110  enum cp_parser_status_kind status;
1111  /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1112     that are looked up in this context must be looked up both in the
1113     scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1114     the context of the containing expression.  */
1115  tree object_type;
1116  /* The next parsing context in the stack.  */
1117  struct cp_parser_context *next;
1118} cp_parser_context;
1119
1120/* Prototypes.  */
1121
1122/* Constructors and destructors.  */
1123
1124static cp_parser_context *cp_parser_context_new
1125  (cp_parser_context *);
1126
1127/* Class variables.  */
1128
1129static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1130
1131/* Constructors and destructors.  */
1132
1133/* Construct a new context.  The context below this one on the stack
1134   is given by NEXT.  */
1135
1136static cp_parser_context *
1137cp_parser_context_new (cp_parser_context* next)
1138{
1139  cp_parser_context *context;
1140
1141  /* Allocate the storage.  */
1142  if (cp_parser_context_free_list != NULL)
1143    {
1144      /* Pull the first entry from the free list.  */
1145      context = cp_parser_context_free_list;
1146      cp_parser_context_free_list = context->next;
1147      memset (context, 0, sizeof (*context));
1148    }
1149  else
1150    context = ggc_alloc_cleared (sizeof (cp_parser_context));
1151  /* No errors have occurred yet in this context.  */
1152  context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1153  /* If this is not the bottomost context, copy information that we
1154     need from the previous context.  */
1155  if (next)
1156    {
1157      /* If, in the NEXT context, we are parsing an `x->' or `x.'
1158	 expression, then we are parsing one in this context, too.  */
1159      context->object_type = next->object_type;
1160      /* Thread the stack.  */
1161      context->next = next;
1162    }
1163
1164  return context;
1165}
1166
1167/* The cp_parser structure represents the C++ parser.  */
1168
1169typedef struct cp_parser GTY(())
1170{
1171  /* The lexer from which we are obtaining tokens.  */
1172  cp_lexer *lexer;
1173
1174  /* The scope in which names should be looked up.  If NULL_TREE, then
1175     we look up names in the scope that is currently open in the
1176     source program.  If non-NULL, this is either a TYPE or
1177     NAMESPACE_DECL for the scope in which we should look.
1178
1179     This value is not cleared automatically after a name is looked
1180     up, so we must be careful to clear it before starting a new look
1181     up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1182     will look up `Z' in the scope of `X', rather than the current
1183     scope.)  Unfortunately, it is difficult to tell when name lookup
1184     is complete, because we sometimes peek at a token, look it up,
1185     and then decide not to consume it.  */
1186  tree scope;
1187
1188  /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1189     last lookup took place.  OBJECT_SCOPE is used if an expression
1190     like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1191     respectively.  QUALIFYING_SCOPE is used for an expression of the
1192     form "X::Y"; it refers to X.  */
1193  tree object_scope;
1194  tree qualifying_scope;
1195
1196  /* A stack of parsing contexts.  All but the bottom entry on the
1197     stack will be tentative contexts.
1198
1199     We parse tentatively in order to determine which construct is in
1200     use in some situations.  For example, in order to determine
1201     whether a statement is an expression-statement or a
1202     declaration-statement we parse it tentatively as a
1203     declaration-statement.  If that fails, we then reparse the same
1204     token stream as an expression-statement.  */
1205  cp_parser_context *context;
1206
1207  /* True if we are parsing GNU C++.  If this flag is not set, then
1208     GNU extensions are not recognized.  */
1209  bool allow_gnu_extensions_p;
1210
1211  /* TRUE if the `>' token should be interpreted as the greater-than
1212     operator.  FALSE if it is the end of a template-id or
1213     template-parameter-list.  */
1214  bool greater_than_is_operator_p;
1215
1216  /* TRUE if default arguments are allowed within a parameter list
1217     that starts at this point. FALSE if only a gnu extension makes
1218     them permissible.  */
1219  bool default_arg_ok_p;
1220
1221  /* TRUE if we are parsing an integral constant-expression.  See
1222     [expr.const] for a precise definition.  */
1223  bool integral_constant_expression_p;
1224
1225  /* TRUE if we are parsing an integral constant-expression -- but a
1226     non-constant expression should be permitted as well.  This flag
1227     is used when parsing an array bound so that GNU variable-length
1228     arrays are tolerated.  */
1229  bool allow_non_integral_constant_expression_p;
1230
1231  /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1232     been seen that makes the expression non-constant.  */
1233  bool non_integral_constant_expression_p;
1234
1235  /* TRUE if we are parsing the argument to "__offsetof__".  */
1236  bool in_offsetof_p;
1237
1238  /* TRUE if local variable names and `this' are forbidden in the
1239     current context.  */
1240  bool local_variables_forbidden_p;
1241
1242  /* TRUE if the declaration we are parsing is part of a
1243     linkage-specification of the form `extern string-literal
1244     declaration'.  */
1245  bool in_unbraced_linkage_specification_p;
1246
1247  /* TRUE if we are presently parsing a declarator, after the
1248     direct-declarator.  */
1249  bool in_declarator_p;
1250
1251  /* TRUE if we are presently parsing a template-argument-list.  */
1252  bool in_template_argument_list_p;
1253
1254  /* TRUE if we are presently parsing the body of an
1255     iteration-statement.  */
1256  bool in_iteration_statement_p;
1257
1258  /* TRUE if we are presently parsing the body of a switch
1259     statement.  */
1260  bool in_switch_statement_p;
1261
1262  /* TRUE if we are parsing a type-id in an expression context.  In
1263     such a situation, both "type (expr)" and "type (type)" are valid
1264     alternatives.  */
1265  bool in_type_id_in_expr_p;
1266
1267  /* If non-NULL, then we are parsing a construct where new type
1268     definitions are not permitted.  The string stored here will be
1269     issued as an error message if a type is defined.  */
1270  const char *type_definition_forbidden_message;
1271
1272  /* A list of lists. The outer list is a stack, used for member
1273     functions of local classes. At each level there are two sub-list,
1274     one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1275     sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1276     TREE_VALUE's. The functions are chained in reverse declaration
1277     order.
1278
1279     The TREE_PURPOSE sublist contains those functions with default
1280     arguments that need post processing, and the TREE_VALUE sublist
1281     contains those functions with definitions that need post
1282     processing.
1283
1284     These lists can only be processed once the outermost class being
1285     defined is complete.  */
1286  tree unparsed_functions_queues;
1287
1288  /* The number of classes whose definitions are currently in
1289     progress.  */
1290  unsigned num_classes_being_defined;
1291
1292  /* The number of template parameter lists that apply directly to the
1293     current declaration.  */
1294  unsigned num_template_parameter_lists;
1295} cp_parser;
1296
1297/* The type of a function that parses some kind of expression.  */
1298typedef tree (*cp_parser_expression_fn) (cp_parser *);
1299
1300/* Prototypes.  */
1301
1302/* Constructors and destructors.  */
1303
1304static cp_parser *cp_parser_new
1305  (void);
1306
1307/* Routines to parse various constructs.
1308
1309   Those that return `tree' will return the error_mark_node (rather
1310   than NULL_TREE) if a parse error occurs, unless otherwise noted.
1311   Sometimes, they will return an ordinary node if error-recovery was
1312   attempted, even though a parse error occurred.  So, to check
1313   whether or not a parse error occurred, you should always use
1314   cp_parser_error_occurred.  If the construct is optional (indicated
1315   either by an `_opt' in the name of the function that does the
1316   parsing or via a FLAGS parameter), then NULL_TREE is returned if
1317   the construct is not present.  */
1318
1319/* Lexical conventions [gram.lex]  */
1320
1321static tree cp_parser_identifier
1322  (cp_parser *);
1323
1324/* Basic concepts [gram.basic]  */
1325
1326static bool cp_parser_translation_unit
1327  (cp_parser *);
1328
1329/* Expressions [gram.expr]  */
1330
1331static tree cp_parser_primary_expression
1332  (cp_parser *, cp_id_kind *, tree *);
1333static tree cp_parser_id_expression
1334  (cp_parser *, bool, bool, bool *, bool);
1335static tree cp_parser_unqualified_id
1336  (cp_parser *, bool, bool, bool);
1337static tree cp_parser_nested_name_specifier_opt
1338  (cp_parser *, bool, bool, bool, bool);
1339static tree cp_parser_nested_name_specifier
1340  (cp_parser *, bool, bool, bool, bool);
1341static tree cp_parser_class_or_namespace_name
1342  (cp_parser *, bool, bool, bool, bool, bool);
1343static tree cp_parser_postfix_expression
1344  (cp_parser *, bool);
1345static tree cp_parser_parenthesized_expression_list
1346  (cp_parser *, bool, bool *);
1347static void cp_parser_pseudo_destructor_name
1348  (cp_parser *, tree *, tree *);
1349static tree cp_parser_unary_expression
1350  (cp_parser *, bool);
1351static enum tree_code cp_parser_unary_operator
1352  (cp_token *);
1353static tree cp_parser_new_expression
1354  (cp_parser *);
1355static tree cp_parser_new_placement
1356  (cp_parser *);
1357static tree cp_parser_new_type_id
1358  (cp_parser *);
1359static tree cp_parser_new_declarator_opt
1360  (cp_parser *);
1361static tree cp_parser_direct_new_declarator
1362  (cp_parser *);
1363static tree cp_parser_new_initializer
1364  (cp_parser *);
1365static tree cp_parser_delete_expression
1366  (cp_parser *);
1367static tree cp_parser_cast_expression
1368  (cp_parser *, bool);
1369static tree cp_parser_pm_expression
1370  (cp_parser *);
1371static tree cp_parser_multiplicative_expression
1372  (cp_parser *);
1373static tree cp_parser_additive_expression
1374  (cp_parser *);
1375static tree cp_parser_shift_expression
1376  (cp_parser *);
1377static tree cp_parser_relational_expression
1378  (cp_parser *);
1379static tree cp_parser_equality_expression
1380  (cp_parser *);
1381static tree cp_parser_and_expression
1382  (cp_parser *);
1383static tree cp_parser_exclusive_or_expression
1384  (cp_parser *);
1385static tree cp_parser_inclusive_or_expression
1386  (cp_parser *);
1387static tree cp_parser_logical_and_expression
1388  (cp_parser *);
1389static tree cp_parser_logical_or_expression
1390  (cp_parser *);
1391static tree cp_parser_question_colon_clause
1392  (cp_parser *, tree);
1393static tree cp_parser_assignment_expression
1394  (cp_parser *);
1395static enum tree_code cp_parser_assignment_operator_opt
1396  (cp_parser *);
1397static tree cp_parser_expression
1398  (cp_parser *);
1399static tree cp_parser_constant_expression
1400  (cp_parser *, bool, bool *);
1401
1402/* Statements [gram.stmt.stmt]  */
1403
1404static void cp_parser_statement
1405  (cp_parser *, bool);
1406static tree cp_parser_labeled_statement
1407  (cp_parser *, bool);
1408static tree cp_parser_expression_statement
1409  (cp_parser *, bool);
1410static tree cp_parser_compound_statement
1411  (cp_parser *, bool);
1412static void cp_parser_statement_seq_opt
1413  (cp_parser *, bool);
1414static tree cp_parser_selection_statement
1415  (cp_parser *);
1416static tree cp_parser_condition
1417  (cp_parser *);
1418static tree cp_parser_iteration_statement
1419  (cp_parser *);
1420static void cp_parser_for_init_statement
1421  (cp_parser *);
1422static tree cp_parser_jump_statement
1423  (cp_parser *);
1424static void cp_parser_declaration_statement
1425  (cp_parser *);
1426
1427static tree cp_parser_implicitly_scoped_statement
1428  (cp_parser *);
1429static void cp_parser_already_scoped_statement
1430  (cp_parser *);
1431
1432/* Declarations [gram.dcl.dcl] */
1433
1434static void cp_parser_declaration_seq_opt
1435  (cp_parser *);
1436static void cp_parser_declaration
1437  (cp_parser *);
1438static void cp_parser_block_declaration
1439  (cp_parser *, bool);
1440static void cp_parser_simple_declaration
1441  (cp_parser *, bool);
1442static tree cp_parser_decl_specifier_seq
1443  (cp_parser *, cp_parser_flags, tree *, int *);
1444static tree cp_parser_storage_class_specifier_opt
1445  (cp_parser *);
1446static tree cp_parser_function_specifier_opt
1447  (cp_parser *);
1448static tree cp_parser_type_specifier
1449  (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1450static tree cp_parser_simple_type_specifier
1451  (cp_parser *, cp_parser_flags, bool);
1452static tree cp_parser_type_name
1453  (cp_parser *);
1454static tree cp_parser_elaborated_type_specifier
1455  (cp_parser *, bool, bool);
1456static tree cp_parser_enum_specifier
1457  (cp_parser *);
1458static void cp_parser_enumerator_list
1459  (cp_parser *, tree);
1460static void cp_parser_enumerator_definition
1461  (cp_parser *, tree);
1462static tree cp_parser_namespace_name
1463  (cp_parser *);
1464static void cp_parser_namespace_definition
1465  (cp_parser *);
1466static void cp_parser_namespace_body
1467  (cp_parser *);
1468static tree cp_parser_qualified_namespace_specifier
1469  (cp_parser *);
1470static void cp_parser_namespace_alias_definition
1471  (cp_parser *);
1472static void cp_parser_using_declaration
1473  (cp_parser *);
1474static void cp_parser_using_directive
1475  (cp_parser *);
1476static void cp_parser_asm_definition
1477  (cp_parser *);
1478static void cp_parser_linkage_specification
1479  (cp_parser *);
1480
1481/* Declarators [gram.dcl.decl] */
1482
1483static tree cp_parser_init_declarator
1484  (cp_parser *, tree, tree, bool, bool, int, bool *);
1485static tree cp_parser_declarator
1486  (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1487static tree cp_parser_direct_declarator
1488  (cp_parser *, cp_parser_declarator_kind, int *);
1489static enum tree_code cp_parser_ptr_operator
1490  (cp_parser *, tree *, tree *);
1491static tree cp_parser_cv_qualifier_seq_opt
1492  (cp_parser *);
1493static tree cp_parser_cv_qualifier_opt
1494  (cp_parser *);
1495static tree cp_parser_declarator_id
1496  (cp_parser *);
1497static tree cp_parser_type_id
1498  (cp_parser *);
1499static tree cp_parser_type_specifier_seq
1500  (cp_parser *);
1501static tree cp_parser_parameter_declaration_clause
1502  (cp_parser *);
1503static tree cp_parser_parameter_declaration_list
1504  (cp_parser *);
1505static tree cp_parser_parameter_declaration
1506  (cp_parser *, bool, bool *);
1507static void cp_parser_function_body
1508  (cp_parser *);
1509static tree cp_parser_initializer
1510  (cp_parser *, bool *, bool *);
1511static tree cp_parser_initializer_clause
1512  (cp_parser *, bool *);
1513static tree cp_parser_initializer_list
1514  (cp_parser *, bool *);
1515
1516static bool cp_parser_ctor_initializer_opt_and_function_body
1517  (cp_parser *);
1518
1519/* Classes [gram.class] */
1520
1521static tree cp_parser_class_name
1522  (cp_parser *, bool, bool, bool, bool, bool, bool);
1523static tree cp_parser_class_specifier
1524  (cp_parser *);
1525static tree cp_parser_class_head
1526  (cp_parser *, bool *, tree *);
1527static enum tag_types cp_parser_class_key
1528  (cp_parser *);
1529static void cp_parser_member_specification_opt
1530  (cp_parser *);
1531static void cp_parser_member_declaration
1532  (cp_parser *);
1533static tree cp_parser_pure_specifier
1534  (cp_parser *);
1535static tree cp_parser_constant_initializer
1536  (cp_parser *);
1537
1538/* Derived classes [gram.class.derived] */
1539
1540static tree cp_parser_base_clause
1541  (cp_parser *);
1542static tree cp_parser_base_specifier
1543  (cp_parser *);
1544
1545/* Special member functions [gram.special] */
1546
1547static tree cp_parser_conversion_function_id
1548  (cp_parser *);
1549static tree cp_parser_conversion_type_id
1550  (cp_parser *);
1551static tree cp_parser_conversion_declarator_opt
1552  (cp_parser *);
1553static bool cp_parser_ctor_initializer_opt
1554  (cp_parser *);
1555static void cp_parser_mem_initializer_list
1556  (cp_parser *);
1557static tree cp_parser_mem_initializer
1558  (cp_parser *);
1559static tree cp_parser_mem_initializer_id
1560  (cp_parser *);
1561
1562/* Overloading [gram.over] */
1563
1564static tree cp_parser_operator_function_id
1565  (cp_parser *);
1566static tree cp_parser_operator
1567  (cp_parser *);
1568
1569/* Templates [gram.temp] */
1570
1571static void cp_parser_template_declaration
1572  (cp_parser *, bool);
1573static tree cp_parser_template_parameter_list
1574  (cp_parser *);
1575static tree cp_parser_template_parameter
1576  (cp_parser *);
1577static tree cp_parser_type_parameter
1578  (cp_parser *);
1579static tree cp_parser_template_id
1580  (cp_parser *, bool, bool, bool);
1581static tree cp_parser_template_name
1582  (cp_parser *, bool, bool, bool, bool *);
1583static tree cp_parser_template_argument_list
1584  (cp_parser *);
1585static tree cp_parser_template_argument
1586  (cp_parser *);
1587static void cp_parser_explicit_instantiation
1588  (cp_parser *);
1589static void cp_parser_explicit_specialization
1590  (cp_parser *);
1591
1592/* Exception handling [gram.exception] */
1593
1594static tree cp_parser_try_block
1595  (cp_parser *);
1596static bool cp_parser_function_try_block
1597  (cp_parser *);
1598static void cp_parser_handler_seq
1599  (cp_parser *);
1600static void cp_parser_handler
1601  (cp_parser *);
1602static tree cp_parser_exception_declaration
1603  (cp_parser *);
1604static tree cp_parser_throw_expression
1605  (cp_parser *);
1606static tree cp_parser_exception_specification_opt
1607  (cp_parser *);
1608static tree cp_parser_type_id_list
1609  (cp_parser *);
1610
1611/* GNU Extensions */
1612
1613static tree cp_parser_asm_specification_opt
1614  (cp_parser *);
1615static tree cp_parser_asm_operand_list
1616  (cp_parser *);
1617static tree cp_parser_asm_clobber_list
1618  (cp_parser *);
1619static tree cp_parser_attributes_opt
1620  (cp_parser *);
1621static tree cp_parser_attribute_list
1622  (cp_parser *);
1623static bool cp_parser_extension_opt
1624  (cp_parser *, int *);
1625static void cp_parser_label_declaration
1626  (cp_parser *);
1627
1628/* Utility Routines */
1629
1630static tree cp_parser_lookup_name
1631  (cp_parser *, tree, bool, bool, bool, bool);
1632static tree cp_parser_lookup_name_simple
1633  (cp_parser *, tree);
1634static tree cp_parser_maybe_treat_template_as_class
1635  (tree, bool);
1636static bool cp_parser_check_declarator_template_parameters
1637  (cp_parser *, tree);
1638static bool cp_parser_check_template_parameters
1639  (cp_parser *, unsigned);
1640static tree cp_parser_simple_cast_expression
1641  (cp_parser *);
1642static tree cp_parser_binary_expression
1643  (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1644static tree cp_parser_global_scope_opt
1645  (cp_parser *, bool);
1646static bool cp_parser_constructor_declarator_p
1647  (cp_parser *, bool);
1648static tree cp_parser_function_definition_from_specifiers_and_declarator
1649  (cp_parser *, tree, tree, tree);
1650static tree cp_parser_function_definition_after_declarator
1651  (cp_parser *, bool);
1652static void cp_parser_template_declaration_after_export
1653  (cp_parser *, bool);
1654static tree cp_parser_single_declaration
1655  (cp_parser *, bool, bool *);
1656static tree cp_parser_functional_cast
1657  (cp_parser *, tree);
1658static tree cp_parser_save_member_function_body
1659  (cp_parser *, tree, tree, tree);
1660static tree cp_parser_enclosed_template_argument_list
1661  (cp_parser *);
1662static void cp_parser_save_default_args
1663  (cp_parser *, tree);
1664static void cp_parser_late_parsing_for_member
1665  (cp_parser *, tree);
1666static void cp_parser_late_parsing_default_args
1667  (cp_parser *, tree);
1668static tree cp_parser_sizeof_operand
1669  (cp_parser *, enum rid);
1670static bool cp_parser_declares_only_class_p
1671  (cp_parser *);
1672static bool cp_parser_friend_p
1673  (tree);
1674static cp_token *cp_parser_require
1675  (cp_parser *, enum cpp_ttype, const char *);
1676static cp_token *cp_parser_require_keyword
1677  (cp_parser *, enum rid, const char *);
1678static bool cp_parser_token_starts_function_definition_p
1679  (cp_token *);
1680static bool cp_parser_next_token_starts_class_definition_p
1681  (cp_parser *);
1682static bool cp_parser_next_token_ends_template_argument_p
1683  (cp_parser *);
1684static bool cp_parser_nth_token_starts_template_argument_list_p
1685  (cp_parser *, size_t);
1686static enum tag_types cp_parser_token_is_class_key
1687  (cp_token *);
1688static void cp_parser_check_class_key
1689  (enum tag_types, tree type);
1690static void cp_parser_check_access_in_redeclaration
1691  (tree type);
1692static bool cp_parser_optional_template_keyword
1693  (cp_parser *);
1694static void cp_parser_pre_parsed_nested_name_specifier
1695  (cp_parser *);
1696static void cp_parser_cache_group
1697  (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1698static void cp_parser_parse_tentatively
1699  (cp_parser *);
1700static void cp_parser_commit_to_tentative_parse
1701  (cp_parser *);
1702static void cp_parser_abort_tentative_parse
1703  (cp_parser *);
1704static bool cp_parser_parse_definitely
1705  (cp_parser *);
1706static inline bool cp_parser_parsing_tentatively
1707  (cp_parser *);
1708static bool cp_parser_committed_to_tentative_parse
1709  (cp_parser *);
1710static void cp_parser_error
1711  (cp_parser *, const char *);
1712static void cp_parser_name_lookup_error
1713  (cp_parser *, tree, tree, const char *);
1714static bool cp_parser_simulate_error
1715  (cp_parser *);
1716static void cp_parser_check_type_definition
1717  (cp_parser *);
1718static void cp_parser_check_for_definition_in_return_type
1719  (tree, int);
1720static void cp_parser_check_for_invalid_template_id
1721  (cp_parser *, tree);
1722static bool cp_parser_non_integral_constant_expression
1723  (cp_parser *, const char *);
1724static bool cp_parser_diagnose_invalid_type_name
1725  (cp_parser *);
1726static int cp_parser_skip_to_closing_parenthesis
1727  (cp_parser *, bool, bool, bool);
1728static void cp_parser_skip_to_end_of_statement
1729  (cp_parser *);
1730static void cp_parser_consume_semicolon_at_end_of_statement
1731  (cp_parser *);
1732static void cp_parser_skip_to_end_of_block_or_statement
1733  (cp_parser *);
1734static void cp_parser_skip_to_closing_brace
1735  (cp_parser *);
1736static void cp_parser_skip_until_found
1737  (cp_parser *, enum cpp_ttype, const char *);
1738static bool cp_parser_error_occurred
1739  (cp_parser *);
1740static bool cp_parser_allow_gnu_extensions_p
1741  (cp_parser *);
1742static bool cp_parser_is_string_literal
1743  (cp_token *);
1744static bool cp_parser_is_keyword
1745  (cp_token *, enum rid);
1746
1747/* Returns nonzero if we are parsing tentatively.  */
1748
1749static inline bool
1750cp_parser_parsing_tentatively (cp_parser* parser)
1751{
1752  return parser->context->next != NULL;
1753}
1754
1755/* Returns nonzero if TOKEN is a string literal.  */
1756
1757static bool
1758cp_parser_is_string_literal (cp_token* token)
1759{
1760  return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1761}
1762
1763/* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1764
1765static bool
1766cp_parser_is_keyword (cp_token* token, enum rid keyword)
1767{
1768  return token->keyword == keyword;
1769}
1770
1771/* Issue the indicated error MESSAGE.  */
1772
1773static void
1774cp_parser_error (cp_parser* parser, const char* message)
1775{
1776  /* Output the MESSAGE -- unless we're parsing tentatively.  */
1777  if (!cp_parser_simulate_error (parser))
1778    {
1779      cp_token *token;
1780      token = cp_lexer_peek_token (parser->lexer);
1781      c_parse_error (message,
1782		     /* Because c_parser_error does not understand
1783			CPP_KEYWORD, keywords are treated like
1784			identifiers.  */
1785		     (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1786		     token->value);
1787    }
1788}
1789
1790/* Issue an error about name-lookup failing.  NAME is the
1791   IDENTIFIER_NODE DECL is the result of
1792   the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1793   the thing that we hoped to find.  */
1794
1795static void
1796cp_parser_name_lookup_error (cp_parser* parser,
1797			     tree name,
1798			     tree decl,
1799			     const char* desired)
1800{
1801  /* If name lookup completely failed, tell the user that NAME was not
1802     declared.  */
1803  if (decl == error_mark_node)
1804    {
1805      if (parser->scope && parser->scope != global_namespace)
1806	error ("`%D::%D' has not been declared",
1807	       parser->scope, name);
1808      else if (parser->scope == global_namespace)
1809	error ("`::%D' has not been declared", name);
1810      else
1811	error ("`%D' has not been declared", name);
1812    }
1813  else if (parser->scope && parser->scope != global_namespace)
1814    error ("`%D::%D' %s", parser->scope, name, desired);
1815  else if (parser->scope == global_namespace)
1816    error ("`::%D' %s", name, desired);
1817  else
1818    error ("`%D' %s", name, desired);
1819}
1820
1821/* If we are parsing tentatively, remember that an error has occurred
1822   during this tentative parse.  Returns true if the error was
1823   simulated; false if a messgae should be issued by the caller.  */
1824
1825static bool
1826cp_parser_simulate_error (cp_parser* parser)
1827{
1828  if (cp_parser_parsing_tentatively (parser)
1829      && !cp_parser_committed_to_tentative_parse (parser))
1830    {
1831      parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1832      return true;
1833    }
1834  return false;
1835}
1836
1837/* This function is called when a type is defined.  If type
1838   definitions are forbidden at this point, an error message is
1839   issued.  */
1840
1841static void
1842cp_parser_check_type_definition (cp_parser* parser)
1843{
1844  /* If types are forbidden here, issue a message.  */
1845  if (parser->type_definition_forbidden_message)
1846    /* Use `%s' to print the string in case there are any escape
1847       characters in the message.  */
1848    error ("%s", parser->type_definition_forbidden_message);
1849}
1850
1851/* This function is called when a declaration is parsed.  If
1852   DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1853   indicates that a type was defined in the decl-specifiers for DECL,
1854   then an error is issued.  */
1855
1856static void
1857cp_parser_check_for_definition_in_return_type (tree declarator,
1858					       int declares_class_or_enum)
1859{
1860  /* [dcl.fct] forbids type definitions in return types.
1861     Unfortunately, it's not easy to know whether or not we are
1862     processing a return type until after the fact.  */
1863  while (declarator
1864	 && (TREE_CODE (declarator) == INDIRECT_REF
1865	     || TREE_CODE (declarator) == ADDR_EXPR))
1866    declarator = TREE_OPERAND (declarator, 0);
1867  if (declarator
1868      && TREE_CODE (declarator) == CALL_EXPR
1869      && declares_class_or_enum & 2)
1870    error ("new types may not be defined in a return type");
1871}
1872
1873/* A type-specifier (TYPE) has been parsed which cannot be followed by
1874   "<" in any valid C++ program.  If the next token is indeed "<",
1875   issue a message warning the user about what appears to be an
1876   invalid attempt to form a template-id.  */
1877
1878static void
1879cp_parser_check_for_invalid_template_id (cp_parser* parser,
1880					 tree type)
1881{
1882  ptrdiff_t start;
1883  cp_token *token;
1884
1885  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1886    {
1887      if (TYPE_P (type))
1888	error ("`%T' is not a template", type);
1889      else if (TREE_CODE (type) == IDENTIFIER_NODE)
1890	error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1891      else
1892	error ("invalid template-id");
1893      /* Remember the location of the invalid "<".  */
1894      if (cp_parser_parsing_tentatively (parser)
1895	  && !cp_parser_committed_to_tentative_parse (parser))
1896	{
1897	  token = cp_lexer_peek_token (parser->lexer);
1898	  token = cp_lexer_prev_token (parser->lexer, token);
1899	  start = cp_lexer_token_difference (parser->lexer,
1900					     parser->lexer->first_token,
1901					     token);
1902	}
1903      else
1904	start = -1;
1905      /* Consume the "<".  */
1906      cp_lexer_consume_token (parser->lexer);
1907      /* Parse the template arguments.  */
1908      cp_parser_enclosed_template_argument_list (parser);
1909      /* Permanently remove the invalid template arguments so that
1910	 this error message is not issued again.  */
1911      if (start >= 0)
1912	{
1913	  token = cp_lexer_advance_token (parser->lexer,
1914					  parser->lexer->first_token,
1915					  start);
1916	  cp_lexer_purge_tokens_after (parser->lexer, token);
1917	}
1918    }
1919}
1920
1921/* If parsing an integral constant-expression, issue an error message
1922   about the fact that THING appeared and return true.  Otherwise,
1923   return false, marking the current expression as non-constant.  */
1924
1925static bool
1926cp_parser_non_integral_constant_expression (cp_parser  *parser,
1927					    const char *thing)
1928{
1929  if (parser->integral_constant_expression_p)
1930    {
1931      if (!parser->allow_non_integral_constant_expression_p)
1932	{
1933	  error ("%s cannot appear in a constant-expression", thing);
1934	  return true;
1935	}
1936      parser->non_integral_constant_expression_p = true;
1937    }
1938  return false;
1939}
1940
1941/* Check for a common situation where a type-name should be present,
1942   but is not, and issue a sensible error message.  Returns true if an
1943   invalid type-name was detected.  */
1944
1945static bool
1946cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1947{
1948  /* If the next two tokens are both identifiers, the code is
1949     erroneous. The usual cause of this situation is code like:
1950
1951       T t;
1952
1953     where "T" should name a type -- but does not.  */
1954  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1955      && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1956    {
1957      tree name;
1958
1959      /* If parsing tentatively, we should commit; we really are
1960	 looking at a declaration.  */
1961      /* Consume the first identifier.  */
1962      name = cp_lexer_consume_token (parser->lexer)->value;
1963      /* Issue an error message.  */
1964      error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1965      /* If we're in a template class, it's possible that the user was
1966	 referring to a type from a base class.  For example:
1967
1968	   template <typename T> struct A { typedef T X; };
1969	   template <typename T> struct B : public A<T> { X x; };
1970
1971	 The user should have said "typename A<T>::X".  */
1972      if (processing_template_decl && current_class_type)
1973	{
1974	  tree b;
1975
1976	  for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1977	       b;
1978	       b = TREE_CHAIN (b))
1979	    {
1980	      tree base_type = BINFO_TYPE (b);
1981	      if (CLASS_TYPE_P (base_type)
1982		  && dependent_type_p (base_type))
1983		{
1984		  tree field;
1985		  /* Go from a particular instantiation of the
1986		     template (which will have an empty TYPE_FIELDs),
1987		     to the main version.  */
1988		  base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1989		  for (field = TYPE_FIELDS (base_type);
1990		       field;
1991		       field = TREE_CHAIN (field))
1992		    if (TREE_CODE (field) == TYPE_DECL
1993			&& DECL_NAME (field) == name)
1994		      {
1995			error ("(perhaps `typename %T::%s' was intended)",
1996			       BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1997			break;
1998		      }
1999		  if (field)
2000		    break;
2001		}
2002	    }
2003	}
2004      /* Skip to the end of the declaration; there's no point in
2005	 trying to process it.  */
2006      cp_parser_skip_to_end_of_statement (parser);
2007
2008      return true;
2009    }
2010
2011  return false;
2012}
2013
2014/* Consume tokens up to, and including, the next non-nested closing `)'.
2015   Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2016   are doing error recovery. Returns -1 if OR_COMMA is true and we
2017   found an unnested comma.  */
2018
2019static int
2020cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2021				       bool recovering,
2022				       bool or_comma,
2023				       bool consume_paren)
2024{
2025  unsigned paren_depth = 0;
2026  unsigned brace_depth = 0;
2027
2028  if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2029      && !cp_parser_committed_to_tentative_parse (parser))
2030    return 0;
2031
2032  while (true)
2033    {
2034      cp_token *token;
2035
2036      /* If we've run out of tokens, then there is no closing `)'.  */
2037      if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2038	return 0;
2039
2040      token = cp_lexer_peek_token (parser->lexer);
2041
2042      /* This matches the processing in skip_to_end_of_statement.  */
2043      if (token->type == CPP_SEMICOLON && !brace_depth)
2044	return 0;
2045      if (token->type == CPP_OPEN_BRACE)
2046	++brace_depth;
2047      if (token->type == CPP_CLOSE_BRACE)
2048	{
2049	  if (!brace_depth--)
2050	    return 0;
2051	}
2052      if (recovering && or_comma && token->type == CPP_COMMA
2053	  && !brace_depth && !paren_depth)
2054	return -1;
2055
2056      if (!brace_depth)
2057	{
2058	  /* If it is an `(', we have entered another level of nesting.  */
2059	  if (token->type == CPP_OPEN_PAREN)
2060	    ++paren_depth;
2061	  /* If it is a `)', then we might be done.  */
2062	  else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2063	    {
2064	      if (consume_paren)
2065		cp_lexer_consume_token (parser->lexer);
2066	      return 1;
2067	    }
2068	}
2069
2070      /* Consume the token.  */
2071      cp_lexer_consume_token (parser->lexer);
2072    }
2073}
2074
2075/* Consume tokens until we reach the end of the current statement.
2076   Normally, that will be just before consuming a `;'.  However, if a
2077   non-nested `}' comes first, then we stop before consuming that.  */
2078
2079static void
2080cp_parser_skip_to_end_of_statement (cp_parser* parser)
2081{
2082  unsigned nesting_depth = 0;
2083
2084  while (true)
2085    {
2086      cp_token *token;
2087
2088      /* Peek at the next token.  */
2089      token = cp_lexer_peek_token (parser->lexer);
2090      /* If we've run out of tokens, stop.  */
2091      if (token->type == CPP_EOF)
2092	break;
2093      /* If the next token is a `;', we have reached the end of the
2094	 statement.  */
2095      if (token->type == CPP_SEMICOLON && !nesting_depth)
2096	break;
2097      /* If the next token is a non-nested `}', then we have reached
2098	 the end of the current block.  */
2099      if (token->type == CPP_CLOSE_BRACE)
2100	{
2101	  /* If this is a non-nested `}', stop before consuming it.
2102	     That way, when confronted with something like:
2103
2104	       { 3 + }
2105
2106	     we stop before consuming the closing `}', even though we
2107	     have not yet reached a `;'.  */
2108	  if (nesting_depth == 0)
2109	    break;
2110	  /* If it is the closing `}' for a block that we have
2111	     scanned, stop -- but only after consuming the token.
2112	     That way given:
2113
2114	        void f g () { ... }
2115		typedef int I;
2116
2117	     we will stop after the body of the erroneously declared
2118	     function, but before consuming the following `typedef'
2119	     declaration.  */
2120	  if (--nesting_depth == 0)
2121	    {
2122	      cp_lexer_consume_token (parser->lexer);
2123	      break;
2124	    }
2125	}
2126      /* If it the next token is a `{', then we are entering a new
2127	 block.  Consume the entire block.  */
2128      else if (token->type == CPP_OPEN_BRACE)
2129	++nesting_depth;
2130      /* Consume the token.  */
2131      cp_lexer_consume_token (parser->lexer);
2132    }
2133}
2134
2135/* This function is called at the end of a statement or declaration.
2136   If the next token is a semicolon, it is consumed; otherwise, error
2137   recovery is attempted.  */
2138
2139static void
2140cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2141{
2142  /* Look for the trailing `;'.  */
2143  if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2144    {
2145      /* If there is additional (erroneous) input, skip to the end of
2146	 the statement.  */
2147      cp_parser_skip_to_end_of_statement (parser);
2148      /* If the next token is now a `;', consume it.  */
2149      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2150	cp_lexer_consume_token (parser->lexer);
2151    }
2152}
2153
2154/* Skip tokens until we have consumed an entire block, or until we
2155   have consumed a non-nested `;'.  */
2156
2157static void
2158cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2159{
2160  unsigned nesting_depth = 0;
2161
2162  while (true)
2163    {
2164      cp_token *token;
2165
2166      /* Peek at the next token.  */
2167      token = cp_lexer_peek_token (parser->lexer);
2168      /* If we've run out of tokens, stop.  */
2169      if (token->type == CPP_EOF)
2170	break;
2171      /* If the next token is a `;', we have reached the end of the
2172	 statement.  */
2173      if (token->type == CPP_SEMICOLON && !nesting_depth)
2174	{
2175	  /* Consume the `;'.  */
2176	  cp_lexer_consume_token (parser->lexer);
2177	  break;
2178	}
2179      /* Consume the token.  */
2180      token = cp_lexer_consume_token (parser->lexer);
2181      /* If the next token is a non-nested `}', then we have reached
2182	 the end of the current block.  */
2183      if (token->type == CPP_CLOSE_BRACE
2184	  && (nesting_depth == 0 || --nesting_depth == 0))
2185	break;
2186      /* If it the next token is a `{', then we are entering a new
2187	 block.  Consume the entire block.  */
2188      if (token->type == CPP_OPEN_BRACE)
2189	++nesting_depth;
2190    }
2191}
2192
2193/* Skip tokens until a non-nested closing curly brace is the next
2194   token.  */
2195
2196static void
2197cp_parser_skip_to_closing_brace (cp_parser *parser)
2198{
2199  unsigned nesting_depth = 0;
2200
2201  while (true)
2202    {
2203      cp_token *token;
2204
2205      /* Peek at the next token.  */
2206      token = cp_lexer_peek_token (parser->lexer);
2207      /* If we've run out of tokens, stop.  */
2208      if (token->type == CPP_EOF)
2209	break;
2210      /* If the next token is a non-nested `}', then we have reached
2211	 the end of the current block.  */
2212      if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2213	break;
2214      /* If it the next token is a `{', then we are entering a new
2215	 block.  Consume the entire block.  */
2216      else if (token->type == CPP_OPEN_BRACE)
2217	++nesting_depth;
2218      /* Consume the token.  */
2219      cp_lexer_consume_token (parser->lexer);
2220    }
2221}
2222
2223/* Create a new C++ parser.  */
2224
2225static cp_parser *
2226cp_parser_new (void)
2227{
2228  cp_parser *parser;
2229  cp_lexer *lexer;
2230
2231  /* cp_lexer_new_main is called before calling ggc_alloc because
2232     cp_lexer_new_main might load a PCH file.  */
2233  lexer = cp_lexer_new_main ();
2234
2235  parser = ggc_alloc_cleared (sizeof (cp_parser));
2236  parser->lexer = lexer;
2237  parser->context = cp_parser_context_new (NULL);
2238
2239  /* For now, we always accept GNU extensions.  */
2240  parser->allow_gnu_extensions_p = 1;
2241
2242  /* The `>' token is a greater-than operator, not the end of a
2243     template-id.  */
2244  parser->greater_than_is_operator_p = true;
2245
2246  parser->default_arg_ok_p = true;
2247
2248  /* We are not parsing a constant-expression.  */
2249  parser->integral_constant_expression_p = false;
2250  parser->allow_non_integral_constant_expression_p = false;
2251  parser->non_integral_constant_expression_p = false;
2252
2253  /* We are not parsing offsetof.  */
2254  parser->in_offsetof_p = false;
2255
2256  /* Local variable names are not forbidden.  */
2257  parser->local_variables_forbidden_p = false;
2258
2259  /* We are not processing an `extern "C"' declaration.  */
2260  parser->in_unbraced_linkage_specification_p = false;
2261
2262  /* We are not processing a declarator.  */
2263  parser->in_declarator_p = false;
2264
2265  /* We are not processing a template-argument-list.  */
2266  parser->in_template_argument_list_p = false;
2267
2268  /* We are not in an iteration statement.  */
2269  parser->in_iteration_statement_p = false;
2270
2271  /* We are not in a switch statement.  */
2272  parser->in_switch_statement_p = false;
2273
2274  /* We are not parsing a type-id inside an expression.  */
2275  parser->in_type_id_in_expr_p = false;
2276
2277  /* The unparsed function queue is empty.  */
2278  parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2279
2280  /* There are no classes being defined.  */
2281  parser->num_classes_being_defined = 0;
2282
2283  /* No template parameters apply.  */
2284  parser->num_template_parameter_lists = 0;
2285
2286  return parser;
2287}
2288
2289/* Lexical conventions [gram.lex]  */
2290
2291/* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2292   identifier.  */
2293
2294static tree
2295cp_parser_identifier (cp_parser* parser)
2296{
2297  cp_token *token;
2298
2299  /* Look for the identifier.  */
2300  token = cp_parser_require (parser, CPP_NAME, "identifier");
2301  /* Return the value.  */
2302  return token ? token->value : error_mark_node;
2303}
2304
2305/* Basic concepts [gram.basic]  */
2306
2307/* Parse a translation-unit.
2308
2309   translation-unit:
2310     declaration-seq [opt]
2311
2312   Returns TRUE if all went well.  */
2313
2314static bool
2315cp_parser_translation_unit (cp_parser* parser)
2316{
2317  while (true)
2318    {
2319      cp_parser_declaration_seq_opt (parser);
2320
2321      /* If there are no tokens left then all went well.  */
2322      if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2323	break;
2324
2325      /* Otherwise, issue an error message.  */
2326      cp_parser_error (parser, "expected declaration");
2327      return false;
2328    }
2329
2330  /* Consume the EOF token.  */
2331  cp_parser_require (parser, CPP_EOF, "end-of-file");
2332
2333  /* Finish up.  */
2334  finish_translation_unit ();
2335
2336  /* All went well.  */
2337  return true;
2338}
2339
2340/* Expressions [gram.expr] */
2341
2342/* Parse a primary-expression.
2343
2344   primary-expression:
2345     literal
2346     this
2347     ( expression )
2348     id-expression
2349
2350   GNU Extensions:
2351
2352   primary-expression:
2353     ( compound-statement )
2354     __builtin_va_arg ( assignment-expression , type-id )
2355
2356   literal:
2357     __null
2358
2359   Returns a representation of the expression.
2360
2361   *IDK indicates what kind of id-expression (if any) was present.
2362
2363   *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2364   used as the operand of a pointer-to-member.  In that case,
2365   *QUALIFYING_CLASS gives the class that is used as the qualifying
2366   class in the pointer-to-member.  */
2367
2368static tree
2369cp_parser_primary_expression (cp_parser *parser,
2370			      cp_id_kind *idk,
2371			      tree *qualifying_class)
2372{
2373  cp_token *token;
2374
2375  /* Assume the primary expression is not an id-expression.  */
2376  *idk = CP_ID_KIND_NONE;
2377  /* And that it cannot be used as pointer-to-member.  */
2378  *qualifying_class = NULL_TREE;
2379
2380  /* Peek at the next token.  */
2381  token = cp_lexer_peek_token (parser->lexer);
2382  switch (token->type)
2383    {
2384      /* literal:
2385	   integer-literal
2386	   character-literal
2387	   floating-literal
2388	   string-literal
2389	   boolean-literal  */
2390    case CPP_CHAR:
2391    case CPP_WCHAR:
2392    case CPP_STRING:
2393    case CPP_WSTRING:
2394    case CPP_NUMBER:
2395      token = cp_lexer_consume_token (parser->lexer);
2396      return token->value;
2397
2398    case CPP_OPEN_PAREN:
2399      {
2400	tree expr;
2401	bool saved_greater_than_is_operator_p;
2402
2403	/* Consume the `('.  */
2404	cp_lexer_consume_token (parser->lexer);
2405	/* Within a parenthesized expression, a `>' token is always
2406	   the greater-than operator.  */
2407	saved_greater_than_is_operator_p
2408	  = parser->greater_than_is_operator_p;
2409	parser->greater_than_is_operator_p = true;
2410	/* If we see `( { ' then we are looking at the beginning of
2411	   a GNU statement-expression.  */
2412	if (cp_parser_allow_gnu_extensions_p (parser)
2413	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2414	  {
2415	    /* Statement-expressions are not allowed by the standard.  */
2416	    if (pedantic)
2417	      pedwarn ("ISO C++ forbids braced-groups within expressions");
2418
2419	    /* And they're not allowed outside of a function-body; you
2420	       cannot, for example, write:
2421
2422	         int i = ({ int j = 3; j + 1; });
2423
2424	       at class or namespace scope.  */
2425	    if (!at_function_scope_p ())
2426	      error ("statement-expressions are allowed only inside functions");
2427	    /* Start the statement-expression.  */
2428	    expr = begin_stmt_expr ();
2429	    /* Parse the compound-statement.  */
2430	    cp_parser_compound_statement (parser, true);
2431	    /* Finish up.  */
2432	    expr = finish_stmt_expr (expr, false);
2433	  }
2434	else
2435	  {
2436	    /* Parse the parenthesized expression.  */
2437	    expr = cp_parser_expression (parser);
2438	    /* Let the front end know that this expression was
2439	       enclosed in parentheses. This matters in case, for
2440	       example, the expression is of the form `A::B', since
2441	       `&A::B' might be a pointer-to-member, but `&(A::B)' is
2442	       not.  */
2443	    finish_parenthesized_expr (expr);
2444	  }
2445	/* The `>' token might be the end of a template-id or
2446	   template-parameter-list now.  */
2447	parser->greater_than_is_operator_p
2448	  = saved_greater_than_is_operator_p;
2449	/* Consume the `)'.  */
2450	if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2451	  cp_parser_skip_to_end_of_statement (parser);
2452
2453	return expr;
2454      }
2455
2456    case CPP_KEYWORD:
2457      switch (token->keyword)
2458	{
2459	  /* These two are the boolean literals.  */
2460	case RID_TRUE:
2461	  cp_lexer_consume_token (parser->lexer);
2462	  return boolean_true_node;
2463	case RID_FALSE:
2464	  cp_lexer_consume_token (parser->lexer);
2465	  return boolean_false_node;
2466
2467	  /* The `__null' literal.  */
2468	case RID_NULL:
2469	  cp_lexer_consume_token (parser->lexer);
2470	  return null_node;
2471
2472	  /* Recognize the `this' keyword.  */
2473	case RID_THIS:
2474	  cp_lexer_consume_token (parser->lexer);
2475	  if (parser->local_variables_forbidden_p)
2476	    {
2477	      error ("`this' may not be used in this context");
2478	      return error_mark_node;
2479	    }
2480	  /* Pointers cannot appear in constant-expressions.  */
2481	  if (cp_parser_non_integral_constant_expression (parser,
2482							  "`this'"))
2483	    return error_mark_node;
2484	  return finish_this_expr ();
2485
2486	  /* The `operator' keyword can be the beginning of an
2487	     id-expression.  */
2488	case RID_OPERATOR:
2489	  goto id_expression;
2490
2491	case RID_FUNCTION_NAME:
2492	case RID_PRETTY_FUNCTION_NAME:
2493	case RID_C99_FUNCTION_NAME:
2494	  /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2495	     __func__ are the names of variables -- but they are
2496	     treated specially.  Therefore, they are handled here,
2497	     rather than relying on the generic id-expression logic
2498	     below.  Grammatically, these names are id-expressions.
2499
2500	     Consume the token.  */
2501	  token = cp_lexer_consume_token (parser->lexer);
2502	  /* Look up the name.  */
2503	  return finish_fname (token->value);
2504
2505	case RID_VA_ARG:
2506	  {
2507	    tree expression;
2508	    tree type;
2509
2510	    /* The `__builtin_va_arg' construct is used to handle
2511	       `va_arg'.  Consume the `__builtin_va_arg' token.  */
2512	    cp_lexer_consume_token (parser->lexer);
2513	    /* Look for the opening `('.  */
2514	    cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2515	    /* Now, parse the assignment-expression.  */
2516	    expression = cp_parser_assignment_expression (parser);
2517	    /* Look for the `,'.  */
2518	    cp_parser_require (parser, CPP_COMMA, "`,'");
2519	    /* Parse the type-id.  */
2520	    type = cp_parser_type_id (parser);
2521	    /* Look for the closing `)'.  */
2522	    cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2523	    /* Using `va_arg' in a constant-expression is not
2524	       allowed.  */
2525	    if (cp_parser_non_integral_constant_expression (parser,
2526							    "`va_arg'"))
2527	      return error_mark_node;
2528	    return build_x_va_arg (expression, type);
2529	  }
2530
2531	case RID_OFFSETOF:
2532	  {
2533	    tree expression;
2534	    bool saved_in_offsetof_p;
2535
2536	    /* Consume the "__offsetof__" token.  */
2537	    cp_lexer_consume_token (parser->lexer);
2538	    /* Consume the opening `('.  */
2539	    cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2540	    /* Parse the parenthesized (almost) constant-expression.  */
2541	    saved_in_offsetof_p = parser->in_offsetof_p;
2542	    parser->in_offsetof_p = true;
2543	    expression
2544	      = cp_parser_constant_expression (parser,
2545					       /*allow_non_constant_p=*/false,
2546					       /*non_constant_p=*/NULL);
2547	    parser->in_offsetof_p = saved_in_offsetof_p;
2548	    /* Consume the closing ')'.  */
2549	    cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2550
2551	    return expression;
2552	  }
2553
2554	default:
2555	  cp_parser_error (parser, "expected primary-expression");
2556	  return error_mark_node;
2557	}
2558
2559      /* An id-expression can start with either an identifier, a
2560	 `::' as the beginning of a qualified-id, or the "operator"
2561	 keyword.  */
2562    case CPP_NAME:
2563    case CPP_SCOPE:
2564    case CPP_TEMPLATE_ID:
2565    case CPP_NESTED_NAME_SPECIFIER:
2566      {
2567	tree id_expression;
2568	tree decl;
2569	const char *error_msg;
2570
2571      id_expression:
2572	/* Parse the id-expression.  */
2573	id_expression
2574	  = cp_parser_id_expression (parser,
2575				     /*template_keyword_p=*/false,
2576				     /*check_dependency_p=*/true,
2577				     /*template_p=*/NULL,
2578				     /*declarator_p=*/false);
2579	if (id_expression == error_mark_node)
2580	  return error_mark_node;
2581	/* If we have a template-id, then no further lookup is
2582	   required.  If the template-id was for a template-class, we
2583	   will sometimes have a TYPE_DECL at this point.  */
2584	else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2585	    || TREE_CODE (id_expression) == TYPE_DECL)
2586	  decl = id_expression;
2587	/* Look up the name.  */
2588	else
2589	  {
2590	    decl = cp_parser_lookup_name_simple (parser, id_expression);
2591	    /* If name lookup gives us a SCOPE_REF, then the
2592	       qualifying scope was dependent.  Just propagate the
2593	       name.  */
2594	    if (TREE_CODE (decl) == SCOPE_REF)
2595	      {
2596		if (TYPE_P (TREE_OPERAND (decl, 0)))
2597		  *qualifying_class = TREE_OPERAND (decl, 0);
2598		return decl;
2599	      }
2600	    /* Check to see if DECL is a local variable in a context
2601	       where that is forbidden.  */
2602	    if (parser->local_variables_forbidden_p
2603		&& local_variable_p (decl))
2604	      {
2605		/* It might be that we only found DECL because we are
2606		   trying to be generous with pre-ISO scoping rules.
2607		   For example, consider:
2608
2609		     int i;
2610		     void g() {
2611		       for (int i = 0; i < 10; ++i) {}
2612		       extern void f(int j = i);
2613		     }
2614
2615		   Here, name look up will originally find the out
2616		   of scope `i'.  We need to issue a warning message,
2617		   but then use the global `i'.  */
2618		decl = check_for_out_of_scope_variable (decl);
2619		if (local_variable_p (decl))
2620		  {
2621		    error ("local variable `%D' may not appear in this context",
2622			   decl);
2623		    return error_mark_node;
2624		  }
2625	      }
2626	  }
2627
2628	decl = finish_id_expression (id_expression, decl, parser->scope,
2629				     idk, qualifying_class,
2630				     parser->integral_constant_expression_p,
2631				     parser->allow_non_integral_constant_expression_p,
2632				     &parser->non_integral_constant_expression_p,
2633				     &error_msg);
2634	if (error_msg)
2635	  cp_parser_error (parser, error_msg);
2636	return decl;
2637      }
2638
2639      /* Anything else is an error.  */
2640    default:
2641      cp_parser_error (parser, "expected primary-expression");
2642      return error_mark_node;
2643    }
2644}
2645
2646/* Parse an id-expression.
2647
2648   id-expression:
2649     unqualified-id
2650     qualified-id
2651
2652   qualified-id:
2653     :: [opt] nested-name-specifier template [opt] unqualified-id
2654     :: identifier
2655     :: operator-function-id
2656     :: template-id
2657
2658   Return a representation of the unqualified portion of the
2659   identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2660   a `::' or nested-name-specifier.
2661
2662   Often, if the id-expression was a qualified-id, the caller will
2663   want to make a SCOPE_REF to represent the qualified-id.  This
2664   function does not do this in order to avoid wastefully creating
2665   SCOPE_REFs when they are not required.
2666
2667   If TEMPLATE_KEYWORD_P is true, then we have just seen the
2668   `template' keyword.
2669
2670   If CHECK_DEPENDENCY_P is false, then names are looked up inside
2671   uninstantiated templates.
2672
2673   If *TEMPLATE_P is non-NULL, it is set to true iff the
2674   `template' keyword is used to explicitly indicate that the entity
2675   named is a template.
2676
2677   If DECLARATOR_P is true, the id-expression is appearing as part of
2678   a declarator, rather than as part of an expression.  */
2679
2680static tree
2681cp_parser_id_expression (cp_parser *parser,
2682			 bool template_keyword_p,
2683			 bool check_dependency_p,
2684			 bool *template_p,
2685			 bool declarator_p)
2686{
2687  bool global_scope_p;
2688  bool nested_name_specifier_p;
2689
2690  /* Assume the `template' keyword was not used.  */
2691  if (template_p)
2692    *template_p = false;
2693
2694  /* Look for the optional `::' operator.  */
2695  global_scope_p
2696    = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2697       != NULL_TREE);
2698  /* Look for the optional nested-name-specifier.  */
2699  nested_name_specifier_p
2700    = (cp_parser_nested_name_specifier_opt (parser,
2701					    /*typename_keyword_p=*/false,
2702					    check_dependency_p,
2703					    /*type_p=*/false,
2704					    /*is_declarator=*/false)
2705       != NULL_TREE);
2706  /* If there is a nested-name-specifier, then we are looking at
2707     the first qualified-id production.  */
2708  if (nested_name_specifier_p)
2709    {
2710      tree saved_scope;
2711      tree saved_object_scope;
2712      tree saved_qualifying_scope;
2713      tree unqualified_id;
2714      bool is_template;
2715
2716      /* See if the next token is the `template' keyword.  */
2717      if (!template_p)
2718	template_p = &is_template;
2719      *template_p = cp_parser_optional_template_keyword (parser);
2720      /* Name lookup we do during the processing of the
2721	 unqualified-id might obliterate SCOPE.  */
2722      saved_scope = parser->scope;
2723      saved_object_scope = parser->object_scope;
2724      saved_qualifying_scope = parser->qualifying_scope;
2725      /* Process the final unqualified-id.  */
2726      unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2727						 check_dependency_p,
2728						 declarator_p);
2729      /* Restore the SAVED_SCOPE for our caller.  */
2730      parser->scope = saved_scope;
2731      parser->object_scope = saved_object_scope;
2732      parser->qualifying_scope = saved_qualifying_scope;
2733
2734      return unqualified_id;
2735    }
2736  /* Otherwise, if we are in global scope, then we are looking at one
2737     of the other qualified-id productions.  */
2738  else if (global_scope_p)
2739    {
2740      cp_token *token;
2741      tree id;
2742
2743      /* Peek at the next token.  */
2744      token = cp_lexer_peek_token (parser->lexer);
2745
2746      /* If it's an identifier, and the next token is not a "<", then
2747	 we can avoid the template-id case.  This is an optimization
2748	 for this common case.  */
2749      if (token->type == CPP_NAME
2750	  && !cp_parser_nth_token_starts_template_argument_list_p
2751	       (parser, 2))
2752	return cp_parser_identifier (parser);
2753
2754      cp_parser_parse_tentatively (parser);
2755      /* Try a template-id.  */
2756      id = cp_parser_template_id (parser,
2757				  /*template_keyword_p=*/false,
2758				  /*check_dependency_p=*/true,
2759				  declarator_p);
2760      /* If that worked, we're done.  */
2761      if (cp_parser_parse_definitely (parser))
2762	return id;
2763
2764      /* Peek at the next token.  (Changes in the token buffer may
2765	 have invalidated the pointer obtained above.)  */
2766      token = cp_lexer_peek_token (parser->lexer);
2767
2768      switch (token->type)
2769	{
2770	case CPP_NAME:
2771	  return cp_parser_identifier (parser);
2772
2773	case CPP_KEYWORD:
2774	  if (token->keyword == RID_OPERATOR)
2775	    return cp_parser_operator_function_id (parser);
2776	  /* Fall through.  */
2777
2778	default:
2779	  cp_parser_error (parser, "expected id-expression");
2780	  return error_mark_node;
2781	}
2782    }
2783  else
2784    return cp_parser_unqualified_id (parser, template_keyword_p,
2785				     /*check_dependency_p=*/true,
2786				     declarator_p);
2787}
2788
2789/* Parse an unqualified-id.
2790
2791   unqualified-id:
2792     identifier
2793     operator-function-id
2794     conversion-function-id
2795     ~ class-name
2796     template-id
2797
2798   If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2799   keyword, in a construct like `A::template ...'.
2800
2801   Returns a representation of unqualified-id.  For the `identifier'
2802   production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
2803   production a BIT_NOT_EXPR is returned; the operand of the
2804   BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
2805   other productions, see the documentation accompanying the
2806   corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
2807   names are looked up in uninstantiated templates.  If DECLARATOR_P
2808   is true, the unqualified-id is appearing as part of a declarator,
2809   rather than as part of an expression.  */
2810
2811static tree
2812cp_parser_unqualified_id (cp_parser* parser,
2813                          bool template_keyword_p,
2814			  bool check_dependency_p,
2815			  bool declarator_p)
2816{
2817  cp_token *token;
2818
2819  /* Peek at the next token.  */
2820  token = cp_lexer_peek_token (parser->lexer);
2821
2822  switch (token->type)
2823    {
2824    case CPP_NAME:
2825      {
2826	tree id;
2827
2828	/* We don't know yet whether or not this will be a
2829	   template-id.  */
2830	cp_parser_parse_tentatively (parser);
2831	/* Try a template-id.  */
2832	id = cp_parser_template_id (parser, template_keyword_p,
2833				    check_dependency_p,
2834				    declarator_p);
2835	/* If it worked, we're done.  */
2836	if (cp_parser_parse_definitely (parser))
2837	  return id;
2838	/* Otherwise, it's an ordinary identifier.  */
2839	return cp_parser_identifier (parser);
2840      }
2841
2842    case CPP_TEMPLATE_ID:
2843      return cp_parser_template_id (parser, template_keyword_p,
2844				    check_dependency_p,
2845				    declarator_p);
2846
2847    case CPP_COMPL:
2848      {
2849	tree type_decl;
2850	tree qualifying_scope;
2851	tree object_scope;
2852	tree scope;
2853
2854	/* Consume the `~' token.  */
2855	cp_lexer_consume_token (parser->lexer);
2856	/* Parse the class-name.  The standard, as written, seems to
2857	   say that:
2858
2859	     template <typename T> struct S { ~S (); };
2860	     template <typename T> S<T>::~S() {}
2861
2862           is invalid, since `~' must be followed by a class-name, but
2863	   `S<T>' is dependent, and so not known to be a class.
2864	   That's not right; we need to look in uninstantiated
2865	   templates.  A further complication arises from:
2866
2867	     template <typename T> void f(T t) {
2868	       t.T::~T();
2869	     }
2870
2871	   Here, it is not possible to look up `T' in the scope of `T'
2872	   itself.  We must look in both the current scope, and the
2873	   scope of the containing complete expression.
2874
2875	   Yet another issue is:
2876
2877             struct S {
2878               int S;
2879               ~S();
2880             };
2881
2882             S::~S() {}
2883
2884           The standard does not seem to say that the `S' in `~S'
2885	   should refer to the type `S' and not the data member
2886	   `S::S'.  */
2887
2888	/* DR 244 says that we look up the name after the "~" in the
2889	   same scope as we looked up the qualifying name.  That idea
2890	   isn't fully worked out; it's more complicated than that.  */
2891	scope = parser->scope;
2892	object_scope = parser->object_scope;
2893	qualifying_scope = parser->qualifying_scope;
2894
2895	/* If the name is of the form "X::~X" it's OK.  */
2896	if (scope && TYPE_P (scope)
2897	    && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2898	    && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2899		== CPP_OPEN_PAREN)
2900	    && (cp_lexer_peek_token (parser->lexer)->value
2901		== TYPE_IDENTIFIER (scope)))
2902	  {
2903	    cp_lexer_consume_token (parser->lexer);
2904	    return build_nt (BIT_NOT_EXPR, scope);
2905	  }
2906
2907	/* If there was an explicit qualification (S::~T), first look
2908	   in the scope given by the qualification (i.e., S).  */
2909	if (scope)
2910	  {
2911	    cp_parser_parse_tentatively (parser);
2912	    type_decl = cp_parser_class_name (parser,
2913					      /*typename_keyword_p=*/false,
2914					      /*template_keyword_p=*/false,
2915					      /*type_p=*/false,
2916					      /*check_dependency=*/false,
2917					      /*class_head_p=*/false,
2918					      declarator_p);
2919	    if (cp_parser_parse_definitely (parser))
2920	      return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2921	  }
2922	/* In "N::S::~S", look in "N" as well.  */
2923	if (scope && qualifying_scope)
2924	  {
2925	    cp_parser_parse_tentatively (parser);
2926	    parser->scope = qualifying_scope;
2927	    parser->object_scope = NULL_TREE;
2928	    parser->qualifying_scope = NULL_TREE;
2929	    type_decl
2930	      = cp_parser_class_name (parser,
2931				      /*typename_keyword_p=*/false,
2932				      /*template_keyword_p=*/false,
2933				      /*type_p=*/false,
2934				      /*check_dependency=*/false,
2935				      /*class_head_p=*/false,
2936				      declarator_p);
2937	    if (cp_parser_parse_definitely (parser))
2938	      return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2939	  }
2940	/* In "p->S::~T", look in the scope given by "*p" as well.  */
2941	else if (object_scope)
2942	  {
2943	    cp_parser_parse_tentatively (parser);
2944	    parser->scope = object_scope;
2945	    parser->object_scope = NULL_TREE;
2946	    parser->qualifying_scope = NULL_TREE;
2947	    type_decl
2948	      = cp_parser_class_name (parser,
2949				      /*typename_keyword_p=*/false,
2950				      /*template_keyword_p=*/false,
2951				      /*type_p=*/false,
2952				      /*check_dependency=*/false,
2953				      /*class_head_p=*/false,
2954				      declarator_p);
2955	    if (cp_parser_parse_definitely (parser))
2956	      return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2957	  }
2958	/* Look in the surrounding context.  */
2959	parser->scope = NULL_TREE;
2960	parser->object_scope = NULL_TREE;
2961	parser->qualifying_scope = NULL_TREE;
2962	type_decl
2963	  = cp_parser_class_name (parser,
2964				  /*typename_keyword_p=*/false,
2965				  /*template_keyword_p=*/false,
2966				  /*type_p=*/false,
2967				  /*check_dependency=*/false,
2968				  /*class_head_p=*/false,
2969				  declarator_p);
2970	/* If an error occurred, assume that the name of the
2971	   destructor is the same as the name of the qualifying
2972	   class.  That allows us to keep parsing after running
2973	   into ill-formed destructor names.  */
2974	if (type_decl == error_mark_node && scope && TYPE_P (scope))
2975	  return build_nt (BIT_NOT_EXPR, scope);
2976	else if (type_decl == error_mark_node)
2977	  return error_mark_node;
2978
2979	/* [class.dtor]
2980
2981	   A typedef-name that names a class shall not be used as the
2982	   identifier in the declarator for a destructor declaration.  */
2983	if (declarator_p
2984	    && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2985	    && !DECL_SELF_REFERENCE_P (type_decl))
2986	  error ("typedef-name `%D' used as destructor declarator",
2987		 type_decl);
2988
2989	return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2990      }
2991
2992    case CPP_KEYWORD:
2993      if (token->keyword == RID_OPERATOR)
2994	{
2995	  tree id;
2996
2997	  /* This could be a template-id, so we try that first.  */
2998	  cp_parser_parse_tentatively (parser);
2999	  /* Try a template-id.  */
3000	  id = cp_parser_template_id (parser, template_keyword_p,
3001				      /*check_dependency_p=*/true,
3002				      declarator_p);
3003	  /* If that worked, we're done.  */
3004	  if (cp_parser_parse_definitely (parser))
3005	    return id;
3006	  /* We still don't know whether we're looking at an
3007	     operator-function-id or a conversion-function-id.  */
3008	  cp_parser_parse_tentatively (parser);
3009	  /* Try an operator-function-id.  */
3010	  id = cp_parser_operator_function_id (parser);
3011	  /* If that didn't work, try a conversion-function-id.  */
3012	  if (!cp_parser_parse_definitely (parser))
3013	    id = cp_parser_conversion_function_id (parser);
3014
3015	  return id;
3016	}
3017      /* Fall through.  */
3018
3019    default:
3020      cp_parser_error (parser, "expected unqualified-id");
3021      return error_mark_node;
3022    }
3023}
3024
3025/* Parse an (optional) nested-name-specifier.
3026
3027   nested-name-specifier:
3028     class-or-namespace-name :: nested-name-specifier [opt]
3029     class-or-namespace-name :: template nested-name-specifier [opt]
3030
3031   PARSER->SCOPE should be set appropriately before this function is
3032   called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3033   effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3034   in name lookups.
3035
3036   Sets PARSER->SCOPE to the class (TYPE) or namespace
3037   (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3038   it unchanged if there is no nested-name-specifier.  Returns the new
3039   scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3040
3041   If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3042   part of a declaration and/or decl-specifier.  */
3043
3044static tree
3045cp_parser_nested_name_specifier_opt (cp_parser *parser,
3046				     bool typename_keyword_p,
3047				     bool check_dependency_p,
3048				     bool type_p,
3049				     bool is_declaration)
3050{
3051  bool success = false;
3052  tree access_check = NULL_TREE;
3053  ptrdiff_t start;
3054  cp_token* token;
3055
3056  /* If the next token corresponds to a nested name specifier, there
3057     is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3058     false, it may have been true before, in which case something
3059     like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3060     of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3061     CHECK_DEPENDENCY_P is false, we have to fall through into the
3062     main loop.  */
3063  if (check_dependency_p
3064      && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3065    {
3066      cp_parser_pre_parsed_nested_name_specifier (parser);
3067      return parser->scope;
3068    }
3069
3070  /* Remember where the nested-name-specifier starts.  */
3071  if (cp_parser_parsing_tentatively (parser)
3072      && !cp_parser_committed_to_tentative_parse (parser))
3073    {
3074      token = cp_lexer_peek_token (parser->lexer);
3075      start = cp_lexer_token_difference (parser->lexer,
3076					 parser->lexer->first_token,
3077					 token);
3078    }
3079  else
3080    start = -1;
3081
3082  push_deferring_access_checks (dk_deferred);
3083
3084  while (true)
3085    {
3086      tree new_scope;
3087      tree old_scope;
3088      tree saved_qualifying_scope;
3089      bool template_keyword_p;
3090
3091      /* Spot cases that cannot be the beginning of a
3092	 nested-name-specifier.  */
3093      token = cp_lexer_peek_token (parser->lexer);
3094
3095      /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3096	 the already parsed nested-name-specifier.  */
3097      if (token->type == CPP_NESTED_NAME_SPECIFIER)
3098	{
3099	  /* Grab the nested-name-specifier and continue the loop.  */
3100	  cp_parser_pre_parsed_nested_name_specifier (parser);
3101	  success = true;
3102	  continue;
3103	}
3104
3105      /* Spot cases that cannot be the beginning of a
3106	 nested-name-specifier.  On the second and subsequent times
3107	 through the loop, we look for the `template' keyword.  */
3108      if (success && token->keyword == RID_TEMPLATE)
3109	;
3110      /* A template-id can start a nested-name-specifier.  */
3111      else if (token->type == CPP_TEMPLATE_ID)
3112	;
3113      else
3114	{
3115	  /* If the next token is not an identifier, then it is
3116	     definitely not a class-or-namespace-name.  */
3117	  if (token->type != CPP_NAME)
3118	    break;
3119	  /* If the following token is neither a `<' (to begin a
3120	     template-id), nor a `::', then we are not looking at a
3121	     nested-name-specifier.  */
3122	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
3123	  if (token->type != CPP_SCOPE
3124	      && !cp_parser_nth_token_starts_template_argument_list_p
3125		  (parser, 2))
3126	    break;
3127	}
3128
3129      /* The nested-name-specifier is optional, so we parse
3130	 tentatively.  */
3131      cp_parser_parse_tentatively (parser);
3132
3133      /* Look for the optional `template' keyword, if this isn't the
3134	 first time through the loop.  */
3135      if (success)
3136	template_keyword_p = cp_parser_optional_template_keyword (parser);
3137      else
3138	template_keyword_p = false;
3139
3140      /* Save the old scope since the name lookup we are about to do
3141	 might destroy it.  */
3142      old_scope = parser->scope;
3143      saved_qualifying_scope = parser->qualifying_scope;
3144      /* Parse the qualifying entity.  */
3145      new_scope
3146	= cp_parser_class_or_namespace_name (parser,
3147					     typename_keyword_p,
3148					     template_keyword_p,
3149					     check_dependency_p,
3150					     type_p,
3151					     is_declaration);
3152      /* Look for the `::' token.  */
3153      cp_parser_require (parser, CPP_SCOPE, "`::'");
3154
3155      /* If we found what we wanted, we keep going; otherwise, we're
3156	 done.  */
3157      if (!cp_parser_parse_definitely (parser))
3158	{
3159	  bool error_p = false;
3160
3161	  /* Restore the OLD_SCOPE since it was valid before the
3162	     failed attempt at finding the last
3163	     class-or-namespace-name.  */
3164	  parser->scope = old_scope;
3165	  parser->qualifying_scope = saved_qualifying_scope;
3166	  /* If the next token is an identifier, and the one after
3167	     that is a `::', then any valid interpretation would have
3168	     found a class-or-namespace-name.  */
3169	  while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3170		 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3171		     == CPP_SCOPE)
3172		 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3173		     != CPP_COMPL))
3174	    {
3175	      token = cp_lexer_consume_token (parser->lexer);
3176	      if (!error_p)
3177		{
3178		  tree decl;
3179
3180		  decl = cp_parser_lookup_name_simple (parser, token->value);
3181		  if (TREE_CODE (decl) == TEMPLATE_DECL)
3182		    error ("`%D' used without template parameters",
3183			   decl);
3184		  else
3185		    cp_parser_name_lookup_error
3186		      (parser, token->value, decl,
3187		       "is not a class or namespace");
3188		  parser->scope = NULL_TREE;
3189		  error_p = true;
3190		  /* Treat this as a successful nested-name-specifier
3191		     due to:
3192
3193		     [basic.lookup.qual]
3194
3195		     If the name found is not a class-name (clause
3196		     _class_) or namespace-name (_namespace.def_), the
3197		     program is ill-formed.  */
3198		  success = true;
3199		}
3200	      cp_lexer_consume_token (parser->lexer);
3201	    }
3202	  break;
3203	}
3204
3205      /* We've found one valid nested-name-specifier.  */
3206      success = true;
3207      /* Make sure we look in the right scope the next time through
3208	 the loop.  */
3209      parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3210		       ? TREE_TYPE (new_scope)
3211		       : new_scope);
3212      /* If it is a class scope, try to complete it; we are about to
3213	 be looking up names inside the class.  */
3214      if (TYPE_P (parser->scope)
3215	  /* Since checking types for dependency can be expensive,
3216	     avoid doing it if the type is already complete.  */
3217	  && !COMPLETE_TYPE_P (parser->scope)
3218	  /* Do not try to complete dependent types.  */
3219	  && !dependent_type_p (parser->scope))
3220	complete_type (parser->scope);
3221    }
3222
3223  /* Retrieve any deferred checks.  Do not pop this access checks yet
3224     so the memory will not be reclaimed during token replacing below.  */
3225  access_check = get_deferred_access_checks ();
3226
3227  /* If parsing tentatively, replace the sequence of tokens that makes
3228     up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3229     token.  That way, should we re-parse the token stream, we will
3230     not have to repeat the effort required to do the parse, nor will
3231     we issue duplicate error messages.  */
3232  if (success && start >= 0)
3233    {
3234      /* Find the token that corresponds to the start of the
3235	 template-id.  */
3236      token = cp_lexer_advance_token (parser->lexer,
3237				      parser->lexer->first_token,
3238				      start);
3239
3240      /* Reset the contents of the START token.  */
3241      token->type = CPP_NESTED_NAME_SPECIFIER;
3242      token->value = build_tree_list (access_check, parser->scope);
3243      TREE_TYPE (token->value) = parser->qualifying_scope;
3244      token->keyword = RID_MAX;
3245      /* Purge all subsequent tokens.  */
3246      cp_lexer_purge_tokens_after (parser->lexer, token);
3247    }
3248
3249  pop_deferring_access_checks ();
3250  return success ? parser->scope : NULL_TREE;
3251}
3252
3253/* Parse a nested-name-specifier.  See
3254   cp_parser_nested_name_specifier_opt for details.  This function
3255   behaves identically, except that it will an issue an error if no
3256   nested-name-specifier is present, and it will return
3257   ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3258   is present.  */
3259
3260static tree
3261cp_parser_nested_name_specifier (cp_parser *parser,
3262				 bool typename_keyword_p,
3263				 bool check_dependency_p,
3264				 bool type_p,
3265				 bool is_declaration)
3266{
3267  tree scope;
3268
3269  /* Look for the nested-name-specifier.  */
3270  scope = cp_parser_nested_name_specifier_opt (parser,
3271					       typename_keyword_p,
3272					       check_dependency_p,
3273					       type_p,
3274					       is_declaration);
3275  /* If it was not present, issue an error message.  */
3276  if (!scope)
3277    {
3278      cp_parser_error (parser, "expected nested-name-specifier");
3279      parser->scope = NULL_TREE;
3280      return error_mark_node;
3281    }
3282
3283  return scope;
3284}
3285
3286/* Parse a class-or-namespace-name.
3287
3288   class-or-namespace-name:
3289     class-name
3290     namespace-name
3291
3292   TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3293   TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3294   CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3295   TYPE_P is TRUE iff the next name should be taken as a class-name,
3296   even the same name is declared to be another entity in the same
3297   scope.
3298
3299   Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3300   specified by the class-or-namespace-name.  If neither is found the
3301   ERROR_MARK_NODE is returned.  */
3302
3303static tree
3304cp_parser_class_or_namespace_name (cp_parser *parser,
3305				   bool typename_keyword_p,
3306				   bool template_keyword_p,
3307				   bool check_dependency_p,
3308				   bool type_p,
3309				   bool is_declaration)
3310{
3311  tree saved_scope;
3312  tree saved_qualifying_scope;
3313  tree saved_object_scope;
3314  tree scope;
3315  bool only_class_p;
3316
3317  /* Before we try to parse the class-name, we must save away the
3318     current PARSER->SCOPE since cp_parser_class_name will destroy
3319     it.  */
3320  saved_scope = parser->scope;
3321  saved_qualifying_scope = parser->qualifying_scope;
3322  saved_object_scope = parser->object_scope;
3323  /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3324     there is no need to look for a namespace-name.  */
3325  only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3326  if (!only_class_p)
3327    cp_parser_parse_tentatively (parser);
3328  scope = cp_parser_class_name (parser,
3329				typename_keyword_p,
3330				template_keyword_p,
3331				type_p,
3332				check_dependency_p,
3333				/*class_head_p=*/false,
3334				is_declaration);
3335  /* If that didn't work, try for a namespace-name.  */
3336  if (!only_class_p && !cp_parser_parse_definitely (parser))
3337    {
3338      /* Restore the saved scope.  */
3339      parser->scope = saved_scope;
3340      parser->qualifying_scope = saved_qualifying_scope;
3341      parser->object_scope = saved_object_scope;
3342      /* If we are not looking at an identifier followed by the scope
3343	 resolution operator, then this is not part of a
3344	 nested-name-specifier.  (Note that this function is only used
3345	 to parse the components of a nested-name-specifier.)  */
3346      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3347	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3348	return error_mark_node;
3349      scope = cp_parser_namespace_name (parser);
3350    }
3351
3352  return scope;
3353}
3354
3355/* Parse a postfix-expression.
3356
3357   postfix-expression:
3358     primary-expression
3359     postfix-expression [ expression ]
3360     postfix-expression ( expression-list [opt] )
3361     simple-type-specifier ( expression-list [opt] )
3362     typename :: [opt] nested-name-specifier identifier
3363       ( expression-list [opt] )
3364     typename :: [opt] nested-name-specifier template [opt] template-id
3365       ( expression-list [opt] )
3366     postfix-expression . template [opt] id-expression
3367     postfix-expression -> template [opt] id-expression
3368     postfix-expression . pseudo-destructor-name
3369     postfix-expression -> pseudo-destructor-name
3370     postfix-expression ++
3371     postfix-expression --
3372     dynamic_cast < type-id > ( expression )
3373     static_cast < type-id > ( expression )
3374     reinterpret_cast < type-id > ( expression )
3375     const_cast < type-id > ( expression )
3376     typeid ( expression )
3377     typeid ( type-id )
3378
3379   GNU Extension:
3380
3381   postfix-expression:
3382     ( type-id ) { initializer-list , [opt] }
3383
3384   This extension is a GNU version of the C99 compound-literal
3385   construct.  (The C99 grammar uses `type-name' instead of `type-id',
3386   but they are essentially the same concept.)
3387
3388   If ADDRESS_P is true, the postfix expression is the operand of the
3389   `&' operator.
3390
3391   Returns a representation of the expression.  */
3392
3393static tree
3394cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3395{
3396  cp_token *token;
3397  enum rid keyword;
3398  cp_id_kind idk = CP_ID_KIND_NONE;
3399  tree postfix_expression = NULL_TREE;
3400  /* Non-NULL only if the current postfix-expression can be used to
3401     form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3402     class used to qualify the member.  */
3403  tree qualifying_class = NULL_TREE;
3404
3405  /* Peek at the next token.  */
3406  token = cp_lexer_peek_token (parser->lexer);
3407  /* Some of the productions are determined by keywords.  */
3408  keyword = token->keyword;
3409  switch (keyword)
3410    {
3411    case RID_DYNCAST:
3412    case RID_STATCAST:
3413    case RID_REINTCAST:
3414    case RID_CONSTCAST:
3415      {
3416	tree type;
3417	tree expression;
3418	const char *saved_message;
3419
3420	/* All of these can be handled in the same way from the point
3421	   of view of parsing.  Begin by consuming the token
3422	   identifying the cast.  */
3423	cp_lexer_consume_token (parser->lexer);
3424
3425	/* New types cannot be defined in the cast.  */
3426	saved_message = parser->type_definition_forbidden_message;
3427	parser->type_definition_forbidden_message
3428	  = "types may not be defined in casts";
3429
3430	/* Look for the opening `<'.  */
3431	cp_parser_require (parser, CPP_LESS, "`<'");
3432	/* Parse the type to which we are casting.  */
3433	type = cp_parser_type_id (parser);
3434	/* Look for the closing `>'.  */
3435	cp_parser_require (parser, CPP_GREATER, "`>'");
3436	/* Restore the old message.  */
3437	parser->type_definition_forbidden_message = saved_message;
3438
3439	/* And the expression which is being cast.  */
3440	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3441	expression = cp_parser_expression (parser);
3442	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3443
3444	/* Only type conversions to integral or enumeration types
3445	   can be used in constant-expressions.  */
3446	if (parser->integral_constant_expression_p
3447	    && !dependent_type_p (type)
3448	    && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3449	    /* A cast to pointer or reference type is allowed in the
3450	       implementation of "offsetof".  */
3451	    && !(parser->in_offsetof_p && POINTER_TYPE_P (type))
3452	    && (cp_parser_non_integral_constant_expression
3453		(parser,
3454		 "a cast to a type other than an integral or "
3455		 "enumeration type")))
3456	  return error_mark_node;
3457
3458	switch (keyword)
3459	  {
3460	  case RID_DYNCAST:
3461	    postfix_expression
3462	      = build_dynamic_cast (type, expression);
3463	    break;
3464	  case RID_STATCAST:
3465	    postfix_expression
3466	      = build_static_cast (type, expression);
3467	    break;
3468	  case RID_REINTCAST:
3469	    postfix_expression
3470	      = build_reinterpret_cast (type, expression);
3471	    break;
3472	  case RID_CONSTCAST:
3473	    postfix_expression
3474	      = build_const_cast (type, expression);
3475	    break;
3476	  default:
3477	    abort ();
3478	  }
3479      }
3480      break;
3481
3482    case RID_TYPEID:
3483      {
3484	tree type;
3485	const char *saved_message;
3486	bool saved_in_type_id_in_expr_p;
3487
3488	/* Consume the `typeid' token.  */
3489	cp_lexer_consume_token (parser->lexer);
3490	/* Look for the `(' token.  */
3491	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3492	/* Types cannot be defined in a `typeid' expression.  */
3493	saved_message = parser->type_definition_forbidden_message;
3494	parser->type_definition_forbidden_message
3495	  = "types may not be defined in a `typeid\' expression";
3496	/* We can't be sure yet whether we're looking at a type-id or an
3497	   expression.  */
3498	cp_parser_parse_tentatively (parser);
3499	/* Try a type-id first.  */
3500	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3501	parser->in_type_id_in_expr_p = true;
3502	type = cp_parser_type_id (parser);
3503	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3504	/* Look for the `)' token.  Otherwise, we can't be sure that
3505	   we're not looking at an expression: consider `typeid (int
3506	   (3))', for example.  */
3507	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3508	/* If all went well, simply lookup the type-id.  */
3509	if (cp_parser_parse_definitely (parser))
3510	  postfix_expression = get_typeid (type);
3511	/* Otherwise, fall back to the expression variant.  */
3512	else
3513	  {
3514	    tree expression;
3515
3516	    /* Look for an expression.  */
3517	    expression = cp_parser_expression (parser);
3518	    /* Compute its typeid.  */
3519	    postfix_expression = build_typeid (expression);
3520	    /* Look for the `)' token.  */
3521	    cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3522	  }
3523	/* `typeid' may not appear in an integral constant expression.  */
3524	if (cp_parser_non_integral_constant_expression(parser,
3525						       "`typeid' operator"))
3526	  return error_mark_node;
3527	/* Restore the saved message.  */
3528	parser->type_definition_forbidden_message = saved_message;
3529      }
3530      break;
3531
3532    case RID_TYPENAME:
3533      {
3534	bool template_p = false;
3535	tree id;
3536	tree type;
3537
3538	/* Consume the `typename' token.  */
3539	cp_lexer_consume_token (parser->lexer);
3540	/* Look for the optional `::' operator.  */
3541	cp_parser_global_scope_opt (parser,
3542				    /*current_scope_valid_p=*/false);
3543	/* Look for the nested-name-specifier.  */
3544	cp_parser_nested_name_specifier (parser,
3545					 /*typename_keyword_p=*/true,
3546					 /*check_dependency_p=*/true,
3547					 /*type_p=*/true,
3548					 /*is_declaration=*/true);
3549	/* Look for the optional `template' keyword.  */
3550	template_p = cp_parser_optional_template_keyword (parser);
3551	/* We don't know whether we're looking at a template-id or an
3552	   identifier.  */
3553	cp_parser_parse_tentatively (parser);
3554	/* Try a template-id.  */
3555	id = cp_parser_template_id (parser, template_p,
3556				    /*check_dependency_p=*/true,
3557				    /*is_declaration=*/true);
3558	/* If that didn't work, try an identifier.  */
3559	if (!cp_parser_parse_definitely (parser))
3560	  id = cp_parser_identifier (parser);
3561	/* If we look up a template-id in a non-dependent qualifying
3562	   scope, there's no need to create a dependent type.  */
3563	if (TREE_CODE (id) == TYPE_DECL
3564	    && !dependent_type_p (parser->scope))
3565	  type = TREE_TYPE (id);
3566	/* Create a TYPENAME_TYPE to represent the type to which the
3567	   functional cast is being performed.  */
3568	else
3569	  type = make_typename_type (parser->scope, id,
3570				     /*complain=*/1);
3571
3572	postfix_expression = cp_parser_functional_cast (parser, type);
3573      }
3574      break;
3575
3576    default:
3577      {
3578	tree type;
3579
3580	/* If the next thing is a simple-type-specifier, we may be
3581	   looking at a functional cast.  We could also be looking at
3582	   an id-expression.  So, we try the functional cast, and if
3583	   that doesn't work we fall back to the primary-expression.  */
3584	cp_parser_parse_tentatively (parser);
3585	/* Look for the simple-type-specifier.  */
3586	type = cp_parser_simple_type_specifier (parser,
3587						CP_PARSER_FLAGS_NONE,
3588						/*identifier_p=*/false);
3589	/* Parse the cast itself.  */
3590	if (!cp_parser_error_occurred (parser))
3591	  postfix_expression
3592	    = cp_parser_functional_cast (parser, type);
3593	/* If that worked, we're done.  */
3594	if (cp_parser_parse_definitely (parser))
3595	  break;
3596
3597	/* If the functional-cast didn't work out, try a
3598	   compound-literal.  */
3599	if (cp_parser_allow_gnu_extensions_p (parser)
3600	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3601	  {
3602	    tree initializer_list = NULL_TREE;
3603	    bool saved_in_type_id_in_expr_p;
3604
3605	    cp_parser_parse_tentatively (parser);
3606	    /* Consume the `('.  */
3607	    cp_lexer_consume_token (parser->lexer);
3608	    /* Parse the type.  */
3609	    saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3610	    parser->in_type_id_in_expr_p = true;
3611	    type = cp_parser_type_id (parser);
3612	    parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3613	    /* Look for the `)'.  */
3614	    cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3615	    /* Look for the `{'.  */
3616	    cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3617	    /* If things aren't going well, there's no need to
3618	       keep going.  */
3619	    if (!cp_parser_error_occurred (parser))
3620	      {
3621		bool non_constant_p;
3622		/* Parse the initializer-list.  */
3623		initializer_list
3624		  = cp_parser_initializer_list (parser, &non_constant_p);
3625		/* Allow a trailing `,'.  */
3626		if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3627		  cp_lexer_consume_token (parser->lexer);
3628		/* Look for the final `}'.  */
3629		cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3630	      }
3631	    /* If that worked, we're definitely looking at a
3632	       compound-literal expression.  */
3633	    if (cp_parser_parse_definitely (parser))
3634	      {
3635		/* Warn the user that a compound literal is not
3636		   allowed in standard C++.  */
3637		if (pedantic)
3638		  pedwarn ("ISO C++ forbids compound-literals");
3639		/* Form the representation of the compound-literal.  */
3640		postfix_expression
3641		  = finish_compound_literal (type, initializer_list);
3642		break;
3643	      }
3644	  }
3645
3646	/* It must be a primary-expression.  */
3647	postfix_expression = cp_parser_primary_expression (parser,
3648							   &idk,
3649							   &qualifying_class);
3650      }
3651      break;
3652    }
3653
3654  /* If we were avoiding committing to the processing of a
3655     qualified-id until we knew whether or not we had a
3656     pointer-to-member, we now know.  */
3657  if (qualifying_class)
3658    {
3659      bool done;
3660
3661      /* Peek at the next token.  */
3662      token = cp_lexer_peek_token (parser->lexer);
3663      done = (token->type != CPP_OPEN_SQUARE
3664	      && token->type != CPP_OPEN_PAREN
3665	      && token->type != CPP_DOT
3666	      && token->type != CPP_DEREF
3667	      && token->type != CPP_PLUS_PLUS
3668	      && token->type != CPP_MINUS_MINUS);
3669
3670      postfix_expression = finish_qualified_id_expr (qualifying_class,
3671						     postfix_expression,
3672						     done,
3673						     address_p);
3674      if (done)
3675	return postfix_expression;
3676    }
3677
3678  /* Keep looping until the postfix-expression is complete.  */
3679  while (true)
3680    {
3681      if (idk == CP_ID_KIND_UNQUALIFIED
3682	  && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3683	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3684	/* It is not a Koenig lookup function call.  */
3685	postfix_expression
3686	  = unqualified_name_lookup_error (postfix_expression);
3687
3688      /* Peek at the next token.  */
3689      token = cp_lexer_peek_token (parser->lexer);
3690
3691      switch (token->type)
3692	{
3693	case CPP_OPEN_SQUARE:
3694	  /* postfix-expression [ expression ] */
3695	  {
3696	    tree index;
3697
3698	    /* Consume the `[' token.  */
3699	    cp_lexer_consume_token (parser->lexer);
3700	    /* Parse the index expression.  */
3701	    index = cp_parser_expression (parser);
3702	    /* Look for the closing `]'.  */
3703	    cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3704
3705	    /* Build the ARRAY_REF.  */
3706	    postfix_expression
3707	      = grok_array_decl (postfix_expression, index);
3708	    idk = CP_ID_KIND_NONE;
3709	    /* Array references are not permitted in
3710	       constant-expressions (but they are allowed
3711	       in offsetof).  */
3712	    if (!parser->in_offsetof_p
3713		&& cp_parser_non_integral_constant_expression
3714		    (parser, "an array reference"))
3715	      postfix_expression = error_mark_node;
3716	  }
3717	  break;
3718
3719	case CPP_OPEN_PAREN:
3720	  /* postfix-expression ( expression-list [opt] ) */
3721	  {
3722	    bool koenig_p;
3723	    tree args = (cp_parser_parenthesized_expression_list
3724			 (parser, false, /*non_constant_p=*/NULL));
3725
3726	    if (args == error_mark_node)
3727	      {
3728		postfix_expression = error_mark_node;
3729		break;
3730	      }
3731
3732	    /* Function calls are not permitted in
3733	       constant-expressions.  */
3734	    if (cp_parser_non_integral_constant_expression (parser,
3735							    "a function call"))
3736	      {
3737		postfix_expression = error_mark_node;
3738		break;
3739	      }
3740
3741	    koenig_p = false;
3742	    if (idk == CP_ID_KIND_UNQUALIFIED)
3743	      {
3744		/* We do not perform argument-dependent lookup if
3745		   normal lookup finds a non-function, in accordance
3746		   with the expected resolution of DR 218.  */
3747		if (args
3748		    && (is_overloaded_fn (postfix_expression)
3749			|| TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3750		  {
3751		    koenig_p = true;
3752		    postfix_expression
3753		      = perform_koenig_lookup (postfix_expression, args);
3754		  }
3755		else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3756		  postfix_expression
3757		    = unqualified_fn_lookup_error (postfix_expression);
3758	      }
3759
3760	    if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3761	      {
3762		tree instance = TREE_OPERAND (postfix_expression, 0);
3763		tree fn = TREE_OPERAND (postfix_expression, 1);
3764
3765		if (processing_template_decl
3766		    && (type_dependent_expression_p (instance)
3767			|| (!BASELINK_P (fn)
3768			    && TREE_CODE (fn) != FIELD_DECL)
3769			|| type_dependent_expression_p (fn)
3770			|| any_type_dependent_arguments_p (args)))
3771		  {
3772		    postfix_expression
3773		      = build_min_nt (CALL_EXPR, postfix_expression, args);
3774		    break;
3775		  }
3776
3777		if (BASELINK_P (fn))
3778		  postfix_expression
3779		    = (build_new_method_call
3780		       (instance, fn, args, NULL_TREE,
3781			(idk == CP_ID_KIND_QUALIFIED
3782			 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3783		else
3784		  postfix_expression
3785		    = finish_call_expr (postfix_expression, args,
3786					/*disallow_virtual=*/false,
3787					/*koenig_p=*/false);
3788	      }
3789	    else if (TREE_CODE (postfix_expression) == OFFSET_REF
3790		     || TREE_CODE (postfix_expression) == MEMBER_REF
3791		     || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3792	      postfix_expression = (build_offset_ref_call_from_tree
3793				    (postfix_expression, args));
3794	    else if (idk == CP_ID_KIND_QUALIFIED)
3795	      /* A call to a static class member, or a namespace-scope
3796		 function.  */
3797	      postfix_expression
3798		= finish_call_expr (postfix_expression, args,
3799				    /*disallow_virtual=*/true,
3800				    koenig_p);
3801	    else
3802	      /* All other function calls.  */
3803	      postfix_expression
3804		= finish_call_expr (postfix_expression, args,
3805				    /*disallow_virtual=*/false,
3806				    koenig_p);
3807
3808	    /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
3809	    idk = CP_ID_KIND_NONE;
3810	  }
3811	  break;
3812
3813	case CPP_DOT:
3814	case CPP_DEREF:
3815	  /* postfix-expression . template [opt] id-expression
3816	     postfix-expression . pseudo-destructor-name
3817	     postfix-expression -> template [opt] id-expression
3818	     postfix-expression -> pseudo-destructor-name */
3819	  {
3820	    tree name;
3821	    bool dependent_p;
3822	    bool template_p;
3823	    tree scope = NULL_TREE;
3824	    enum cpp_ttype token_type = token->type;
3825
3826	    /* If this is a `->' operator, dereference the pointer.  */
3827	    if (token->type == CPP_DEREF)
3828	      postfix_expression = build_x_arrow (postfix_expression);
3829	    /* Check to see whether or not the expression is
3830	       type-dependent.  */
3831	    dependent_p = type_dependent_expression_p (postfix_expression);
3832	    /* The identifier following the `->' or `.' is not
3833	       qualified.  */
3834	    parser->scope = NULL_TREE;
3835	    parser->qualifying_scope = NULL_TREE;
3836	    parser->object_scope = NULL_TREE;
3837	    idk = CP_ID_KIND_NONE;
3838	    /* Enter the scope corresponding to the type of the object
3839	       given by the POSTFIX_EXPRESSION.  */
3840	    if (!dependent_p
3841		&& TREE_TYPE (postfix_expression) != NULL_TREE)
3842	      {
3843		scope = TREE_TYPE (postfix_expression);
3844		/* According to the standard, no expression should
3845		   ever have reference type.  Unfortunately, we do not
3846		   currently match the standard in this respect in
3847		   that our internal representation of an expression
3848		   may have reference type even when the standard says
3849		   it does not.  Therefore, we have to manually obtain
3850		   the underlying type here.  */
3851		scope = non_reference (scope);
3852		/* The type of the POSTFIX_EXPRESSION must be
3853		   complete.  */
3854		scope = complete_type_or_else (scope, NULL_TREE);
3855		/* Let the name lookup machinery know that we are
3856		   processing a class member access expression.  */
3857		parser->context->object_type = scope;
3858		/* If something went wrong, we want to be able to
3859		   discern that case, as opposed to the case where
3860		   there was no SCOPE due to the type of expression
3861		   being dependent.  */
3862		if (!scope)
3863		  scope = error_mark_node;
3864		/* If the SCOPE was erroneous, make the various
3865		   semantic analysis functions exit quickly -- and
3866		   without issuing additional error messages.  */
3867		if (scope == error_mark_node)
3868		  postfix_expression = error_mark_node;
3869	      }
3870
3871	    /* Consume the `.' or `->' operator.  */
3872	    cp_lexer_consume_token (parser->lexer);
3873	    /* If the SCOPE is not a scalar type, we are looking at an
3874	       ordinary class member access expression, rather than a
3875	       pseudo-destructor-name.  */
3876	    if (!scope || !SCALAR_TYPE_P (scope))
3877	      {
3878		template_p = cp_parser_optional_template_keyword (parser);
3879		/* Parse the id-expression.  */
3880		name = cp_parser_id_expression (parser,
3881						template_p,
3882						/*check_dependency_p=*/true,
3883						/*template_p=*/NULL,
3884						/*declarator_p=*/false);
3885		/* In general, build a SCOPE_REF if the member name is
3886		   qualified.  However, if the name was not dependent
3887		   and has already been resolved; there is no need to
3888		   build the SCOPE_REF.  For example;
3889
3890                     struct X { void f(); };
3891                     template <typename T> void f(T* t) { t->X::f(); }
3892
3893                   Even though "t" is dependent, "X::f" is not and has
3894		   been resolved to a BASELINK; there is no need to
3895		   include scope information.  */
3896
3897		/* But we do need to remember that there was an explicit
3898		   scope for virtual function calls.  */
3899		if (parser->scope)
3900		  idk = CP_ID_KIND_QUALIFIED;
3901
3902		if (name != error_mark_node
3903		    && !BASELINK_P (name)
3904		    && parser->scope)
3905		  {
3906		    name = build_nt (SCOPE_REF, parser->scope, name);
3907		    parser->scope = NULL_TREE;
3908		    parser->qualifying_scope = NULL_TREE;
3909		    parser->object_scope = NULL_TREE;
3910		  }
3911		if (scope && name && BASELINK_P (name))
3912		  adjust_result_of_qualified_name_lookup
3913		    (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
3914		postfix_expression
3915		  = finish_class_member_access_expr (postfix_expression, name);
3916	      }
3917	    /* Otherwise, try the pseudo-destructor-name production.  */
3918	    else
3919	      {
3920		tree s = NULL_TREE;
3921		tree type;
3922
3923		/* Parse the pseudo-destructor-name.  */
3924		cp_parser_pseudo_destructor_name (parser, &s, &type);
3925		/* Form the call.  */
3926		postfix_expression
3927		  = finish_pseudo_destructor_expr (postfix_expression,
3928						   s, TREE_TYPE (type));
3929	      }
3930
3931	    /* We no longer need to look up names in the scope of the
3932	       object on the left-hand side of the `.' or `->'
3933	       operator.  */
3934	    parser->context->object_type = NULL_TREE;
3935	    /* These operators may not appear in constant-expressions.  */
3936	    if (/* The "->" operator is allowed in the implementation
3937		   of "offsetof".  The "." operator may appear in the
3938		   name of the member.  */
3939		!parser->in_offsetof_p
3940		&& (cp_parser_non_integral_constant_expression
3941		    (parser,
3942		     token_type == CPP_DEREF ? "'->'" : "`.'")))
3943	      postfix_expression = error_mark_node;
3944	  }
3945	  break;
3946
3947	case CPP_PLUS_PLUS:
3948	  /* postfix-expression ++  */
3949	  /* Consume the `++' token.  */
3950	  cp_lexer_consume_token (parser->lexer);
3951	  /* Generate a representation for the complete expression.  */
3952	  postfix_expression
3953	    = finish_increment_expr (postfix_expression,
3954				     POSTINCREMENT_EXPR);
3955	  /* Increments may not appear in constant-expressions.  */
3956	  if (cp_parser_non_integral_constant_expression (parser,
3957							  "an increment"))
3958	    postfix_expression = error_mark_node;
3959	  idk = CP_ID_KIND_NONE;
3960	  break;
3961
3962	case CPP_MINUS_MINUS:
3963	  /* postfix-expression -- */
3964	  /* Consume the `--' token.  */
3965	  cp_lexer_consume_token (parser->lexer);
3966	  /* Generate a representation for the complete expression.  */
3967	  postfix_expression
3968	    = finish_increment_expr (postfix_expression,
3969				     POSTDECREMENT_EXPR);
3970	  /* Decrements may not appear in constant-expressions.  */
3971	  if (cp_parser_non_integral_constant_expression (parser,
3972							  "a decrement"))
3973	    postfix_expression = error_mark_node;
3974	  idk = CP_ID_KIND_NONE;
3975	  break;
3976
3977	default:
3978	  return postfix_expression;
3979	}
3980    }
3981
3982  /* We should never get here.  */
3983  abort ();
3984  return error_mark_node;
3985}
3986
3987/* Parse a parenthesized expression-list.
3988
3989   expression-list:
3990     assignment-expression
3991     expression-list, assignment-expression
3992
3993   attribute-list:
3994     expression-list
3995     identifier
3996     identifier, expression-list
3997
3998   Returns a TREE_LIST.  The TREE_VALUE of each node is a
3999   representation of an assignment-expression.  Note that a TREE_LIST
4000   is returned even if there is only a single expression in the list.
4001   error_mark_node is returned if the ( and or ) are
4002   missing. NULL_TREE is returned on no expressions. The parentheses
4003   are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4004   list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4005   indicates whether or not all of the expressions in the list were
4006   constant.  */
4007
4008static tree
4009cp_parser_parenthesized_expression_list (cp_parser* parser,
4010					 bool is_attribute_list,
4011					 bool *non_constant_p)
4012{
4013  tree expression_list = NULL_TREE;
4014  tree identifier = NULL_TREE;
4015
4016  /* Assume all the expressions will be constant.  */
4017  if (non_constant_p)
4018    *non_constant_p = false;
4019
4020  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4021    return error_mark_node;
4022
4023  /* Consume expressions until there are no more.  */
4024  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4025    while (true)
4026      {
4027	tree expr;
4028
4029	/* At the beginning of attribute lists, check to see if the
4030	   next token is an identifier.  */
4031	if (is_attribute_list
4032	    && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4033	  {
4034	    cp_token *token;
4035
4036	    /* Consume the identifier.  */
4037	    token = cp_lexer_consume_token (parser->lexer);
4038	    /* Save the identifier.  */
4039	    identifier = token->value;
4040	  }
4041	else
4042	  {
4043	    /* Parse the next assignment-expression.  */
4044	    if (non_constant_p)
4045	      {
4046		bool expr_non_constant_p;
4047		expr = (cp_parser_constant_expression
4048			(parser, /*allow_non_constant_p=*/true,
4049			 &expr_non_constant_p));
4050		if (expr_non_constant_p)
4051		  *non_constant_p = true;
4052	      }
4053	    else
4054	      expr = cp_parser_assignment_expression (parser);
4055
4056	     /* Add it to the list.  We add error_mark_node
4057		expressions to the list, so that we can still tell if
4058		the correct form for a parenthesized expression-list
4059		is found. That gives better errors.  */
4060	    expression_list = tree_cons (NULL_TREE, expr, expression_list);
4061
4062	    if (expr == error_mark_node)
4063	      goto skip_comma;
4064	  }
4065
4066	/* After the first item, attribute lists look the same as
4067	   expression lists.  */
4068	is_attribute_list = false;
4069
4070      get_comma:;
4071	/* If the next token isn't a `,', then we are done.  */
4072	if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4073	  break;
4074
4075	/* Otherwise, consume the `,' and keep going.  */
4076	cp_lexer_consume_token (parser->lexer);
4077      }
4078
4079  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4080    {
4081      int ending;
4082
4083    skip_comma:;
4084      /* We try and resync to an unnested comma, as that will give the
4085	 user better diagnostics.  */
4086      ending = cp_parser_skip_to_closing_parenthesis (parser,
4087						      /*recovering=*/true,
4088						      /*or_comma=*/true,
4089						      /*consume_paren=*/true);
4090      if (ending < 0)
4091	goto get_comma;
4092      if (!ending)
4093	return error_mark_node;
4094    }
4095
4096  /* We built up the list in reverse order so we must reverse it now.  */
4097  expression_list = nreverse (expression_list);
4098  if (identifier)
4099    expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4100
4101  return expression_list;
4102}
4103
4104/* Parse a pseudo-destructor-name.
4105
4106   pseudo-destructor-name:
4107     :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4108     :: [opt] nested-name-specifier template template-id :: ~ type-name
4109     :: [opt] nested-name-specifier [opt] ~ type-name
4110
4111   If either of the first two productions is used, sets *SCOPE to the
4112   TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4113   NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4114   or ERROR_MARK_NODE if the parse fails.  */
4115
4116static void
4117cp_parser_pseudo_destructor_name (cp_parser* parser,
4118                                  tree* scope,
4119                                  tree* type)
4120{
4121  bool nested_name_specifier_p;
4122
4123  /* Look for the optional `::' operator.  */
4124  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4125  /* Look for the optional nested-name-specifier.  */
4126  nested_name_specifier_p
4127    = (cp_parser_nested_name_specifier_opt (parser,
4128					    /*typename_keyword_p=*/false,
4129					    /*check_dependency_p=*/true,
4130					    /*type_p=*/false,
4131					    /*is_declaration=*/true)
4132       != NULL_TREE);
4133  /* Now, if we saw a nested-name-specifier, we might be doing the
4134     second production.  */
4135  if (nested_name_specifier_p
4136      && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4137    {
4138      /* Consume the `template' keyword.  */
4139      cp_lexer_consume_token (parser->lexer);
4140      /* Parse the template-id.  */
4141      cp_parser_template_id (parser,
4142			     /*template_keyword_p=*/true,
4143			     /*check_dependency_p=*/false,
4144			     /*is_declaration=*/true);
4145      /* Look for the `::' token.  */
4146      cp_parser_require (parser, CPP_SCOPE, "`::'");
4147    }
4148  /* If the next token is not a `~', then there might be some
4149     additional qualification.  */
4150  else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4151    {
4152      /* Look for the type-name.  */
4153      *scope = TREE_TYPE (cp_parser_type_name (parser));
4154
4155      /* If we didn't get an aggregate type, or we don't have ::~,
4156	 then something has gone wrong.  Since the only caller of this
4157	 function is looking for something after `.' or `->' after a
4158	 scalar type, most likely the program is trying to get a
4159	 member of a non-aggregate type.  */
4160      if (*scope == error_mark_node
4161	  || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4162	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4163	{
4164	  cp_parser_error (parser, "request for member of non-aggregate type");
4165	  *type = error_mark_node;
4166	  return;
4167	}
4168
4169      /* Look for the `::' token.  */
4170      cp_parser_require (parser, CPP_SCOPE, "`::'");
4171    }
4172  else
4173    *scope = NULL_TREE;
4174
4175  /* Look for the `~'.  */
4176  cp_parser_require (parser, CPP_COMPL, "`~'");
4177  /* Look for the type-name again.  We are not responsible for
4178     checking that it matches the first type-name.  */
4179  *type = cp_parser_type_name (parser);
4180}
4181
4182/* Parse a unary-expression.
4183
4184   unary-expression:
4185     postfix-expression
4186     ++ cast-expression
4187     -- cast-expression
4188     unary-operator cast-expression
4189     sizeof unary-expression
4190     sizeof ( type-id )
4191     new-expression
4192     delete-expression
4193
4194   GNU Extensions:
4195
4196   unary-expression:
4197     __extension__ cast-expression
4198     __alignof__ unary-expression
4199     __alignof__ ( type-id )
4200     __real__ cast-expression
4201     __imag__ cast-expression
4202     && identifier
4203
4204   ADDRESS_P is true iff the unary-expression is appearing as the
4205   operand of the `&' operator.
4206
4207   Returns a representation of the expression.  */
4208
4209static tree
4210cp_parser_unary_expression (cp_parser *parser, bool address_p)
4211{
4212  cp_token *token;
4213  enum tree_code unary_operator;
4214
4215  /* Peek at the next token.  */
4216  token = cp_lexer_peek_token (parser->lexer);
4217  /* Some keywords give away the kind of expression.  */
4218  if (token->type == CPP_KEYWORD)
4219    {
4220      enum rid keyword = token->keyword;
4221
4222      switch (keyword)
4223	{
4224	case RID_ALIGNOF:
4225	case RID_SIZEOF:
4226	  {
4227	    tree operand;
4228	    enum tree_code op;
4229
4230	    op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4231	    /* Consume the token.  */
4232	    cp_lexer_consume_token (parser->lexer);
4233	    /* Parse the operand.  */
4234	    operand = cp_parser_sizeof_operand (parser, keyword);
4235
4236	    if (TYPE_P (operand))
4237	      return cxx_sizeof_or_alignof_type (operand, op, true);
4238	    else
4239	      return cxx_sizeof_or_alignof_expr (operand, op);
4240	  }
4241
4242	case RID_NEW:
4243	  return cp_parser_new_expression (parser);
4244
4245	case RID_DELETE:
4246	  return cp_parser_delete_expression (parser);
4247
4248	case RID_EXTENSION:
4249	  {
4250	    /* The saved value of the PEDANTIC flag.  */
4251	    int saved_pedantic;
4252	    tree expr;
4253
4254	    /* Save away the PEDANTIC flag.  */
4255	    cp_parser_extension_opt (parser, &saved_pedantic);
4256	    /* Parse the cast-expression.  */
4257	    expr = cp_parser_simple_cast_expression (parser);
4258	    /* Restore the PEDANTIC flag.  */
4259	    pedantic = saved_pedantic;
4260
4261	    return expr;
4262	  }
4263
4264	case RID_REALPART:
4265	case RID_IMAGPART:
4266	  {
4267	    tree expression;
4268
4269	    /* Consume the `__real__' or `__imag__' token.  */
4270	    cp_lexer_consume_token (parser->lexer);
4271	    /* Parse the cast-expression.  */
4272	    expression = cp_parser_simple_cast_expression (parser);
4273	    /* Create the complete representation.  */
4274	    return build_x_unary_op ((keyword == RID_REALPART
4275				      ? REALPART_EXPR : IMAGPART_EXPR),
4276				     expression);
4277	  }
4278	  break;
4279
4280	default:
4281	  break;
4282	}
4283    }
4284
4285  /* Look for the `:: new' and `:: delete', which also signal the
4286     beginning of a new-expression, or delete-expression,
4287     respectively.  If the next token is `::', then it might be one of
4288     these.  */
4289  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4290    {
4291      enum rid keyword;
4292
4293      /* See if the token after the `::' is one of the keywords in
4294	 which we're interested.  */
4295      keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4296      /* If it's `new', we have a new-expression.  */
4297      if (keyword == RID_NEW)
4298	return cp_parser_new_expression (parser);
4299      /* Similarly, for `delete'.  */
4300      else if (keyword == RID_DELETE)
4301	return cp_parser_delete_expression (parser);
4302    }
4303
4304  /* Look for a unary operator.  */
4305  unary_operator = cp_parser_unary_operator (token);
4306  /* The `++' and `--' operators can be handled similarly, even though
4307     they are not technically unary-operators in the grammar.  */
4308  if (unary_operator == ERROR_MARK)
4309    {
4310      if (token->type == CPP_PLUS_PLUS)
4311	unary_operator = PREINCREMENT_EXPR;
4312      else if (token->type == CPP_MINUS_MINUS)
4313	unary_operator = PREDECREMENT_EXPR;
4314      /* Handle the GNU address-of-label extension.  */
4315      else if (cp_parser_allow_gnu_extensions_p (parser)
4316	       && token->type == CPP_AND_AND)
4317	{
4318	  tree identifier;
4319
4320	  /* Consume the '&&' token.  */
4321	  cp_lexer_consume_token (parser->lexer);
4322	  /* Look for the identifier.  */
4323	  identifier = cp_parser_identifier (parser);
4324	  /* Create an expression representing the address.  */
4325	  return finish_label_address_expr (identifier);
4326	}
4327    }
4328  if (unary_operator != ERROR_MARK)
4329    {
4330      tree cast_expression;
4331      tree expression = error_mark_node;
4332      const char *non_constant_p = NULL;
4333
4334      /* Consume the operator token.  */
4335      token = cp_lexer_consume_token (parser->lexer);
4336      /* Parse the cast-expression.  */
4337      cast_expression
4338	= cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4339      /* Now, build an appropriate representation.  */
4340      switch (unary_operator)
4341	{
4342	case INDIRECT_REF:
4343	  non_constant_p = "`*'";
4344	  expression = build_x_indirect_ref (cast_expression, "unary *");
4345	  break;
4346
4347	case ADDR_EXPR:
4348	  /* The "&" operator is allowed in the implementation of
4349	     "offsetof".  */
4350	  if (!parser->in_offsetof_p)
4351	    non_constant_p = "`&'";
4352	  /* Fall through.  */
4353	case BIT_NOT_EXPR:
4354	  expression = build_x_unary_op (unary_operator, cast_expression);
4355	  break;
4356
4357	case PREINCREMENT_EXPR:
4358	case PREDECREMENT_EXPR:
4359	  non_constant_p = (unary_operator == PREINCREMENT_EXPR
4360			    ? "`++'" : "`--'");
4361	  /* Fall through.  */
4362	case CONVERT_EXPR:
4363	case NEGATE_EXPR:
4364	case TRUTH_NOT_EXPR:
4365	  expression = finish_unary_op_expr (unary_operator, cast_expression);
4366	  break;
4367
4368	default:
4369	  abort ();
4370	}
4371
4372      if (non_constant_p
4373	  && cp_parser_non_integral_constant_expression (parser,
4374							 non_constant_p))
4375	expression = error_mark_node;
4376
4377      return expression;
4378    }
4379
4380  return cp_parser_postfix_expression (parser, address_p);
4381}
4382
4383/* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4384   unary-operator, the corresponding tree code is returned.  */
4385
4386static enum tree_code
4387cp_parser_unary_operator (cp_token* token)
4388{
4389  switch (token->type)
4390    {
4391    case CPP_MULT:
4392      return INDIRECT_REF;
4393
4394    case CPP_AND:
4395      return ADDR_EXPR;
4396
4397    case CPP_PLUS:
4398      return CONVERT_EXPR;
4399
4400    case CPP_MINUS:
4401      return NEGATE_EXPR;
4402
4403    case CPP_NOT:
4404      return TRUTH_NOT_EXPR;
4405
4406    case CPP_COMPL:
4407      return BIT_NOT_EXPR;
4408
4409    default:
4410      return ERROR_MARK;
4411    }
4412}
4413
4414/* Parse a new-expression.
4415
4416   new-expression:
4417     :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4418     :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4419
4420   Returns a representation of the expression.  */
4421
4422static tree
4423cp_parser_new_expression (cp_parser* parser)
4424{
4425  bool global_scope_p;
4426  tree placement;
4427  tree type;
4428  tree initializer;
4429
4430  /* Look for the optional `::' operator.  */
4431  global_scope_p
4432    = (cp_parser_global_scope_opt (parser,
4433				   /*current_scope_valid_p=*/false)
4434       != NULL_TREE);
4435  /* Look for the `new' operator.  */
4436  cp_parser_require_keyword (parser, RID_NEW, "`new'");
4437  /* There's no easy way to tell a new-placement from the
4438     `( type-id )' construct.  */
4439  cp_parser_parse_tentatively (parser);
4440  /* Look for a new-placement.  */
4441  placement = cp_parser_new_placement (parser);
4442  /* If that didn't work out, there's no new-placement.  */
4443  if (!cp_parser_parse_definitely (parser))
4444    placement = NULL_TREE;
4445
4446  /* If the next token is a `(', then we have a parenthesized
4447     type-id.  */
4448  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4449    {
4450      /* Consume the `('.  */
4451      cp_lexer_consume_token (parser->lexer);
4452      /* Parse the type-id.  */
4453      type = cp_parser_type_id (parser);
4454      /* Look for the closing `)'.  */
4455      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4456      /* There should not be a direct-new-declarator in this production,
4457         but GCC used to allowed this, so we check and emit a sensible error
4458	 message for this case.  */
4459      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4460	{
4461	  error ("array bound forbidden after parenthesized type-id");
4462	  inform ("try removing the parentheses around the type-id");
4463	  cp_parser_direct_new_declarator (parser);
4464	}
4465    }
4466  /* Otherwise, there must be a new-type-id.  */
4467  else
4468    type = cp_parser_new_type_id (parser);
4469
4470  /* If the next token is a `(', then we have a new-initializer.  */
4471  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4472    initializer = cp_parser_new_initializer (parser);
4473  else
4474    initializer = NULL_TREE;
4475
4476  /* A new-expression may not appear in an integral constant
4477     expression.  */
4478  if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4479    return error_mark_node;
4480
4481  /* Create a representation of the new-expression.  */
4482  return build_new (placement, type, initializer, global_scope_p);
4483}
4484
4485/* Parse a new-placement.
4486
4487   new-placement:
4488     ( expression-list )
4489
4490   Returns the same representation as for an expression-list.  */
4491
4492static tree
4493cp_parser_new_placement (cp_parser* parser)
4494{
4495  tree expression_list;
4496
4497  /* Parse the expression-list.  */
4498  expression_list = (cp_parser_parenthesized_expression_list
4499		     (parser, false, /*non_constant_p=*/NULL));
4500
4501  return expression_list;
4502}
4503
4504/* Parse a new-type-id.
4505
4506   new-type-id:
4507     type-specifier-seq new-declarator [opt]
4508
4509   Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4510   and whose TREE_VALUE is the new-declarator.  */
4511
4512static tree
4513cp_parser_new_type_id (cp_parser* parser)
4514{
4515  tree type_specifier_seq;
4516  tree declarator;
4517  const char *saved_message;
4518
4519  /* The type-specifier sequence must not contain type definitions.
4520     (It cannot contain declarations of new types either, but if they
4521     are not definitions we will catch that because they are not
4522     complete.)  */
4523  saved_message = parser->type_definition_forbidden_message;
4524  parser->type_definition_forbidden_message
4525    = "types may not be defined in a new-type-id";
4526  /* Parse the type-specifier-seq.  */
4527  type_specifier_seq = cp_parser_type_specifier_seq (parser);
4528  /* Restore the old message.  */
4529  parser->type_definition_forbidden_message = saved_message;
4530  /* Parse the new-declarator.  */
4531  declarator = cp_parser_new_declarator_opt (parser);
4532
4533  return build_tree_list (type_specifier_seq, declarator);
4534}
4535
4536/* Parse an (optional) new-declarator.
4537
4538   new-declarator:
4539     ptr-operator new-declarator [opt]
4540     direct-new-declarator
4541
4542   Returns a representation of the declarator.  See
4543   cp_parser_declarator for the representations used.  */
4544
4545static tree
4546cp_parser_new_declarator_opt (cp_parser* parser)
4547{
4548  enum tree_code code;
4549  tree type;
4550  tree cv_qualifier_seq;
4551
4552  /* We don't know if there's a ptr-operator next, or not.  */
4553  cp_parser_parse_tentatively (parser);
4554  /* Look for a ptr-operator.  */
4555  code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4556  /* If that worked, look for more new-declarators.  */
4557  if (cp_parser_parse_definitely (parser))
4558    {
4559      tree declarator;
4560
4561      /* Parse another optional declarator.  */
4562      declarator = cp_parser_new_declarator_opt (parser);
4563
4564      /* Create the representation of the declarator.  */
4565      if (code == INDIRECT_REF)
4566	declarator = make_pointer_declarator (cv_qualifier_seq,
4567					      declarator);
4568      else
4569	declarator = make_reference_declarator (cv_qualifier_seq,
4570						declarator);
4571
4572     /* Handle the pointer-to-member case.  */
4573     if (type)
4574       declarator = build_nt (SCOPE_REF, type, declarator);
4575
4576      return declarator;
4577    }
4578
4579  /* If the next token is a `[', there is a direct-new-declarator.  */
4580  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4581    return cp_parser_direct_new_declarator (parser);
4582
4583  return NULL_TREE;
4584}
4585
4586/* Parse a direct-new-declarator.
4587
4588   direct-new-declarator:
4589     [ expression ]
4590     direct-new-declarator [constant-expression]
4591
4592   Returns an ARRAY_REF, following the same conventions as are
4593   documented for cp_parser_direct_declarator.  */
4594
4595static tree
4596cp_parser_direct_new_declarator (cp_parser* parser)
4597{
4598  tree declarator = NULL_TREE;
4599
4600  while (true)
4601    {
4602      tree expression;
4603
4604      /* Look for the opening `['.  */
4605      cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4606      /* The first expression is not required to be constant.  */
4607      if (!declarator)
4608	{
4609	  expression = cp_parser_expression (parser);
4610	  /* The standard requires that the expression have integral
4611	     type.  DR 74 adds enumeration types.  We believe that the
4612	     real intent is that these expressions be handled like the
4613	     expression in a `switch' condition, which also allows
4614	     classes with a single conversion to integral or
4615	     enumeration type.  */
4616	  if (!processing_template_decl)
4617	    {
4618	      expression
4619		= build_expr_type_conversion (WANT_INT | WANT_ENUM,
4620					      expression,
4621					      /*complain=*/true);
4622	      if (!expression)
4623		{
4624		  error ("expression in new-declarator must have integral or enumeration type");
4625		  expression = error_mark_node;
4626		}
4627	    }
4628	}
4629      /* But all the other expressions must be.  */
4630      else
4631	expression
4632	  = cp_parser_constant_expression (parser,
4633					   /*allow_non_constant=*/false,
4634					   NULL);
4635      /* Look for the closing `]'.  */
4636      cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4637
4638      /* Add this bound to the declarator.  */
4639      declarator = build_nt (ARRAY_REF, declarator, expression);
4640
4641      /* If the next token is not a `[', then there are no more
4642	 bounds.  */
4643      if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4644	break;
4645    }
4646
4647  return declarator;
4648}
4649
4650/* Parse a new-initializer.
4651
4652   new-initializer:
4653     ( expression-list [opt] )
4654
4655   Returns a representation of the expression-list.  If there is no
4656   expression-list, VOID_ZERO_NODE is returned.  */
4657
4658static tree
4659cp_parser_new_initializer (cp_parser* parser)
4660{
4661  tree expression_list;
4662
4663  expression_list = (cp_parser_parenthesized_expression_list
4664		     (parser, false, /*non_constant_p=*/NULL));
4665  if (!expression_list)
4666    expression_list = void_zero_node;
4667
4668  return expression_list;
4669}
4670
4671/* Parse a delete-expression.
4672
4673   delete-expression:
4674     :: [opt] delete cast-expression
4675     :: [opt] delete [ ] cast-expression
4676
4677   Returns a representation of the expression.  */
4678
4679static tree
4680cp_parser_delete_expression (cp_parser* parser)
4681{
4682  bool global_scope_p;
4683  bool array_p;
4684  tree expression;
4685
4686  /* Look for the optional `::' operator.  */
4687  global_scope_p
4688    = (cp_parser_global_scope_opt (parser,
4689				   /*current_scope_valid_p=*/false)
4690       != NULL_TREE);
4691  /* Look for the `delete' keyword.  */
4692  cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4693  /* See if the array syntax is in use.  */
4694  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4695    {
4696      /* Consume the `[' token.  */
4697      cp_lexer_consume_token (parser->lexer);
4698      /* Look for the `]' token.  */
4699      cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4700      /* Remember that this is the `[]' construct.  */
4701      array_p = true;
4702    }
4703  else
4704    array_p = false;
4705
4706  /* Parse the cast-expression.  */
4707  expression = cp_parser_simple_cast_expression (parser);
4708
4709  /* A delete-expression may not appear in an integral constant
4710     expression.  */
4711  if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4712    return error_mark_node;
4713
4714  return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4715}
4716
4717/* Parse a cast-expression.
4718
4719   cast-expression:
4720     unary-expression
4721     ( type-id ) cast-expression
4722
4723   Returns a representation of the expression.  */
4724
4725static tree
4726cp_parser_cast_expression (cp_parser *parser, bool address_p)
4727{
4728  /* If it's a `(', then we might be looking at a cast.  */
4729  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4730    {
4731      tree type = NULL_TREE;
4732      tree expr = NULL_TREE;
4733      bool compound_literal_p;
4734      const char *saved_message;
4735
4736      /* There's no way to know yet whether or not this is a cast.
4737	 For example, `(int (3))' is a unary-expression, while `(int)
4738	 3' is a cast.  So, we resort to parsing tentatively.  */
4739      cp_parser_parse_tentatively (parser);
4740      /* Types may not be defined in a cast.  */
4741      saved_message = parser->type_definition_forbidden_message;
4742      parser->type_definition_forbidden_message
4743	= "types may not be defined in casts";
4744      /* Consume the `('.  */
4745      cp_lexer_consume_token (parser->lexer);
4746      /* A very tricky bit is that `(struct S) { 3 }' is a
4747	 compound-literal (which we permit in C++ as an extension).
4748	 But, that construct is not a cast-expression -- it is a
4749	 postfix-expression.  (The reason is that `(struct S) { 3 }.i'
4750	 is legal; if the compound-literal were a cast-expression,
4751	 you'd need an extra set of parentheses.)  But, if we parse
4752	 the type-id, and it happens to be a class-specifier, then we
4753	 will commit to the parse at that point, because we cannot
4754	 undo the action that is done when creating a new class.  So,
4755	 then we cannot back up and do a postfix-expression.
4756
4757	 Therefore, we scan ahead to the closing `)', and check to see
4758	 if the token after the `)' is a `{'.  If so, we are not
4759	 looking at a cast-expression.
4760
4761	 Save tokens so that we can put them back.  */
4762      cp_lexer_save_tokens (parser->lexer);
4763      /* Skip tokens until the next token is a closing parenthesis.
4764	 If we find the closing `)', and the next token is a `{', then
4765	 we are looking at a compound-literal.  */
4766      compound_literal_p
4767	= (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4768						  /*consume_paren=*/true)
4769	   && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4770      /* Roll back the tokens we skipped.  */
4771      cp_lexer_rollback_tokens (parser->lexer);
4772      /* If we were looking at a compound-literal, simulate an error
4773	 so that the call to cp_parser_parse_definitely below will
4774	 fail.  */
4775      if (compound_literal_p)
4776	cp_parser_simulate_error (parser);
4777      else
4778	{
4779	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4780	  parser->in_type_id_in_expr_p = true;
4781	  /* Look for the type-id.  */
4782	  type = cp_parser_type_id (parser);
4783	  /* Look for the closing `)'.  */
4784	  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4785	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4786	}
4787
4788      /* Restore the saved message.  */
4789      parser->type_definition_forbidden_message = saved_message;
4790
4791      /* If ok so far, parse the dependent expression. We cannot be
4792         sure it is a cast. Consider `(T ())'.  It is a parenthesized
4793         ctor of T, but looks like a cast to function returning T
4794         without a dependent expression.  */
4795      if (!cp_parser_error_occurred (parser))
4796	expr = cp_parser_simple_cast_expression (parser);
4797
4798      if (cp_parser_parse_definitely (parser))
4799	{
4800	  /* Warn about old-style casts, if so requested.  */
4801	  if (warn_old_style_cast
4802	      && !in_system_header
4803	      && !VOID_TYPE_P (type)
4804	      && current_lang_name != lang_name_c)
4805	    warning ("use of old-style cast");
4806
4807	  /* Only type conversions to integral or enumeration types
4808	     can be used in constant-expressions.  */
4809	  if (parser->integral_constant_expression_p
4810	      && !dependent_type_p (type)
4811	      && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4812	      && (cp_parser_non_integral_constant_expression
4813		  (parser,
4814		   "a casts to a type other than an integral or "
4815		   "enumeration type")))
4816	    return error_mark_node;
4817
4818	  /* Perform the cast.  */
4819	  expr = build_c_cast (type, expr);
4820	  return expr;
4821	}
4822    }
4823
4824  /* If we get here, then it's not a cast, so it must be a
4825     unary-expression.  */
4826  return cp_parser_unary_expression (parser, address_p);
4827}
4828
4829/* Parse a pm-expression.
4830
4831   pm-expression:
4832     cast-expression
4833     pm-expression .* cast-expression
4834     pm-expression ->* cast-expression
4835
4836     Returns a representation of the expression.  */
4837
4838static tree
4839cp_parser_pm_expression (cp_parser* parser)
4840{
4841  static const cp_parser_token_tree_map map = {
4842    { CPP_DEREF_STAR, MEMBER_REF },
4843    { CPP_DOT_STAR, DOTSTAR_EXPR },
4844    { CPP_EOF, ERROR_MARK }
4845  };
4846
4847  return cp_parser_binary_expression (parser, map,
4848				      cp_parser_simple_cast_expression);
4849}
4850
4851/* Parse a multiplicative-expression.
4852
4853   mulitplicative-expression:
4854     pm-expression
4855     multiplicative-expression * pm-expression
4856     multiplicative-expression / pm-expression
4857     multiplicative-expression % pm-expression
4858
4859   Returns a representation of the expression.  */
4860
4861static tree
4862cp_parser_multiplicative_expression (cp_parser* parser)
4863{
4864  static const cp_parser_token_tree_map map = {
4865    { CPP_MULT, MULT_EXPR },
4866    { CPP_DIV, TRUNC_DIV_EXPR },
4867    { CPP_MOD, TRUNC_MOD_EXPR },
4868    { CPP_EOF, ERROR_MARK }
4869  };
4870
4871  return cp_parser_binary_expression (parser,
4872				      map,
4873				      cp_parser_pm_expression);
4874}
4875
4876/* Parse an additive-expression.
4877
4878   additive-expression:
4879     multiplicative-expression
4880     additive-expression + multiplicative-expression
4881     additive-expression - multiplicative-expression
4882
4883   Returns a representation of the expression.  */
4884
4885static tree
4886cp_parser_additive_expression (cp_parser* parser)
4887{
4888  static const cp_parser_token_tree_map map = {
4889    { CPP_PLUS, PLUS_EXPR },
4890    { CPP_MINUS, MINUS_EXPR },
4891    { CPP_EOF, ERROR_MARK }
4892  };
4893
4894  return cp_parser_binary_expression (parser,
4895				      map,
4896				      cp_parser_multiplicative_expression);
4897}
4898
4899/* Parse a shift-expression.
4900
4901   shift-expression:
4902     additive-expression
4903     shift-expression << additive-expression
4904     shift-expression >> additive-expression
4905
4906   Returns a representation of the expression.  */
4907
4908static tree
4909cp_parser_shift_expression (cp_parser* parser)
4910{
4911  static const cp_parser_token_tree_map map = {
4912    { CPP_LSHIFT, LSHIFT_EXPR },
4913    { CPP_RSHIFT, RSHIFT_EXPR },
4914    { CPP_EOF, ERROR_MARK }
4915  };
4916
4917  return cp_parser_binary_expression (parser,
4918				      map,
4919				      cp_parser_additive_expression);
4920}
4921
4922/* Parse a relational-expression.
4923
4924   relational-expression:
4925     shift-expression
4926     relational-expression < shift-expression
4927     relational-expression > shift-expression
4928     relational-expression <= shift-expression
4929     relational-expression >= shift-expression
4930
4931   GNU Extension:
4932
4933   relational-expression:
4934     relational-expression <? shift-expression
4935     relational-expression >? shift-expression
4936
4937   Returns a representation of the expression.  */
4938
4939static tree
4940cp_parser_relational_expression (cp_parser* parser)
4941{
4942  static const cp_parser_token_tree_map map = {
4943    { CPP_LESS, LT_EXPR },
4944    { CPP_GREATER, GT_EXPR },
4945    { CPP_LESS_EQ, LE_EXPR },
4946    { CPP_GREATER_EQ, GE_EXPR },
4947    { CPP_MIN, MIN_EXPR },
4948    { CPP_MAX, MAX_EXPR },
4949    { CPP_EOF, ERROR_MARK }
4950  };
4951
4952  return cp_parser_binary_expression (parser,
4953				      map,
4954				      cp_parser_shift_expression);
4955}
4956
4957/* Parse an equality-expression.
4958
4959   equality-expression:
4960     relational-expression
4961     equality-expression == relational-expression
4962     equality-expression != relational-expression
4963
4964   Returns a representation of the expression.  */
4965
4966static tree
4967cp_parser_equality_expression (cp_parser* parser)
4968{
4969  static const cp_parser_token_tree_map map = {
4970    { CPP_EQ_EQ, EQ_EXPR },
4971    { CPP_NOT_EQ, NE_EXPR },
4972    { CPP_EOF, ERROR_MARK }
4973  };
4974
4975  return cp_parser_binary_expression (parser,
4976				      map,
4977				      cp_parser_relational_expression);
4978}
4979
4980/* Parse an and-expression.
4981
4982   and-expression:
4983     equality-expression
4984     and-expression & equality-expression
4985
4986   Returns a representation of the expression.  */
4987
4988static tree
4989cp_parser_and_expression (cp_parser* parser)
4990{
4991  static const cp_parser_token_tree_map map = {
4992    { CPP_AND, BIT_AND_EXPR },
4993    { CPP_EOF, ERROR_MARK }
4994  };
4995
4996  return cp_parser_binary_expression (parser,
4997				      map,
4998				      cp_parser_equality_expression);
4999}
5000
5001/* Parse an exclusive-or-expression.
5002
5003   exclusive-or-expression:
5004     and-expression
5005     exclusive-or-expression ^ and-expression
5006
5007   Returns a representation of the expression.  */
5008
5009static tree
5010cp_parser_exclusive_or_expression (cp_parser* parser)
5011{
5012  static const cp_parser_token_tree_map map = {
5013    { CPP_XOR, BIT_XOR_EXPR },
5014    { CPP_EOF, ERROR_MARK }
5015  };
5016
5017  return cp_parser_binary_expression (parser,
5018				      map,
5019				      cp_parser_and_expression);
5020}
5021
5022
5023/* Parse an inclusive-or-expression.
5024
5025   inclusive-or-expression:
5026     exclusive-or-expression
5027     inclusive-or-expression | exclusive-or-expression
5028
5029   Returns a representation of the expression.  */
5030
5031static tree
5032cp_parser_inclusive_or_expression (cp_parser* parser)
5033{
5034  static const cp_parser_token_tree_map map = {
5035    { CPP_OR, BIT_IOR_EXPR },
5036    { CPP_EOF, ERROR_MARK }
5037  };
5038
5039  return cp_parser_binary_expression (parser,
5040				      map,
5041				      cp_parser_exclusive_or_expression);
5042}
5043
5044/* Parse a logical-and-expression.
5045
5046   logical-and-expression:
5047     inclusive-or-expression
5048     logical-and-expression && inclusive-or-expression
5049
5050   Returns a representation of the expression.  */
5051
5052static tree
5053cp_parser_logical_and_expression (cp_parser* parser)
5054{
5055  static const cp_parser_token_tree_map map = {
5056    { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5057    { CPP_EOF, ERROR_MARK }
5058  };
5059
5060  return cp_parser_binary_expression (parser,
5061				      map,
5062				      cp_parser_inclusive_or_expression);
5063}
5064
5065/* Parse a logical-or-expression.
5066
5067   logical-or-expression:
5068     logical-and-expression
5069     logical-or-expression || logical-and-expression
5070
5071   Returns a representation of the expression.  */
5072
5073static tree
5074cp_parser_logical_or_expression (cp_parser* parser)
5075{
5076  static const cp_parser_token_tree_map map = {
5077    { CPP_OR_OR, TRUTH_ORIF_EXPR },
5078    { CPP_EOF, ERROR_MARK }
5079  };
5080
5081  return cp_parser_binary_expression (parser,
5082				      map,
5083				      cp_parser_logical_and_expression);
5084}
5085
5086/* Parse the `? expression : assignment-expression' part of a
5087   conditional-expression.  The LOGICAL_OR_EXPR is the
5088   logical-or-expression that started the conditional-expression.
5089   Returns a representation of the entire conditional-expression.
5090
5091   This routine is used by cp_parser_assignment_expression.
5092
5093     ? expression : assignment-expression
5094
5095   GNU Extensions:
5096
5097     ? : assignment-expression */
5098
5099static tree
5100cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5101{
5102  tree expr;
5103  tree assignment_expr;
5104
5105  /* Consume the `?' token.  */
5106  cp_lexer_consume_token (parser->lexer);
5107  if (cp_parser_allow_gnu_extensions_p (parser)
5108      && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5109    /* Implicit true clause.  */
5110    expr = NULL_TREE;
5111  else
5112    /* Parse the expression.  */
5113    expr = cp_parser_expression (parser);
5114
5115  /* The next token should be a `:'.  */
5116  cp_parser_require (parser, CPP_COLON, "`:'");
5117  /* Parse the assignment-expression.  */
5118  assignment_expr = cp_parser_assignment_expression (parser);
5119
5120  /* Build the conditional-expression.  */
5121  return build_x_conditional_expr (logical_or_expr,
5122				   expr,
5123				   assignment_expr);
5124}
5125
5126/* Parse an assignment-expression.
5127
5128   assignment-expression:
5129     conditional-expression
5130     logical-or-expression assignment-operator assignment_expression
5131     throw-expression
5132
5133   Returns a representation for the expression.  */
5134
5135static tree
5136cp_parser_assignment_expression (cp_parser* parser)
5137{
5138  tree expr;
5139
5140  /* If the next token is the `throw' keyword, then we're looking at
5141     a throw-expression.  */
5142  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5143    expr = cp_parser_throw_expression (parser);
5144  /* Otherwise, it must be that we are looking at a
5145     logical-or-expression.  */
5146  else
5147    {
5148      /* Parse the logical-or-expression.  */
5149      expr = cp_parser_logical_or_expression (parser);
5150      /* If the next token is a `?' then we're actually looking at a
5151	 conditional-expression.  */
5152      if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5153	return cp_parser_question_colon_clause (parser, expr);
5154      else
5155	{
5156	  enum tree_code assignment_operator;
5157
5158	  /* If it's an assignment-operator, we're using the second
5159	     production.  */
5160	  assignment_operator
5161	    = cp_parser_assignment_operator_opt (parser);
5162	  if (assignment_operator != ERROR_MARK)
5163	    {
5164	      tree rhs;
5165
5166	      /* Parse the right-hand side of the assignment.  */
5167	      rhs = cp_parser_assignment_expression (parser);
5168	      /* An assignment may not appear in a
5169		 constant-expression.  */
5170	      if (cp_parser_non_integral_constant_expression (parser,
5171							      "an assignment"))
5172		return error_mark_node;
5173	      /* Build the assignment expression.  */
5174	      expr = build_x_modify_expr (expr,
5175					  assignment_operator,
5176					  rhs);
5177	    }
5178	}
5179    }
5180
5181  return expr;
5182}
5183
5184/* Parse an (optional) assignment-operator.
5185
5186   assignment-operator: one of
5187     = *= /= %= += -= >>= <<= &= ^= |=
5188
5189   GNU Extension:
5190
5191   assignment-operator: one of
5192     <?= >?=
5193
5194   If the next token is an assignment operator, the corresponding tree
5195   code is returned, and the token is consumed.  For example, for
5196   `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5197   NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5198   TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5199   operator, ERROR_MARK is returned.  */
5200
5201static enum tree_code
5202cp_parser_assignment_operator_opt (cp_parser* parser)
5203{
5204  enum tree_code op;
5205  cp_token *token;
5206
5207  /* Peek at the next toen.  */
5208  token = cp_lexer_peek_token (parser->lexer);
5209
5210  switch (token->type)
5211    {
5212    case CPP_EQ:
5213      op = NOP_EXPR;
5214      break;
5215
5216    case CPP_MULT_EQ:
5217      op = MULT_EXPR;
5218      break;
5219
5220    case CPP_DIV_EQ:
5221      op = TRUNC_DIV_EXPR;
5222      break;
5223
5224    case CPP_MOD_EQ:
5225      op = TRUNC_MOD_EXPR;
5226      break;
5227
5228    case CPP_PLUS_EQ:
5229      op = PLUS_EXPR;
5230      break;
5231
5232    case CPP_MINUS_EQ:
5233      op = MINUS_EXPR;
5234      break;
5235
5236    case CPP_RSHIFT_EQ:
5237      op = RSHIFT_EXPR;
5238      break;
5239
5240    case CPP_LSHIFT_EQ:
5241      op = LSHIFT_EXPR;
5242      break;
5243
5244    case CPP_AND_EQ:
5245      op = BIT_AND_EXPR;
5246      break;
5247
5248    case CPP_XOR_EQ:
5249      op = BIT_XOR_EXPR;
5250      break;
5251
5252    case CPP_OR_EQ:
5253      op = BIT_IOR_EXPR;
5254      break;
5255
5256    case CPP_MIN_EQ:
5257      op = MIN_EXPR;
5258      break;
5259
5260    case CPP_MAX_EQ:
5261      op = MAX_EXPR;
5262      break;
5263
5264    default:
5265      /* Nothing else is an assignment operator.  */
5266      op = ERROR_MARK;
5267    }
5268
5269  /* If it was an assignment operator, consume it.  */
5270  if (op != ERROR_MARK)
5271    cp_lexer_consume_token (parser->lexer);
5272
5273  return op;
5274}
5275
5276/* Parse an expression.
5277
5278   expression:
5279     assignment-expression
5280     expression , assignment-expression
5281
5282   Returns a representation of the expression.  */
5283
5284static tree
5285cp_parser_expression (cp_parser* parser)
5286{
5287  tree expression = NULL_TREE;
5288
5289  while (true)
5290    {
5291      tree assignment_expression;
5292
5293      /* Parse the next assignment-expression.  */
5294      assignment_expression
5295	= cp_parser_assignment_expression (parser);
5296      /* If this is the first assignment-expression, we can just
5297	 save it away.  */
5298      if (!expression)
5299	expression = assignment_expression;
5300      else
5301	expression = build_x_compound_expr (expression,
5302					    assignment_expression);
5303      /* If the next token is not a comma, then we are done with the
5304	 expression.  */
5305      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5306	break;
5307      /* Consume the `,'.  */
5308      cp_lexer_consume_token (parser->lexer);
5309      /* A comma operator cannot appear in a constant-expression.  */
5310      if (cp_parser_non_integral_constant_expression (parser,
5311						      "a comma operator"))
5312	expression = error_mark_node;
5313    }
5314
5315  return expression;
5316}
5317
5318/* Parse a constant-expression.
5319
5320   constant-expression:
5321     conditional-expression
5322
5323  If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5324  accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5325  constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5326  is false, NON_CONSTANT_P should be NULL.  */
5327
5328static tree
5329cp_parser_constant_expression (cp_parser* parser,
5330			       bool allow_non_constant_p,
5331			       bool *non_constant_p)
5332{
5333  bool saved_integral_constant_expression_p;
5334  bool saved_allow_non_integral_constant_expression_p;
5335  bool saved_non_integral_constant_expression_p;
5336  tree expression;
5337
5338  /* It might seem that we could simply parse the
5339     conditional-expression, and then check to see if it were
5340     TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5341     one that the compiler can figure out is constant, possibly after
5342     doing some simplifications or optimizations.  The standard has a
5343     precise definition of constant-expression, and we must honor
5344     that, even though it is somewhat more restrictive.
5345
5346     For example:
5347
5348       int i[(2, 3)];
5349
5350     is not a legal declaration, because `(2, 3)' is not a
5351     constant-expression.  The `,' operator is forbidden in a
5352     constant-expression.  However, GCC's constant-folding machinery
5353     will fold this operation to an INTEGER_CST for `3'.  */
5354
5355  /* Save the old settings.  */
5356  saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5357  saved_allow_non_integral_constant_expression_p
5358    = parser->allow_non_integral_constant_expression_p;
5359  saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5360  /* We are now parsing a constant-expression.  */
5361  parser->integral_constant_expression_p = true;
5362  parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5363  parser->non_integral_constant_expression_p = false;
5364  /* Although the grammar says "conditional-expression", we parse an
5365     "assignment-expression", which also permits "throw-expression"
5366     and the use of assignment operators.  In the case that
5367     ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5368     otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5369     actually essential that we look for an assignment-expression.
5370     For example, cp_parser_initializer_clauses uses this function to
5371     determine whether a particular assignment-expression is in fact
5372     constant.  */
5373  expression = cp_parser_assignment_expression (parser);
5374  /* Restore the old settings.  */
5375  parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5376  parser->allow_non_integral_constant_expression_p
5377    = saved_allow_non_integral_constant_expression_p;
5378  if (allow_non_constant_p)
5379    *non_constant_p = parser->non_integral_constant_expression_p;
5380  parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5381
5382  return expression;
5383}
5384
5385/* Statements [gram.stmt.stmt]  */
5386
5387/* Parse a statement.
5388
5389   statement:
5390     labeled-statement
5391     expression-statement
5392     compound-statement
5393     selection-statement
5394     iteration-statement
5395     jump-statement
5396     declaration-statement
5397     try-block  */
5398
5399static void
5400cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5401{
5402  tree statement;
5403  cp_token *token;
5404  int statement_line_number;
5405
5406  /* There is no statement yet.  */
5407  statement = NULL_TREE;
5408  /* Peek at the next token.  */
5409  token = cp_lexer_peek_token (parser->lexer);
5410  /* Remember the line number of the first token in the statement.  */
5411  statement_line_number = token->location.line;
5412  /* If this is a keyword, then that will often determine what kind of
5413     statement we have.  */
5414  if (token->type == CPP_KEYWORD)
5415    {
5416      enum rid keyword = token->keyword;
5417
5418      switch (keyword)
5419	{
5420	case RID_CASE:
5421	case RID_DEFAULT:
5422	  statement = cp_parser_labeled_statement (parser,
5423						   in_statement_expr_p);
5424	  break;
5425
5426	case RID_IF:
5427	case RID_SWITCH:
5428	  statement = cp_parser_selection_statement (parser);
5429	  break;
5430
5431	case RID_WHILE:
5432	case RID_DO:
5433	case RID_FOR:
5434	  statement = cp_parser_iteration_statement (parser);
5435	  break;
5436
5437	case RID_BREAK:
5438	case RID_CONTINUE:
5439	case RID_RETURN:
5440	case RID_GOTO:
5441	  statement = cp_parser_jump_statement (parser);
5442	  break;
5443
5444	case RID_TRY:
5445	  statement = cp_parser_try_block (parser);
5446	  break;
5447
5448	default:
5449	  /* It might be a keyword like `int' that can start a
5450	     declaration-statement.  */
5451	  break;
5452	}
5453    }
5454  else if (token->type == CPP_NAME)
5455    {
5456      /* If the next token is a `:', then we are looking at a
5457	 labeled-statement.  */
5458      token = cp_lexer_peek_nth_token (parser->lexer, 2);
5459      if (token->type == CPP_COLON)
5460	statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5461    }
5462  /* Anything that starts with a `{' must be a compound-statement.  */
5463  else if (token->type == CPP_OPEN_BRACE)
5464    statement = cp_parser_compound_statement (parser, false);
5465
5466  /* Everything else must be a declaration-statement or an
5467     expression-statement.  Try for the declaration-statement
5468     first, unless we are looking at a `;', in which case we know that
5469     we have an expression-statement.  */
5470  if (!statement)
5471    {
5472      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5473	{
5474	  cp_parser_parse_tentatively (parser);
5475	  /* Try to parse the declaration-statement.  */
5476	  cp_parser_declaration_statement (parser);
5477	  /* If that worked, we're done.  */
5478	  if (cp_parser_parse_definitely (parser))
5479	    return;
5480	}
5481      /* Look for an expression-statement instead.  */
5482      statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5483    }
5484
5485  /* Set the line number for the statement.  */
5486  if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5487    STMT_LINENO (statement) = statement_line_number;
5488}
5489
5490/* Parse a labeled-statement.
5491
5492   labeled-statement:
5493     identifier : statement
5494     case constant-expression : statement
5495     default : statement
5496
5497   GNU Extension:
5498
5499   labeled-statement:
5500     case constant-expression ... constant-expression : statement
5501
5502   Returns the new CASE_LABEL, for a `case' or `default' label.  For
5503   an ordinary label, returns a LABEL_STMT.  */
5504
5505static tree
5506cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5507{
5508  cp_token *token;
5509  tree statement = error_mark_node;
5510
5511  /* The next token should be an identifier.  */
5512  token = cp_lexer_peek_token (parser->lexer);
5513  if (token->type != CPP_NAME
5514      && token->type != CPP_KEYWORD)
5515    {
5516      cp_parser_error (parser, "expected labeled-statement");
5517      return error_mark_node;
5518    }
5519
5520  switch (token->keyword)
5521    {
5522    case RID_CASE:
5523      {
5524	tree expr, expr_hi;
5525	cp_token *ellipsis;
5526
5527	/* Consume the `case' token.  */
5528	cp_lexer_consume_token (parser->lexer);
5529	/* Parse the constant-expression.  */
5530	expr = cp_parser_constant_expression (parser,
5531					      /*allow_non_constant_p=*/false,
5532					      NULL);
5533
5534	ellipsis = cp_lexer_peek_token (parser->lexer);
5535	if (ellipsis->type == CPP_ELLIPSIS)
5536	  {
5537            /* Consume the `...' token.  */
5538	    cp_lexer_consume_token (parser->lexer);
5539	    expr_hi =
5540	      cp_parser_constant_expression (parser,
5541	    				     /*allow_non_constant_p=*/false,
5542					     NULL);
5543	    /* We don't need to emit warnings here, as the common code
5544	       will do this for us.  */
5545	  }
5546	else
5547	  expr_hi = NULL_TREE;
5548
5549	if (!parser->in_switch_statement_p)
5550	  error ("case label `%E' not within a switch statement", expr);
5551	else
5552	  statement = finish_case_label (expr, expr_hi);
5553      }
5554      break;
5555
5556    case RID_DEFAULT:
5557      /* Consume the `default' token.  */
5558      cp_lexer_consume_token (parser->lexer);
5559      if (!parser->in_switch_statement_p)
5560	error ("case label not within a switch statement");
5561      else
5562	statement = finish_case_label (NULL_TREE, NULL_TREE);
5563      break;
5564
5565    default:
5566      /* Anything else must be an ordinary label.  */
5567      statement = finish_label_stmt (cp_parser_identifier (parser));
5568      break;
5569    }
5570
5571  /* Require the `:' token.  */
5572  cp_parser_require (parser, CPP_COLON, "`:'");
5573  /* Parse the labeled statement.  */
5574  cp_parser_statement (parser, in_statement_expr_p);
5575
5576  /* Return the label, in the case of a `case' or `default' label.  */
5577  return statement;
5578}
5579
5580/* Parse an expression-statement.
5581
5582   expression-statement:
5583     expression [opt] ;
5584
5585   Returns the new EXPR_STMT -- or NULL_TREE if the expression
5586   statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5587   indicates whether this expression-statement is part of an
5588   expression statement.  */
5589
5590static tree
5591cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5592{
5593  tree statement = NULL_TREE;
5594
5595  /* If the next token is a ';', then there is no expression
5596     statement.  */
5597  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5598    statement = cp_parser_expression (parser);
5599
5600  /* Consume the final `;'.  */
5601  cp_parser_consume_semicolon_at_end_of_statement (parser);
5602
5603  if (in_statement_expr_p
5604      && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5605    {
5606      /* This is the final expression statement of a statement
5607	 expression.  */
5608      statement = finish_stmt_expr_expr (statement);
5609    }
5610  else if (statement)
5611    statement = finish_expr_stmt (statement);
5612  else
5613    finish_stmt ();
5614
5615  return statement;
5616}
5617
5618/* Parse a compound-statement.
5619
5620   compound-statement:
5621     { statement-seq [opt] }
5622
5623   Returns a COMPOUND_STMT representing the statement.  */
5624
5625static tree
5626cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5627{
5628  tree compound_stmt;
5629
5630  /* Consume the `{'.  */
5631  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5632    return error_mark_node;
5633  /* Begin the compound-statement.  */
5634  compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5635  /* Parse an (optional) statement-seq.  */
5636  cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5637  /* Finish the compound-statement.  */
5638  finish_compound_stmt (compound_stmt);
5639  /* Consume the `}'.  */
5640  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5641
5642  return compound_stmt;
5643}
5644
5645/* Parse an (optional) statement-seq.
5646
5647   statement-seq:
5648     statement
5649     statement-seq [opt] statement  */
5650
5651static void
5652cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5653{
5654  /* Scan statements until there aren't any more.  */
5655  while (true)
5656    {
5657      /* If we're looking at a `}', then we've run out of statements.  */
5658      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5659	  || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5660	break;
5661
5662      /* Parse the statement.  */
5663      cp_parser_statement (parser, in_statement_expr_p);
5664    }
5665}
5666
5667/* Parse a selection-statement.
5668
5669   selection-statement:
5670     if ( condition ) statement
5671     if ( condition ) statement else statement
5672     switch ( condition ) statement
5673
5674   Returns the new IF_STMT or SWITCH_STMT.  */
5675
5676static tree
5677cp_parser_selection_statement (cp_parser* parser)
5678{
5679  cp_token *token;
5680  enum rid keyword;
5681
5682  /* Peek at the next token.  */
5683  token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5684
5685  /* See what kind of keyword it is.  */
5686  keyword = token->keyword;
5687  switch (keyword)
5688    {
5689    case RID_IF:
5690    case RID_SWITCH:
5691      {
5692	tree statement;
5693	tree condition;
5694
5695	/* Look for the `('.  */
5696	if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5697	  {
5698	    cp_parser_skip_to_end_of_statement (parser);
5699	    return error_mark_node;
5700	  }
5701
5702	/* Begin the selection-statement.  */
5703	if (keyword == RID_IF)
5704	  statement = begin_if_stmt ();
5705	else
5706	  statement = begin_switch_stmt ();
5707
5708	/* Parse the condition.  */
5709	condition = cp_parser_condition (parser);
5710	/* Look for the `)'.  */
5711	if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5712	  cp_parser_skip_to_closing_parenthesis (parser, true, false,
5713						 /*consume_paren=*/true);
5714
5715	if (keyword == RID_IF)
5716	  {
5717	    tree then_stmt;
5718
5719	    /* Add the condition.  */
5720	    finish_if_stmt_cond (condition, statement);
5721
5722	    /* Parse the then-clause.  */
5723	    then_stmt = cp_parser_implicitly_scoped_statement (parser);
5724	    finish_then_clause (statement);
5725
5726	    /* If the next token is `else', parse the else-clause.  */
5727	    if (cp_lexer_next_token_is_keyword (parser->lexer,
5728						RID_ELSE))
5729	      {
5730		tree else_stmt;
5731
5732		/* Consume the `else' keyword.  */
5733		cp_lexer_consume_token (parser->lexer);
5734		/* Parse the else-clause.  */
5735		else_stmt
5736		  = cp_parser_implicitly_scoped_statement (parser);
5737		finish_else_clause (statement);
5738	      }
5739
5740	    /* Now we're all done with the if-statement.  */
5741	    finish_if_stmt ();
5742	  }
5743	else
5744	  {
5745	    tree body;
5746	    bool in_switch_statement_p;
5747
5748	    /* Add the condition.  */
5749	    finish_switch_cond (condition, statement);
5750
5751	    /* Parse the body of the switch-statement.  */
5752	    in_switch_statement_p = parser->in_switch_statement_p;
5753	    parser->in_switch_statement_p = true;
5754	    body = cp_parser_implicitly_scoped_statement (parser);
5755	    parser->in_switch_statement_p = in_switch_statement_p;
5756
5757	    /* Now we're all done with the switch-statement.  */
5758	    finish_switch_stmt (statement);
5759	  }
5760
5761	return statement;
5762      }
5763      break;
5764
5765    default:
5766      cp_parser_error (parser, "expected selection-statement");
5767      return error_mark_node;
5768    }
5769}
5770
5771/* Parse a condition.
5772
5773   condition:
5774     expression
5775     type-specifier-seq declarator = assignment-expression
5776
5777   GNU Extension:
5778
5779   condition:
5780     type-specifier-seq declarator asm-specification [opt]
5781       attributes [opt] = assignment-expression
5782
5783   Returns the expression that should be tested.  */
5784
5785static tree
5786cp_parser_condition (cp_parser* parser)
5787{
5788  tree type_specifiers;
5789  const char *saved_message;
5790
5791  /* Try the declaration first.  */
5792  cp_parser_parse_tentatively (parser);
5793  /* New types are not allowed in the type-specifier-seq for a
5794     condition.  */
5795  saved_message = parser->type_definition_forbidden_message;
5796  parser->type_definition_forbidden_message
5797    = "types may not be defined in conditions";
5798  /* Parse the type-specifier-seq.  */
5799  type_specifiers = cp_parser_type_specifier_seq (parser);
5800  /* Restore the saved message.  */
5801  parser->type_definition_forbidden_message = saved_message;
5802  /* If all is well, we might be looking at a declaration.  */
5803  if (!cp_parser_error_occurred (parser))
5804    {
5805      tree decl;
5806      tree asm_specification;
5807      tree attributes;
5808      tree declarator;
5809      tree initializer = NULL_TREE;
5810
5811      /* Parse the declarator.  */
5812      declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5813					 /*ctor_dtor_or_conv_p=*/NULL,
5814					 /*parenthesized_p=*/NULL);
5815      /* Parse the attributes.  */
5816      attributes = cp_parser_attributes_opt (parser);
5817      /* Parse the asm-specification.  */
5818      asm_specification = cp_parser_asm_specification_opt (parser);
5819      /* If the next token is not an `=', then we might still be
5820	 looking at an expression.  For example:
5821
5822	   if (A(a).x)
5823
5824	 looks like a decl-specifier-seq and a declarator -- but then
5825	 there is no `=', so this is an expression.  */
5826      cp_parser_require (parser, CPP_EQ, "`='");
5827      /* If we did see an `=', then we are looking at a declaration
5828	 for sure.  */
5829      if (cp_parser_parse_definitely (parser))
5830	{
5831	  /* Create the declaration.  */
5832	  decl = start_decl (declarator, type_specifiers,
5833			     /*initialized_p=*/true,
5834			     attributes, /*prefix_attributes=*/NULL_TREE);
5835	  /* Parse the assignment-expression.  */
5836	  initializer = cp_parser_assignment_expression (parser);
5837
5838	  /* Process the initializer.  */
5839	  cp_finish_decl (decl,
5840			  initializer,
5841			  asm_specification,
5842			  LOOKUP_ONLYCONVERTING);
5843
5844	  return convert_from_reference (decl);
5845	}
5846    }
5847  /* If we didn't even get past the declarator successfully, we are
5848     definitely not looking at a declaration.  */
5849  else
5850    cp_parser_abort_tentative_parse (parser);
5851
5852  /* Otherwise, we are looking at an expression.  */
5853  return cp_parser_expression (parser);
5854}
5855
5856/* Parse an iteration-statement.
5857
5858   iteration-statement:
5859     while ( condition ) statement
5860     do statement while ( expression ) ;
5861     for ( for-init-statement condition [opt] ; expression [opt] )
5862       statement
5863
5864   Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
5865
5866static tree
5867cp_parser_iteration_statement (cp_parser* parser)
5868{
5869  cp_token *token;
5870  enum rid keyword;
5871  tree statement;
5872  bool in_iteration_statement_p;
5873
5874
5875  /* Peek at the next token.  */
5876  token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5877  if (!token)
5878    return error_mark_node;
5879
5880  /* Remember whether or not we are already within an iteration
5881     statement.  */
5882  in_iteration_statement_p = parser->in_iteration_statement_p;
5883
5884  /* See what kind of keyword it is.  */
5885  keyword = token->keyword;
5886  switch (keyword)
5887    {
5888    case RID_WHILE:
5889      {
5890	tree condition;
5891
5892	/* Begin the while-statement.  */
5893	statement = begin_while_stmt ();
5894	/* Look for the `('.  */
5895	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5896	/* Parse the condition.  */
5897	condition = cp_parser_condition (parser);
5898	finish_while_stmt_cond (condition, statement);
5899	/* Look for the `)'.  */
5900	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5901	/* Parse the dependent statement.  */
5902	parser->in_iteration_statement_p = true;
5903	cp_parser_already_scoped_statement (parser);
5904	parser->in_iteration_statement_p = in_iteration_statement_p;
5905	/* We're done with the while-statement.  */
5906	finish_while_stmt (statement);
5907      }
5908      break;
5909
5910    case RID_DO:
5911      {
5912	tree expression;
5913
5914	/* Begin the do-statement.  */
5915	statement = begin_do_stmt ();
5916	/* Parse the body of the do-statement.  */
5917	parser->in_iteration_statement_p = true;
5918	cp_parser_implicitly_scoped_statement (parser);
5919	parser->in_iteration_statement_p = in_iteration_statement_p;
5920	finish_do_body (statement);
5921	/* Look for the `while' keyword.  */
5922	cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5923	/* Look for the `('.  */
5924	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5925	/* Parse the expression.  */
5926	expression = cp_parser_expression (parser);
5927	/* We're done with the do-statement.  */
5928	finish_do_stmt (expression, statement);
5929	/* Look for the `)'.  */
5930	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5931	/* Look for the `;'.  */
5932	cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5933      }
5934      break;
5935
5936    case RID_FOR:
5937      {
5938	tree condition = NULL_TREE;
5939	tree expression = NULL_TREE;
5940
5941	/* Begin the for-statement.  */
5942	statement = begin_for_stmt ();
5943	/* Look for the `('.  */
5944	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5945	/* Parse the initialization.  */
5946	cp_parser_for_init_statement (parser);
5947	finish_for_init_stmt (statement);
5948
5949	/* If there's a condition, process it.  */
5950	if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5951	  condition = cp_parser_condition (parser);
5952	finish_for_cond (condition, statement);
5953	/* Look for the `;'.  */
5954	cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5955
5956	/* If there's an expression, process it.  */
5957	if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5958	  expression = cp_parser_expression (parser);
5959	finish_for_expr (expression, statement);
5960	/* Look for the `)'.  */
5961	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5962
5963	/* Parse the body of the for-statement.  */
5964	parser->in_iteration_statement_p = true;
5965	cp_parser_already_scoped_statement (parser);
5966	parser->in_iteration_statement_p = in_iteration_statement_p;
5967
5968	/* We're done with the for-statement.  */
5969	finish_for_stmt (statement);
5970      }
5971      break;
5972
5973    default:
5974      cp_parser_error (parser, "expected iteration-statement");
5975      statement = error_mark_node;
5976      break;
5977    }
5978
5979  return statement;
5980}
5981
5982/* Parse a for-init-statement.
5983
5984   for-init-statement:
5985     expression-statement
5986     simple-declaration  */
5987
5988static void
5989cp_parser_for_init_statement (cp_parser* parser)
5990{
5991  /* If the next token is a `;', then we have an empty
5992     expression-statement.  Grammatically, this is also a
5993     simple-declaration, but an invalid one, because it does not
5994     declare anything.  Therefore, if we did not handle this case
5995     specially, we would issue an error message about an invalid
5996     declaration.  */
5997  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5998    {
5999      /* We're going to speculatively look for a declaration, falling back
6000	 to an expression, if necessary.  */
6001      cp_parser_parse_tentatively (parser);
6002      /* Parse the declaration.  */
6003      cp_parser_simple_declaration (parser,
6004				    /*function_definition_allowed_p=*/false);
6005      /* If the tentative parse failed, then we shall need to look for an
6006	 expression-statement.  */
6007      if (cp_parser_parse_definitely (parser))
6008	return;
6009    }
6010
6011  cp_parser_expression_statement (parser, false);
6012}
6013
6014/* Parse a jump-statement.
6015
6016   jump-statement:
6017     break ;
6018     continue ;
6019     return expression [opt] ;
6020     goto identifier ;
6021
6022   GNU extension:
6023
6024   jump-statement:
6025     goto * expression ;
6026
6027   Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6028   GOTO_STMT.  */
6029
6030static tree
6031cp_parser_jump_statement (cp_parser* parser)
6032{
6033  tree statement = error_mark_node;
6034  cp_token *token;
6035  enum rid keyword;
6036
6037  /* Peek at the next token.  */
6038  token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6039  if (!token)
6040    return error_mark_node;
6041
6042  /* See what kind of keyword it is.  */
6043  keyword = token->keyword;
6044  switch (keyword)
6045    {
6046    case RID_BREAK:
6047      if (!parser->in_switch_statement_p
6048	  && !parser->in_iteration_statement_p)
6049	{
6050	  error ("break statement not within loop or switch");
6051	  statement = error_mark_node;
6052	}
6053      else
6054	statement = finish_break_stmt ();
6055      cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6056      break;
6057
6058    case RID_CONTINUE:
6059      if (!parser->in_iteration_statement_p)
6060	{
6061	  error ("continue statement not within a loop");
6062	  statement = error_mark_node;
6063	}
6064      else
6065	statement = finish_continue_stmt ();
6066      cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6067      break;
6068
6069    case RID_RETURN:
6070      {
6071	tree expr;
6072
6073	/* If the next token is a `;', then there is no
6074	   expression.  */
6075	if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6076	  expr = cp_parser_expression (parser);
6077	else
6078	  expr = NULL_TREE;
6079	/* Build the return-statement.  */
6080	statement = finish_return_stmt (expr);
6081	/* Look for the final `;'.  */
6082	cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6083      }
6084      break;
6085
6086    case RID_GOTO:
6087      /* Create the goto-statement.  */
6088      if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6089	{
6090	  /* Issue a warning about this use of a GNU extension.  */
6091	  if (pedantic)
6092	    pedwarn ("ISO C++ forbids computed gotos");
6093	  /* Consume the '*' token.  */
6094	  cp_lexer_consume_token (parser->lexer);
6095	  /* Parse the dependent expression.  */
6096	  finish_goto_stmt (cp_parser_expression (parser));
6097	}
6098      else
6099	finish_goto_stmt (cp_parser_identifier (parser));
6100      /* Look for the final `;'.  */
6101      cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6102      break;
6103
6104    default:
6105      cp_parser_error (parser, "expected jump-statement");
6106      break;
6107    }
6108
6109  return statement;
6110}
6111
6112/* Parse a declaration-statement.
6113
6114   declaration-statement:
6115     block-declaration  */
6116
6117static void
6118cp_parser_declaration_statement (cp_parser* parser)
6119{
6120  /* Parse the block-declaration.  */
6121  cp_parser_block_declaration (parser, /*statement_p=*/true);
6122
6123  /* Finish off the statement.  */
6124  finish_stmt ();
6125}
6126
6127/* Some dependent statements (like `if (cond) statement'), are
6128   implicitly in their own scope.  In other words, if the statement is
6129   a single statement (as opposed to a compound-statement), it is
6130   none-the-less treated as if it were enclosed in braces.  Any
6131   declarations appearing in the dependent statement are out of scope
6132   after control passes that point.  This function parses a statement,
6133   but ensures that is in its own scope, even if it is not a
6134   compound-statement.
6135
6136   Returns the new statement.  */
6137
6138static tree
6139cp_parser_implicitly_scoped_statement (cp_parser* parser)
6140{
6141  tree statement;
6142
6143  /* If the token is not a `{', then we must take special action.  */
6144  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6145    {
6146      /* Create a compound-statement.  */
6147      statement = begin_compound_stmt (/*has_no_scope=*/false);
6148      /* Parse the dependent-statement.  */
6149      cp_parser_statement (parser, false);
6150      /* Finish the dummy compound-statement.  */
6151      finish_compound_stmt (statement);
6152    }
6153  /* Otherwise, we simply parse the statement directly.  */
6154  else
6155    statement = cp_parser_compound_statement (parser, false);
6156
6157  /* Return the statement.  */
6158  return statement;
6159}
6160
6161/* For some dependent statements (like `while (cond) statement'), we
6162   have already created a scope.  Therefore, even if the dependent
6163   statement is a compound-statement, we do not want to create another
6164   scope.  */
6165
6166static void
6167cp_parser_already_scoped_statement (cp_parser* parser)
6168{
6169  /* If the token is not a `{', then we must take special action.  */
6170  if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6171    {
6172      tree statement;
6173
6174      /* Create a compound-statement.  */
6175      statement = begin_compound_stmt (/*has_no_scope=*/true);
6176      /* Parse the dependent-statement.  */
6177      cp_parser_statement (parser, false);
6178      /* Finish the dummy compound-statement.  */
6179      finish_compound_stmt (statement);
6180    }
6181  /* Otherwise, we simply parse the statement directly.  */
6182  else
6183    cp_parser_statement (parser, false);
6184}
6185
6186/* Declarations [gram.dcl.dcl] */
6187
6188/* Parse an optional declaration-sequence.
6189
6190   declaration-seq:
6191     declaration
6192     declaration-seq declaration  */
6193
6194static void
6195cp_parser_declaration_seq_opt (cp_parser* parser)
6196{
6197  while (true)
6198    {
6199      cp_token *token;
6200
6201      token = cp_lexer_peek_token (parser->lexer);
6202
6203      if (token->type == CPP_CLOSE_BRACE
6204	  || token->type == CPP_EOF)
6205	break;
6206
6207      if (token->type == CPP_SEMICOLON)
6208	{
6209	  /* A declaration consisting of a single semicolon is
6210	     invalid.  Allow it unless we're being pedantic.  */
6211	  if (pedantic && !in_system_header)
6212	    pedwarn ("extra `;'");
6213	  cp_lexer_consume_token (parser->lexer);
6214	  continue;
6215	}
6216
6217      /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6218	 parser to enter or exit implicit `extern "C"' blocks.  */
6219      while (pending_lang_change > 0)
6220	{
6221	  push_lang_context (lang_name_c);
6222	  --pending_lang_change;
6223	}
6224      while (pending_lang_change < 0)
6225	{
6226	  pop_lang_context ();
6227	  ++pending_lang_change;
6228	}
6229
6230      /* Parse the declaration itself.  */
6231      cp_parser_declaration (parser);
6232    }
6233}
6234
6235/* Parse a declaration.
6236
6237   declaration:
6238     block-declaration
6239     function-definition
6240     template-declaration
6241     explicit-instantiation
6242     explicit-specialization
6243     linkage-specification
6244     namespace-definition
6245
6246   GNU extension:
6247
6248   declaration:
6249      __extension__ declaration */
6250
6251static void
6252cp_parser_declaration (cp_parser* parser)
6253{
6254  cp_token token1;
6255  cp_token token2;
6256  int saved_pedantic;
6257
6258  /* Check for the `__extension__' keyword.  */
6259  if (cp_parser_extension_opt (parser, &saved_pedantic))
6260    {
6261      /* Parse the qualified declaration.  */
6262      cp_parser_declaration (parser);
6263      /* Restore the PEDANTIC flag.  */
6264      pedantic = saved_pedantic;
6265
6266      return;
6267    }
6268
6269  /* Try to figure out what kind of declaration is present.  */
6270  token1 = *cp_lexer_peek_token (parser->lexer);
6271  if (token1.type != CPP_EOF)
6272    token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6273
6274  /* If the next token is `extern' and the following token is a string
6275     literal, then we have a linkage specification.  */
6276  if (token1.keyword == RID_EXTERN
6277      && cp_parser_is_string_literal (&token2))
6278    cp_parser_linkage_specification (parser);
6279  /* If the next token is `template', then we have either a template
6280     declaration, an explicit instantiation, or an explicit
6281     specialization.  */
6282  else if (token1.keyword == RID_TEMPLATE)
6283    {
6284      /* `template <>' indicates a template specialization.  */
6285      if (token2.type == CPP_LESS
6286	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6287	cp_parser_explicit_specialization (parser);
6288      /* `template <' indicates a template declaration.  */
6289      else if (token2.type == CPP_LESS)
6290	cp_parser_template_declaration (parser, /*member_p=*/false);
6291      /* Anything else must be an explicit instantiation.  */
6292      else
6293	cp_parser_explicit_instantiation (parser);
6294    }
6295  /* If the next token is `export', then we have a template
6296     declaration.  */
6297  else if (token1.keyword == RID_EXPORT)
6298    cp_parser_template_declaration (parser, /*member_p=*/false);
6299  /* If the next token is `extern', 'static' or 'inline' and the one
6300     after that is `template', we have a GNU extended explicit
6301     instantiation directive.  */
6302  else if (cp_parser_allow_gnu_extensions_p (parser)
6303	   && (token1.keyword == RID_EXTERN
6304	       || token1.keyword == RID_STATIC
6305	       || token1.keyword == RID_INLINE)
6306	   && token2.keyword == RID_TEMPLATE)
6307    cp_parser_explicit_instantiation (parser);
6308  /* If the next token is `namespace', check for a named or unnamed
6309     namespace definition.  */
6310  else if (token1.keyword == RID_NAMESPACE
6311	   && (/* A named namespace definition.  */
6312	       (token2.type == CPP_NAME
6313		&& (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6314		    == CPP_OPEN_BRACE))
6315	       /* An unnamed namespace definition.  */
6316	       || token2.type == CPP_OPEN_BRACE))
6317    cp_parser_namespace_definition (parser);
6318  /* We must have either a block declaration or a function
6319     definition.  */
6320  else
6321    /* Try to parse a block-declaration, or a function-definition.  */
6322    cp_parser_block_declaration (parser, /*statement_p=*/false);
6323}
6324
6325/* Parse a block-declaration.
6326
6327   block-declaration:
6328     simple-declaration
6329     asm-definition
6330     namespace-alias-definition
6331     using-declaration
6332     using-directive
6333
6334   GNU Extension:
6335
6336   block-declaration:
6337     __extension__ block-declaration
6338     label-declaration
6339
6340   If STATEMENT_P is TRUE, then this block-declaration is occurring as
6341   part of a declaration-statement.  */
6342
6343static void
6344cp_parser_block_declaration (cp_parser *parser,
6345			     bool      statement_p)
6346{
6347  cp_token *token1;
6348  int saved_pedantic;
6349
6350  /* Check for the `__extension__' keyword.  */
6351  if (cp_parser_extension_opt (parser, &saved_pedantic))
6352    {
6353      /* Parse the qualified declaration.  */
6354      cp_parser_block_declaration (parser, statement_p);
6355      /* Restore the PEDANTIC flag.  */
6356      pedantic = saved_pedantic;
6357
6358      return;
6359    }
6360
6361  /* Peek at the next token to figure out which kind of declaration is
6362     present.  */
6363  token1 = cp_lexer_peek_token (parser->lexer);
6364
6365  /* If the next keyword is `asm', we have an asm-definition.  */
6366  if (token1->keyword == RID_ASM)
6367    {
6368      if (statement_p)
6369	cp_parser_commit_to_tentative_parse (parser);
6370      cp_parser_asm_definition (parser);
6371    }
6372  /* If the next keyword is `namespace', we have a
6373     namespace-alias-definition.  */
6374  else if (token1->keyword == RID_NAMESPACE)
6375    cp_parser_namespace_alias_definition (parser);
6376  /* If the next keyword is `using', we have either a
6377     using-declaration or a using-directive.  */
6378  else if (token1->keyword == RID_USING)
6379    {
6380      cp_token *token2;
6381
6382      if (statement_p)
6383	cp_parser_commit_to_tentative_parse (parser);
6384      /* If the token after `using' is `namespace', then we have a
6385	 using-directive.  */
6386      token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6387      if (token2->keyword == RID_NAMESPACE)
6388	cp_parser_using_directive (parser);
6389      /* Otherwise, it's a using-declaration.  */
6390      else
6391	cp_parser_using_declaration (parser);
6392    }
6393  /* If the next keyword is `__label__' we have a label declaration.  */
6394  else if (token1->keyword == RID_LABEL)
6395    {
6396      if (statement_p)
6397	cp_parser_commit_to_tentative_parse (parser);
6398      cp_parser_label_declaration (parser);
6399    }
6400  /* Anything else must be a simple-declaration.  */
6401  else
6402    cp_parser_simple_declaration (parser, !statement_p);
6403}
6404
6405/* Parse a simple-declaration.
6406
6407   simple-declaration:
6408     decl-specifier-seq [opt] init-declarator-list [opt] ;
6409
6410   init-declarator-list:
6411     init-declarator
6412     init-declarator-list , init-declarator
6413
6414   If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6415   function-definition as a simple-declaration.  */
6416
6417static void
6418cp_parser_simple_declaration (cp_parser* parser,
6419                              bool function_definition_allowed_p)
6420{
6421  tree decl_specifiers;
6422  tree attributes;
6423  int declares_class_or_enum;
6424  bool saw_declarator;
6425
6426  /* Defer access checks until we know what is being declared; the
6427     checks for names appearing in the decl-specifier-seq should be
6428     done as if we were in the scope of the thing being declared.  */
6429  push_deferring_access_checks (dk_deferred);
6430
6431  /* Parse the decl-specifier-seq.  We have to keep track of whether
6432     or not the decl-specifier-seq declares a named class or
6433     enumeration type, since that is the only case in which the
6434     init-declarator-list is allowed to be empty.
6435
6436     [dcl.dcl]
6437
6438     In a simple-declaration, the optional init-declarator-list can be
6439     omitted only when declaring a class or enumeration, that is when
6440     the decl-specifier-seq contains either a class-specifier, an
6441     elaborated-type-specifier, or an enum-specifier.  */
6442  decl_specifiers
6443    = cp_parser_decl_specifier_seq (parser,
6444				    CP_PARSER_FLAGS_OPTIONAL,
6445				    &attributes,
6446				    &declares_class_or_enum);
6447  /* We no longer need to defer access checks.  */
6448  stop_deferring_access_checks ();
6449
6450  /* In a block scope, a valid declaration must always have a
6451     decl-specifier-seq.  By not trying to parse declarators, we can
6452     resolve the declaration/expression ambiguity more quickly.  */
6453  if (!function_definition_allowed_p && !decl_specifiers)
6454    {
6455      cp_parser_error (parser, "expected declaration");
6456      goto done;
6457    }
6458
6459  /* If the next two tokens are both identifiers, the code is
6460     erroneous. The usual cause of this situation is code like:
6461
6462       T t;
6463
6464     where "T" should name a type -- but does not.  */
6465  if (cp_parser_diagnose_invalid_type_name (parser))
6466    {
6467      /* If parsing tentatively, we should commit; we really are
6468	 looking at a declaration.  */
6469      cp_parser_commit_to_tentative_parse (parser);
6470      /* Give up.  */
6471      goto done;
6472    }
6473
6474  /* Keep going until we hit the `;' at the end of the simple
6475     declaration.  */
6476  saw_declarator = false;
6477  while (cp_lexer_next_token_is_not (parser->lexer,
6478				     CPP_SEMICOLON))
6479    {
6480      cp_token *token;
6481      bool function_definition_p;
6482      tree decl;
6483
6484      saw_declarator = true;
6485      /* Parse the init-declarator.  */
6486      decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6487					function_definition_allowed_p,
6488					/*member_p=*/false,
6489					declares_class_or_enum,
6490					&function_definition_p);
6491      /* If an error occurred while parsing tentatively, exit quickly.
6492	 (That usually happens when in the body of a function; each
6493	 statement is treated as a declaration-statement until proven
6494	 otherwise.)  */
6495      if (cp_parser_error_occurred (parser))
6496	goto done;
6497      /* Handle function definitions specially.  */
6498      if (function_definition_p)
6499	{
6500	  /* If the next token is a `,', then we are probably
6501	     processing something like:
6502
6503	       void f() {}, *p;
6504
6505	     which is erroneous.  */
6506	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6507	    error ("mixing declarations and function-definitions is forbidden");
6508	  /* Otherwise, we're done with the list of declarators.  */
6509	  else
6510	    {
6511	      pop_deferring_access_checks ();
6512	      return;
6513	    }
6514	}
6515      /* The next token should be either a `,' or a `;'.  */
6516      token = cp_lexer_peek_token (parser->lexer);
6517      /* If it's a `,', there are more declarators to come.  */
6518      if (token->type == CPP_COMMA)
6519	cp_lexer_consume_token (parser->lexer);
6520      /* If it's a `;', we are done.  */
6521      else if (token->type == CPP_SEMICOLON)
6522	break;
6523      /* Anything else is an error.  */
6524      else
6525	{
6526	  cp_parser_error (parser, "expected `,' or `;'");
6527	  /* Skip tokens until we reach the end of the statement.  */
6528	  cp_parser_skip_to_end_of_statement (parser);
6529	  /* If the next token is now a `;', consume it.  */
6530	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6531	    cp_lexer_consume_token (parser->lexer);
6532	  goto done;
6533	}
6534      /* After the first time around, a function-definition is not
6535	 allowed -- even if it was OK at first.  For example:
6536
6537           int i, f() {}
6538
6539         is not valid.  */
6540      function_definition_allowed_p = false;
6541    }
6542
6543  /* Issue an error message if no declarators are present, and the
6544     decl-specifier-seq does not itself declare a class or
6545     enumeration.  */
6546  if (!saw_declarator)
6547    {
6548      if (cp_parser_declares_only_class_p (parser))
6549	shadow_tag (decl_specifiers);
6550      /* Perform any deferred access checks.  */
6551      perform_deferred_access_checks ();
6552    }
6553
6554  /* Consume the `;'.  */
6555  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6556
6557 done:
6558  pop_deferring_access_checks ();
6559}
6560
6561/* Parse a decl-specifier-seq.
6562
6563   decl-specifier-seq:
6564     decl-specifier-seq [opt] decl-specifier
6565
6566   decl-specifier:
6567     storage-class-specifier
6568     type-specifier
6569     function-specifier
6570     friend
6571     typedef
6572
6573   GNU Extension:
6574
6575   decl-specifier:
6576     attributes
6577
6578   Returns a TREE_LIST, giving the decl-specifiers in the order they
6579   appear in the source code.  The TREE_VALUE of each node is the
6580   decl-specifier.  For a keyword (such as `auto' or `friend'), the
6581   TREE_VALUE is simply the corresponding TREE_IDENTIFIER.  For the
6582   representation of a type-specifier, see cp_parser_type_specifier.
6583
6584   If there are attributes, they will be stored in *ATTRIBUTES,
6585   represented as described above cp_parser_attributes.
6586
6587   If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6588   appears, and the entity that will be a friend is not going to be a
6589   class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
6590   even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6591   friendship is granted might not be a class.
6592
6593   *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6594   flags:
6595
6596     1: one of the decl-specifiers is an elaborated-type-specifier
6597        (i.e., a type declaration)
6598     2: one of the decl-specifiers is an enum-specifier or a
6599        class-specifier (i.e., a type definition)
6600
6601   */
6602
6603static tree
6604cp_parser_decl_specifier_seq (cp_parser* parser,
6605                              cp_parser_flags flags,
6606                              tree* attributes,
6607			      int* declares_class_or_enum)
6608{
6609  tree decl_specs = NULL_TREE;
6610  bool friend_p = false;
6611  bool constructor_possible_p = !parser->in_declarator_p;
6612
6613  /* Assume no class or enumeration type is declared.  */
6614  *declares_class_or_enum = 0;
6615
6616  /* Assume there are no attributes.  */
6617  *attributes = NULL_TREE;
6618
6619  /* Keep reading specifiers until there are no more to read.  */
6620  while (true)
6621    {
6622      tree decl_spec = NULL_TREE;
6623      bool constructor_p;
6624      cp_token *token;
6625
6626      /* Peek at the next token.  */
6627      token = cp_lexer_peek_token (parser->lexer);
6628      /* Handle attributes.  */
6629      if (token->keyword == RID_ATTRIBUTE)
6630	{
6631	  /* Parse the attributes.  */
6632	  decl_spec = cp_parser_attributes_opt (parser);
6633	  /* Add them to the list.  */
6634	  *attributes = chainon (*attributes, decl_spec);
6635	  continue;
6636	}
6637      /* If the next token is an appropriate keyword, we can simply
6638	 add it to the list.  */
6639      switch (token->keyword)
6640	{
6641	case RID_FRIEND:
6642	  /* decl-specifier:
6643	       friend  */
6644	  if (friend_p)
6645	    error ("duplicate `friend'");
6646	  else
6647	    friend_p = true;
6648	  /* The representation of the specifier is simply the
6649	     appropriate TREE_IDENTIFIER node.  */
6650	  decl_spec = token->value;
6651	  /* Consume the token.  */
6652	  cp_lexer_consume_token (parser->lexer);
6653	  break;
6654
6655	  /* function-specifier:
6656	       inline
6657	       virtual
6658	       explicit  */
6659	case RID_INLINE:
6660	case RID_VIRTUAL:
6661	case RID_EXPLICIT:
6662	  decl_spec = cp_parser_function_specifier_opt (parser);
6663	  break;
6664
6665	  /* decl-specifier:
6666	       typedef  */
6667	case RID_TYPEDEF:
6668	  /* The representation of the specifier is simply the
6669	     appropriate TREE_IDENTIFIER node.  */
6670	  decl_spec = token->value;
6671	  /* Consume the token.  */
6672	  cp_lexer_consume_token (parser->lexer);
6673	  /* A constructor declarator cannot appear in a typedef.  */
6674	  constructor_possible_p = false;
6675	  /* The "typedef" keyword can only occur in a declaration; we
6676	     may as well commit at this point.  */
6677	  cp_parser_commit_to_tentative_parse (parser);
6678	  break;
6679
6680	  /* storage-class-specifier:
6681	       auto
6682	       register
6683	       static
6684	       extern
6685	       mutable
6686
6687             GNU Extension:
6688	       thread  */
6689	case RID_AUTO:
6690	case RID_REGISTER:
6691	case RID_STATIC:
6692	case RID_EXTERN:
6693	case RID_MUTABLE:
6694	case RID_THREAD:
6695	  decl_spec = cp_parser_storage_class_specifier_opt (parser);
6696	  break;
6697
6698	default:
6699	  break;
6700	}
6701
6702      /* Constructors are a special case.  The `S' in `S()' is not a
6703	 decl-specifier; it is the beginning of the declarator.  */
6704      constructor_p = (!decl_spec
6705		       && constructor_possible_p
6706		       && cp_parser_constructor_declarator_p (parser,
6707							      friend_p));
6708
6709      /* If we don't have a DECL_SPEC yet, then we must be looking at
6710	 a type-specifier.  */
6711      if (!decl_spec && !constructor_p)
6712	{
6713	  int decl_spec_declares_class_or_enum;
6714	  bool is_cv_qualifier;
6715
6716	  decl_spec
6717	    = cp_parser_type_specifier (parser, flags,
6718					friend_p,
6719					/*is_declaration=*/true,
6720					&decl_spec_declares_class_or_enum,
6721					&is_cv_qualifier);
6722
6723	  *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6724
6725	  /* If this type-specifier referenced a user-defined type
6726	     (a typedef, class-name, etc.), then we can't allow any
6727	     more such type-specifiers henceforth.
6728
6729	     [dcl.spec]
6730
6731	     The longest sequence of decl-specifiers that could
6732	     possibly be a type name is taken as the
6733	     decl-specifier-seq of a declaration.  The sequence shall
6734	     be self-consistent as described below.
6735
6736	     [dcl.type]
6737
6738	     As a general rule, at most one type-specifier is allowed
6739	     in the complete decl-specifier-seq of a declaration.  The
6740	     only exceptions are the following:
6741
6742	     -- const or volatile can be combined with any other
6743		type-specifier.
6744
6745	     -- signed or unsigned can be combined with char, long,
6746		short, or int.
6747
6748	     -- ..
6749
6750	     Example:
6751
6752	       typedef char* Pc;
6753	       void g (const int Pc);
6754
6755	     Here, Pc is *not* part of the decl-specifier seq; it's
6756	     the declarator.  Therefore, once we see a type-specifier
6757	     (other than a cv-qualifier), we forbid any additional
6758	     user-defined types.  We *do* still allow things like `int
6759	     int' to be considered a decl-specifier-seq, and issue the
6760	     error message later.  */
6761	  if (decl_spec && !is_cv_qualifier)
6762	    flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6763	  /* A constructor declarator cannot follow a type-specifier.  */
6764	  if (decl_spec)
6765	    constructor_possible_p = false;
6766	}
6767
6768      /* If we still do not have a DECL_SPEC, then there are no more
6769	 decl-specifiers.  */
6770      if (!decl_spec)
6771	{
6772	  /* Issue an error message, unless the entire construct was
6773             optional.  */
6774	  if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6775	    {
6776	      cp_parser_error (parser, "expected decl specifier");
6777	      return error_mark_node;
6778	    }
6779
6780	  break;
6781	}
6782
6783      /* Add the DECL_SPEC to the list of specifiers.  */
6784      if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6785	decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6786
6787      /* After we see one decl-specifier, further decl-specifiers are
6788	 always optional.  */
6789      flags |= CP_PARSER_FLAGS_OPTIONAL;
6790    }
6791
6792  /* Don't allow a friend specifier with a class definition.  */
6793  if (friend_p && (*declares_class_or_enum & 2))
6794    error ("class definition may not be declared a friend");
6795
6796  /* We have built up the DECL_SPECS in reverse order.  Return them in
6797     the correct order.  */
6798  return nreverse (decl_specs);
6799}
6800
6801/* Parse an (optional) storage-class-specifier.
6802
6803   storage-class-specifier:
6804     auto
6805     register
6806     static
6807     extern
6808     mutable
6809
6810   GNU Extension:
6811
6812   storage-class-specifier:
6813     thread
6814
6815   Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6816
6817static tree
6818cp_parser_storage_class_specifier_opt (cp_parser* parser)
6819{
6820  switch (cp_lexer_peek_token (parser->lexer)->keyword)
6821    {
6822    case RID_AUTO:
6823    case RID_REGISTER:
6824    case RID_STATIC:
6825    case RID_EXTERN:
6826    case RID_MUTABLE:
6827    case RID_THREAD:
6828      /* Consume the token.  */
6829      return cp_lexer_consume_token (parser->lexer)->value;
6830
6831    default:
6832      return NULL_TREE;
6833    }
6834}
6835
6836/* Parse an (optional) function-specifier.
6837
6838   function-specifier:
6839     inline
6840     virtual
6841     explicit
6842
6843   Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6844
6845static tree
6846cp_parser_function_specifier_opt (cp_parser* parser)
6847{
6848  switch (cp_lexer_peek_token (parser->lexer)->keyword)
6849    {
6850    case RID_INLINE:
6851    case RID_VIRTUAL:
6852    case RID_EXPLICIT:
6853      /* Consume the token.  */
6854      return cp_lexer_consume_token (parser->lexer)->value;
6855
6856    default:
6857      return NULL_TREE;
6858    }
6859}
6860
6861/* Parse a linkage-specification.
6862
6863   linkage-specification:
6864     extern string-literal { declaration-seq [opt] }
6865     extern string-literal declaration  */
6866
6867static void
6868cp_parser_linkage_specification (cp_parser* parser)
6869{
6870  cp_token *token;
6871  tree linkage;
6872
6873  /* Look for the `extern' keyword.  */
6874  cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6875
6876  /* Peek at the next token.  */
6877  token = cp_lexer_peek_token (parser->lexer);
6878  /* If it's not a string-literal, then there's a problem.  */
6879  if (!cp_parser_is_string_literal (token))
6880    {
6881      cp_parser_error (parser, "expected language-name");
6882      return;
6883    }
6884  /* Consume the token.  */
6885  cp_lexer_consume_token (parser->lexer);
6886
6887  /* Transform the literal into an identifier.  If the literal is a
6888     wide-character string, or contains embedded NULs, then we can't
6889     handle it as the user wants.  */
6890  if (token->type == CPP_WSTRING
6891      || (strlen (TREE_STRING_POINTER (token->value))
6892	  != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6893    {
6894      cp_parser_error (parser, "invalid linkage-specification");
6895      /* Assume C++ linkage.  */
6896      linkage = get_identifier ("c++");
6897    }
6898  /* If it's a simple string constant, things are easier.  */
6899  else
6900    linkage = get_identifier (TREE_STRING_POINTER (token->value));
6901
6902  /* We're now using the new linkage.  */
6903  push_lang_context (linkage);
6904
6905  /* If the next token is a `{', then we're using the first
6906     production.  */
6907  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6908    {
6909      /* Consume the `{' token.  */
6910      cp_lexer_consume_token (parser->lexer);
6911      /* Parse the declarations.  */
6912      cp_parser_declaration_seq_opt (parser);
6913      /* Look for the closing `}'.  */
6914      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6915    }
6916  /* Otherwise, there's just one declaration.  */
6917  else
6918    {
6919      bool saved_in_unbraced_linkage_specification_p;
6920
6921      saved_in_unbraced_linkage_specification_p
6922	= parser->in_unbraced_linkage_specification_p;
6923      parser->in_unbraced_linkage_specification_p = true;
6924      have_extern_spec = true;
6925      cp_parser_declaration (parser);
6926      have_extern_spec = false;
6927      parser->in_unbraced_linkage_specification_p
6928	= saved_in_unbraced_linkage_specification_p;
6929    }
6930
6931  /* We're done with the linkage-specification.  */
6932  pop_lang_context ();
6933}
6934
6935/* Special member functions [gram.special] */
6936
6937/* Parse a conversion-function-id.
6938
6939   conversion-function-id:
6940     operator conversion-type-id
6941
6942   Returns an IDENTIFIER_NODE representing the operator.  */
6943
6944static tree
6945cp_parser_conversion_function_id (cp_parser* parser)
6946{
6947  tree type;
6948  tree saved_scope;
6949  tree saved_qualifying_scope;
6950  tree saved_object_scope;
6951  bool pop_p = false;
6952
6953  /* Look for the `operator' token.  */
6954  if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6955    return error_mark_node;
6956  /* When we parse the conversion-type-id, the current scope will be
6957     reset.  However, we need that information in able to look up the
6958     conversion function later, so we save it here.  */
6959  saved_scope = parser->scope;
6960  saved_qualifying_scope = parser->qualifying_scope;
6961  saved_object_scope = parser->object_scope;
6962  /* We must enter the scope of the class so that the names of
6963     entities declared within the class are available in the
6964     conversion-type-id.  For example, consider:
6965
6966       struct S {
6967         typedef int I;
6968	 operator I();
6969       };
6970
6971       S::operator I() { ... }
6972
6973     In order to see that `I' is a type-name in the definition, we
6974     must be in the scope of `S'.  */
6975  if (saved_scope)
6976    pop_p = push_scope (saved_scope);
6977  /* Parse the conversion-type-id.  */
6978  type = cp_parser_conversion_type_id (parser);
6979  /* Leave the scope of the class, if any.  */
6980  if (pop_p)
6981    pop_scope (saved_scope);
6982  /* Restore the saved scope.  */
6983  parser->scope = saved_scope;
6984  parser->qualifying_scope = saved_qualifying_scope;
6985  parser->object_scope = saved_object_scope;
6986  /* If the TYPE is invalid, indicate failure.  */
6987  if (type == error_mark_node)
6988    return error_mark_node;
6989  return mangle_conv_op_name_for_type (type);
6990}
6991
6992/* Parse a conversion-type-id:
6993
6994   conversion-type-id:
6995     type-specifier-seq conversion-declarator [opt]
6996
6997   Returns the TYPE specified.  */
6998
6999static tree
7000cp_parser_conversion_type_id (cp_parser* parser)
7001{
7002  tree attributes;
7003  tree type_specifiers;
7004  tree declarator;
7005
7006  /* Parse the attributes.  */
7007  attributes = cp_parser_attributes_opt (parser);
7008  /* Parse the type-specifiers.  */
7009  type_specifiers = cp_parser_type_specifier_seq (parser);
7010  /* If that didn't work, stop.  */
7011  if (type_specifiers == error_mark_node)
7012    return error_mark_node;
7013  /* Parse the conversion-declarator.  */
7014  declarator = cp_parser_conversion_declarator_opt (parser);
7015
7016  return grokdeclarator (declarator, type_specifiers, TYPENAME,
7017			 /*initialized=*/0, &attributes);
7018}
7019
7020/* Parse an (optional) conversion-declarator.
7021
7022   conversion-declarator:
7023     ptr-operator conversion-declarator [opt]
7024
7025   Returns a representation of the declarator.  See
7026   cp_parser_declarator for details.  */
7027
7028static tree
7029cp_parser_conversion_declarator_opt (cp_parser* parser)
7030{
7031  enum tree_code code;
7032  tree class_type;
7033  tree cv_qualifier_seq;
7034
7035  /* We don't know if there's a ptr-operator next, or not.  */
7036  cp_parser_parse_tentatively (parser);
7037  /* Try the ptr-operator.  */
7038  code = cp_parser_ptr_operator (parser, &class_type,
7039				 &cv_qualifier_seq);
7040  /* If it worked, look for more conversion-declarators.  */
7041  if (cp_parser_parse_definitely (parser))
7042    {
7043     tree declarator;
7044
7045     /* Parse another optional declarator.  */
7046     declarator = cp_parser_conversion_declarator_opt (parser);
7047
7048     /* Create the representation of the declarator.  */
7049     if (code == INDIRECT_REF)
7050       declarator = make_pointer_declarator (cv_qualifier_seq,
7051					     declarator);
7052     else
7053       declarator =  make_reference_declarator (cv_qualifier_seq,
7054						declarator);
7055
7056     /* Handle the pointer-to-member case.  */
7057     if (class_type)
7058       declarator = build_nt (SCOPE_REF, class_type, declarator);
7059
7060     return declarator;
7061   }
7062
7063  return NULL_TREE;
7064}
7065
7066/* Parse an (optional) ctor-initializer.
7067
7068   ctor-initializer:
7069     : mem-initializer-list
7070
7071   Returns TRUE iff the ctor-initializer was actually present.  */
7072
7073static bool
7074cp_parser_ctor_initializer_opt (cp_parser* parser)
7075{
7076  /* If the next token is not a `:', then there is no
7077     ctor-initializer.  */
7078  if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7079    {
7080      /* Do default initialization of any bases and members.  */
7081      if (DECL_CONSTRUCTOR_P (current_function_decl))
7082	finish_mem_initializers (NULL_TREE);
7083
7084      return false;
7085    }
7086
7087  /* Consume the `:' token.  */
7088  cp_lexer_consume_token (parser->lexer);
7089  /* And the mem-initializer-list.  */
7090  cp_parser_mem_initializer_list (parser);
7091
7092  return true;
7093}
7094
7095/* Parse a mem-initializer-list.
7096
7097   mem-initializer-list:
7098     mem-initializer
7099     mem-initializer , mem-initializer-list  */
7100
7101static void
7102cp_parser_mem_initializer_list (cp_parser* parser)
7103{
7104  tree mem_initializer_list = NULL_TREE;
7105
7106  /* Let the semantic analysis code know that we are starting the
7107     mem-initializer-list.  */
7108  if (!DECL_CONSTRUCTOR_P (current_function_decl))
7109    error ("only constructors take base initializers");
7110
7111  /* Loop through the list.  */
7112  while (true)
7113    {
7114      tree mem_initializer;
7115
7116      /* Parse the mem-initializer.  */
7117      mem_initializer = cp_parser_mem_initializer (parser);
7118      /* Add it to the list, unless it was erroneous.  */
7119      if (mem_initializer)
7120	{
7121	  TREE_CHAIN (mem_initializer) = mem_initializer_list;
7122	  mem_initializer_list = mem_initializer;
7123	}
7124      /* If the next token is not a `,', we're done.  */
7125      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7126	break;
7127      /* Consume the `,' token.  */
7128      cp_lexer_consume_token (parser->lexer);
7129    }
7130
7131  /* Perform semantic analysis.  */
7132  if (DECL_CONSTRUCTOR_P (current_function_decl))
7133    finish_mem_initializers (mem_initializer_list);
7134}
7135
7136/* Parse a mem-initializer.
7137
7138   mem-initializer:
7139     mem-initializer-id ( expression-list [opt] )
7140
7141   GNU extension:
7142
7143   mem-initializer:
7144     ( expression-list [opt] )
7145
7146   Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7147   class) or FIELD_DECL (for a non-static data member) to initialize;
7148   the TREE_VALUE is the expression-list.  */
7149
7150static tree
7151cp_parser_mem_initializer (cp_parser* parser)
7152{
7153  tree mem_initializer_id;
7154  tree expression_list;
7155  tree member;
7156
7157  /* Find out what is being initialized.  */
7158  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7159    {
7160      pedwarn ("anachronistic old-style base class initializer");
7161      mem_initializer_id = NULL_TREE;
7162    }
7163  else
7164    mem_initializer_id = cp_parser_mem_initializer_id (parser);
7165  member = expand_member_init (mem_initializer_id);
7166  if (member && !DECL_P (member))
7167    in_base_initializer = 1;
7168
7169  expression_list
7170    = cp_parser_parenthesized_expression_list (parser, false,
7171					       /*non_constant_p=*/NULL);
7172  if (!expression_list)
7173    expression_list = void_type_node;
7174
7175  in_base_initializer = 0;
7176
7177  return member ? build_tree_list (member, expression_list) : NULL_TREE;
7178}
7179
7180/* Parse a mem-initializer-id.
7181
7182   mem-initializer-id:
7183     :: [opt] nested-name-specifier [opt] class-name
7184     identifier
7185
7186   Returns a TYPE indicating the class to be initializer for the first
7187   production.  Returns an IDENTIFIER_NODE indicating the data member
7188   to be initialized for the second production.  */
7189
7190static tree
7191cp_parser_mem_initializer_id (cp_parser* parser)
7192{
7193  bool global_scope_p;
7194  bool nested_name_specifier_p;
7195  bool template_p = false;
7196  tree id;
7197
7198  /* `typename' is not allowed in this context ([temp.res]).  */
7199  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7200    {
7201      error ("keyword `typename' not allowed in this context (a qualified "
7202	     "member initializer is implicitly a type)");
7203      cp_lexer_consume_token (parser->lexer);
7204    }
7205  /* Look for the optional `::' operator.  */
7206  global_scope_p
7207    = (cp_parser_global_scope_opt (parser,
7208				   /*current_scope_valid_p=*/false)
7209       != NULL_TREE);
7210  /* Look for the optional nested-name-specifier.  The simplest way to
7211     implement:
7212
7213       [temp.res]
7214
7215       The keyword `typename' is not permitted in a base-specifier or
7216       mem-initializer; in these contexts a qualified name that
7217       depends on a template-parameter is implicitly assumed to be a
7218       type name.
7219
7220     is to assume that we have seen the `typename' keyword at this
7221     point.  */
7222  nested_name_specifier_p
7223    = (cp_parser_nested_name_specifier_opt (parser,
7224					    /*typename_keyword_p=*/true,
7225					    /*check_dependency_p=*/true,
7226					    /*type_p=*/true,
7227					    /*is_declaration=*/true)
7228       != NULL_TREE);
7229  if (nested_name_specifier_p)
7230    template_p = cp_parser_optional_template_keyword (parser);
7231  /* If there is a `::' operator or a nested-name-specifier, then we
7232     are definitely looking for a class-name.  */
7233  if (global_scope_p || nested_name_specifier_p)
7234    return cp_parser_class_name (parser,
7235				 /*typename_keyword_p=*/true,
7236				 /*template_keyword_p=*/template_p,
7237				 /*type_p=*/false,
7238				 /*check_dependency_p=*/true,
7239				 /*class_head_p=*/false,
7240				 /*is_declaration=*/true);
7241  /* Otherwise, we could also be looking for an ordinary identifier.  */
7242  cp_parser_parse_tentatively (parser);
7243  /* Try a class-name.  */
7244  id = cp_parser_class_name (parser,
7245			     /*typename_keyword_p=*/true,
7246			     /*template_keyword_p=*/false,
7247			     /*type_p=*/false,
7248			     /*check_dependency_p=*/true,
7249			     /*class_head_p=*/false,
7250			     /*is_declaration=*/true);
7251  /* If we found one, we're done.  */
7252  if (cp_parser_parse_definitely (parser))
7253    return id;
7254  /* Otherwise, look for an ordinary identifier.  */
7255  return cp_parser_identifier (parser);
7256}
7257
7258/* Overloading [gram.over] */
7259
7260/* Parse an operator-function-id.
7261
7262   operator-function-id:
7263     operator operator
7264
7265   Returns an IDENTIFIER_NODE for the operator which is a
7266   human-readable spelling of the identifier, e.g., `operator +'.  */
7267
7268static tree
7269cp_parser_operator_function_id (cp_parser* parser)
7270{
7271  /* Look for the `operator' keyword.  */
7272  if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7273    return error_mark_node;
7274  /* And then the name of the operator itself.  */
7275  return cp_parser_operator (parser);
7276}
7277
7278/* Parse an operator.
7279
7280   operator:
7281     new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7282     += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7283     || ++ -- , ->* -> () []
7284
7285   GNU Extensions:
7286
7287   operator:
7288     <? >? <?= >?=
7289
7290   Returns an IDENTIFIER_NODE for the operator which is a
7291   human-readable spelling of the identifier, e.g., `operator +'.  */
7292
7293static tree
7294cp_parser_operator (cp_parser* parser)
7295{
7296  tree id = NULL_TREE;
7297  cp_token *token;
7298
7299  /* Peek at the next token.  */
7300  token = cp_lexer_peek_token (parser->lexer);
7301  /* Figure out which operator we have.  */
7302  switch (token->type)
7303    {
7304    case CPP_KEYWORD:
7305      {
7306	enum tree_code op;
7307
7308	/* The keyword should be either `new' or `delete'.  */
7309	if (token->keyword == RID_NEW)
7310	  op = NEW_EXPR;
7311	else if (token->keyword == RID_DELETE)
7312	  op = DELETE_EXPR;
7313	else
7314	  break;
7315
7316	/* Consume the `new' or `delete' token.  */
7317	cp_lexer_consume_token (parser->lexer);
7318
7319	/* Peek at the next token.  */
7320	token = cp_lexer_peek_token (parser->lexer);
7321	/* If it's a `[' token then this is the array variant of the
7322	   operator.  */
7323	if (token->type == CPP_OPEN_SQUARE)
7324	  {
7325	    /* Consume the `[' token.  */
7326	    cp_lexer_consume_token (parser->lexer);
7327	    /* Look for the `]' token.  */
7328	    cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7329	    id = ansi_opname (op == NEW_EXPR
7330			      ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7331	  }
7332	/* Otherwise, we have the non-array variant.  */
7333	else
7334	  id = ansi_opname (op);
7335
7336	return id;
7337      }
7338
7339    case CPP_PLUS:
7340      id = ansi_opname (PLUS_EXPR);
7341      break;
7342
7343    case CPP_MINUS:
7344      id = ansi_opname (MINUS_EXPR);
7345      break;
7346
7347    case CPP_MULT:
7348      id = ansi_opname (MULT_EXPR);
7349      break;
7350
7351    case CPP_DIV:
7352      id = ansi_opname (TRUNC_DIV_EXPR);
7353      break;
7354
7355    case CPP_MOD:
7356      id = ansi_opname (TRUNC_MOD_EXPR);
7357      break;
7358
7359    case CPP_XOR:
7360      id = ansi_opname (BIT_XOR_EXPR);
7361      break;
7362
7363    case CPP_AND:
7364      id = ansi_opname (BIT_AND_EXPR);
7365      break;
7366
7367    case CPP_OR:
7368      id = ansi_opname (BIT_IOR_EXPR);
7369      break;
7370
7371    case CPP_COMPL:
7372      id = ansi_opname (BIT_NOT_EXPR);
7373      break;
7374
7375    case CPP_NOT:
7376      id = ansi_opname (TRUTH_NOT_EXPR);
7377      break;
7378
7379    case CPP_EQ:
7380      id = ansi_assopname (NOP_EXPR);
7381      break;
7382
7383    case CPP_LESS:
7384      id = ansi_opname (LT_EXPR);
7385      break;
7386
7387    case CPP_GREATER:
7388      id = ansi_opname (GT_EXPR);
7389      break;
7390
7391    case CPP_PLUS_EQ:
7392      id = ansi_assopname (PLUS_EXPR);
7393      break;
7394
7395    case CPP_MINUS_EQ:
7396      id = ansi_assopname (MINUS_EXPR);
7397      break;
7398
7399    case CPP_MULT_EQ:
7400      id = ansi_assopname (MULT_EXPR);
7401      break;
7402
7403    case CPP_DIV_EQ:
7404      id = ansi_assopname (TRUNC_DIV_EXPR);
7405      break;
7406
7407    case CPP_MOD_EQ:
7408      id = ansi_assopname (TRUNC_MOD_EXPR);
7409      break;
7410
7411    case CPP_XOR_EQ:
7412      id = ansi_assopname (BIT_XOR_EXPR);
7413      break;
7414
7415    case CPP_AND_EQ:
7416      id = ansi_assopname (BIT_AND_EXPR);
7417      break;
7418
7419    case CPP_OR_EQ:
7420      id = ansi_assopname (BIT_IOR_EXPR);
7421      break;
7422
7423    case CPP_LSHIFT:
7424      id = ansi_opname (LSHIFT_EXPR);
7425      break;
7426
7427    case CPP_RSHIFT:
7428      id = ansi_opname (RSHIFT_EXPR);
7429      break;
7430
7431    case CPP_LSHIFT_EQ:
7432      id = ansi_assopname (LSHIFT_EXPR);
7433      break;
7434
7435    case CPP_RSHIFT_EQ:
7436      id = ansi_assopname (RSHIFT_EXPR);
7437      break;
7438
7439    case CPP_EQ_EQ:
7440      id = ansi_opname (EQ_EXPR);
7441      break;
7442
7443    case CPP_NOT_EQ:
7444      id = ansi_opname (NE_EXPR);
7445      break;
7446
7447    case CPP_LESS_EQ:
7448      id = ansi_opname (LE_EXPR);
7449      break;
7450
7451    case CPP_GREATER_EQ:
7452      id = ansi_opname (GE_EXPR);
7453      break;
7454
7455    case CPP_AND_AND:
7456      id = ansi_opname (TRUTH_ANDIF_EXPR);
7457      break;
7458
7459    case CPP_OR_OR:
7460      id = ansi_opname (TRUTH_ORIF_EXPR);
7461      break;
7462
7463    case CPP_PLUS_PLUS:
7464      id = ansi_opname (POSTINCREMENT_EXPR);
7465      break;
7466
7467    case CPP_MINUS_MINUS:
7468      id = ansi_opname (PREDECREMENT_EXPR);
7469      break;
7470
7471    case CPP_COMMA:
7472      id = ansi_opname (COMPOUND_EXPR);
7473      break;
7474
7475    case CPP_DEREF_STAR:
7476      id = ansi_opname (MEMBER_REF);
7477      break;
7478
7479    case CPP_DEREF:
7480      id = ansi_opname (COMPONENT_REF);
7481      break;
7482
7483    case CPP_OPEN_PAREN:
7484      /* Consume the `('.  */
7485      cp_lexer_consume_token (parser->lexer);
7486      /* Look for the matching `)'.  */
7487      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7488      return ansi_opname (CALL_EXPR);
7489
7490    case CPP_OPEN_SQUARE:
7491      /* Consume the `['.  */
7492      cp_lexer_consume_token (parser->lexer);
7493      /* Look for the matching `]'.  */
7494      cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7495      return ansi_opname (ARRAY_REF);
7496
7497      /* Extensions.  */
7498    case CPP_MIN:
7499      id = ansi_opname (MIN_EXPR);
7500      break;
7501
7502    case CPP_MAX:
7503      id = ansi_opname (MAX_EXPR);
7504      break;
7505
7506    case CPP_MIN_EQ:
7507      id = ansi_assopname (MIN_EXPR);
7508      break;
7509
7510    case CPP_MAX_EQ:
7511      id = ansi_assopname (MAX_EXPR);
7512      break;
7513
7514    default:
7515      /* Anything else is an error.  */
7516      break;
7517    }
7518
7519  /* If we have selected an identifier, we need to consume the
7520     operator token.  */
7521  if (id)
7522    cp_lexer_consume_token (parser->lexer);
7523  /* Otherwise, no valid operator name was present.  */
7524  else
7525    {
7526      cp_parser_error (parser, "expected operator");
7527      id = error_mark_node;
7528    }
7529
7530  return id;
7531}
7532
7533/* Parse a template-declaration.
7534
7535   template-declaration:
7536     export [opt] template < template-parameter-list > declaration
7537
7538   If MEMBER_P is TRUE, this template-declaration occurs within a
7539   class-specifier.
7540
7541   The grammar rule given by the standard isn't correct.  What
7542   is really meant is:
7543
7544   template-declaration:
7545     export [opt] template-parameter-list-seq
7546       decl-specifier-seq [opt] init-declarator [opt] ;
7547     export [opt] template-parameter-list-seq
7548       function-definition
7549
7550   template-parameter-list-seq:
7551     template-parameter-list-seq [opt]
7552     template < template-parameter-list >  */
7553
7554static void
7555cp_parser_template_declaration (cp_parser* parser, bool member_p)
7556{
7557  /* Check for `export'.  */
7558  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7559    {
7560      /* Consume the `export' token.  */
7561      cp_lexer_consume_token (parser->lexer);
7562      /* Warn that we do not support `export'.  */
7563      warning ("keyword `export' not implemented, and will be ignored");
7564    }
7565
7566  cp_parser_template_declaration_after_export (parser, member_p);
7567}
7568
7569/* Parse a template-parameter-list.
7570
7571   template-parameter-list:
7572     template-parameter
7573     template-parameter-list , template-parameter
7574
7575   Returns a TREE_LIST.  Each node represents a template parameter.
7576   The nodes are connected via their TREE_CHAINs.  */
7577
7578static tree
7579cp_parser_template_parameter_list (cp_parser* parser)
7580{
7581  tree parameter_list = NULL_TREE;
7582
7583  while (true)
7584    {
7585      tree parameter;
7586      cp_token *token;
7587
7588      /* Parse the template-parameter.  */
7589      parameter = cp_parser_template_parameter (parser);
7590      /* Add it to the list.  */
7591      parameter_list = process_template_parm (parameter_list,
7592					      parameter);
7593
7594      /* Peek at the next token.  */
7595      token = cp_lexer_peek_token (parser->lexer);
7596      /* If it's not a `,', we're done.  */
7597      if (token->type != CPP_COMMA)
7598	break;
7599      /* Otherwise, consume the `,' token.  */
7600      cp_lexer_consume_token (parser->lexer);
7601    }
7602
7603  return parameter_list;
7604}
7605
7606/* Parse a template-parameter.
7607
7608   template-parameter:
7609     type-parameter
7610     parameter-declaration
7611
7612   Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
7613   TREE_PURPOSE is the default value, if any.  */
7614
7615static tree
7616cp_parser_template_parameter (cp_parser* parser)
7617{
7618  cp_token *token;
7619
7620  /* Peek at the next token.  */
7621  token = cp_lexer_peek_token (parser->lexer);
7622  /* If it is `class' or `template', we have a type-parameter.  */
7623  if (token->keyword == RID_TEMPLATE)
7624    return cp_parser_type_parameter (parser);
7625  /* If it is `class' or `typename' we do not know yet whether it is a
7626     type parameter or a non-type parameter.  Consider:
7627
7628       template <typename T, typename T::X X> ...
7629
7630     or:
7631
7632       template <class C, class D*> ...
7633
7634     Here, the first parameter is a type parameter, and the second is
7635     a non-type parameter.  We can tell by looking at the token after
7636     the identifier -- if it is a `,', `=', or `>' then we have a type
7637     parameter.  */
7638  if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7639    {
7640      /* Peek at the token after `class' or `typename'.  */
7641      token = cp_lexer_peek_nth_token (parser->lexer, 2);
7642      /* If it's an identifier, skip it.  */
7643      if (token->type == CPP_NAME)
7644	token = cp_lexer_peek_nth_token (parser->lexer, 3);
7645      /* Now, see if the token looks like the end of a template
7646	 parameter.  */
7647      if (token->type == CPP_COMMA
7648	  || token->type == CPP_EQ
7649	  || token->type == CPP_GREATER)
7650	return cp_parser_type_parameter (parser);
7651    }
7652
7653  /* Otherwise, it is a non-type parameter.
7654
7655     [temp.param]
7656
7657     When parsing a default template-argument for a non-type
7658     template-parameter, the first non-nested `>' is taken as the end
7659     of the template parameter-list rather than a greater-than
7660     operator.  */
7661  return
7662    cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7663				     /*parenthesized_p=*/NULL);
7664}
7665
7666/* Parse a type-parameter.
7667
7668   type-parameter:
7669     class identifier [opt]
7670     class identifier [opt] = type-id
7671     typename identifier [opt]
7672     typename identifier [opt] = type-id
7673     template < template-parameter-list > class identifier [opt]
7674     template < template-parameter-list > class identifier [opt]
7675       = id-expression
7676
7677   Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
7678   TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
7679   the declaration of the parameter.  */
7680
7681static tree
7682cp_parser_type_parameter (cp_parser* parser)
7683{
7684  cp_token *token;
7685  tree parameter;
7686
7687  /* Look for a keyword to tell us what kind of parameter this is.  */
7688  token = cp_parser_require (parser, CPP_KEYWORD,
7689			     "`class', `typename', or `template'");
7690  if (!token)
7691    return error_mark_node;
7692
7693  switch (token->keyword)
7694    {
7695    case RID_CLASS:
7696    case RID_TYPENAME:
7697      {
7698	tree identifier;
7699	tree default_argument;
7700
7701	/* If the next token is an identifier, then it names the
7702           parameter.  */
7703	if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7704	  identifier = cp_parser_identifier (parser);
7705	else
7706	  identifier = NULL_TREE;
7707
7708	/* Create the parameter.  */
7709	parameter = finish_template_type_parm (class_type_node, identifier);
7710
7711	/* If the next token is an `=', we have a default argument.  */
7712	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7713	  {
7714	    /* Consume the `=' token.  */
7715	    cp_lexer_consume_token (parser->lexer);
7716	    /* Parse the default-argument.  */
7717	    default_argument = cp_parser_type_id (parser);
7718	  }
7719	else
7720	  default_argument = NULL_TREE;
7721
7722	/* Create the combined representation of the parameter and the
7723	   default argument.  */
7724	parameter = build_tree_list (default_argument, parameter);
7725      }
7726      break;
7727
7728    case RID_TEMPLATE:
7729      {
7730	tree parameter_list;
7731	tree identifier;
7732	tree default_argument;
7733
7734	/* Look for the `<'.  */
7735	cp_parser_require (parser, CPP_LESS, "`<'");
7736	/* Parse the template-parameter-list.  */
7737	begin_template_parm_list ();
7738	parameter_list
7739	  = cp_parser_template_parameter_list (parser);
7740	parameter_list = end_template_parm_list (parameter_list);
7741	/* Look for the `>'.  */
7742	cp_parser_require (parser, CPP_GREATER, "`>'");
7743	/* Look for the `class' keyword.  */
7744	cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7745	/* If the next token is an `=', then there is a
7746	   default-argument.  If the next token is a `>', we are at
7747	   the end of the parameter-list.  If the next token is a `,',
7748	   then we are at the end of this parameter.  */
7749	if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7750	    && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7751	    && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7752	  identifier = cp_parser_identifier (parser);
7753	else
7754	  identifier = NULL_TREE;
7755	/* Create the template parameter.  */
7756	parameter = finish_template_template_parm (class_type_node,
7757						   identifier);
7758
7759	/* If the next token is an `=', then there is a
7760	   default-argument.  */
7761	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7762	  {
7763	    bool is_template;
7764
7765	    /* Consume the `='.  */
7766	    cp_lexer_consume_token (parser->lexer);
7767	    /* Parse the id-expression.  */
7768	    default_argument
7769	      = cp_parser_id_expression (parser,
7770					 /*template_keyword_p=*/false,
7771					 /*check_dependency_p=*/true,
7772					 /*template_p=*/&is_template,
7773					 /*declarator_p=*/false);
7774	    if (TREE_CODE (default_argument) == TYPE_DECL)
7775	      /* If the id-expression was a template-id that refers to
7776		 a template-class, we already have the declaration here,
7777		 so no further lookup is needed.  */
7778		 ;
7779	    else
7780	      /* Look up the name.  */
7781	      default_argument
7782		= cp_parser_lookup_name (parser, default_argument,
7783					/*is_type=*/false,
7784					/*is_template=*/is_template,
7785					/*is_namespace=*/false,
7786					/*check_dependency=*/true);
7787	    /* See if the default argument is valid.  */
7788	    default_argument
7789	      = check_template_template_default_arg (default_argument);
7790	  }
7791	else
7792	  default_argument = NULL_TREE;
7793
7794	/* Create the combined representation of the parameter and the
7795	   default argument.  */
7796	parameter =  build_tree_list (default_argument, parameter);
7797      }
7798      break;
7799
7800    default:
7801      /* Anything else is an error.  */
7802      cp_parser_error (parser,
7803		       "expected `class', `typename', or `template'");
7804      parameter = error_mark_node;
7805    }
7806
7807  return parameter;
7808}
7809
7810/* Parse a template-id.
7811
7812   template-id:
7813     template-name < template-argument-list [opt] >
7814
7815   If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7816   `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
7817   returned.  Otherwise, if the template-name names a function, or set
7818   of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
7819   names a class, returns a TYPE_DECL for the specialization.
7820
7821   If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7822   uninstantiated templates.  */
7823
7824static tree
7825cp_parser_template_id (cp_parser *parser,
7826		       bool template_keyword_p,
7827		       bool check_dependency_p,
7828		       bool is_declaration)
7829{
7830  tree template;
7831  tree arguments;
7832  tree template_id;
7833  ptrdiff_t start_of_id;
7834  tree access_check = NULL_TREE;
7835  cp_token *next_token, *next_token_2;
7836  bool is_identifier;
7837
7838  /* If the next token corresponds to a template-id, there is no need
7839     to reparse it.  */
7840  next_token = cp_lexer_peek_token (parser->lexer);
7841  if (next_token->type == CPP_TEMPLATE_ID)
7842    {
7843      tree value;
7844      tree check;
7845
7846      /* Get the stored value.  */
7847      value = cp_lexer_consume_token (parser->lexer)->value;
7848      /* Perform any access checks that were deferred.  */
7849      for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7850	perform_or_defer_access_check (TREE_PURPOSE (check),
7851				       TREE_VALUE (check));
7852      /* Return the stored value.  */
7853      return TREE_VALUE (value);
7854    }
7855
7856  /* Avoid performing name lookup if there is no possibility of
7857     finding a template-id.  */
7858  if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7859      || (next_token->type == CPP_NAME
7860	  && !cp_parser_nth_token_starts_template_argument_list_p
7861	       (parser, 2)))
7862    {
7863      cp_parser_error (parser, "expected template-id");
7864      return error_mark_node;
7865    }
7866
7867  /* Remember where the template-id starts.  */
7868  if (cp_parser_parsing_tentatively (parser)
7869      && !cp_parser_committed_to_tentative_parse (parser))
7870    {
7871      next_token = cp_lexer_peek_token (parser->lexer);
7872      start_of_id = cp_lexer_token_difference (parser->lexer,
7873					       parser->lexer->first_token,
7874					       next_token);
7875    }
7876  else
7877    start_of_id = -1;
7878
7879  push_deferring_access_checks (dk_deferred);
7880
7881  /* Parse the template-name.  */
7882  is_identifier = false;
7883  template = cp_parser_template_name (parser, template_keyword_p,
7884				      check_dependency_p,
7885				      is_declaration,
7886				      &is_identifier);
7887  if (template == error_mark_node || is_identifier)
7888    {
7889      pop_deferring_access_checks ();
7890      return template;
7891    }
7892
7893  /* If we find the sequence `[:' after a template-name, it's probably
7894     a digraph-typo for `< ::'. Substitute the tokens and check if we can
7895     parse correctly the argument list.  */
7896  next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7897  next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7898  if (next_token->type == CPP_OPEN_SQUARE
7899      && next_token->flags & DIGRAPH
7900      && next_token_2->type == CPP_COLON
7901      && !(next_token_2->flags & PREV_WHITE))
7902    {
7903      cp_parser_parse_tentatively (parser);
7904      /* Change `:' into `::'.  */
7905      next_token_2->type = CPP_SCOPE;
7906      /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7907         CPP_LESS.  */
7908      cp_lexer_consume_token (parser->lexer);
7909      /* Parse the arguments.  */
7910      arguments = cp_parser_enclosed_template_argument_list (parser);
7911      if (!cp_parser_parse_definitely (parser))
7912	{
7913	  /* If we couldn't parse an argument list, then we revert our changes
7914	     and return simply an error. Maybe this is not a template-id
7915	     after all.  */
7916	  next_token_2->type = CPP_COLON;
7917	  cp_parser_error (parser, "expected `<'");
7918	  pop_deferring_access_checks ();
7919	  return error_mark_node;
7920	}
7921      /* Otherwise, emit an error about the invalid digraph, but continue
7922         parsing because we got our argument list.  */
7923      pedwarn ("`<::' cannot begin a template-argument list");
7924      inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7925	      "between `<' and `::'");
7926      if (!flag_permissive)
7927	{
7928	  static bool hint;
7929	  if (!hint)
7930	    {
7931	      inform ("(if you use `-fpermissive' G++ will accept your code)");
7932	      hint = true;
7933	    }
7934	}
7935    }
7936  else
7937    {
7938      /* Look for the `<' that starts the template-argument-list.  */
7939      if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7940	{
7941	  pop_deferring_access_checks ();
7942	  return error_mark_node;
7943	}
7944      /* Parse the arguments.  */
7945      arguments = cp_parser_enclosed_template_argument_list (parser);
7946    }
7947
7948  /* Build a representation of the specialization.  */
7949  if (TREE_CODE (template) == IDENTIFIER_NODE)
7950    template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7951  else if (DECL_CLASS_TEMPLATE_P (template)
7952	   || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7953    template_id
7954      = finish_template_type (template, arguments,
7955			      cp_lexer_next_token_is (parser->lexer,
7956						      CPP_SCOPE));
7957  else
7958    {
7959      /* If it's not a class-template or a template-template, it should be
7960	 a function-template.  */
7961      my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7962			   || TREE_CODE (template) == OVERLOAD
7963			   || BASELINK_P (template)),
7964			  20010716);
7965
7966      template_id = lookup_template_function (template, arguments);
7967    }
7968
7969  /* Retrieve any deferred checks.  Do not pop this access checks yet
7970     so the memory will not be reclaimed during token replacing below.  */
7971  access_check = get_deferred_access_checks ();
7972
7973  /* If parsing tentatively, replace the sequence of tokens that makes
7974     up the template-id with a CPP_TEMPLATE_ID token.  That way,
7975     should we re-parse the token stream, we will not have to repeat
7976     the effort required to do the parse, nor will we issue duplicate
7977     error messages about problems during instantiation of the
7978     template.  */
7979  if (start_of_id >= 0)
7980    {
7981      cp_token *token;
7982
7983      /* Find the token that corresponds to the start of the
7984	 template-id.  */
7985      token = cp_lexer_advance_token (parser->lexer,
7986				      parser->lexer->first_token,
7987				      start_of_id);
7988
7989      /* Reset the contents of the START_OF_ID token.  */
7990      token->type = CPP_TEMPLATE_ID;
7991      token->value = build_tree_list (access_check, template_id);
7992      token->keyword = RID_MAX;
7993      /* Purge all subsequent tokens.  */
7994      cp_lexer_purge_tokens_after (parser->lexer, token);
7995    }
7996
7997  pop_deferring_access_checks ();
7998  return template_id;
7999}
8000
8001/* Parse a template-name.
8002
8003   template-name:
8004     identifier
8005
8006   The standard should actually say:
8007
8008   template-name:
8009     identifier
8010     operator-function-id
8011
8012   A defect report has been filed about this issue.
8013
8014   A conversion-function-id cannot be a template name because they cannot
8015   be part of a template-id. In fact, looking at this code:
8016
8017   a.operator K<int>()
8018
8019   the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8020   It is impossible to call a templated conversion-function-id with an
8021   explicit argument list, since the only allowed template parameter is
8022   the type to which it is converting.
8023
8024   If TEMPLATE_KEYWORD_P is true, then we have just seen the
8025   `template' keyword, in a construction like:
8026
8027     T::template f<3>()
8028
8029   In that case `f' is taken to be a template-name, even though there
8030   is no way of knowing for sure.
8031
8032   Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8033   name refers to a set of overloaded functions, at least one of which
8034   is a template, or an IDENTIFIER_NODE with the name of the template,
8035   if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8036   names are looked up inside uninstantiated templates.  */
8037
8038static tree
8039cp_parser_template_name (cp_parser* parser,
8040                         bool template_keyword_p,
8041                         bool check_dependency_p,
8042			 bool is_declaration,
8043			 bool *is_identifier)
8044{
8045  tree identifier;
8046  tree decl;
8047  tree fns;
8048
8049  /* If the next token is `operator', then we have either an
8050     operator-function-id or a conversion-function-id.  */
8051  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8052    {
8053      /* We don't know whether we're looking at an
8054	 operator-function-id or a conversion-function-id.  */
8055      cp_parser_parse_tentatively (parser);
8056      /* Try an operator-function-id.  */
8057      identifier = cp_parser_operator_function_id (parser);
8058      /* If that didn't work, try a conversion-function-id.  */
8059      if (!cp_parser_parse_definitely (parser))
8060        {
8061	  cp_parser_error (parser, "expected template-name");
8062	  return error_mark_node;
8063        }
8064    }
8065  /* Look for the identifier.  */
8066  else
8067    identifier = cp_parser_identifier (parser);
8068
8069  /* If we didn't find an identifier, we don't have a template-id.  */
8070  if (identifier == error_mark_node)
8071    return error_mark_node;
8072
8073  /* If the name immediately followed the `template' keyword, then it
8074     is a template-name.  However, if the next token is not `<', then
8075     we do not treat it as a template-name, since it is not being used
8076     as part of a template-id.  This enables us to handle constructs
8077     like:
8078
8079       template <typename T> struct S { S(); };
8080       template <typename T> S<T>::S();
8081
8082     correctly.  We would treat `S' as a template -- if it were `S<T>'
8083     -- but we do not if there is no `<'.  */
8084
8085  if (processing_template_decl
8086      && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8087    {
8088      /* In a declaration, in a dependent context, we pretend that the
8089	 "template" keyword was present in order to improve error
8090	 recovery.  For example, given:
8091
8092	   template <typename T> void f(T::X<int>);
8093
8094	 we want to treat "X<int>" as a template-id.  */
8095      if (is_declaration
8096	  && !template_keyword_p
8097	  && parser->scope && TYPE_P (parser->scope)
8098	  && dependent_type_p (parser->scope)
8099	  /* Do not do this for dtors (or ctors), since they never
8100	     need the template keyword before their name.  */
8101	  && !constructor_name_p (identifier, parser->scope))
8102	{
8103	  ptrdiff_t start;
8104	  cp_token* token;
8105	  /* Explain what went wrong.  */
8106	  error ("non-template `%D' used as template", identifier);
8107	  inform ("use `%T::template %D' to indicate that it is a template",
8108		  parser->scope, identifier);
8109	  /* If parsing tentatively, find the location of the "<"
8110	     token.  */
8111	  if (cp_parser_parsing_tentatively (parser)
8112	      && !cp_parser_committed_to_tentative_parse (parser))
8113	    {
8114	      cp_parser_simulate_error (parser);
8115	      token = cp_lexer_peek_token (parser->lexer);
8116	      token = cp_lexer_prev_token (parser->lexer, token);
8117	      start = cp_lexer_token_difference (parser->lexer,
8118						 parser->lexer->first_token,
8119						 token);
8120	    }
8121	  else
8122	    start = -1;
8123	  /* Parse the template arguments so that we can issue error
8124	     messages about them.  */
8125	  cp_lexer_consume_token (parser->lexer);
8126	  cp_parser_enclosed_template_argument_list (parser);
8127	  /* Skip tokens until we find a good place from which to
8128	     continue parsing.  */
8129	  cp_parser_skip_to_closing_parenthesis (parser,
8130						 /*recovering=*/true,
8131						 /*or_comma=*/true,
8132						 /*consume_paren=*/false);
8133	  /* If parsing tentatively, permanently remove the
8134	     template argument list.  That will prevent duplicate
8135	     error messages from being issued about the missing
8136	     "template" keyword.  */
8137	  if (start >= 0)
8138	    {
8139	      token = cp_lexer_advance_token (parser->lexer,
8140					      parser->lexer->first_token,
8141					      start);
8142	      cp_lexer_purge_tokens_after (parser->lexer, token);
8143	    }
8144	  if (is_identifier)
8145	    *is_identifier = true;
8146	  return identifier;
8147	}
8148
8149      /* If the "template" keyword is present, then there is generally
8150	 no point in doing name-lookup, so we just return IDENTIFIER.
8151	 But, if the qualifying scope is non-dependent then we can
8152	 (and must) do name-lookup normally.  */
8153      if (template_keyword_p
8154	  && (!parser->scope
8155	      || (TYPE_P (parser->scope)
8156		  && dependent_type_p (parser->scope))))
8157	return identifier;
8158    }
8159
8160  /* Look up the name.  */
8161  decl = cp_parser_lookup_name (parser, identifier,
8162				/*is_type=*/false,
8163				/*is_template=*/false,
8164				/*is_namespace=*/false,
8165				check_dependency_p);
8166  decl = maybe_get_template_decl_from_type_decl (decl);
8167
8168  /* If DECL is a template, then the name was a template-name.  */
8169  if (TREE_CODE (decl) == TEMPLATE_DECL)
8170    ;
8171  else
8172    {
8173      /* The standard does not explicitly indicate whether a name that
8174	 names a set of overloaded declarations, some of which are
8175	 templates, is a template-name.  However, such a name should
8176	 be a template-name; otherwise, there is no way to form a
8177	 template-id for the overloaded templates.  */
8178      fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8179      if (TREE_CODE (fns) == OVERLOAD)
8180	{
8181	  tree fn;
8182
8183	  for (fn = fns; fn; fn = OVL_NEXT (fn))
8184	    if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8185	      break;
8186	}
8187      else
8188	{
8189	  /* Otherwise, the name does not name a template.  */
8190	  cp_parser_error (parser, "expected template-name");
8191	  return error_mark_node;
8192	}
8193    }
8194
8195  /* If DECL is dependent, and refers to a function, then just return
8196     its name; we will look it up again during template instantiation.  */
8197  if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8198    {
8199      tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8200      if (TYPE_P (scope) && dependent_type_p (scope))
8201	return identifier;
8202    }
8203
8204  return decl;
8205}
8206
8207/* Parse a template-argument-list.
8208
8209   template-argument-list:
8210     template-argument
8211     template-argument-list , template-argument
8212
8213   Returns a TREE_VEC containing the arguments.  */
8214
8215static tree
8216cp_parser_template_argument_list (cp_parser* parser)
8217{
8218  tree fixed_args[10];
8219  unsigned n_args = 0;
8220  unsigned alloced = 10;
8221  tree *arg_ary = fixed_args;
8222  tree vec;
8223  bool saved_in_template_argument_list_p;
8224
8225  saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8226  parser->in_template_argument_list_p = true;
8227  do
8228    {
8229      tree argument;
8230
8231      if (n_args)
8232	/* Consume the comma.  */
8233	cp_lexer_consume_token (parser->lexer);
8234
8235      /* Parse the template-argument.  */
8236      argument = cp_parser_template_argument (parser);
8237      if (n_args == alloced)
8238	{
8239	  alloced *= 2;
8240
8241	  if (arg_ary == fixed_args)
8242	    {
8243	      arg_ary = xmalloc (sizeof (tree) * alloced);
8244	      memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8245	    }
8246	  else
8247	    arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8248	}
8249      arg_ary[n_args++] = argument;
8250    }
8251  while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8252
8253  vec = make_tree_vec (n_args);
8254
8255  while (n_args--)
8256    TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8257
8258  if (arg_ary != fixed_args)
8259    free (arg_ary);
8260  parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8261  return vec;
8262}
8263
8264/* Parse a template-argument.
8265
8266   template-argument:
8267     assignment-expression
8268     type-id
8269     id-expression
8270
8271   The representation is that of an assignment-expression, type-id, or
8272   id-expression -- except that the qualified id-expression is
8273   evaluated, so that the value returned is either a DECL or an
8274   OVERLOAD.
8275
8276   Although the standard says "assignment-expression", it forbids
8277   throw-expressions or assignments in the template argument.
8278   Therefore, we use "conditional-expression" instead.  */
8279
8280static tree
8281cp_parser_template_argument (cp_parser* parser)
8282{
8283  tree argument;
8284  bool template_p;
8285  bool address_p;
8286  bool maybe_type_id = false;
8287  cp_token *token;
8288  cp_id_kind idk;
8289  tree qualifying_class;
8290
8291  /* There's really no way to know what we're looking at, so we just
8292     try each alternative in order.
8293
8294       [temp.arg]
8295
8296       In a template-argument, an ambiguity between a type-id and an
8297       expression is resolved to a type-id, regardless of the form of
8298       the corresponding template-parameter.
8299
8300     Therefore, we try a type-id first.  */
8301  cp_parser_parse_tentatively (parser);
8302  argument = cp_parser_type_id (parser);
8303  /* If there was no error parsing the type-id but the next token is a '>>',
8304     we probably found a typo for '> >'. But there are type-id which are
8305     also valid expressions. For instance:
8306
8307     struct X { int operator >> (int); };
8308     template <int V> struct Foo {};
8309     Foo<X () >> 5> r;
8310
8311     Here 'X()' is a valid type-id of a function type, but the user just
8312     wanted to write the expression "X() >> 5". Thus, we remember that we
8313     found a valid type-id, but we still try to parse the argument as an
8314     expression to see what happens.  */
8315  if (!cp_parser_error_occurred (parser)
8316      && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8317    {
8318      maybe_type_id = true;
8319      cp_parser_abort_tentative_parse (parser);
8320    }
8321  else
8322    {
8323      /* If the next token isn't a `,' or a `>', then this argument wasn't
8324      really finished. This means that the argument is not a valid
8325      type-id.  */
8326      if (!cp_parser_next_token_ends_template_argument_p (parser))
8327	cp_parser_error (parser, "expected template-argument");
8328      /* If that worked, we're done.  */
8329      if (cp_parser_parse_definitely (parser))
8330	return argument;
8331    }
8332  /* We're still not sure what the argument will be.  */
8333  cp_parser_parse_tentatively (parser);
8334  /* Try a template.  */
8335  argument = cp_parser_id_expression (parser,
8336				      /*template_keyword_p=*/false,
8337				      /*check_dependency_p=*/true,
8338				      &template_p,
8339				      /*declarator_p=*/false);
8340  /* If the next token isn't a `,' or a `>', then this argument wasn't
8341     really finished.  */
8342  if (!cp_parser_next_token_ends_template_argument_p (parser))
8343    cp_parser_error (parser, "expected template-argument");
8344  if (!cp_parser_error_occurred (parser))
8345    {
8346      /* Figure out what is being referred to.  If the id-expression
8347	 was for a class template specialization, then we will have a
8348	 TYPE_DECL at this point.  There is no need to do name lookup
8349	 at this point in that case.  */
8350      if (TREE_CODE (argument) != TYPE_DECL)
8351	argument = cp_parser_lookup_name (parser, argument,
8352					  /*is_type=*/false,
8353					  /*is_template=*/template_p,
8354					  /*is_namespace=*/false,
8355					  /*check_dependency=*/true);
8356      if (TREE_CODE (argument) != TEMPLATE_DECL
8357	  && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8358	cp_parser_error (parser, "expected template-name");
8359    }
8360  if (cp_parser_parse_definitely (parser))
8361    return argument;
8362  /* It must be a non-type argument.  There permitted cases are given
8363     in [temp.arg.nontype]:
8364
8365     -- an integral constant-expression of integral or enumeration
8366        type; or
8367
8368     -- the name of a non-type template-parameter; or
8369
8370     -- the name of an object or function with external linkage...
8371
8372     -- the address of an object or function with external linkage...
8373
8374     -- a pointer to member...  */
8375  /* Look for a non-type template parameter.  */
8376  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8377    {
8378      cp_parser_parse_tentatively (parser);
8379      argument = cp_parser_primary_expression (parser,
8380					       &idk,
8381					       &qualifying_class);
8382      if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8383	  || !cp_parser_next_token_ends_template_argument_p (parser))
8384	cp_parser_simulate_error (parser);
8385      if (cp_parser_parse_definitely (parser))
8386	return argument;
8387    }
8388  /* If the next token is "&", the argument must be the address of an
8389     object or function with external linkage.  */
8390  address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8391  if (address_p)
8392    cp_lexer_consume_token (parser->lexer);
8393  /* See if we might have an id-expression.  */
8394  token = cp_lexer_peek_token (parser->lexer);
8395  if (token->type == CPP_NAME
8396      || token->keyword == RID_OPERATOR
8397      || token->type == CPP_SCOPE
8398      || token->type == CPP_TEMPLATE_ID
8399      || token->type == CPP_NESTED_NAME_SPECIFIER)
8400    {
8401      cp_parser_parse_tentatively (parser);
8402      argument = cp_parser_primary_expression (parser,
8403					       &idk,
8404					       &qualifying_class);
8405      if (cp_parser_error_occurred (parser)
8406	  || !cp_parser_next_token_ends_template_argument_p (parser))
8407	cp_parser_abort_tentative_parse (parser);
8408      else
8409	{
8410	  if (qualifying_class)
8411	    argument = finish_qualified_id_expr (qualifying_class,
8412						 argument,
8413						 /*done=*/true,
8414						 address_p);
8415	  if (TREE_CODE (argument) == VAR_DECL)
8416	    {
8417	      /* A variable without external linkage might still be a
8418		 valid constant-expression, so no error is issued here
8419		 if the external-linkage check fails.  */
8420	      if (!DECL_EXTERNAL_LINKAGE_P (argument))
8421		cp_parser_simulate_error (parser);
8422	    }
8423	  else if (is_overloaded_fn (argument))
8424	    /* All overloaded functions are allowed; if the external
8425	       linkage test does not pass, an error will be issued
8426	       later.  */
8427	    ;
8428	  else if (address_p
8429		   && (TREE_CODE (argument) == OFFSET_REF
8430		       || TREE_CODE (argument) == SCOPE_REF))
8431	    /* A pointer-to-member.  */
8432	    ;
8433	  else
8434	    cp_parser_simulate_error (parser);
8435
8436	  if (cp_parser_parse_definitely (parser))
8437	    {
8438	      if (address_p)
8439		argument = build_x_unary_op (ADDR_EXPR, argument);
8440	      return argument;
8441	    }
8442	}
8443    }
8444  /* If the argument started with "&", there are no other valid
8445     alternatives at this point.  */
8446  if (address_p)
8447    {
8448      cp_parser_error (parser, "invalid non-type template argument");
8449      return error_mark_node;
8450    }
8451  /* If the argument wasn't successfully parsed as a type-id followed
8452     by '>>', the argument can only be a constant expression now.
8453     Otherwise, we try parsing the constant-expression tentatively,
8454     because the argument could really be a type-id.  */
8455  if (maybe_type_id)
8456    cp_parser_parse_tentatively (parser);
8457  argument = cp_parser_constant_expression (parser,
8458					    /*allow_non_constant_p=*/false,
8459					    /*non_constant_p=*/NULL);
8460  argument = fold_non_dependent_expr (argument);
8461  if (!maybe_type_id)
8462    return argument;
8463  if (!cp_parser_next_token_ends_template_argument_p (parser))
8464    cp_parser_error (parser, "expected template-argument");
8465  if (cp_parser_parse_definitely (parser))
8466    return argument;
8467  /* We did our best to parse the argument as a non type-id, but that
8468     was the only alternative that matched (albeit with a '>' after
8469     it). We can assume it's just a typo from the user, and a
8470     diagnostic will then be issued.  */
8471  return cp_parser_type_id (parser);
8472}
8473
8474/* Parse an explicit-instantiation.
8475
8476   explicit-instantiation:
8477     template declaration
8478
8479   Although the standard says `declaration', what it really means is:
8480
8481   explicit-instantiation:
8482     template decl-specifier-seq [opt] declarator [opt] ;
8483
8484   Things like `template int S<int>::i = 5, int S<double>::j;' are not
8485   supposed to be allowed.  A defect report has been filed about this
8486   issue.
8487
8488   GNU Extension:
8489
8490   explicit-instantiation:
8491     storage-class-specifier template
8492       decl-specifier-seq [opt] declarator [opt] ;
8493     function-specifier template
8494       decl-specifier-seq [opt] declarator [opt] ;  */
8495
8496static void
8497cp_parser_explicit_instantiation (cp_parser* parser)
8498{
8499  int declares_class_or_enum;
8500  tree decl_specifiers;
8501  tree attributes;
8502  tree extension_specifier = NULL_TREE;
8503
8504  /* Look for an (optional) storage-class-specifier or
8505     function-specifier.  */
8506  if (cp_parser_allow_gnu_extensions_p (parser))
8507    {
8508      extension_specifier
8509	= cp_parser_storage_class_specifier_opt (parser);
8510      if (!extension_specifier)
8511	extension_specifier = cp_parser_function_specifier_opt (parser);
8512    }
8513
8514  /* Look for the `template' keyword.  */
8515  cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8516  /* Let the front end know that we are processing an explicit
8517     instantiation.  */
8518  begin_explicit_instantiation ();
8519  /* [temp.explicit] says that we are supposed to ignore access
8520     control while processing explicit instantiation directives.  */
8521  push_deferring_access_checks (dk_no_check);
8522  /* Parse a decl-specifier-seq.  */
8523  decl_specifiers
8524    = cp_parser_decl_specifier_seq (parser,
8525				    CP_PARSER_FLAGS_OPTIONAL,
8526				    &attributes,
8527				    &declares_class_or_enum);
8528  /* If there was exactly one decl-specifier, and it declared a class,
8529     and there's no declarator, then we have an explicit type
8530     instantiation.  */
8531  if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8532    {
8533      tree type;
8534
8535      type = check_tag_decl (decl_specifiers);
8536      /* Turn access control back on for names used during
8537	 template instantiation.  */
8538      pop_deferring_access_checks ();
8539      if (type)
8540	do_type_instantiation (type, extension_specifier, /*complain=*/1);
8541    }
8542  else
8543    {
8544      tree declarator;
8545      tree decl;
8546
8547      /* Parse the declarator.  */
8548      declarator
8549	= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8550				/*ctor_dtor_or_conv_p=*/NULL,
8551				/*parenthesized_p=*/NULL);
8552      cp_parser_check_for_definition_in_return_type (declarator,
8553						     declares_class_or_enum);
8554      if (declarator != error_mark_node)
8555	{
8556	  decl = grokdeclarator (declarator, decl_specifiers,
8557				 NORMAL, 0, NULL);
8558	  /* Turn access control back on for names used during
8559	     template instantiation.  */
8560	  pop_deferring_access_checks ();
8561	  /* Do the explicit instantiation.  */
8562	  do_decl_instantiation (decl, extension_specifier);
8563	}
8564      else
8565	{
8566	  pop_deferring_access_checks ();
8567	  /* Skip the body of the explicit instantiation.  */
8568	  cp_parser_skip_to_end_of_statement (parser);
8569	}
8570    }
8571  /* We're done with the instantiation.  */
8572  end_explicit_instantiation ();
8573
8574  cp_parser_consume_semicolon_at_end_of_statement (parser);
8575}
8576
8577/* Parse an explicit-specialization.
8578
8579   explicit-specialization:
8580     template < > declaration
8581
8582   Although the standard says `declaration', what it really means is:
8583
8584   explicit-specialization:
8585     template <> decl-specifier [opt] init-declarator [opt] ;
8586     template <> function-definition
8587     template <> explicit-specialization
8588     template <> template-declaration  */
8589
8590static void
8591cp_parser_explicit_specialization (cp_parser* parser)
8592{
8593  /* Look for the `template' keyword.  */
8594  cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8595  /* Look for the `<'.  */
8596  cp_parser_require (parser, CPP_LESS, "`<'");
8597  /* Look for the `>'.  */
8598  cp_parser_require (parser, CPP_GREATER, "`>'");
8599  /* We have processed another parameter list.  */
8600  ++parser->num_template_parameter_lists;
8601  /* Let the front end know that we are beginning a specialization.  */
8602  begin_specialization ();
8603
8604  /* If the next keyword is `template', we need to figure out whether
8605     or not we're looking a template-declaration.  */
8606  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8607    {
8608      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8609	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8610	cp_parser_template_declaration_after_export (parser,
8611						     /*member_p=*/false);
8612      else
8613	cp_parser_explicit_specialization (parser);
8614    }
8615  else
8616    /* Parse the dependent declaration.  */
8617    cp_parser_single_declaration (parser,
8618				  /*member_p=*/false,
8619				  /*friend_p=*/NULL);
8620
8621  /* We're done with the specialization.  */
8622  end_specialization ();
8623  /* We're done with this parameter list.  */
8624  --parser->num_template_parameter_lists;
8625}
8626
8627/* Parse a type-specifier.
8628
8629   type-specifier:
8630     simple-type-specifier
8631     class-specifier
8632     enum-specifier
8633     elaborated-type-specifier
8634     cv-qualifier
8635
8636   GNU Extension:
8637
8638   type-specifier:
8639     __complex__
8640
8641   Returns a representation of the type-specifier.  If the
8642   type-specifier is a keyword (like `int' or `const', or
8643   `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8644   For a class-specifier, enum-specifier, or elaborated-type-specifier
8645   a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8646
8647   If IS_FRIEND is TRUE then this type-specifier is being declared a
8648   `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
8649   appearing in a decl-specifier-seq.
8650
8651   If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8652   class-specifier, enum-specifier, or elaborated-type-specifier, then
8653   *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
8654   if a type is declared; 2 if it is defined.  Otherwise, it is set to
8655   zero.
8656
8657   If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8658   cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
8659   is set to FALSE.  */
8660
8661static tree
8662cp_parser_type_specifier (cp_parser* parser,
8663			  cp_parser_flags flags,
8664			  bool is_friend,
8665			  bool is_declaration,
8666			  int* declares_class_or_enum,
8667			  bool* is_cv_qualifier)
8668{
8669  tree type_spec = NULL_TREE;
8670  cp_token *token;
8671  enum rid keyword;
8672
8673  /* Assume this type-specifier does not declare a new type.  */
8674  if (declares_class_or_enum)
8675    *declares_class_or_enum = 0;
8676  /* And that it does not specify a cv-qualifier.  */
8677  if (is_cv_qualifier)
8678    *is_cv_qualifier = false;
8679  /* Peek at the next token.  */
8680  token = cp_lexer_peek_token (parser->lexer);
8681
8682  /* If we're looking at a keyword, we can use that to guide the
8683     production we choose.  */
8684  keyword = token->keyword;
8685  switch (keyword)
8686    {
8687      /* Any of these indicate either a class-specifier, or an
8688	 elaborated-type-specifier.  */
8689    case RID_CLASS:
8690    case RID_STRUCT:
8691    case RID_UNION:
8692    case RID_ENUM:
8693      /* Parse tentatively so that we can back up if we don't find a
8694	 class-specifier or enum-specifier.  */
8695      cp_parser_parse_tentatively (parser);
8696      /* Look for the class-specifier or enum-specifier.  */
8697      if (keyword == RID_ENUM)
8698	type_spec = cp_parser_enum_specifier (parser);
8699      else
8700	type_spec = cp_parser_class_specifier (parser);
8701
8702      /* If that worked, we're done.  */
8703      if (cp_parser_parse_definitely (parser))
8704	{
8705	  if (declares_class_or_enum)
8706	    *declares_class_or_enum = 2;
8707	  return type_spec;
8708	}
8709
8710      /* Fall through.  */
8711
8712    case RID_TYPENAME:
8713      /* Look for an elaborated-type-specifier.  */
8714      type_spec = cp_parser_elaborated_type_specifier (parser,
8715						       is_friend,
8716						       is_declaration);
8717      /* We're declaring a class or enum -- unless we're using
8718	 `typename'.  */
8719      if (declares_class_or_enum && keyword != RID_TYPENAME)
8720	*declares_class_or_enum = 1;
8721      return type_spec;
8722
8723    case RID_CONST:
8724    case RID_VOLATILE:
8725    case RID_RESTRICT:
8726      type_spec = cp_parser_cv_qualifier_opt (parser);
8727      /* Even though we call a routine that looks for an optional
8728	 qualifier, we know that there should be one.  */
8729      my_friendly_assert (type_spec != NULL, 20000328);
8730      /* This type-specifier was a cv-qualified.  */
8731      if (is_cv_qualifier)
8732	*is_cv_qualifier = true;
8733
8734      return type_spec;
8735
8736    case RID_COMPLEX:
8737      /* The `__complex__' keyword is a GNU extension.  */
8738      return cp_lexer_consume_token (parser->lexer)->value;
8739
8740    default:
8741      break;
8742    }
8743
8744  /* If we do not already have a type-specifier, assume we are looking
8745     at a simple-type-specifier.  */
8746  type_spec = cp_parser_simple_type_specifier (parser, flags,
8747					       /*identifier_p=*/true);
8748
8749  /* If we didn't find a type-specifier, and a type-specifier was not
8750     optional in this context, issue an error message.  */
8751  if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8752    {
8753      cp_parser_error (parser, "expected type specifier");
8754      return error_mark_node;
8755    }
8756
8757  return type_spec;
8758}
8759
8760/* Parse a simple-type-specifier.
8761
8762   simple-type-specifier:
8763     :: [opt] nested-name-specifier [opt] type-name
8764     :: [opt] nested-name-specifier template template-id
8765     char
8766     wchar_t
8767     bool
8768     short
8769     int
8770     long
8771     signed
8772     unsigned
8773     float
8774     double
8775     void
8776
8777   GNU Extension:
8778
8779   simple-type-specifier:
8780     __typeof__ unary-expression
8781     __typeof__ ( type-id )
8782
8783   For the various keywords, the value returned is simply the
8784   TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8785   For the first two productions, and if IDENTIFIER_P is false, the
8786   value returned is the indicated TYPE_DECL.  */
8787
8788static tree
8789cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8790				 bool identifier_p)
8791{
8792  tree type = NULL_TREE;
8793  cp_token *token;
8794
8795  /* Peek at the next token.  */
8796  token = cp_lexer_peek_token (parser->lexer);
8797
8798  /* If we're looking at a keyword, things are easy.  */
8799  switch (token->keyword)
8800    {
8801    case RID_CHAR:
8802      type = char_type_node;
8803      break;
8804    case RID_WCHAR:
8805      type = wchar_type_node;
8806      break;
8807    case RID_BOOL:
8808      type = boolean_type_node;
8809      break;
8810    case RID_SHORT:
8811      type = short_integer_type_node;
8812      break;
8813    case RID_INT:
8814      type = integer_type_node;
8815      break;
8816    case RID_LONG:
8817      type = long_integer_type_node;
8818      break;
8819    case RID_SIGNED:
8820      type = integer_type_node;
8821      break;
8822    case RID_UNSIGNED:
8823      type = unsigned_type_node;
8824      break;
8825    case RID_FLOAT:
8826      type = float_type_node;
8827      break;
8828    case RID_DOUBLE:
8829      type = double_type_node;
8830      break;
8831    case RID_VOID:
8832      type = void_type_node;
8833      break;
8834
8835    case RID_TYPEOF:
8836      {
8837	tree operand;
8838
8839	/* Consume the `typeof' token.  */
8840	cp_lexer_consume_token (parser->lexer);
8841	/* Parse the operand to `typeof'.  */
8842	operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8843	/* If it is not already a TYPE, take its type.  */
8844	if (!TYPE_P (operand))
8845	  operand = finish_typeof (operand);
8846
8847	return operand;
8848      }
8849
8850    default:
8851      break;
8852    }
8853
8854  /* If the type-specifier was for a built-in type, we're done.  */
8855  if (type)
8856    {
8857      tree id;
8858
8859      /* Consume the token.  */
8860      id = cp_lexer_consume_token (parser->lexer)->value;
8861
8862      /* There is no valid C++ program where a non-template type is
8863	 followed by a "<".  That usually indicates that the user thought
8864	 that the type was a template.  */
8865      cp_parser_check_for_invalid_template_id (parser, type);
8866
8867      return identifier_p ? id : TYPE_NAME (type);
8868    }
8869
8870  /* The type-specifier must be a user-defined type.  */
8871  if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8872    {
8873      bool qualified_p;
8874      bool global_p;
8875
8876      /* Don't gobble tokens or issue error messages if this is an
8877	 optional type-specifier.  */
8878      if (flags & CP_PARSER_FLAGS_OPTIONAL)
8879	cp_parser_parse_tentatively (parser);
8880
8881      /* Look for the optional `::' operator.  */
8882      global_p
8883	= (cp_parser_global_scope_opt (parser,
8884				       /*current_scope_valid_p=*/false)
8885	   != NULL_TREE);
8886      /* Look for the nested-name specifier.  */
8887      qualified_p
8888	= (cp_parser_nested_name_specifier_opt (parser,
8889						/*typename_keyword_p=*/false,
8890						/*check_dependency_p=*/true,
8891						/*type_p=*/false,
8892						/*is_declaration=*/false)
8893	   != NULL_TREE);
8894      /* If we have seen a nested-name-specifier, and the next token
8895	 is `template', then we are using the template-id production.  */
8896      if (parser->scope
8897	  && cp_parser_optional_template_keyword (parser))
8898	{
8899	  /* Look for the template-id.  */
8900	  type = cp_parser_template_id (parser,
8901					/*template_keyword_p=*/true,
8902					/*check_dependency_p=*/true,
8903					/*is_declaration=*/false);
8904	  /* If the template-id did not name a type, we are out of
8905	     luck.  */
8906	  if (TREE_CODE (type) != TYPE_DECL)
8907	    {
8908	      cp_parser_error (parser, "expected template-id for type");
8909	      type = NULL_TREE;
8910	    }
8911	}
8912      /* Otherwise, look for a type-name.  */
8913      else
8914	type = cp_parser_type_name (parser);
8915      /* Keep track of all name-lookups performed in class scopes.  */
8916      if (type
8917	  && !global_p
8918	  && !qualified_p
8919	  && TREE_CODE (type) == TYPE_DECL
8920	  && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
8921	maybe_note_name_used_in_class (DECL_NAME (type), type);
8922      /* If it didn't work out, we don't have a TYPE.  */
8923      if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8924	  && !cp_parser_parse_definitely (parser))
8925	type = NULL_TREE;
8926    }
8927
8928  /* If we didn't get a type-name, issue an error message.  */
8929  if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8930    {
8931      cp_parser_error (parser, "expected type-name");
8932      return error_mark_node;
8933    }
8934
8935  /* There is no valid C++ program where a non-template type is
8936     followed by a "<".  That usually indicates that the user thought
8937     that the type was a template.  */
8938  if (type && type != error_mark_node)
8939    cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8940
8941  return type;
8942}
8943
8944/* Parse a type-name.
8945
8946   type-name:
8947     class-name
8948     enum-name
8949     typedef-name
8950
8951   enum-name:
8952     identifier
8953
8954   typedef-name:
8955     identifier
8956
8957   Returns a TYPE_DECL for the the type.  */
8958
8959static tree
8960cp_parser_type_name (cp_parser* parser)
8961{
8962  tree type_decl;
8963  tree identifier;
8964
8965  /* We can't know yet whether it is a class-name or not.  */
8966  cp_parser_parse_tentatively (parser);
8967  /* Try a class-name.  */
8968  type_decl = cp_parser_class_name (parser,
8969				    /*typename_keyword_p=*/false,
8970				    /*template_keyword_p=*/false,
8971				    /*type_p=*/false,
8972				    /*check_dependency_p=*/true,
8973				    /*class_head_p=*/false,
8974				    /*is_declaration=*/false);
8975  /* If it's not a class-name, keep looking.  */
8976  if (!cp_parser_parse_definitely (parser))
8977    {
8978      /* It must be a typedef-name or an enum-name.  */
8979      identifier = cp_parser_identifier (parser);
8980      if (identifier == error_mark_node)
8981	return error_mark_node;
8982
8983      /* Look up the type-name.  */
8984      type_decl = cp_parser_lookup_name_simple (parser, identifier);
8985      /* Issue an error if we did not find a type-name.  */
8986      if (TREE_CODE (type_decl) != TYPE_DECL)
8987	{
8988	  if (!cp_parser_simulate_error (parser))
8989	    cp_parser_name_lookup_error (parser, identifier, type_decl,
8990					 "is not a type");
8991	  type_decl = error_mark_node;
8992	}
8993      /* Remember that the name was used in the definition of the
8994	 current class so that we can check later to see if the
8995	 meaning would have been different after the class was
8996	 entirely defined.  */
8997      else if (type_decl != error_mark_node
8998	       && !parser->scope)
8999	maybe_note_name_used_in_class (identifier, type_decl);
9000    }
9001
9002  return type_decl;
9003}
9004
9005
9006/* Parse an elaborated-type-specifier.  Note that the grammar given
9007   here incorporates the resolution to DR68.
9008
9009   elaborated-type-specifier:
9010     class-key :: [opt] nested-name-specifier [opt] identifier
9011     class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9012     enum :: [opt] nested-name-specifier [opt] identifier
9013     typename :: [opt] nested-name-specifier identifier
9014     typename :: [opt] nested-name-specifier template [opt]
9015       template-id
9016
9017   GNU extension:
9018
9019   elaborated-type-specifier:
9020     class-key attributes :: [opt] nested-name-specifier [opt] identifier
9021     class-key attributes :: [opt] nested-name-specifier [opt]
9022               template [opt] template-id
9023     enum attributes :: [opt] nested-name-specifier [opt] identifier
9024
9025   If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9026   declared `friend'.  If IS_DECLARATION is TRUE, then this
9027   elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9028   something is being declared.
9029
9030   Returns the TYPE specified.  */
9031
9032static tree
9033cp_parser_elaborated_type_specifier (cp_parser* parser,
9034                                     bool is_friend,
9035                                     bool is_declaration)
9036{
9037  enum tag_types tag_type;
9038  tree identifier;
9039  tree type = NULL_TREE;
9040  tree attributes = NULL_TREE;
9041
9042  /* See if we're looking at the `enum' keyword.  */
9043  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9044    {
9045      /* Consume the `enum' token.  */
9046      cp_lexer_consume_token (parser->lexer);
9047      /* Remember that it's an enumeration type.  */
9048      tag_type = enum_type;
9049      /* Parse the attributes.  */
9050      attributes = cp_parser_attributes_opt (parser);
9051    }
9052  /* Or, it might be `typename'.  */
9053  else if (cp_lexer_next_token_is_keyword (parser->lexer,
9054					   RID_TYPENAME))
9055    {
9056      /* Consume the `typename' token.  */
9057      cp_lexer_consume_token (parser->lexer);
9058      /* Remember that it's a `typename' type.  */
9059      tag_type = typename_type;
9060      /* The `typename' keyword is only allowed in templates.  */
9061      if (!processing_template_decl)
9062	pedwarn ("using `typename' outside of template");
9063    }
9064  /* Otherwise it must be a class-key.  */
9065  else
9066    {
9067      tag_type = cp_parser_class_key (parser);
9068      if (tag_type == none_type)
9069	return error_mark_node;
9070      /* Parse the attributes.  */
9071      attributes = cp_parser_attributes_opt (parser);
9072    }
9073
9074  /* Look for the `::' operator.  */
9075  cp_parser_global_scope_opt (parser,
9076			      /*current_scope_valid_p=*/false);
9077  /* Look for the nested-name-specifier.  */
9078  if (tag_type == typename_type)
9079    {
9080      if (cp_parser_nested_name_specifier (parser,
9081					   /*typename_keyword_p=*/true,
9082					   /*check_dependency_p=*/true,
9083					   /*type_p=*/true,
9084					   is_declaration)
9085	  == error_mark_node)
9086	return error_mark_node;
9087    }
9088  else
9089    /* Even though `typename' is not present, the proposed resolution
9090       to Core Issue 180 says that in `class A<T>::B', `B' should be
9091       considered a type-name, even if `A<T>' is dependent.  */
9092    cp_parser_nested_name_specifier_opt (parser,
9093					 /*typename_keyword_p=*/true,
9094					 /*check_dependency_p=*/true,
9095					 /*type_p=*/true,
9096					 is_declaration);
9097  /* For everything but enumeration types, consider a template-id.  */
9098  if (tag_type != enum_type)
9099    {
9100      bool template_p = false;
9101      tree decl;
9102
9103      /* Allow the `template' keyword.  */
9104      template_p = cp_parser_optional_template_keyword (parser);
9105      /* If we didn't see `template', we don't know if there's a
9106         template-id or not.  */
9107      if (!template_p)
9108	cp_parser_parse_tentatively (parser);
9109      /* Parse the template-id.  */
9110      decl = cp_parser_template_id (parser, template_p,
9111				    /*check_dependency_p=*/true,
9112				    is_declaration);
9113      /* If we didn't find a template-id, look for an ordinary
9114         identifier.  */
9115      if (!template_p && !cp_parser_parse_definitely (parser))
9116	;
9117      /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9118	 in effect, then we must assume that, upon instantiation, the
9119	 template will correspond to a class.  */
9120      else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9121	       && tag_type == typename_type)
9122	type = make_typename_type (parser->scope, decl,
9123				   /*complain=*/1);
9124      else
9125	type = TREE_TYPE (decl);
9126    }
9127
9128  /* For an enumeration type, consider only a plain identifier.  */
9129  if (!type)
9130    {
9131      identifier = cp_parser_identifier (parser);
9132
9133      if (identifier == error_mark_node)
9134	{
9135	  parser->scope = NULL_TREE;
9136	  return error_mark_node;
9137	}
9138
9139      /* For a `typename', we needn't call xref_tag.  */
9140      if (tag_type == typename_type)
9141	return make_typename_type (parser->scope, identifier,
9142				   /*complain=*/1);
9143      /* Look up a qualified name in the usual way.  */
9144      if (parser->scope)
9145	{
9146	  tree decl;
9147
9148	  /* In an elaborated-type-specifier, names are assumed to name
9149	     types, so we set IS_TYPE to TRUE when calling
9150	     cp_parser_lookup_name.  */
9151	  decl = cp_parser_lookup_name (parser, identifier,
9152					/*is_type=*/true,
9153					/*is_template=*/false,
9154					/*is_namespace=*/false,
9155					/*check_dependency=*/true);
9156
9157	  /* If we are parsing friend declaration, DECL may be a
9158	     TEMPLATE_DECL tree node here.  However, we need to check
9159	     whether this TEMPLATE_DECL results in valid code.  Consider
9160	     the following example:
9161
9162	       namespace N {
9163		 template <class T> class C {};
9164	       }
9165	       class X {
9166		 template <class T> friend class N::C; // #1, valid code
9167	       };
9168	       template <class T> class Y {
9169		 friend class N::C;		       // #2, invalid code
9170	       };
9171
9172	     For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9173	     name lookup of `N::C'.  We see that friend declaration must
9174	     be template for the code to be valid.  Note that
9175	     processing_template_decl does not work here since it is
9176	     always 1 for the above two cases.  */
9177
9178	  decl = (cp_parser_maybe_treat_template_as_class
9179		  (decl, /*tag_name_p=*/is_friend
9180			 && parser->num_template_parameter_lists));
9181
9182	  if (TREE_CODE (decl) != TYPE_DECL)
9183	    {
9184	      error ("expected type-name");
9185	      return error_mark_node;
9186	    }
9187
9188	  if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9189	    check_elaborated_type_specifier
9190	      (tag_type, decl,
9191	       (parser->num_template_parameter_lists
9192		|| DECL_SELF_REFERENCE_P (decl)));
9193
9194	  type = TREE_TYPE (decl);
9195	}
9196      else
9197	{
9198	  /* An elaborated-type-specifier sometimes introduces a new type and
9199	     sometimes names an existing type.  Normally, the rule is that it
9200	     introduces a new type only if there is not an existing type of
9201	     the same name already in scope.  For example, given:
9202
9203	       struct S {};
9204	       void f() { struct S s; }
9205
9206	     the `struct S' in the body of `f' is the same `struct S' as in
9207	     the global scope; the existing definition is used.  However, if
9208	     there were no global declaration, this would introduce a new
9209	     local class named `S'.
9210
9211	     An exception to this rule applies to the following code:
9212
9213	       namespace N { struct S; }
9214
9215	     Here, the elaborated-type-specifier names a new type
9216	     unconditionally; even if there is already an `S' in the
9217	     containing scope this declaration names a new type.
9218	     This exception only applies if the elaborated-type-specifier
9219	     forms the complete declaration:
9220
9221	       [class.name]
9222
9223	       A declaration consisting solely of `class-key identifier ;' is
9224	       either a redeclaration of the name in the current scope or a
9225	       forward declaration of the identifier as a class name.  It
9226	       introduces the name into the current scope.
9227
9228	     We are in this situation precisely when the next token is a `;'.
9229
9230	     An exception to the exception is that a `friend' declaration does
9231	     *not* name a new type; i.e., given:
9232
9233	       struct S { friend struct T; };
9234
9235	     `T' is not a new type in the scope of `S'.
9236
9237	     Also, `new struct S' or `sizeof (struct S)' never results in the
9238	     definition of a new type; a new type can only be declared in a
9239	     declaration context.  */
9240
9241 	  /* Warn about attributes. They are ignored.  */
9242 	  if (attributes)
9243	    warning ("type attributes are honored only at type definition");
9244
9245	  type = xref_tag (tag_type, identifier,
9246			   (is_friend
9247			    || !is_declaration
9248			    || cp_lexer_next_token_is_not (parser->lexer,
9249							   CPP_SEMICOLON)),
9250			   parser->num_template_parameter_lists);
9251	}
9252    }
9253  if (tag_type != enum_type)
9254    cp_parser_check_class_key (tag_type, type);
9255
9256  /* A "<" cannot follow an elaborated type specifier.  If that
9257     happens, the user was probably trying to form a template-id.  */
9258  cp_parser_check_for_invalid_template_id (parser, type);
9259
9260  return type;
9261}
9262
9263/* Parse an enum-specifier.
9264
9265   enum-specifier:
9266     enum identifier [opt] { enumerator-list [opt] }
9267
9268   Returns an ENUM_TYPE representing the enumeration.  */
9269
9270static tree
9271cp_parser_enum_specifier (cp_parser* parser)
9272{
9273  cp_token *token;
9274  tree identifier = NULL_TREE;
9275  tree type;
9276
9277  /* Look for the `enum' keyword.  */
9278  if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9279    return error_mark_node;
9280  /* Peek at the next token.  */
9281  token = cp_lexer_peek_token (parser->lexer);
9282
9283  /* See if it is an identifier.  */
9284  if (token->type == CPP_NAME)
9285    identifier = cp_parser_identifier (parser);
9286
9287  /* Look for the `{'.  */
9288  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9289    return error_mark_node;
9290
9291  /* At this point, we're going ahead with the enum-specifier, even
9292     if some other problem occurs.  */
9293  cp_parser_commit_to_tentative_parse (parser);
9294
9295  /* Issue an error message if type-definitions are forbidden here.  */
9296  cp_parser_check_type_definition (parser);
9297
9298  /* Create the new type.  */
9299  type = start_enum (identifier ? identifier : make_anon_name ());
9300
9301  /* Peek at the next token.  */
9302  token = cp_lexer_peek_token (parser->lexer);
9303  /* If it's not a `}', then there are some enumerators.  */
9304  if (token->type != CPP_CLOSE_BRACE)
9305    cp_parser_enumerator_list (parser, type);
9306  /* Look for the `}'.  */
9307  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9308
9309  /* Finish up the enumeration.  */
9310  finish_enum (type);
9311
9312  return type;
9313}
9314
9315/* Parse an enumerator-list.  The enumerators all have the indicated
9316   TYPE.
9317
9318   enumerator-list:
9319     enumerator-definition
9320     enumerator-list , enumerator-definition  */
9321
9322static void
9323cp_parser_enumerator_list (cp_parser* parser, tree type)
9324{
9325  while (true)
9326    {
9327      cp_token *token;
9328
9329      /* Parse an enumerator-definition.  */
9330      cp_parser_enumerator_definition (parser, type);
9331      /* Peek at the next token.  */
9332      token = cp_lexer_peek_token (parser->lexer);
9333      /* If it's not a `,', then we've reached the end of the
9334	 list.  */
9335      if (token->type != CPP_COMMA)
9336	break;
9337      /* Otherwise, consume the `,' and keep going.  */
9338      cp_lexer_consume_token (parser->lexer);
9339      /* If the next token is a `}', there is a trailing comma.  */
9340      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9341	{
9342	  if (pedantic && !in_system_header)
9343	    pedwarn ("comma at end of enumerator list");
9344	  break;
9345	}
9346    }
9347}
9348
9349/* Parse an enumerator-definition.  The enumerator has the indicated
9350   TYPE.
9351
9352   enumerator-definition:
9353     enumerator
9354     enumerator = constant-expression
9355
9356   enumerator:
9357     identifier  */
9358
9359static void
9360cp_parser_enumerator_definition (cp_parser* parser, tree type)
9361{
9362  cp_token *token;
9363  tree identifier;
9364  tree value;
9365
9366  /* Look for the identifier.  */
9367  identifier = cp_parser_identifier (parser);
9368  if (identifier == error_mark_node)
9369    return;
9370
9371  /* Peek at the next token.  */
9372  token = cp_lexer_peek_token (parser->lexer);
9373  /* If it's an `=', then there's an explicit value.  */
9374  if (token->type == CPP_EQ)
9375    {
9376      /* Consume the `=' token.  */
9377      cp_lexer_consume_token (parser->lexer);
9378      /* Parse the value.  */
9379      value = cp_parser_constant_expression (parser,
9380					     /*allow_non_constant_p=*/false,
9381					     NULL);
9382    }
9383  else
9384    value = NULL_TREE;
9385
9386  /* Create the enumerator.  */
9387  build_enumerator (identifier, value, type);
9388}
9389
9390/* Parse a namespace-name.
9391
9392   namespace-name:
9393     original-namespace-name
9394     namespace-alias
9395
9396   Returns the NAMESPACE_DECL for the namespace.  */
9397
9398static tree
9399cp_parser_namespace_name (cp_parser* parser)
9400{
9401  tree identifier;
9402  tree namespace_decl;
9403
9404  /* Get the name of the namespace.  */
9405  identifier = cp_parser_identifier (parser);
9406  if (identifier == error_mark_node)
9407    return error_mark_node;
9408
9409  /* Look up the identifier in the currently active scope.  Look only
9410     for namespaces, due to:
9411
9412       [basic.lookup.udir]
9413
9414       When looking up a namespace-name in a using-directive or alias
9415       definition, only namespace names are considered.
9416
9417     And:
9418
9419       [basic.lookup.qual]
9420
9421       During the lookup of a name preceding the :: scope resolution
9422       operator, object, function, and enumerator names are ignored.
9423
9424     (Note that cp_parser_class_or_namespace_name only calls this
9425     function if the token after the name is the scope resolution
9426     operator.)  */
9427  namespace_decl = cp_parser_lookup_name (parser, identifier,
9428					  /*is_type=*/false,
9429					  /*is_template=*/false,
9430					  /*is_namespace=*/true,
9431					  /*check_dependency=*/true);
9432  /* If it's not a namespace, issue an error.  */
9433  if (namespace_decl == error_mark_node
9434      || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9435    {
9436      cp_parser_error (parser, "expected namespace-name");
9437      namespace_decl = error_mark_node;
9438    }
9439
9440  return namespace_decl;
9441}
9442
9443/* Parse a namespace-definition.
9444
9445   namespace-definition:
9446     named-namespace-definition
9447     unnamed-namespace-definition
9448
9449   named-namespace-definition:
9450     original-namespace-definition
9451     extension-namespace-definition
9452
9453   original-namespace-definition:
9454     namespace identifier { namespace-body }
9455
9456   extension-namespace-definition:
9457     namespace original-namespace-name { namespace-body }
9458
9459   unnamed-namespace-definition:
9460     namespace { namespace-body } */
9461
9462static void
9463cp_parser_namespace_definition (cp_parser* parser)
9464{
9465  tree identifier;
9466
9467  /* Look for the `namespace' keyword.  */
9468  cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9469
9470  /* Get the name of the namespace.  We do not attempt to distinguish
9471     between an original-namespace-definition and an
9472     extension-namespace-definition at this point.  The semantic
9473     analysis routines are responsible for that.  */
9474  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9475    identifier = cp_parser_identifier (parser);
9476  else
9477    identifier = NULL_TREE;
9478
9479  /* Look for the `{' to start the namespace.  */
9480  cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9481  /* Start the namespace.  */
9482  push_namespace (identifier);
9483  /* Parse the body of the namespace.  */
9484  cp_parser_namespace_body (parser);
9485  /* Finish the namespace.  */
9486  pop_namespace ();
9487  /* Look for the final `}'.  */
9488  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9489}
9490
9491/* Parse a namespace-body.
9492
9493   namespace-body:
9494     declaration-seq [opt]  */
9495
9496static void
9497cp_parser_namespace_body (cp_parser* parser)
9498{
9499  cp_parser_declaration_seq_opt (parser);
9500}
9501
9502/* Parse a namespace-alias-definition.
9503
9504   namespace-alias-definition:
9505     namespace identifier = qualified-namespace-specifier ;  */
9506
9507static void
9508cp_parser_namespace_alias_definition (cp_parser* parser)
9509{
9510  tree identifier;
9511  tree namespace_specifier;
9512
9513  /* Look for the `namespace' keyword.  */
9514  cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9515  /* Look for the identifier.  */
9516  identifier = cp_parser_identifier (parser);
9517  if (identifier == error_mark_node)
9518    return;
9519  /* Look for the `=' token.  */
9520  cp_parser_require (parser, CPP_EQ, "`='");
9521  /* Look for the qualified-namespace-specifier.  */
9522  namespace_specifier
9523    = cp_parser_qualified_namespace_specifier (parser);
9524  /* Look for the `;' token.  */
9525  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9526
9527  /* Register the alias in the symbol table.  */
9528  do_namespace_alias (identifier, namespace_specifier);
9529}
9530
9531/* Parse a qualified-namespace-specifier.
9532
9533   qualified-namespace-specifier:
9534     :: [opt] nested-name-specifier [opt] namespace-name
9535
9536   Returns a NAMESPACE_DECL corresponding to the specified
9537   namespace.  */
9538
9539static tree
9540cp_parser_qualified_namespace_specifier (cp_parser* parser)
9541{
9542  /* Look for the optional `::'.  */
9543  cp_parser_global_scope_opt (parser,
9544			      /*current_scope_valid_p=*/false);
9545
9546  /* Look for the optional nested-name-specifier.  */
9547  cp_parser_nested_name_specifier_opt (parser,
9548				       /*typename_keyword_p=*/false,
9549				       /*check_dependency_p=*/true,
9550				       /*type_p=*/false,
9551				       /*is_declaration=*/true);
9552
9553  return cp_parser_namespace_name (parser);
9554}
9555
9556/* Parse a using-declaration.
9557
9558   using-declaration:
9559     using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9560     using :: unqualified-id ;  */
9561
9562static void
9563cp_parser_using_declaration (cp_parser* parser)
9564{
9565  cp_token *token;
9566  bool typename_p = false;
9567  bool global_scope_p;
9568  tree decl;
9569  tree identifier;
9570  tree scope;
9571  tree qscope;
9572
9573  /* Look for the `using' keyword.  */
9574  cp_parser_require_keyword (parser, RID_USING, "`using'");
9575
9576  /* Peek at the next token.  */
9577  token = cp_lexer_peek_token (parser->lexer);
9578  /* See if it's `typename'.  */
9579  if (token->keyword == RID_TYPENAME)
9580    {
9581      /* Remember that we've seen it.  */
9582      typename_p = true;
9583      /* Consume the `typename' token.  */
9584      cp_lexer_consume_token (parser->lexer);
9585    }
9586
9587  /* Look for the optional global scope qualification.  */
9588  global_scope_p
9589    = (cp_parser_global_scope_opt (parser,
9590				   /*current_scope_valid_p=*/false)
9591       != NULL_TREE);
9592
9593  /* If we saw `typename', or didn't see `::', then there must be a
9594     nested-name-specifier present.  */
9595  if (typename_p || !global_scope_p)
9596    qscope = cp_parser_nested_name_specifier (parser, typename_p,
9597					      /*check_dependency_p=*/true,
9598					      /*type_p=*/false,
9599					      /*is_declaration=*/true);
9600  /* Otherwise, we could be in either of the two productions.  In that
9601     case, treat the nested-name-specifier as optional.  */
9602  else
9603    qscope = cp_parser_nested_name_specifier_opt (parser,
9604						  /*typename_keyword_p=*/false,
9605						  /*check_dependency_p=*/true,
9606						  /*type_p=*/false,
9607						  /*is_declaration=*/true);
9608  if (!qscope)
9609    qscope = global_namespace;
9610
9611  /* Parse the unqualified-id.  */
9612  identifier = cp_parser_unqualified_id (parser,
9613					 /*template_keyword_p=*/false,
9614					 /*check_dependency_p=*/true,
9615					 /*declarator_p=*/true);
9616
9617  /* The function we call to handle a using-declaration is different
9618     depending on what scope we are in.  */
9619  if (identifier == error_mark_node)
9620    ;
9621  else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9622	   && TREE_CODE (identifier) != BIT_NOT_EXPR)
9623    /* [namespace.udecl]
9624
9625       A using declaration shall not name a template-id.  */
9626    error ("a template-id may not appear in a using-declaration");
9627  else
9628    {
9629      scope = current_scope ();
9630      if (scope && TYPE_P (scope))
9631	{
9632	  /* Create the USING_DECL.  */
9633	  decl = do_class_using_decl (build_nt (SCOPE_REF,
9634						parser->scope,
9635						identifier));
9636	  /* Add it to the list of members in this class.  */
9637	  finish_member_declaration (decl);
9638	}
9639      else
9640	{
9641	  decl = cp_parser_lookup_name_simple (parser, identifier);
9642	  if (decl == error_mark_node)
9643	    cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9644	  else if (scope)
9645	    do_local_using_decl (decl, qscope, identifier);
9646	  else
9647	    do_toplevel_using_decl (decl, qscope, identifier);
9648	}
9649    }
9650
9651  /* Look for the final `;'.  */
9652  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9653}
9654
9655/* Parse a using-directive.
9656
9657   using-directive:
9658     using namespace :: [opt] nested-name-specifier [opt]
9659       namespace-name ;  */
9660
9661static void
9662cp_parser_using_directive (cp_parser* parser)
9663{
9664  tree namespace_decl;
9665  tree attribs;
9666
9667  /* Look for the `using' keyword.  */
9668  cp_parser_require_keyword (parser, RID_USING, "`using'");
9669  /* And the `namespace' keyword.  */
9670  cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9671  /* Look for the optional `::' operator.  */
9672  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9673  /* And the optional nested-name-specifier.  */
9674  cp_parser_nested_name_specifier_opt (parser,
9675				       /*typename_keyword_p=*/false,
9676				       /*check_dependency_p=*/true,
9677				       /*type_p=*/false,
9678				       /*is_declaration=*/true);
9679  /* Get the namespace being used.  */
9680  namespace_decl = cp_parser_namespace_name (parser);
9681  /* And any specified attributes.  */
9682  attribs = cp_parser_attributes_opt (parser);
9683  /* Update the symbol table.  */
9684  parse_using_directive (namespace_decl, attribs);
9685  /* Look for the final `;'.  */
9686  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9687}
9688
9689/* Parse an asm-definition.
9690
9691   asm-definition:
9692     asm ( string-literal ) ;
9693
9694   GNU Extension:
9695
9696   asm-definition:
9697     asm volatile [opt] ( string-literal ) ;
9698     asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9699     asm volatile [opt] ( string-literal : asm-operand-list [opt]
9700                          : asm-operand-list [opt] ) ;
9701     asm volatile [opt] ( string-literal : asm-operand-list [opt]
9702                          : asm-operand-list [opt]
9703                          : asm-operand-list [opt] ) ;  */
9704
9705static void
9706cp_parser_asm_definition (cp_parser* parser)
9707{
9708  cp_token *token;
9709  tree string;
9710  tree outputs = NULL_TREE;
9711  tree inputs = NULL_TREE;
9712  tree clobbers = NULL_TREE;
9713  tree asm_stmt;
9714  bool volatile_p = false;
9715  bool extended_p = false;
9716
9717  /* Look for the `asm' keyword.  */
9718  cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9719  /* See if the next token is `volatile'.  */
9720  if (cp_parser_allow_gnu_extensions_p (parser)
9721      && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9722    {
9723      /* Remember that we saw the `volatile' keyword.  */
9724      volatile_p = true;
9725      /* Consume the token.  */
9726      cp_lexer_consume_token (parser->lexer);
9727    }
9728  /* Look for the opening `('.  */
9729  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9730  /* Look for the string.  */
9731  token = cp_parser_require (parser, CPP_STRING, "asm body");
9732  if (!token)
9733    return;
9734  string = token->value;
9735  /* If we're allowing GNU extensions, check for the extended assembly
9736     syntax.  Unfortunately, the `:' tokens need not be separated by
9737     a space in C, and so, for compatibility, we tolerate that here
9738     too.  Doing that means that we have to treat the `::' operator as
9739     two `:' tokens.  */
9740  if (cp_parser_allow_gnu_extensions_p (parser)
9741      && at_function_scope_p ()
9742      && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9743	  || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9744    {
9745      bool inputs_p = false;
9746      bool clobbers_p = false;
9747
9748      /* The extended syntax was used.  */
9749      extended_p = true;
9750
9751      /* Look for outputs.  */
9752      if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9753	{
9754	  /* Consume the `:'.  */
9755	  cp_lexer_consume_token (parser->lexer);
9756	  /* Parse the output-operands.  */
9757	  if (cp_lexer_next_token_is_not (parser->lexer,
9758					  CPP_COLON)
9759	      && cp_lexer_next_token_is_not (parser->lexer,
9760					     CPP_SCOPE)
9761	      && cp_lexer_next_token_is_not (parser->lexer,
9762					     CPP_CLOSE_PAREN))
9763	    outputs = cp_parser_asm_operand_list (parser);
9764	}
9765      /* If the next token is `::', there are no outputs, and the
9766	 next token is the beginning of the inputs.  */
9767      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9768	{
9769	  /* Consume the `::' token.  */
9770	  cp_lexer_consume_token (parser->lexer);
9771	  /* The inputs are coming next.  */
9772	  inputs_p = true;
9773	}
9774
9775      /* Look for inputs.  */
9776      if (inputs_p
9777	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9778	{
9779	  if (!inputs_p)
9780	    /* Consume the `:'.  */
9781	    cp_lexer_consume_token (parser->lexer);
9782	  /* Parse the output-operands.  */
9783	  if (cp_lexer_next_token_is_not (parser->lexer,
9784					  CPP_COLON)
9785	      && cp_lexer_next_token_is_not (parser->lexer,
9786					     CPP_SCOPE)
9787	      && cp_lexer_next_token_is_not (parser->lexer,
9788					     CPP_CLOSE_PAREN))
9789	    inputs = cp_parser_asm_operand_list (parser);
9790	}
9791      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9792	/* The clobbers are coming next.  */
9793	clobbers_p = true;
9794
9795      /* Look for clobbers.  */
9796      if (clobbers_p
9797	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9798	{
9799	  if (!clobbers_p)
9800	    /* Consume the `:'.  */
9801	    cp_lexer_consume_token (parser->lexer);
9802	  /* Parse the clobbers.  */
9803	  if (cp_lexer_next_token_is_not (parser->lexer,
9804					  CPP_CLOSE_PAREN))
9805	    clobbers = cp_parser_asm_clobber_list (parser);
9806	}
9807    }
9808  /* Look for the closing `)'.  */
9809  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9810    cp_parser_skip_to_closing_parenthesis (parser, true, false,
9811					   /*consume_paren=*/true);
9812  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9813
9814  /* Create the ASM_STMT.  */
9815  if (at_function_scope_p ())
9816    {
9817      asm_stmt =
9818	finish_asm_stmt (volatile_p
9819			 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9820			 string, outputs, inputs, clobbers);
9821      /* If the extended syntax was not used, mark the ASM_STMT.  */
9822      if (!extended_p)
9823	ASM_INPUT_P (asm_stmt) = 1;
9824    }
9825  else
9826    assemble_asm (string);
9827}
9828
9829/* Declarators [gram.dcl.decl] */
9830
9831/* Parse an init-declarator.
9832
9833   init-declarator:
9834     declarator initializer [opt]
9835
9836   GNU Extension:
9837
9838   init-declarator:
9839     declarator asm-specification [opt] attributes [opt] initializer [opt]
9840
9841   function-definition:
9842     decl-specifier-seq [opt] declarator ctor-initializer [opt]
9843       function-body
9844     decl-specifier-seq [opt] declarator function-try-block
9845
9846   GNU Extension:
9847
9848   function-definition:
9849     __extension__ function-definition
9850
9851   The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9852   Returns a representation of the entity declared.  If MEMBER_P is TRUE,
9853   then this declarator appears in a class scope.  The new DECL created
9854   by this declarator is returned.
9855
9856   If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9857   for a function-definition here as well.  If the declarator is a
9858   declarator for a function-definition, *FUNCTION_DEFINITION_P will
9859   be TRUE upon return.  By that point, the function-definition will
9860   have been completely parsed.
9861
9862   FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9863   is FALSE.  */
9864
9865static tree
9866cp_parser_init_declarator (cp_parser* parser,
9867			   tree decl_specifiers,
9868			   tree prefix_attributes,
9869			   bool function_definition_allowed_p,
9870			   bool member_p,
9871			   int declares_class_or_enum,
9872			   bool* function_definition_p)
9873{
9874  cp_token *token;
9875  tree declarator;
9876  tree attributes;
9877  tree asm_specification;
9878  tree initializer;
9879  tree decl = NULL_TREE;
9880  tree scope;
9881  bool is_initialized;
9882  bool is_parenthesized_init;
9883  bool is_non_constant_init;
9884  int ctor_dtor_or_conv_p;
9885  bool friend_p;
9886  bool pop_p = false;
9887
9888  /* Assume that this is not the declarator for a function
9889     definition.  */
9890  if (function_definition_p)
9891    *function_definition_p = false;
9892
9893  /* Defer access checks while parsing the declarator; we cannot know
9894     what names are accessible until we know what is being
9895     declared.  */
9896  resume_deferring_access_checks ();
9897
9898  /* Parse the declarator.  */
9899  declarator
9900    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9901			    &ctor_dtor_or_conv_p,
9902			    /*parenthesized_p=*/NULL);
9903  /* Gather up the deferred checks.  */
9904  stop_deferring_access_checks ();
9905
9906  /* If the DECLARATOR was erroneous, there's no need to go
9907     further.  */
9908  if (declarator == error_mark_node)
9909    return error_mark_node;
9910
9911  cp_parser_check_for_definition_in_return_type (declarator,
9912						 declares_class_or_enum);
9913
9914  /* Figure out what scope the entity declared by the DECLARATOR is
9915     located in.  `grokdeclarator' sometimes changes the scope, so
9916     we compute it now.  */
9917  scope = get_scope_of_declarator (declarator);
9918
9919  /* If we're allowing GNU extensions, look for an asm-specification
9920     and attributes.  */
9921  if (cp_parser_allow_gnu_extensions_p (parser))
9922    {
9923      /* Look for an asm-specification.  */
9924      asm_specification = cp_parser_asm_specification_opt (parser);
9925      /* And attributes.  */
9926      attributes = cp_parser_attributes_opt (parser);
9927    }
9928  else
9929    {
9930      asm_specification = NULL_TREE;
9931      attributes = NULL_TREE;
9932    }
9933
9934  /* Peek at the next token.  */
9935  token = cp_lexer_peek_token (parser->lexer);
9936  /* Check to see if the token indicates the start of a
9937     function-definition.  */
9938  if (cp_parser_token_starts_function_definition_p (token))
9939    {
9940      if (!function_definition_allowed_p)
9941	{
9942	  /* If a function-definition should not appear here, issue an
9943	     error message.  */
9944	  cp_parser_error (parser,
9945			   "a function-definition is not allowed here");
9946	  return error_mark_node;
9947	}
9948      else
9949	{
9950	  /* Neither attributes nor an asm-specification are allowed
9951	     on a function-definition.  */
9952	  if (asm_specification)
9953	    error ("an asm-specification is not allowed on a function-definition");
9954	  if (attributes)
9955	    error ("attributes are not allowed on a function-definition");
9956	  /* This is a function-definition.  */
9957	  *function_definition_p = true;
9958
9959	  /* Parse the function definition.  */
9960	  if (member_p)
9961	    decl = cp_parser_save_member_function_body (parser,
9962							decl_specifiers,
9963							declarator,
9964							prefix_attributes);
9965	  else
9966	    decl
9967	      = (cp_parser_function_definition_from_specifiers_and_declarator
9968		 (parser, decl_specifiers, prefix_attributes, declarator));
9969
9970	  return decl;
9971	}
9972    }
9973
9974  /* [dcl.dcl]
9975
9976     Only in function declarations for constructors, destructors, and
9977     type conversions can the decl-specifier-seq be omitted.
9978
9979     We explicitly postpone this check past the point where we handle
9980     function-definitions because we tolerate function-definitions
9981     that are missing their return types in some modes.  */
9982  if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9983    {
9984      cp_parser_error (parser,
9985		       "expected constructor, destructor, or type conversion");
9986      return error_mark_node;
9987    }
9988
9989  /* An `=' or an `(' indicates an initializer.  */
9990  is_initialized = (token->type == CPP_EQ
9991		     || token->type == CPP_OPEN_PAREN);
9992  /* If the init-declarator isn't initialized and isn't followed by a
9993     `,' or `;', it's not a valid init-declarator.  */
9994  if (!is_initialized
9995      && token->type != CPP_COMMA
9996      && token->type != CPP_SEMICOLON)
9997    {
9998      cp_parser_error (parser, "expected init-declarator");
9999      return error_mark_node;
10000    }
10001
10002  /* Because start_decl has side-effects, we should only call it if we
10003     know we're going ahead.  By this point, we know that we cannot
10004     possibly be looking at any other construct.  */
10005  cp_parser_commit_to_tentative_parse (parser);
10006
10007  /* If the decl specifiers were bad, issue an error now that we're
10008     sure this was intended to be a declarator.  Then continue
10009     declaring the variable(s), as int, to try to cut down on further
10010     errors.  */
10011  if (decl_specifiers != NULL
10012      && TREE_VALUE (decl_specifiers) == error_mark_node)
10013    {
10014      cp_parser_error (parser, "invalid type in declaration");
10015      TREE_VALUE (decl_specifiers) = integer_type_node;
10016    }
10017
10018  /* Check to see whether or not this declaration is a friend.  */
10019  friend_p = cp_parser_friend_p (decl_specifiers);
10020
10021  /* Check that the number of template-parameter-lists is OK.  */
10022  if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10023    return error_mark_node;
10024
10025  /* Enter the newly declared entry in the symbol table.  If we're
10026     processing a declaration in a class-specifier, we wait until
10027     after processing the initializer.  */
10028  if (!member_p)
10029    {
10030      if (parser->in_unbraced_linkage_specification_p)
10031	{
10032	  decl_specifiers = tree_cons (error_mark_node,
10033				       get_identifier ("extern"),
10034				       decl_specifiers);
10035	  have_extern_spec = false;
10036	}
10037      decl = start_decl (declarator, decl_specifiers,
10038			 is_initialized, attributes, prefix_attributes);
10039    }
10040
10041  /* Enter the SCOPE.  That way unqualified names appearing in the
10042     initializer will be looked up in SCOPE.  */
10043  if (scope)
10044    pop_p = push_scope (scope);
10045
10046  /* Perform deferred access control checks, now that we know in which
10047     SCOPE the declared entity resides.  */
10048  if (!member_p && decl)
10049    {
10050      tree saved_current_function_decl = NULL_TREE;
10051
10052      /* If the entity being declared is a function, pretend that we
10053	 are in its scope.  If it is a `friend', it may have access to
10054	 things that would not otherwise be accessible.  */
10055      if (TREE_CODE (decl) == FUNCTION_DECL)
10056	{
10057	  saved_current_function_decl = current_function_decl;
10058	  current_function_decl = decl;
10059	}
10060
10061      /* Perform the access control checks for the declarator and the
10062	 the decl-specifiers.  */
10063      perform_deferred_access_checks ();
10064
10065      /* Restore the saved value.  */
10066      if (TREE_CODE (decl) == FUNCTION_DECL)
10067	current_function_decl = saved_current_function_decl;
10068    }
10069
10070  /* Parse the initializer.  */
10071  if (is_initialized)
10072    initializer = cp_parser_initializer (parser,
10073					 &is_parenthesized_init,
10074					 &is_non_constant_init);
10075  else
10076    {
10077      initializer = NULL_TREE;
10078      is_parenthesized_init = false;
10079      is_non_constant_init = true;
10080    }
10081
10082  /* The old parser allows attributes to appear after a parenthesized
10083     initializer.  Mark Mitchell proposed removing this functionality
10084     on the GCC mailing lists on 2002-08-13.  This parser accepts the
10085     attributes -- but ignores them.  */
10086  if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10087    if (cp_parser_attributes_opt (parser))
10088      warning ("attributes after parenthesized initializer ignored");
10089
10090  /* Leave the SCOPE, now that we have processed the initializer.  It
10091     is important to do this before calling cp_finish_decl because it
10092     makes decisions about whether to create DECL_STMTs or not based
10093     on the current scope.  */
10094  if (pop_p)
10095    pop_scope (scope);
10096
10097  /* For an in-class declaration, use `grokfield' to create the
10098     declaration.  */
10099  if (member_p)
10100    {
10101      decl = grokfield (declarator, decl_specifiers,
10102			initializer, /*asmspec=*/NULL_TREE,
10103			/*attributes=*/NULL_TREE);
10104      if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10105	cp_parser_save_default_args (parser, decl);
10106    }
10107
10108  /* Finish processing the declaration.  But, skip friend
10109     declarations.  */
10110  if (!friend_p && decl)
10111    cp_finish_decl (decl,
10112		    initializer,
10113		    asm_specification,
10114		    /* If the initializer is in parentheses, then this is
10115		       a direct-initialization, which means that an
10116		       `explicit' constructor is OK.  Otherwise, an
10117		       `explicit' constructor cannot be used.  */
10118		    ((is_parenthesized_init || !is_initialized)
10119		     ? 0 : LOOKUP_ONLYCONVERTING));
10120
10121  /* Remember whether or not variables were initialized by
10122     constant-expressions.  */
10123  if (decl && TREE_CODE (decl) == VAR_DECL
10124      && is_initialized && !is_non_constant_init)
10125    DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10126
10127  return decl;
10128}
10129
10130/* Parse a declarator.
10131
10132   declarator:
10133     direct-declarator
10134     ptr-operator declarator
10135
10136   abstract-declarator:
10137     ptr-operator abstract-declarator [opt]
10138     direct-abstract-declarator
10139
10140   GNU Extensions:
10141
10142   declarator:
10143     attributes [opt] direct-declarator
10144     attributes [opt] ptr-operator declarator
10145
10146   abstract-declarator:
10147     attributes [opt] ptr-operator abstract-declarator [opt]
10148     attributes [opt] direct-abstract-declarator
10149
10150   Returns a representation of the declarator.  If the declarator has
10151   the form `* declarator', then an INDIRECT_REF is returned, whose
10152   only operand is the sub-declarator.  Analogously, `& declarator' is
10153   represented as an ADDR_EXPR.  For `X::* declarator', a SCOPE_REF is
10154   used.  The first operand is the TYPE for `X'.  The second operand
10155   is an INDIRECT_REF whose operand is the sub-declarator.
10156
10157   Otherwise, the representation is as for a direct-declarator.
10158
10159   (It would be better to define a structure type to represent
10160   declarators, rather than abusing `tree' nodes to represent
10161   declarators.  That would be much clearer and save some memory.
10162   There is no reason for declarators to be garbage-collected, for
10163   example; they are created during parser and no longer needed after
10164   `grokdeclarator' has been called.)
10165
10166   For a ptr-operator that has the optional cv-qualifier-seq,
10167   cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10168   node.
10169
10170   If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10171   detect constructor, destructor or conversion operators. It is set
10172   to -1 if the declarator is a name, and +1 if it is a
10173   function. Otherwise it is set to zero. Usually you just want to
10174   test for >0, but internally the negative value is used.
10175
10176   (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10177   a decl-specifier-seq unless it declares a constructor, destructor,
10178   or conversion.  It might seem that we could check this condition in
10179   semantic analysis, rather than parsing, but that makes it difficult
10180   to handle something like `f()'.  We want to notice that there are
10181   no decl-specifiers, and therefore realize that this is an
10182   expression, not a declaration.)
10183
10184   If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10185   the declarator is a direct-declarator of the form "(...)".  */
10186
10187static tree
10188cp_parser_declarator (cp_parser* parser,
10189                      cp_parser_declarator_kind dcl_kind,
10190                      int* ctor_dtor_or_conv_p,
10191		      bool* parenthesized_p)
10192{
10193  cp_token *token;
10194  tree declarator;
10195  enum tree_code code;
10196  tree cv_qualifier_seq;
10197  tree class_type;
10198  tree attributes = NULL_TREE;
10199
10200  /* Assume this is not a constructor, destructor, or type-conversion
10201     operator.  */
10202  if (ctor_dtor_or_conv_p)
10203    *ctor_dtor_or_conv_p = 0;
10204
10205  if (cp_parser_allow_gnu_extensions_p (parser))
10206    attributes = cp_parser_attributes_opt (parser);
10207
10208  /* Peek at the next token.  */
10209  token = cp_lexer_peek_token (parser->lexer);
10210
10211  /* Check for the ptr-operator production.  */
10212  cp_parser_parse_tentatively (parser);
10213  /* Parse the ptr-operator.  */
10214  code = cp_parser_ptr_operator (parser,
10215				 &class_type,
10216				 &cv_qualifier_seq);
10217  /* If that worked, then we have a ptr-operator.  */
10218  if (cp_parser_parse_definitely (parser))
10219    {
10220      /* If a ptr-operator was found, then this declarator was not
10221	 parenthesized.  */
10222      if (parenthesized_p)
10223	*parenthesized_p = true;
10224      /* The dependent declarator is optional if we are parsing an
10225	 abstract-declarator.  */
10226      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10227	cp_parser_parse_tentatively (parser);
10228
10229      /* Parse the dependent declarator.  */
10230      declarator = cp_parser_declarator (parser, dcl_kind,
10231					 /*ctor_dtor_or_conv_p=*/NULL,
10232					 /*parenthesized_p=*/NULL);
10233
10234      /* If we are parsing an abstract-declarator, we must handle the
10235	 case where the dependent declarator is absent.  */
10236      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10237	  && !cp_parser_parse_definitely (parser))
10238	declarator = NULL_TREE;
10239
10240      /* Build the representation of the ptr-operator.  */
10241      if (code == INDIRECT_REF)
10242	declarator = make_pointer_declarator (cv_qualifier_seq,
10243					      declarator);
10244      else
10245	declarator = make_reference_declarator (cv_qualifier_seq,
10246						declarator);
10247      /* Handle the pointer-to-member case.  */
10248      if (class_type)
10249	declarator = build_nt (SCOPE_REF, class_type, declarator);
10250    }
10251  /* Everything else is a direct-declarator.  */
10252  else
10253    {
10254      if (parenthesized_p)
10255	*parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10256						   CPP_OPEN_PAREN);
10257      declarator = cp_parser_direct_declarator (parser, dcl_kind,
10258						ctor_dtor_or_conv_p);
10259    }
10260
10261  if (attributes && declarator != error_mark_node)
10262    declarator = tree_cons (attributes, declarator, NULL_TREE);
10263
10264  return declarator;
10265}
10266
10267/* Parse a direct-declarator or direct-abstract-declarator.
10268
10269   direct-declarator:
10270     declarator-id
10271     direct-declarator ( parameter-declaration-clause )
10272       cv-qualifier-seq [opt]
10273       exception-specification [opt]
10274     direct-declarator [ constant-expression [opt] ]
10275     ( declarator )
10276
10277   direct-abstract-declarator:
10278     direct-abstract-declarator [opt]
10279       ( parameter-declaration-clause )
10280       cv-qualifier-seq [opt]
10281       exception-specification [opt]
10282     direct-abstract-declarator [opt] [ constant-expression [opt] ]
10283     ( abstract-declarator )
10284
10285   Returns a representation of the declarator.  DCL_KIND is
10286   CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10287   direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10288   we are parsing a direct-declarator.  It is
10289   CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10290   of ambiguity we prefer an abstract declarator, as per
10291   [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
10292   cp_parser_declarator.
10293
10294   For the declarator-id production, the representation is as for an
10295   id-expression, except that a qualified name is represented as a
10296   SCOPE_REF.  A function-declarator is represented as a CALL_EXPR;
10297   see the documentation of the FUNCTION_DECLARATOR_* macros for
10298   information about how to find the various declarator components.
10299   An array-declarator is represented as an ARRAY_REF.  The
10300   direct-declarator is the first operand; the constant-expression
10301   indicating the size of the array is the second operand.  */
10302
10303static tree
10304cp_parser_direct_declarator (cp_parser* parser,
10305                             cp_parser_declarator_kind dcl_kind,
10306                             int* ctor_dtor_or_conv_p)
10307{
10308  cp_token *token;
10309  tree declarator = NULL_TREE;
10310  tree scope = NULL_TREE;
10311  bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10312  bool saved_in_declarator_p = parser->in_declarator_p;
10313  bool first = true;
10314  bool pop_p = false;
10315
10316  while (true)
10317    {
10318      /* Peek at the next token.  */
10319      token = cp_lexer_peek_token (parser->lexer);
10320      if (token->type == CPP_OPEN_PAREN)
10321	{
10322	  /* This is either a parameter-declaration-clause, or a
10323  	     parenthesized declarator. When we know we are parsing a
10324  	     named declarator, it must be a parenthesized declarator
10325  	     if FIRST is true. For instance, `(int)' is a
10326  	     parameter-declaration-clause, with an omitted
10327  	     direct-abstract-declarator. But `((*))', is a
10328  	     parenthesized abstract declarator. Finally, when T is a
10329  	     template parameter `(T)' is a
10330  	     parameter-declaration-clause, and not a parenthesized
10331  	     named declarator.
10332
10333	     We first try and parse a parameter-declaration-clause,
10334	     and then try a nested declarator (if FIRST is true).
10335
10336	     It is not an error for it not to be a
10337	     parameter-declaration-clause, even when FIRST is
10338	     false. Consider,
10339
10340	       int i (int);
10341	       int i (3);
10342
10343	     The first is the declaration of a function while the
10344	     second is a the definition of a variable, including its
10345	     initializer.
10346
10347	     Having seen only the parenthesis, we cannot know which of
10348	     these two alternatives should be selected.  Even more
10349	     complex are examples like:
10350
10351               int i (int (a));
10352	       int i (int (3));
10353
10354	     The former is a function-declaration; the latter is a
10355	     variable initialization.
10356
10357	     Thus again, we try a parameter-declaration-clause, and if
10358	     that fails, we back out and return.  */
10359
10360	  if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10361	    {
10362	      tree params;
10363	      unsigned saved_num_template_parameter_lists;
10364
10365	      cp_parser_parse_tentatively (parser);
10366
10367	      /* Consume the `('.  */
10368	      cp_lexer_consume_token (parser->lexer);
10369	      if (first)
10370		{
10371		  /* If this is going to be an abstract declarator, we're
10372		     in a declarator and we can't have default args.  */
10373		  parser->default_arg_ok_p = false;
10374		  parser->in_declarator_p = true;
10375		}
10376
10377	      /* Inside the function parameter list, surrounding
10378		 template-parameter-lists do not apply.  */
10379	      saved_num_template_parameter_lists
10380		= parser->num_template_parameter_lists;
10381	      parser->num_template_parameter_lists = 0;
10382
10383	      /* Parse the parameter-declaration-clause.  */
10384	      params = cp_parser_parameter_declaration_clause (parser);
10385
10386	      parser->num_template_parameter_lists
10387		= saved_num_template_parameter_lists;
10388
10389	      /* If all went well, parse the cv-qualifier-seq and the
10390	     	 exception-specification.  */
10391	      if (cp_parser_parse_definitely (parser))
10392		{
10393		  tree cv_qualifiers;
10394		  tree exception_specification;
10395
10396		  if (ctor_dtor_or_conv_p)
10397		    *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10398		  first = false;
10399		  /* Consume the `)'.  */
10400		  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10401
10402		  /* Parse the cv-qualifier-seq.  */
10403		  cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10404		  /* And the exception-specification.  */
10405		  exception_specification
10406		    = cp_parser_exception_specification_opt (parser);
10407
10408		  /* Create the function-declarator.  */
10409		  declarator = make_call_declarator (declarator,
10410						     params,
10411						     cv_qualifiers,
10412						     exception_specification);
10413		  /* Any subsequent parameter lists are to do with
10414	 	     return type, so are not those of the declared
10415	 	     function.  */
10416		  parser->default_arg_ok_p = false;
10417
10418		  /* Repeat the main loop.  */
10419		  continue;
10420		}
10421	    }
10422
10423	  /* If this is the first, we can try a parenthesized
10424	     declarator.  */
10425	  if (first)
10426	    {
10427	      bool saved_in_type_id_in_expr_p;
10428
10429	      parser->default_arg_ok_p = saved_default_arg_ok_p;
10430	      parser->in_declarator_p = saved_in_declarator_p;
10431
10432	      /* Consume the `('.  */
10433	      cp_lexer_consume_token (parser->lexer);
10434	      /* Parse the nested declarator.  */
10435	      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10436	      parser->in_type_id_in_expr_p = true;
10437	      declarator
10438		= cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10439					/*parenthesized_p=*/NULL);
10440	      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10441	      first = false;
10442	      /* Expect a `)'.  */
10443	      if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10444		declarator = error_mark_node;
10445	      if (declarator == error_mark_node)
10446		break;
10447
10448	      goto handle_declarator;
10449	    }
10450	  /* Otherwise, we must be done.  */
10451	  else
10452	    break;
10453	}
10454      else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10455	       && token->type == CPP_OPEN_SQUARE)
10456	{
10457	  /* Parse an array-declarator.  */
10458	  tree bounds;
10459
10460	  if (ctor_dtor_or_conv_p)
10461	    *ctor_dtor_or_conv_p = 0;
10462
10463	  first = false;
10464	  parser->default_arg_ok_p = false;
10465	  parser->in_declarator_p = true;
10466	  /* Consume the `['.  */
10467	  cp_lexer_consume_token (parser->lexer);
10468	  /* Peek at the next token.  */
10469	  token = cp_lexer_peek_token (parser->lexer);
10470	  /* If the next token is `]', then there is no
10471	     constant-expression.  */
10472	  if (token->type != CPP_CLOSE_SQUARE)
10473	    {
10474	      bool non_constant_p;
10475
10476	      bounds
10477		= cp_parser_constant_expression (parser,
10478						 /*allow_non_constant=*/true,
10479						 &non_constant_p);
10480	      if (!non_constant_p)
10481		bounds = fold_non_dependent_expr (bounds);
10482	    }
10483	  else
10484	    bounds = NULL_TREE;
10485	  /* Look for the closing `]'.  */
10486	  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10487	    {
10488	      declarator = error_mark_node;
10489	      break;
10490	    }
10491
10492	  declarator = build_nt (ARRAY_REF, declarator, bounds);
10493	}
10494      else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10495	{
10496	  /* Parse a declarator-id */
10497	  if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10498	    cp_parser_parse_tentatively (parser);
10499	  declarator = cp_parser_declarator_id (parser);
10500	  if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10501	    {
10502	      if (!cp_parser_parse_definitely (parser))
10503		declarator = error_mark_node;
10504	      else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10505		{
10506		  cp_parser_error (parser, "expected unqualified-id");
10507		  declarator = error_mark_node;
10508		}
10509	    }
10510
10511	  if (declarator == error_mark_node)
10512	    break;
10513
10514	  if (TREE_CODE (declarator) == SCOPE_REF
10515	      && !current_scope ())
10516	    {
10517	      tree scope = TREE_OPERAND (declarator, 0);
10518
10519	      /* In the declaration of a member of a template class
10520	     	 outside of the class itself, the SCOPE will sometimes
10521	     	 be a TYPENAME_TYPE.  For example, given:
10522
10523               	 template <typename T>
10524	       	 int S<T>::R::i = 3;
10525
10526             	 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
10527             	 this context, we must resolve S<T>::R to an ordinary
10528             	 type, rather than a typename type.
10529
10530	     	 The reason we normally avoid resolving TYPENAME_TYPEs
10531	     	 is that a specialization of `S' might render
10532	     	 `S<T>::R' not a type.  However, if `S' is
10533	     	 specialized, then this `i' will not be used, so there
10534	     	 is no harm in resolving the types here.  */
10535	      if (TREE_CODE (scope) == TYPENAME_TYPE)
10536		{
10537		  tree type;
10538
10539		  /* Resolve the TYPENAME_TYPE.  */
10540		  type = resolve_typename_type (scope,
10541						/*only_current_p=*/false);
10542		  /* If that failed, the declarator is invalid.  */
10543		  if (type == error_mark_node)
10544		    error ("`%T::%D' is not a type",
10545			   TYPE_CONTEXT (scope),
10546			   TYPE_IDENTIFIER (scope));
10547		  /* Build a new DECLARATOR.  */
10548		  declarator = build_nt (SCOPE_REF,
10549					 type,
10550					 TREE_OPERAND (declarator, 1));
10551		}
10552	    }
10553
10554	  /* Check to see whether the declarator-id names a constructor,
10555	     destructor, or conversion.  */
10556	  if (declarator && ctor_dtor_or_conv_p
10557	      && ((TREE_CODE (declarator) == SCOPE_REF
10558		   && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10559		  || (TREE_CODE (declarator) != SCOPE_REF
10560		      && at_class_scope_p ())))
10561	    {
10562	      tree unqualified_name;
10563	      tree class_type;
10564
10565	      /* Get the unqualified part of the name.  */
10566	      if (TREE_CODE (declarator) == SCOPE_REF)
10567		{
10568		  class_type = TREE_OPERAND (declarator, 0);
10569		  unqualified_name = TREE_OPERAND (declarator, 1);
10570		}
10571	      else
10572		{
10573		  class_type = current_class_type;
10574		  unqualified_name = declarator;
10575		}
10576
10577	      /* See if it names ctor, dtor or conv.  */
10578	      if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10579		  || IDENTIFIER_TYPENAME_P (unqualified_name)
10580		  || constructor_name_p (unqualified_name, class_type)
10581		  || (TREE_CODE (unqualified_name) == TYPE_DECL
10582		      && same_type_p (TREE_TYPE (unqualified_name),
10583				      class_type)))
10584		*ctor_dtor_or_conv_p = -1;
10585	    }
10586
10587	handle_declarator:;
10588	  scope = get_scope_of_declarator (declarator);
10589	  if (scope)
10590	    /* Any names that appear after the declarator-id for a
10591	       member are looked up in the containing scope.  */
10592	    pop_p = push_scope (scope);
10593	  parser->in_declarator_p = true;
10594	  if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10595	      || (declarator
10596		  && (TREE_CODE (declarator) == SCOPE_REF
10597		      || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10598	    /* Default args are only allowed on function
10599	       declarations.  */
10600	    parser->default_arg_ok_p = saved_default_arg_ok_p;
10601	  else
10602	    parser->default_arg_ok_p = false;
10603
10604	  first = false;
10605	}
10606      /* We're done.  */
10607      else
10608	break;
10609    }
10610
10611  /* For an abstract declarator, we might wind up with nothing at this
10612     point.  That's an error; the declarator is not optional.  */
10613  if (!declarator)
10614    cp_parser_error (parser, "expected declarator");
10615
10616  /* If we entered a scope, we must exit it now.  */
10617  if (pop_p)
10618    pop_scope (scope);
10619
10620  parser->default_arg_ok_p = saved_default_arg_ok_p;
10621  parser->in_declarator_p = saved_in_declarator_p;
10622
10623  return declarator;
10624}
10625
10626/* Parse a ptr-operator.
10627
10628   ptr-operator:
10629     * cv-qualifier-seq [opt]
10630     &
10631     :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10632
10633   GNU Extension:
10634
10635   ptr-operator:
10636     & cv-qualifier-seq [opt]
10637
10638   Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10639   used.  Returns ADDR_EXPR if a reference was used.  In the
10640   case of a pointer-to-member, *TYPE is filled in with the
10641   TYPE containing the member.  *CV_QUALIFIER_SEQ is filled in
10642   with the cv-qualifier-seq, or NULL_TREE, if there are no
10643   cv-qualifiers.  Returns ERROR_MARK if an error occurred.  */
10644
10645static enum tree_code
10646cp_parser_ptr_operator (cp_parser* parser,
10647                        tree* type,
10648                        tree* cv_qualifier_seq)
10649{
10650  enum tree_code code = ERROR_MARK;
10651  cp_token *token;
10652
10653  /* Assume that it's not a pointer-to-member.  */
10654  *type = NULL_TREE;
10655  /* And that there are no cv-qualifiers.  */
10656  *cv_qualifier_seq = NULL_TREE;
10657
10658  /* Peek at the next token.  */
10659  token = cp_lexer_peek_token (parser->lexer);
10660  /* If it's a `*' or `&' we have a pointer or reference.  */
10661  if (token->type == CPP_MULT || token->type == CPP_AND)
10662    {
10663      /* Remember which ptr-operator we were processing.  */
10664      code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10665
10666      /* Consume the `*' or `&'.  */
10667      cp_lexer_consume_token (parser->lexer);
10668
10669      /* A `*' can be followed by a cv-qualifier-seq, and so can a
10670	 `&', if we are allowing GNU extensions.  (The only qualifier
10671	 that can legally appear after `&' is `restrict', but that is
10672	 enforced during semantic analysis.  */
10673      if (code == INDIRECT_REF
10674	  || cp_parser_allow_gnu_extensions_p (parser))
10675	*cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10676    }
10677  else
10678    {
10679      /* Try the pointer-to-member case.  */
10680      cp_parser_parse_tentatively (parser);
10681      /* Look for the optional `::' operator.  */
10682      cp_parser_global_scope_opt (parser,
10683				  /*current_scope_valid_p=*/false);
10684      /* Look for the nested-name specifier.  */
10685      cp_parser_nested_name_specifier (parser,
10686				       /*typename_keyword_p=*/false,
10687				       /*check_dependency_p=*/true,
10688				       /*type_p=*/false,
10689				       /*is_declaration=*/false);
10690      /* If we found it, and the next token is a `*', then we are
10691	 indeed looking at a pointer-to-member operator.  */
10692      if (!cp_parser_error_occurred (parser)
10693	  && cp_parser_require (parser, CPP_MULT, "`*'"))
10694	{
10695	  /* The type of which the member is a member is given by the
10696	     current SCOPE.  */
10697	  *type = parser->scope;
10698	  /* The next name will not be qualified.  */
10699	  parser->scope = NULL_TREE;
10700	  parser->qualifying_scope = NULL_TREE;
10701	  parser->object_scope = NULL_TREE;
10702	  /* Indicate that the `*' operator was used.  */
10703	  code = INDIRECT_REF;
10704	  /* Look for the optional cv-qualifier-seq.  */
10705	  *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10706	}
10707      /* If that didn't work we don't have a ptr-operator.  */
10708      if (!cp_parser_parse_definitely (parser))
10709	cp_parser_error (parser, "expected ptr-operator");
10710    }
10711
10712  return code;
10713}
10714
10715/* Parse an (optional) cv-qualifier-seq.
10716
10717   cv-qualifier-seq:
10718     cv-qualifier cv-qualifier-seq [opt]
10719
10720   Returns a TREE_LIST.  The TREE_VALUE of each node is the
10721   representation of a cv-qualifier.  */
10722
10723static tree
10724cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10725{
10726  tree cv_qualifiers = NULL_TREE;
10727
10728  while (true)
10729    {
10730      tree cv_qualifier;
10731
10732      /* Look for the next cv-qualifier.  */
10733      cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10734      /* If we didn't find one, we're done.  */
10735      if (!cv_qualifier)
10736	break;
10737
10738      /* Add this cv-qualifier to the list.  */
10739      cv_qualifiers
10740	= tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10741    }
10742
10743  /* We built up the list in reverse order.  */
10744  return nreverse (cv_qualifiers);
10745}
10746
10747/* Parse an (optional) cv-qualifier.
10748
10749   cv-qualifier:
10750     const
10751     volatile
10752
10753   GNU Extension:
10754
10755   cv-qualifier:
10756     __restrict__ */
10757
10758static tree
10759cp_parser_cv_qualifier_opt (cp_parser* parser)
10760{
10761  cp_token *token;
10762  tree cv_qualifier = NULL_TREE;
10763
10764  /* Peek at the next token.  */
10765  token = cp_lexer_peek_token (parser->lexer);
10766  /* See if it's a cv-qualifier.  */
10767  switch (token->keyword)
10768    {
10769    case RID_CONST:
10770    case RID_VOLATILE:
10771    case RID_RESTRICT:
10772      /* Save the value of the token.  */
10773      cv_qualifier = token->value;
10774      /* Consume the token.  */
10775      cp_lexer_consume_token (parser->lexer);
10776      break;
10777
10778    default:
10779      break;
10780    }
10781
10782  return cv_qualifier;
10783}
10784
10785/* Parse a declarator-id.
10786
10787   declarator-id:
10788     id-expression
10789     :: [opt] nested-name-specifier [opt] type-name
10790
10791   In the `id-expression' case, the value returned is as for
10792   cp_parser_id_expression if the id-expression was an unqualified-id.
10793   If the id-expression was a qualified-id, then a SCOPE_REF is
10794   returned.  The first operand is the scope (either a NAMESPACE_DECL
10795   or TREE_TYPE), but the second is still just a representation of an
10796   unqualified-id.  */
10797
10798static tree
10799cp_parser_declarator_id (cp_parser* parser)
10800{
10801  tree id_expression;
10802
10803  /* The expression must be an id-expression.  Assume that qualified
10804     names are the names of types so that:
10805
10806       template <class T>
10807       int S<T>::R::i = 3;
10808
10809     will work; we must treat `S<T>::R' as the name of a type.
10810     Similarly, assume that qualified names are templates, where
10811     required, so that:
10812
10813       template <class T>
10814       int S<T>::R<T>::i = 3;
10815
10816     will work, too.  */
10817  id_expression = cp_parser_id_expression (parser,
10818					   /*template_keyword_p=*/false,
10819					   /*check_dependency_p=*/false,
10820					   /*template_p=*/NULL,
10821					   /*declarator_p=*/true);
10822  /* If the name was qualified, create a SCOPE_REF to represent
10823     that.  */
10824  if (parser->scope)
10825    {
10826      id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10827      parser->scope = NULL_TREE;
10828    }
10829
10830  return id_expression;
10831}
10832
10833/* Parse a type-id.
10834
10835   type-id:
10836     type-specifier-seq abstract-declarator [opt]
10837
10838   Returns the TYPE specified.  */
10839
10840static tree
10841cp_parser_type_id (cp_parser* parser)
10842{
10843  tree type_specifier_seq;
10844  tree abstract_declarator;
10845
10846  /* Parse the type-specifier-seq.  */
10847  type_specifier_seq
10848    = cp_parser_type_specifier_seq (parser);
10849  if (type_specifier_seq == error_mark_node)
10850    return error_mark_node;
10851
10852  /* There might or might not be an abstract declarator.  */
10853  cp_parser_parse_tentatively (parser);
10854  /* Look for the declarator.  */
10855  abstract_declarator
10856    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10857			    /*parenthesized_p=*/NULL);
10858  /* Check to see if there really was a declarator.  */
10859  if (!cp_parser_parse_definitely (parser))
10860    abstract_declarator = NULL_TREE;
10861
10862  return groktypename (build_tree_list (type_specifier_seq,
10863					abstract_declarator));
10864}
10865
10866/* Parse a type-specifier-seq.
10867
10868   type-specifier-seq:
10869     type-specifier type-specifier-seq [opt]
10870
10871   GNU extension:
10872
10873   type-specifier-seq:
10874     attributes type-specifier-seq [opt]
10875
10876   Returns a TREE_LIST.  Either the TREE_VALUE of each node is a
10877   type-specifier, or the TREE_PURPOSE is a list of attributes.  */
10878
10879static tree
10880cp_parser_type_specifier_seq (cp_parser* parser)
10881{
10882  bool seen_type_specifier = false;
10883  tree type_specifier_seq = NULL_TREE;
10884
10885  /* Parse the type-specifiers and attributes.  */
10886  while (true)
10887    {
10888      tree type_specifier;
10889
10890      /* Check for attributes first.  */
10891      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10892	{
10893	  type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10894					  NULL_TREE,
10895					  type_specifier_seq);
10896	  continue;
10897	}
10898
10899      /* After the first type-specifier, others are optional.  */
10900      if (seen_type_specifier)
10901	cp_parser_parse_tentatively (parser);
10902      /* Look for the type-specifier.  */
10903      type_specifier = cp_parser_type_specifier (parser,
10904						 CP_PARSER_FLAGS_NONE,
10905						 /*is_friend=*/false,
10906						 /*is_declaration=*/false,
10907						 NULL,
10908						 NULL);
10909      /* If the first type-specifier could not be found, this is not a
10910	 type-specifier-seq at all.  */
10911      if (!seen_type_specifier && type_specifier == error_mark_node)
10912	return error_mark_node;
10913      /* If subsequent type-specifiers could not be found, the
10914	 type-specifier-seq is complete.  */
10915      else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10916	break;
10917
10918      /* Add the new type-specifier to the list.  */
10919      type_specifier_seq
10920	= tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10921      seen_type_specifier = true;
10922    }
10923
10924  /* We built up the list in reverse order.  */
10925  return nreverse (type_specifier_seq);
10926}
10927
10928/* Parse a parameter-declaration-clause.
10929
10930   parameter-declaration-clause:
10931     parameter-declaration-list [opt] ... [opt]
10932     parameter-declaration-list , ...
10933
10934   Returns a representation for the parameter declarations.  Each node
10935   is a TREE_LIST.  (See cp_parser_parameter_declaration for the exact
10936   representation.)  If the parameter-declaration-clause ends with an
10937   ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10938   list.  A return value of NULL_TREE indicates a
10939   parameter-declaration-clause consisting only of an ellipsis.  */
10940
10941static tree
10942cp_parser_parameter_declaration_clause (cp_parser* parser)
10943{
10944  tree parameters;
10945  cp_token *token;
10946  bool ellipsis_p;
10947
10948  /* Peek at the next token.  */
10949  token = cp_lexer_peek_token (parser->lexer);
10950  /* Check for trivial parameter-declaration-clauses.  */
10951  if (token->type == CPP_ELLIPSIS)
10952    {
10953      /* Consume the `...' token.  */
10954      cp_lexer_consume_token (parser->lexer);
10955      return NULL_TREE;
10956    }
10957  else if (token->type == CPP_CLOSE_PAREN)
10958    /* There are no parameters.  */
10959    {
10960#ifndef NO_IMPLICIT_EXTERN_C
10961      if (in_system_header && current_class_type == NULL
10962	  && current_lang_name == lang_name_c)
10963	return NULL_TREE;
10964      else
10965#endif
10966	return void_list_node;
10967    }
10968  /* Check for `(void)', too, which is a special case.  */
10969  else if (token->keyword == RID_VOID
10970	   && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10971	       == CPP_CLOSE_PAREN))
10972    {
10973      /* Consume the `void' token.  */
10974      cp_lexer_consume_token (parser->lexer);
10975      /* There are no parameters.  */
10976      return void_list_node;
10977    }
10978
10979  /* Parse the parameter-declaration-list.  */
10980  parameters = cp_parser_parameter_declaration_list (parser);
10981  /* If a parse error occurred while parsing the
10982     parameter-declaration-list, then the entire
10983     parameter-declaration-clause is erroneous.  */
10984  if (parameters == error_mark_node)
10985    return error_mark_node;
10986
10987  /* Peek at the next token.  */
10988  token = cp_lexer_peek_token (parser->lexer);
10989  /* If it's a `,', the clause should terminate with an ellipsis.  */
10990  if (token->type == CPP_COMMA)
10991    {
10992      /* Consume the `,'.  */
10993      cp_lexer_consume_token (parser->lexer);
10994      /* Expect an ellipsis.  */
10995      ellipsis_p
10996	= (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10997    }
10998  /* It might also be `...' if the optional trailing `,' was
10999     omitted.  */
11000  else if (token->type == CPP_ELLIPSIS)
11001    {
11002      /* Consume the `...' token.  */
11003      cp_lexer_consume_token (parser->lexer);
11004      /* And remember that we saw it.  */
11005      ellipsis_p = true;
11006    }
11007  else
11008    ellipsis_p = false;
11009
11010  /* Finish the parameter list.  */
11011  return finish_parmlist (parameters, ellipsis_p);
11012}
11013
11014/* Parse a parameter-declaration-list.
11015
11016   parameter-declaration-list:
11017     parameter-declaration
11018     parameter-declaration-list , parameter-declaration
11019
11020   Returns a representation of the parameter-declaration-list, as for
11021   cp_parser_parameter_declaration_clause.  However, the
11022   `void_list_node' is never appended to the list.  */
11023
11024static tree
11025cp_parser_parameter_declaration_list (cp_parser* parser)
11026{
11027  tree parameters = NULL_TREE;
11028
11029  /* Look for more parameters.  */
11030  while (true)
11031    {
11032      tree parameter;
11033      bool parenthesized_p;
11034      /* Parse the parameter.  */
11035      parameter
11036	= cp_parser_parameter_declaration (parser,
11037					   /*template_parm_p=*/false,
11038					   &parenthesized_p);
11039
11040      /* If a parse error occurred parsing the parameter declaration,
11041	 then the entire parameter-declaration-list is erroneous.  */
11042      if (parameter == error_mark_node)
11043	{
11044	  parameters = error_mark_node;
11045	  break;
11046	}
11047      /* Add the new parameter to the list.  */
11048      TREE_CHAIN (parameter) = parameters;
11049      parameters = parameter;
11050
11051      /* Peek at the next token.  */
11052      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11053	  || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11054	/* The parameter-declaration-list is complete.  */
11055	break;
11056      else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11057	{
11058	  cp_token *token;
11059
11060	  /* Peek at the next token.  */
11061	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
11062	  /* If it's an ellipsis, then the list is complete.  */
11063	  if (token->type == CPP_ELLIPSIS)
11064	    break;
11065	  /* Otherwise, there must be more parameters.  Consume the
11066	     `,'.  */
11067	  cp_lexer_consume_token (parser->lexer);
11068	  /* When parsing something like:
11069
11070	        int i(float f, double d)
11071
11072             we can tell after seeing the declaration for "f" that we
11073	     are not looking at an initialization of a variable "i",
11074	     but rather at the declaration of a function "i".
11075
11076	     Due to the fact that the parsing of template arguments
11077	     (as specified to a template-id) requires backtracking we
11078	     cannot use this technique when inside a template argument
11079	     list.  */
11080	  if (!parser->in_template_argument_list_p
11081	      && !parser->in_type_id_in_expr_p
11082	      && cp_parser_parsing_tentatively (parser)
11083	      && !cp_parser_committed_to_tentative_parse (parser)
11084	      /* However, a parameter-declaration of the form
11085		 "foat(f)" (which is a valid declaration of a
11086		 parameter "f") can also be interpreted as an
11087		 expression (the conversion of "f" to "float").  */
11088	      && !parenthesized_p)
11089	    cp_parser_commit_to_tentative_parse (parser);
11090	}
11091      else
11092	{
11093	  cp_parser_error (parser, "expected `,' or `...'");
11094	  if (!cp_parser_parsing_tentatively (parser)
11095	      || cp_parser_committed_to_tentative_parse (parser))
11096	    cp_parser_skip_to_closing_parenthesis (parser,
11097						   /*recovering=*/true,
11098						   /*or_comma=*/false,
11099						   /*consume_paren=*/false);
11100	  break;
11101	}
11102    }
11103
11104  /* We built up the list in reverse order; straighten it out now.  */
11105  return nreverse (parameters);
11106}
11107
11108/* Parse a parameter declaration.
11109
11110   parameter-declaration:
11111     decl-specifier-seq declarator
11112     decl-specifier-seq declarator = assignment-expression
11113     decl-specifier-seq abstract-declarator [opt]
11114     decl-specifier-seq abstract-declarator [opt] = assignment-expression
11115
11116   If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11117   declares a template parameter.  (In that case, a non-nested `>'
11118   token encountered during the parsing of the assignment-expression
11119   is not interpreted as a greater-than operator.)
11120
11121   Returns a TREE_LIST representing the parameter-declaration.  The
11122   TREE_PURPOSE is the default argument expression, or NULL_TREE if
11123   there is no default argument.  The TREE_VALUE is a representation
11124   of the decl-specifier-seq and declarator.  In particular, the
11125   TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11126   decl-specifier-seq and whose TREE_VALUE represents the declarator.
11127   If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11128   the declarator is of the form "(p)".  */
11129
11130static tree
11131cp_parser_parameter_declaration (cp_parser *parser,
11132				 bool template_parm_p,
11133				 bool *parenthesized_p)
11134{
11135  int declares_class_or_enum;
11136  bool greater_than_is_operator_p;
11137  tree decl_specifiers;
11138  tree attributes;
11139  tree declarator;
11140  tree default_argument;
11141  tree parameter;
11142  cp_token *token;
11143  const char *saved_message;
11144
11145  /* In a template parameter, `>' is not an operator.
11146
11147     [temp.param]
11148
11149     When parsing a default template-argument for a non-type
11150     template-parameter, the first non-nested `>' is taken as the end
11151     of the template parameter-list rather than a greater-than
11152     operator.  */
11153  greater_than_is_operator_p = !template_parm_p;
11154
11155  /* Type definitions may not appear in parameter types.  */
11156  saved_message = parser->type_definition_forbidden_message;
11157  parser->type_definition_forbidden_message
11158    = "types may not be defined in parameter types";
11159
11160  /* Parse the declaration-specifiers.  */
11161  decl_specifiers
11162    = cp_parser_decl_specifier_seq (parser,
11163				    CP_PARSER_FLAGS_NONE,
11164				    &attributes,
11165				    &declares_class_or_enum);
11166  /* If an error occurred, there's no reason to attempt to parse the
11167     rest of the declaration.  */
11168  if (cp_parser_error_occurred (parser))
11169    {
11170      parser->type_definition_forbidden_message = saved_message;
11171      return error_mark_node;
11172    }
11173
11174  /* Peek at the next token.  */
11175  token = cp_lexer_peek_token (parser->lexer);
11176  /* If the next token is a `)', `,', `=', `>', or `...', then there
11177     is no declarator.  */
11178  if (token->type == CPP_CLOSE_PAREN
11179      || token->type == CPP_COMMA
11180      || token->type == CPP_EQ
11181      || token->type == CPP_ELLIPSIS
11182      || token->type == CPP_GREATER)
11183    {
11184      declarator = NULL_TREE;
11185      if (parenthesized_p)
11186	*parenthesized_p = false;
11187    }
11188  /* Otherwise, there should be a declarator.  */
11189  else
11190    {
11191      bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11192      parser->default_arg_ok_p = false;
11193
11194      /* After seeing a decl-specifier-seq, if the next token is not a
11195	 "(", there is no possibility that the code is a valid
11196	 expression.  Therefore, if parsing tentatively, we commit at
11197	 this point.  */
11198      if (!parser->in_template_argument_list_p
11199	  /* In an expression context, having seen:
11200
11201	       (int((char ...
11202
11203	     we cannot be sure whether we are looking at a
11204	     function-type (taking a "char" as a parameter) or a cast
11205	     of some object of type "char" to "int".  */
11206	  && !parser->in_type_id_in_expr_p
11207	  && cp_parser_parsing_tentatively (parser)
11208	  && !cp_parser_committed_to_tentative_parse (parser)
11209	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11210	cp_parser_commit_to_tentative_parse (parser);
11211      /* Parse the declarator.  */
11212      declarator = cp_parser_declarator (parser,
11213					 CP_PARSER_DECLARATOR_EITHER,
11214					 /*ctor_dtor_or_conv_p=*/NULL,
11215					 parenthesized_p);
11216      parser->default_arg_ok_p = saved_default_arg_ok_p;
11217      /* After the declarator, allow more attributes.  */
11218      attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11219    }
11220
11221  /* The restriction on defining new types applies only to the type
11222     of the parameter, not to the default argument.  */
11223  parser->type_definition_forbidden_message = saved_message;
11224
11225  /* If the next token is `=', then process a default argument.  */
11226  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11227    {
11228      bool saved_greater_than_is_operator_p;
11229      /* Consume the `='.  */
11230      cp_lexer_consume_token (parser->lexer);
11231
11232      /* If we are defining a class, then the tokens that make up the
11233	 default argument must be saved and processed later.  */
11234      if (!template_parm_p && at_class_scope_p ()
11235	  && TYPE_BEING_DEFINED (current_class_type))
11236	{
11237	  unsigned depth = 0;
11238
11239	  /* Create a DEFAULT_ARG to represented the unparsed default
11240             argument.  */
11241	  default_argument = make_node (DEFAULT_ARG);
11242	  DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11243
11244	  /* Add tokens until we have processed the entire default
11245	     argument.  */
11246	  while (true)
11247	    {
11248	      bool done = false;
11249	      cp_token *token;
11250
11251	      /* Peek at the next token.  */
11252	      token = cp_lexer_peek_token (parser->lexer);
11253	      /* What we do depends on what token we have.  */
11254	      switch (token->type)
11255		{
11256		  /* In valid code, a default argument must be
11257		     immediately followed by a `,' `)', or `...'.  */
11258		case CPP_COMMA:
11259		case CPP_CLOSE_PAREN:
11260		case CPP_ELLIPSIS:
11261		  /* If we run into a non-nested `;', `}', or `]',
11262		     then the code is invalid -- but the default
11263		     argument is certainly over.  */
11264		case CPP_SEMICOLON:
11265		case CPP_CLOSE_BRACE:
11266		case CPP_CLOSE_SQUARE:
11267		  if (depth == 0)
11268		    done = true;
11269		  /* Update DEPTH, if necessary.  */
11270		  else if (token->type == CPP_CLOSE_PAREN
11271			   || token->type == CPP_CLOSE_BRACE
11272			   || token->type == CPP_CLOSE_SQUARE)
11273		    --depth;
11274		  break;
11275
11276		case CPP_OPEN_PAREN:
11277		case CPP_OPEN_SQUARE:
11278		case CPP_OPEN_BRACE:
11279		  ++depth;
11280		  break;
11281
11282		case CPP_GREATER:
11283		  /* If we see a non-nested `>', and `>' is not an
11284		     operator, then it marks the end of the default
11285		     argument.  */
11286		  if (!depth && !greater_than_is_operator_p)
11287		    done = true;
11288		  break;
11289
11290		  /* If we run out of tokens, issue an error message.  */
11291		case CPP_EOF:
11292		  error ("file ends in default argument");
11293		  done = true;
11294		  break;
11295
11296		case CPP_NAME:
11297		case CPP_SCOPE:
11298		  /* In these cases, we should look for template-ids.
11299		     For example, if the default argument is
11300		     `X<int, double>()', we need to do name lookup to
11301		     figure out whether or not `X' is a template; if
11302		     so, the `,' does not end the default argument.
11303
11304		     That is not yet done.  */
11305		  break;
11306
11307		default:
11308		  break;
11309		}
11310
11311	      /* If we've reached the end, stop.  */
11312	      if (done)
11313		break;
11314
11315	      /* Add the token to the token block.  */
11316	      token = cp_lexer_consume_token (parser->lexer);
11317	      cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11318					 token);
11319	    }
11320	}
11321      /* Outside of a class definition, we can just parse the
11322         assignment-expression.  */
11323      else
11324	{
11325	  bool saved_local_variables_forbidden_p;
11326
11327	  /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11328	     set correctly.  */
11329	  saved_greater_than_is_operator_p
11330	    = parser->greater_than_is_operator_p;
11331	  parser->greater_than_is_operator_p = greater_than_is_operator_p;
11332	  /* Local variable names (and the `this' keyword) may not
11333	     appear in a default argument.  */
11334	  saved_local_variables_forbidden_p
11335	    = parser->local_variables_forbidden_p;
11336	  parser->local_variables_forbidden_p = true;
11337	  /* Parse the assignment-expression.  */
11338	  default_argument = cp_parser_assignment_expression (parser);
11339	  /* Restore saved state.  */
11340	  parser->greater_than_is_operator_p
11341	    = saved_greater_than_is_operator_p;
11342	  parser->local_variables_forbidden_p
11343	    = saved_local_variables_forbidden_p;
11344	}
11345      if (!parser->default_arg_ok_p)
11346	{
11347	  if (!flag_pedantic_errors)
11348	    warning ("deprecated use of default argument for parameter of non-function");
11349	  else
11350	    {
11351	      error ("default arguments are only permitted for function parameters");
11352	      default_argument = NULL_TREE;
11353	    }
11354	}
11355    }
11356  else
11357    default_argument = NULL_TREE;
11358
11359  /* Create the representation of the parameter.  */
11360  if (attributes)
11361    decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11362  parameter = build_tree_list (default_argument,
11363			       build_tree_list (decl_specifiers,
11364						declarator));
11365
11366  return parameter;
11367}
11368
11369/* Parse a function-body.
11370
11371   function-body:
11372     compound_statement  */
11373
11374static void
11375cp_parser_function_body (cp_parser *parser)
11376{
11377  cp_parser_compound_statement (parser, false);
11378}
11379
11380/* Parse a ctor-initializer-opt followed by a function-body.  Return
11381   true if a ctor-initializer was present.  */
11382
11383static bool
11384cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11385{
11386  tree body;
11387  bool ctor_initializer_p;
11388
11389  /* Begin the function body.  */
11390  body = begin_function_body ();
11391  /* Parse the optional ctor-initializer.  */
11392  ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11393  /* Parse the function-body.  */
11394  cp_parser_function_body (parser);
11395  /* Finish the function body.  */
11396  finish_function_body (body);
11397
11398  return ctor_initializer_p;
11399}
11400
11401/* Parse an initializer.
11402
11403   initializer:
11404     = initializer-clause
11405     ( expression-list )
11406
11407   Returns a expression representing the initializer.  If no
11408   initializer is present, NULL_TREE is returned.
11409
11410   *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11411   production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11412   set to FALSE if there is no initializer present.  If there is an
11413   initializer, and it is not a constant-expression, *NON_CONSTANT_P
11414   is set to true; otherwise it is set to false.  */
11415
11416static tree
11417cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11418		       bool* non_constant_p)
11419{
11420  cp_token *token;
11421  tree init;
11422
11423  /* Peek at the next token.  */
11424  token = cp_lexer_peek_token (parser->lexer);
11425
11426  /* Let our caller know whether or not this initializer was
11427     parenthesized.  */
11428  *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11429  /* Assume that the initializer is constant.  */
11430  *non_constant_p = false;
11431
11432  if (token->type == CPP_EQ)
11433    {
11434      /* Consume the `='.  */
11435      cp_lexer_consume_token (parser->lexer);
11436      /* Parse the initializer-clause.  */
11437      init = cp_parser_initializer_clause (parser, non_constant_p);
11438    }
11439  else if (token->type == CPP_OPEN_PAREN)
11440    init = cp_parser_parenthesized_expression_list (parser, false,
11441						    non_constant_p);
11442  else
11443    {
11444      /* Anything else is an error.  */
11445      cp_parser_error (parser, "expected initializer");
11446      init = error_mark_node;
11447    }
11448
11449  return init;
11450}
11451
11452/* Parse an initializer-clause.
11453
11454   initializer-clause:
11455     assignment-expression
11456     { initializer-list , [opt] }
11457     { }
11458
11459   Returns an expression representing the initializer.
11460
11461   If the `assignment-expression' production is used the value
11462   returned is simply a representation for the expression.
11463
11464   Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11465   the elements of the initializer-list (or NULL_TREE, if the last
11466   production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11467   NULL_TREE.  There is no way to detect whether or not the optional
11468   trailing `,' was provided.  NON_CONSTANT_P is as for
11469   cp_parser_initializer.  */
11470
11471static tree
11472cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11473{
11474  tree initializer;
11475
11476  /* If it is not a `{', then we are looking at an
11477     assignment-expression.  */
11478  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11479    {
11480      initializer
11481	= cp_parser_constant_expression (parser,
11482					/*allow_non_constant_p=*/true,
11483					non_constant_p);
11484      if (!*non_constant_p)
11485	initializer = fold_non_dependent_expr (initializer);
11486    }
11487  else
11488    {
11489      /* Consume the `{' token.  */
11490      cp_lexer_consume_token (parser->lexer);
11491      /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11492      initializer = make_node (CONSTRUCTOR);
11493      /* Mark it with TREE_HAS_CONSTRUCTOR.  This should not be
11494	 necessary, but check_initializer depends upon it, for
11495	 now.  */
11496      TREE_HAS_CONSTRUCTOR (initializer) = 1;
11497      /* If it's not a `}', then there is a non-trivial initializer.  */
11498      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11499	{
11500	  /* Parse the initializer list.  */
11501	  CONSTRUCTOR_ELTS (initializer)
11502	    = cp_parser_initializer_list (parser, non_constant_p);
11503	  /* A trailing `,' token is allowed.  */
11504	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11505	    cp_lexer_consume_token (parser->lexer);
11506	}
11507      /* Now, there should be a trailing `}'.  */
11508      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11509    }
11510
11511  return initializer;
11512}
11513
11514/* Parse an initializer-list.
11515
11516   initializer-list:
11517     initializer-clause
11518     initializer-list , initializer-clause
11519
11520   GNU Extension:
11521
11522   initializer-list:
11523     identifier : initializer-clause
11524     initializer-list, identifier : initializer-clause
11525
11526   Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
11527   for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
11528   IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
11529   as for cp_parser_initializer.  */
11530
11531static tree
11532cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11533{
11534  tree initializers = NULL_TREE;
11535
11536  /* Assume all of the expressions are constant.  */
11537  *non_constant_p = false;
11538
11539  /* Parse the rest of the list.  */
11540  while (true)
11541    {
11542      cp_token *token;
11543      tree identifier;
11544      tree initializer;
11545      bool clause_non_constant_p;
11546
11547      /* If the next token is an identifier and the following one is a
11548	 colon, we are looking at the GNU designated-initializer
11549	 syntax.  */
11550      if (cp_parser_allow_gnu_extensions_p (parser)
11551	  && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11552	  && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11553	{
11554	  /* Consume the identifier.  */
11555	  identifier = cp_lexer_consume_token (parser->lexer)->value;
11556	  /* Consume the `:'.  */
11557	  cp_lexer_consume_token (parser->lexer);
11558	}
11559      else
11560	identifier = NULL_TREE;
11561
11562      /* Parse the initializer.  */
11563      initializer = cp_parser_initializer_clause (parser,
11564						  &clause_non_constant_p);
11565      /* If any clause is non-constant, so is the entire initializer.  */
11566      if (clause_non_constant_p)
11567	*non_constant_p = true;
11568      /* Add it to the list.  */
11569      initializers = tree_cons (identifier, initializer, initializers);
11570
11571      /* If the next token is not a comma, we have reached the end of
11572	 the list.  */
11573      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11574	break;
11575
11576      /* Peek at the next token.  */
11577      token = cp_lexer_peek_nth_token (parser->lexer, 2);
11578      /* If the next token is a `}', then we're still done.  An
11579	 initializer-clause can have a trailing `,' after the
11580	 initializer-list and before the closing `}'.  */
11581      if (token->type == CPP_CLOSE_BRACE)
11582	break;
11583
11584      /* Consume the `,' token.  */
11585      cp_lexer_consume_token (parser->lexer);
11586    }
11587
11588  /* The initializers were built up in reverse order, so we need to
11589     reverse them now.  */
11590  return nreverse (initializers);
11591}
11592
11593/* Classes [gram.class] */
11594
11595/* Parse a class-name.
11596
11597   class-name:
11598     identifier
11599     template-id
11600
11601   TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11602   to indicate that names looked up in dependent types should be
11603   assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
11604   keyword has been used to indicate that the name that appears next
11605   is a template.  TYPE_P is true iff the next name should be treated
11606   as class-name, even if it is declared to be some other kind of name
11607   as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11608   dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
11609   being defined in a class-head.
11610
11611   Returns the TYPE_DECL representing the class.  */
11612
11613static tree
11614cp_parser_class_name (cp_parser *parser,
11615		      bool typename_keyword_p,
11616		      bool template_keyword_p,
11617		      bool type_p,
11618		      bool check_dependency_p,
11619		      bool class_head_p,
11620		      bool is_declaration)
11621{
11622  tree decl;
11623  tree scope;
11624  bool typename_p;
11625  cp_token *token;
11626
11627  /* All class-names start with an identifier.  */
11628  token = cp_lexer_peek_token (parser->lexer);
11629  if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11630    {
11631      cp_parser_error (parser, "expected class-name");
11632      return error_mark_node;
11633    }
11634
11635  /* PARSER->SCOPE can be cleared when parsing the template-arguments
11636     to a template-id, so we save it here.  */
11637  scope = parser->scope;
11638  if (scope == error_mark_node)
11639    return error_mark_node;
11640
11641  /* Any name names a type if we're following the `typename' keyword
11642     in a qualified name where the enclosing scope is type-dependent.  */
11643  typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11644		&& dependent_type_p (scope));
11645  /* Handle the common case (an identifier, but not a template-id)
11646     efficiently.  */
11647  if (token->type == CPP_NAME
11648      && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11649    {
11650      tree identifier;
11651
11652      /* Look for the identifier.  */
11653      identifier = cp_parser_identifier (parser);
11654      /* If the next token isn't an identifier, we are certainly not
11655	 looking at a class-name.  */
11656      if (identifier == error_mark_node)
11657	decl = error_mark_node;
11658      /* If we know this is a type-name, there's no need to look it
11659	 up.  */
11660      else if (typename_p)
11661	decl = identifier;
11662      else
11663	{
11664	  /* If the next token is a `::', then the name must be a type
11665	     name.
11666
11667	     [basic.lookup.qual]
11668
11669	     During the lookup for a name preceding the :: scope
11670	     resolution operator, object, function, and enumerator
11671	     names are ignored.  */
11672	  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11673	    type_p = true;
11674	  /* Look up the name.  */
11675	  decl = cp_parser_lookup_name (parser, identifier,
11676					type_p,
11677					/*is_template=*/false,
11678					/*is_namespace=*/false,
11679					check_dependency_p);
11680	}
11681    }
11682  else
11683    {
11684      /* Try a template-id.  */
11685      decl = cp_parser_template_id (parser, template_keyword_p,
11686				    check_dependency_p,
11687				    is_declaration);
11688      if (decl == error_mark_node)
11689	return error_mark_node;
11690    }
11691
11692  decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11693
11694  /* If this is a typename, create a TYPENAME_TYPE.  */
11695  if (typename_p && decl != error_mark_node)
11696    {
11697      decl = make_typename_type (scope, decl, /*complain=*/1);
11698      if (decl != error_mark_node)
11699	decl = TYPE_NAME (decl);
11700    }
11701
11702  /* Check to see that it is really the name of a class.  */
11703  if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11704      && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11705      && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11706    /* Situations like this:
11707
11708	 template <typename T> struct A {
11709	   typename T::template X<int>::I i;
11710	 };
11711
11712       are problematic.  Is `T::template X<int>' a class-name?  The
11713       standard does not seem to be definitive, but there is no other
11714       valid interpretation of the following `::'.  Therefore, those
11715       names are considered class-names.  */
11716    decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11717  else if (decl == error_mark_node
11718	   || TREE_CODE (decl) != TYPE_DECL
11719	   || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11720    {
11721      cp_parser_error (parser, "expected class-name");
11722      return error_mark_node;
11723    }
11724
11725  return decl;
11726}
11727
11728/* Parse a class-specifier.
11729
11730   class-specifier:
11731     class-head { member-specification [opt] }
11732
11733   Returns the TREE_TYPE representing the class.  */
11734
11735static tree
11736cp_parser_class_specifier (cp_parser* parser)
11737{
11738  cp_token *token;
11739  tree type;
11740  tree attributes;
11741  int has_trailing_semicolon;
11742  bool nested_name_specifier_p;
11743  unsigned saved_num_template_parameter_lists;
11744  bool pop_p = false;
11745
11746  push_deferring_access_checks (dk_no_deferred);
11747
11748  /* Parse the class-head.  */
11749  type = cp_parser_class_head (parser,
11750			       &nested_name_specifier_p,
11751			       &attributes);
11752  /* If the class-head was a semantic disaster, skip the entire body
11753     of the class.  */
11754  if (!type)
11755    {
11756      cp_parser_skip_to_end_of_block_or_statement (parser);
11757      pop_deferring_access_checks ();
11758      return error_mark_node;
11759    }
11760
11761  /* Look for the `{'.  */
11762  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11763    {
11764      pop_deferring_access_checks ();
11765      return error_mark_node;
11766    }
11767
11768  /* Issue an error message if type-definitions are forbidden here.  */
11769  cp_parser_check_type_definition (parser);
11770  /* Remember that we are defining one more class.  */
11771  ++parser->num_classes_being_defined;
11772  /* Inside the class, surrounding template-parameter-lists do not
11773     apply.  */
11774  saved_num_template_parameter_lists
11775    = parser->num_template_parameter_lists;
11776  parser->num_template_parameter_lists = 0;
11777
11778  /* Start the class.  */
11779  if (nested_name_specifier_p)
11780    pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11781  type = begin_class_definition (type);
11782  if (type == error_mark_node)
11783    /* If the type is erroneous, skip the entire body of the class.  */
11784    cp_parser_skip_to_closing_brace (parser);
11785  else
11786    /* Parse the member-specification.  */
11787    cp_parser_member_specification_opt (parser);
11788  /* Look for the trailing `}'.  */
11789  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11790  /* We get better error messages by noticing a common problem: a
11791     missing trailing `;'.  */
11792  token = cp_lexer_peek_token (parser->lexer);
11793  has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11794  /* Look for trailing attributes to apply to this class.  */
11795  if (cp_parser_allow_gnu_extensions_p (parser))
11796    {
11797      tree sub_attr = cp_parser_attributes_opt (parser);
11798      attributes = chainon (attributes, sub_attr);
11799    }
11800  if (type != error_mark_node)
11801    type = finish_struct (type, attributes);
11802  if (pop_p)
11803    pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11804  /* If this class is not itself within the scope of another class,
11805     then we need to parse the bodies of all of the queued function
11806     definitions.  Note that the queued functions defined in a class
11807     are not always processed immediately following the
11808     class-specifier for that class.  Consider:
11809
11810       struct A {
11811         struct B { void f() { sizeof (A); } };
11812       };
11813
11814     If `f' were processed before the processing of `A' were
11815     completed, there would be no way to compute the size of `A'.
11816     Note that the nesting we are interested in here is lexical --
11817     not the semantic nesting given by TYPE_CONTEXT.  In particular,
11818     for:
11819
11820       struct A { struct B; };
11821       struct A::B { void f() { } };
11822
11823     there is no need to delay the parsing of `A::B::f'.  */
11824  if (--parser->num_classes_being_defined == 0)
11825    {
11826      tree queue_entry;
11827      tree fn;
11828
11829      /* In a first pass, parse default arguments to the functions.
11830	 Then, in a second pass, parse the bodies of the functions.
11831	 This two-phased approach handles cases like:
11832
11833	    struct S {
11834              void f() { g(); }
11835              void g(int i = 3);
11836            };
11837
11838         */
11839      for (TREE_PURPOSE (parser->unparsed_functions_queues)
11840	     = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11841	   (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11842	   TREE_PURPOSE (parser->unparsed_functions_queues)
11843	     = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11844	{
11845	  fn = TREE_VALUE (queue_entry);
11846	  /* Make sure that any template parameters are in scope.  */
11847	  maybe_begin_member_template_processing (fn);
11848	  /* If there are default arguments that have not yet been processed,
11849	     take care of them now.  */
11850	  cp_parser_late_parsing_default_args (parser, fn);
11851	  /* Remove any template parameters from the symbol table.  */
11852	  maybe_end_member_template_processing ();
11853	}
11854      /* Now parse the body of the functions.  */
11855      for (TREE_VALUE (parser->unparsed_functions_queues)
11856	     = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11857	   (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11858	   TREE_VALUE (parser->unparsed_functions_queues)
11859	     = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11860	{
11861	  /* Figure out which function we need to process.  */
11862	  fn = TREE_VALUE (queue_entry);
11863
11864	  /* A hack to prevent garbage collection.  */
11865	  function_depth++;
11866
11867	  /* Parse the function.  */
11868	  cp_parser_late_parsing_for_member (parser, fn);
11869	  function_depth--;
11870	}
11871
11872    }
11873
11874  /* Put back any saved access checks.  */
11875  pop_deferring_access_checks ();
11876
11877  /* Restore the count of active template-parameter-lists.  */
11878  parser->num_template_parameter_lists
11879    = saved_num_template_parameter_lists;
11880
11881  return type;
11882}
11883
11884/* Parse a class-head.
11885
11886   class-head:
11887     class-key identifier [opt] base-clause [opt]
11888     class-key nested-name-specifier identifier base-clause [opt]
11889     class-key nested-name-specifier [opt] template-id
11890       base-clause [opt]
11891
11892   GNU Extensions:
11893     class-key attributes identifier [opt] base-clause [opt]
11894     class-key attributes nested-name-specifier identifier base-clause [opt]
11895     class-key attributes nested-name-specifier [opt] template-id
11896       base-clause [opt]
11897
11898   Returns the TYPE of the indicated class.  Sets
11899   *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11900   involving a nested-name-specifier was used, and FALSE otherwise.
11901
11902   Returns NULL_TREE if the class-head is syntactically valid, but
11903   semantically invalid in a way that means we should skip the entire
11904   body of the class.  */
11905
11906static tree
11907cp_parser_class_head (cp_parser* parser,
11908		      bool* nested_name_specifier_p,
11909		      tree *attributes_p)
11910{
11911  cp_token *token;
11912  tree nested_name_specifier;
11913  enum tag_types class_key;
11914  tree id = NULL_TREE;
11915  tree type = NULL_TREE;
11916  tree attributes;
11917  bool template_id_p = false;
11918  bool qualified_p = false;
11919  bool invalid_nested_name_p = false;
11920  bool invalid_explicit_specialization_p = false;
11921  bool pop_p = false;
11922  unsigned num_templates;
11923
11924  /* Assume no nested-name-specifier will be present.  */
11925  *nested_name_specifier_p = false;
11926  /* Assume no template parameter lists will be used in defining the
11927     type.  */
11928  num_templates = 0;
11929
11930  /* Look for the class-key.  */
11931  class_key = cp_parser_class_key (parser);
11932  if (class_key == none_type)
11933    return error_mark_node;
11934
11935  /* Parse the attributes.  */
11936  attributes = cp_parser_attributes_opt (parser);
11937
11938  /* If the next token is `::', that is invalid -- but sometimes
11939     people do try to write:
11940
11941       struct ::S {};
11942
11943     Handle this gracefully by accepting the extra qualifier, and then
11944     issuing an error about it later if this really is a
11945     class-head.  If it turns out just to be an elaborated type
11946     specifier, remain silent.  */
11947  if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11948    qualified_p = true;
11949
11950  push_deferring_access_checks (dk_no_check);
11951
11952  /* Determine the name of the class.  Begin by looking for an
11953     optional nested-name-specifier.  */
11954  nested_name_specifier
11955    = cp_parser_nested_name_specifier_opt (parser,
11956					   /*typename_keyword_p=*/false,
11957					   /*check_dependency_p=*/false,
11958					   /*type_p=*/false,
11959					   /*is_declaration=*/false);
11960  /* If there was a nested-name-specifier, then there *must* be an
11961     identifier.  */
11962  if (nested_name_specifier)
11963    {
11964      /* Although the grammar says `identifier', it really means
11965	 `class-name' or `template-name'.  You are only allowed to
11966	 define a class that has already been declared with this
11967	 syntax.
11968
11969	 The proposed resolution for Core Issue 180 says that whever
11970	 you see `class T::X' you should treat `X' as a type-name.
11971
11972	 It is OK to define an inaccessible class; for example:
11973
11974           class A { class B; };
11975           class A::B {};
11976
11977         We do not know if we will see a class-name, or a
11978	 template-name.  We look for a class-name first, in case the
11979	 class-name is a template-id; if we looked for the
11980	 template-name first we would stop after the template-name.  */
11981      cp_parser_parse_tentatively (parser);
11982      type = cp_parser_class_name (parser,
11983				   /*typename_keyword_p=*/false,
11984				   /*template_keyword_p=*/false,
11985				   /*type_p=*/true,
11986				   /*check_dependency_p=*/false,
11987				   /*class_head_p=*/true,
11988				   /*is_declaration=*/false);
11989      /* If that didn't work, ignore the nested-name-specifier.  */
11990      if (!cp_parser_parse_definitely (parser))
11991	{
11992	  invalid_nested_name_p = true;
11993	  id = cp_parser_identifier (parser);
11994	  if (id == error_mark_node)
11995	    id = NULL_TREE;
11996	}
11997      /* If we could not find a corresponding TYPE, treat this
11998	 declaration like an unqualified declaration.  */
11999      if (type == error_mark_node)
12000	nested_name_specifier = NULL_TREE;
12001      /* Otherwise, count the number of templates used in TYPE and its
12002	 containing scopes.  */
12003      else
12004	{
12005	  tree scope;
12006
12007	  for (scope = TREE_TYPE (type);
12008	       scope && TREE_CODE (scope) != NAMESPACE_DECL;
12009	       scope = (TYPE_P (scope)
12010			? TYPE_CONTEXT (scope)
12011			: DECL_CONTEXT (scope)))
12012	    if (TYPE_P (scope)
12013		&& CLASS_TYPE_P (scope)
12014		&& CLASSTYPE_TEMPLATE_INFO (scope)
12015		&& PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12016		&& !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12017	      ++num_templates;
12018	}
12019    }
12020  /* Otherwise, the identifier is optional.  */
12021  else
12022    {
12023      /* We don't know whether what comes next is a template-id,
12024	 an identifier, or nothing at all.  */
12025      cp_parser_parse_tentatively (parser);
12026      /* Check for a template-id.  */
12027      id = cp_parser_template_id (parser,
12028				  /*template_keyword_p=*/false,
12029				  /*check_dependency_p=*/true,
12030				  /*is_declaration=*/true);
12031      /* If that didn't work, it could still be an identifier.  */
12032      if (!cp_parser_parse_definitely (parser))
12033	{
12034	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12035	    id = cp_parser_identifier (parser);
12036	  else
12037	    id = NULL_TREE;
12038	}
12039      else
12040	{
12041	  template_id_p = true;
12042	  ++num_templates;
12043	}
12044    }
12045
12046  pop_deferring_access_checks ();
12047
12048  if (id)
12049    cp_parser_check_for_invalid_template_id (parser, id);
12050
12051  /* If it's not a `:' or a `{' then we can't really be looking at a
12052     class-head, since a class-head only appears as part of a
12053     class-specifier.  We have to detect this situation before calling
12054     xref_tag, since that has irreversible side-effects.  */
12055  if (!cp_parser_next_token_starts_class_definition_p (parser))
12056    {
12057      cp_parser_error (parser, "expected `{' or `:'");
12058      return error_mark_node;
12059    }
12060
12061  /* At this point, we're going ahead with the class-specifier, even
12062     if some other problem occurs.  */
12063  cp_parser_commit_to_tentative_parse (parser);
12064  /* Issue the error about the overly-qualified name now.  */
12065  if (qualified_p)
12066    cp_parser_error (parser,
12067		     "global qualification of class name is invalid");
12068  else if (invalid_nested_name_p)
12069    cp_parser_error (parser,
12070		     "qualified name does not name a class");
12071  else if (nested_name_specifier)
12072    {
12073      tree scope;
12074      /* Figure out in what scope the declaration is being placed.  */
12075      scope = current_scope ();
12076      if (!scope)
12077	scope = current_namespace;
12078      /* If that scope does not contain the scope in which the
12079	 class was originally declared, the program is invalid.  */
12080      if (scope && !is_ancestor (scope, nested_name_specifier))
12081	{
12082	  error ("declaration of `%D' in `%D' which does not "
12083		 "enclose `%D'", type, scope, nested_name_specifier);
12084	  type = NULL_TREE;
12085	  goto done;
12086	}
12087      /* [dcl.meaning]
12088
12089         A declarator-id shall not be qualified exception of the
12090	 definition of a ... nested class outside of its class
12091	 ... [or] a the definition or explicit instantiation of a
12092	 class member of a namespace outside of its namespace.  */
12093      if (scope == nested_name_specifier)
12094	{
12095	  pedwarn ("extra qualification ignored");
12096	  nested_name_specifier = NULL_TREE;
12097	  num_templates = 0;
12098	}
12099    }
12100  /* An explicit-specialization must be preceded by "template <>".  If
12101     it is not, try to recover gracefully.  */
12102  if (at_namespace_scope_p ()
12103      && parser->num_template_parameter_lists == 0
12104      && template_id_p)
12105    {
12106      error ("an explicit specialization must be preceded by 'template <>'");
12107      invalid_explicit_specialization_p = true;
12108      /* Take the same action that would have been taken by
12109	 cp_parser_explicit_specialization.  */
12110      ++parser->num_template_parameter_lists;
12111      begin_specialization ();
12112    }
12113  /* There must be no "return" statements between this point and the
12114     end of this function; set "type "to the correct return value and
12115     use "goto done;" to return.  */
12116  /* Make sure that the right number of template parameters were
12117     present.  */
12118  if (!cp_parser_check_template_parameters (parser, num_templates))
12119    {
12120      /* If something went wrong, there is no point in even trying to
12121	 process the class-definition.  */
12122      type = NULL_TREE;
12123      goto done;
12124    }
12125
12126  /* Look up the type.  */
12127  if (template_id_p)
12128    {
12129      type = TREE_TYPE (id);
12130      maybe_process_partial_specialization (type);
12131    }
12132  else if (!nested_name_specifier)
12133    {
12134      /* If the class was unnamed, create a dummy name.  */
12135      if (!id)
12136	id = make_anon_name ();
12137      type = xref_tag (class_key, id, /*globalize=*/false,
12138		       parser->num_template_parameter_lists);
12139    }
12140  else
12141    {
12142      tree class_type;
12143      bool pop_p = false;
12144
12145      /* Given:
12146
12147	    template <typename T> struct S { struct T };
12148	    template <typename T> struct S<T>::T { };
12149
12150	 we will get a TYPENAME_TYPE when processing the definition of
12151	 `S::T'.  We need to resolve it to the actual type before we
12152	 try to define it.  */
12153      if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12154	{
12155	  class_type = resolve_typename_type (TREE_TYPE (type),
12156					      /*only_current_p=*/false);
12157	  if (class_type != error_mark_node)
12158	    type = TYPE_NAME (class_type);
12159	  else
12160	    {
12161	      cp_parser_error (parser, "could not resolve typename type");
12162	      type = error_mark_node;
12163	    }
12164	}
12165
12166      maybe_process_partial_specialization (TREE_TYPE (type));
12167      class_type = current_class_type;
12168      /* Enter the scope indicated by the nested-name-specifier.  */
12169      if (nested_name_specifier)
12170	pop_p = push_scope (nested_name_specifier);
12171      /* Get the canonical version of this type.  */
12172      type = TYPE_MAIN_DECL (TREE_TYPE (type));
12173      if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12174	  && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12175	type = push_template_decl (type);
12176      type = TREE_TYPE (type);
12177      if (nested_name_specifier)
12178	{
12179	  *nested_name_specifier_p = true;
12180	  if (pop_p)
12181	    pop_scope (nested_name_specifier);
12182	}
12183    }
12184  /* Indicate whether this class was declared as a `class' or as a
12185     `struct'.  */
12186  if (TREE_CODE (type) == RECORD_TYPE)
12187    CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12188  cp_parser_check_class_key (class_key, type);
12189
12190  /* Enter the scope containing the class; the names of base classes
12191     should be looked up in that context.  For example, given:
12192
12193       struct A { struct B {}; struct C; };
12194       struct A::C : B {};
12195
12196     is valid.  */
12197  if (nested_name_specifier)
12198    pop_p = push_scope (nested_name_specifier);
12199  /* Now, look for the base-clause.  */
12200  token = cp_lexer_peek_token (parser->lexer);
12201  if (token->type == CPP_COLON)
12202    {
12203      tree bases;
12204
12205      /* Get the list of base-classes.  */
12206      bases = cp_parser_base_clause (parser);
12207      /* Process them.  */
12208      xref_basetypes (type, bases);
12209    }
12210  /* Leave the scope given by the nested-name-specifier.  We will
12211     enter the class scope itself while processing the members.  */
12212  if (pop_p)
12213    pop_scope (nested_name_specifier);
12214
12215 done:
12216  if (invalid_explicit_specialization_p)
12217    {
12218      end_specialization ();
12219      --parser->num_template_parameter_lists;
12220    }
12221  *attributes_p = attributes;
12222  return type;
12223}
12224
12225/* Parse a class-key.
12226
12227   class-key:
12228     class
12229     struct
12230     union
12231
12232   Returns the kind of class-key specified, or none_type to indicate
12233   error.  */
12234
12235static enum tag_types
12236cp_parser_class_key (cp_parser* parser)
12237{
12238  cp_token *token;
12239  enum tag_types tag_type;
12240
12241  /* Look for the class-key.  */
12242  token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12243  if (!token)
12244    return none_type;
12245
12246  /* Check to see if the TOKEN is a class-key.  */
12247  tag_type = cp_parser_token_is_class_key (token);
12248  if (!tag_type)
12249    cp_parser_error (parser, "expected class-key");
12250  return tag_type;
12251}
12252
12253/* Parse an (optional) member-specification.
12254
12255   member-specification:
12256     member-declaration member-specification [opt]
12257     access-specifier : member-specification [opt]  */
12258
12259static void
12260cp_parser_member_specification_opt (cp_parser* parser)
12261{
12262  while (true)
12263    {
12264      cp_token *token;
12265      enum rid keyword;
12266
12267      /* Peek at the next token.  */
12268      token = cp_lexer_peek_token (parser->lexer);
12269      /* If it's a `}', or EOF then we've seen all the members.  */
12270      if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12271	break;
12272
12273      /* See if this token is a keyword.  */
12274      keyword = token->keyword;
12275      switch (keyword)
12276	{
12277	case RID_PUBLIC:
12278	case RID_PROTECTED:
12279	case RID_PRIVATE:
12280	  /* Consume the access-specifier.  */
12281	  cp_lexer_consume_token (parser->lexer);
12282	  /* Remember which access-specifier is active.  */
12283	  current_access_specifier = token->value;
12284	  /* Look for the `:'.  */
12285	  cp_parser_require (parser, CPP_COLON, "`:'");
12286	  break;
12287
12288	default:
12289	  /* Otherwise, the next construction must be a
12290	     member-declaration.  */
12291	  cp_parser_member_declaration (parser);
12292	}
12293    }
12294}
12295
12296/* Parse a member-declaration.
12297
12298   member-declaration:
12299     decl-specifier-seq [opt] member-declarator-list [opt] ;
12300     function-definition ; [opt]
12301     :: [opt] nested-name-specifier template [opt] unqualified-id ;
12302     using-declaration
12303     template-declaration
12304
12305   member-declarator-list:
12306     member-declarator
12307     member-declarator-list , member-declarator
12308
12309   member-declarator:
12310     declarator pure-specifier [opt]
12311     declarator constant-initializer [opt]
12312     identifier [opt] : constant-expression
12313
12314   GNU Extensions:
12315
12316   member-declaration:
12317     __extension__ member-declaration
12318
12319   member-declarator:
12320     declarator attributes [opt] pure-specifier [opt]
12321     declarator attributes [opt] constant-initializer [opt]
12322     identifier [opt] attributes [opt] : constant-expression  */
12323
12324static void
12325cp_parser_member_declaration (cp_parser* parser)
12326{
12327  tree decl_specifiers;
12328  tree prefix_attributes;
12329  tree decl;
12330  int declares_class_or_enum;
12331  bool friend_p;
12332  cp_token *token;
12333  int saved_pedantic;
12334
12335  /* Check for the `__extension__' keyword.  */
12336  if (cp_parser_extension_opt (parser, &saved_pedantic))
12337    {
12338      /* Recurse.  */
12339      cp_parser_member_declaration (parser);
12340      /* Restore the old value of the PEDANTIC flag.  */
12341      pedantic = saved_pedantic;
12342
12343      return;
12344    }
12345
12346  /* Check for a template-declaration.  */
12347  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12348    {
12349      /* Parse the template-declaration.  */
12350      cp_parser_template_declaration (parser, /*member_p=*/true);
12351
12352      return;
12353    }
12354
12355  /* Check for a using-declaration.  */
12356  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12357    {
12358      /* Parse the using-declaration.  */
12359      cp_parser_using_declaration (parser);
12360
12361      return;
12362    }
12363
12364  /* Parse the decl-specifier-seq.  */
12365  decl_specifiers
12366    = cp_parser_decl_specifier_seq (parser,
12367				    CP_PARSER_FLAGS_OPTIONAL,
12368				    &prefix_attributes,
12369				    &declares_class_or_enum);
12370  /* Check for an invalid type-name.  */
12371  if (cp_parser_diagnose_invalid_type_name (parser))
12372    return;
12373  /* If there is no declarator, then the decl-specifier-seq should
12374     specify a type.  */
12375  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12376    {
12377      /* If there was no decl-specifier-seq, and the next token is a
12378	 `;', then we have something like:
12379
12380	   struct S { ; };
12381
12382	 [class.mem]
12383
12384	 Each member-declaration shall declare at least one member
12385	 name of the class.  */
12386      if (!decl_specifiers)
12387	{
12388	  if (pedantic)
12389	    pedwarn ("extra semicolon");
12390	}
12391      else
12392	{
12393	  tree type;
12394
12395	  /* See if this declaration is a friend.  */
12396	  friend_p = cp_parser_friend_p (decl_specifiers);
12397	  /* If there were decl-specifiers, check to see if there was
12398	     a class-declaration.  */
12399	  type = check_tag_decl (decl_specifiers);
12400	  /* Nested classes have already been added to the class, but
12401	     a `friend' needs to be explicitly registered.  */
12402	  if (friend_p)
12403	    {
12404	      /* If the `friend' keyword was present, the friend must
12405		 be introduced with a class-key.  */
12406	       if (!declares_class_or_enum)
12407		 error ("a class-key must be used when declaring a friend");
12408	       /* In this case:
12409
12410		    template <typename T> struct A {
12411                      friend struct A<T>::B;
12412                    };
12413
12414		  A<T>::B will be represented by a TYPENAME_TYPE, and
12415		  therefore not recognized by check_tag_decl.  */
12416	       if (!type)
12417		 {
12418		   tree specifier;
12419
12420		   for (specifier = decl_specifiers;
12421			specifier;
12422			specifier = TREE_CHAIN (specifier))
12423		     {
12424		       tree s = TREE_VALUE (specifier);
12425
12426		       if (TREE_CODE (s) == IDENTIFIER_NODE)
12427                         get_global_value_if_present (s, &type);
12428		       if (TREE_CODE (s) == TYPE_DECL)
12429			 s = TREE_TYPE (s);
12430		       if (TYPE_P (s))
12431			 {
12432			   type = s;
12433			   break;
12434			 }
12435		     }
12436		 }
12437	       if (!type || !TYPE_P (type))
12438		 error ("friend declaration does not name a class or "
12439			"function");
12440	       else
12441		 make_friend_class (current_class_type, type,
12442				    /*complain=*/true);
12443	    }
12444	  /* If there is no TYPE, an error message will already have
12445	     been issued.  */
12446	  else if (!type)
12447	    ;
12448	  /* An anonymous aggregate has to be handled specially; such
12449	     a declaration really declares a data member (with a
12450	     particular type), as opposed to a nested class.  */
12451	  else if (ANON_AGGR_TYPE_P (type))
12452	    {
12453	      /* Remove constructors and such from TYPE, now that we
12454		 know it is an anonymous aggregate.  */
12455	      fixup_anonymous_aggr (type);
12456	      /* And make the corresponding data member.  */
12457	      decl = build_decl (FIELD_DECL, NULL_TREE, type);
12458	      /* Add it to the class.  */
12459	      finish_member_declaration (decl);
12460	    }
12461	  else
12462	    cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12463	}
12464    }
12465  else
12466    {
12467      /* See if these declarations will be friends.  */
12468      friend_p = cp_parser_friend_p (decl_specifiers);
12469
12470      /* Keep going until we hit the `;' at the end of the
12471	 declaration.  */
12472      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12473	{
12474	  tree attributes = NULL_TREE;
12475	  tree first_attribute;
12476
12477	  /* Peek at the next token.  */
12478	  token = cp_lexer_peek_token (parser->lexer);
12479
12480	  /* Check for a bitfield declaration.  */
12481	  if (token->type == CPP_COLON
12482	      || (token->type == CPP_NAME
12483		  && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12484		  == CPP_COLON))
12485	    {
12486	      tree identifier;
12487	      tree width;
12488
12489	      /* Get the name of the bitfield.  Note that we cannot just
12490		 check TOKEN here because it may have been invalidated by
12491		 the call to cp_lexer_peek_nth_token above.  */
12492	      if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12493		identifier = cp_parser_identifier (parser);
12494	      else
12495		identifier = NULL_TREE;
12496
12497	      /* Consume the `:' token.  */
12498	      cp_lexer_consume_token (parser->lexer);
12499	      /* Get the width of the bitfield.  */
12500	      width
12501		= cp_parser_constant_expression (parser,
12502						 /*allow_non_constant=*/false,
12503						 NULL);
12504
12505	      /* Look for attributes that apply to the bitfield.  */
12506	      attributes = cp_parser_attributes_opt (parser);
12507	      /* Remember which attributes are prefix attributes and
12508		 which are not.  */
12509	      first_attribute = attributes;
12510	      /* Combine the attributes.  */
12511	      attributes = chainon (prefix_attributes, attributes);
12512
12513	      /* Create the bitfield declaration.  */
12514	      decl = grokbitfield (identifier,
12515				   decl_specifiers,
12516				   width);
12517	      /* Apply the attributes.  */
12518	      cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12519	    }
12520	  else
12521	    {
12522	      tree declarator;
12523	      tree initializer;
12524	      tree asm_specification;
12525	      int ctor_dtor_or_conv_p;
12526
12527	      /* Parse the declarator.  */
12528	      declarator
12529		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12530					&ctor_dtor_or_conv_p,
12531					/*parenthesized_p=*/NULL);
12532
12533	      /* If something went wrong parsing the declarator, make sure
12534		 that we at least consume some tokens.  */
12535	      if (declarator == error_mark_node)
12536		{
12537		  /* Skip to the end of the statement.  */
12538		  cp_parser_skip_to_end_of_statement (parser);
12539		  /* If the next token is not a semicolon, that is
12540		     probably because we just skipped over the body of
12541		     a function.  So, we consume a semicolon if
12542		     present, but do not issue an error message if it
12543		     is not present.  */
12544		  if (cp_lexer_next_token_is (parser->lexer,
12545					      CPP_SEMICOLON))
12546		    cp_lexer_consume_token (parser->lexer);
12547		  return;
12548		}
12549
12550	      cp_parser_check_for_definition_in_return_type
12551		(declarator, declares_class_or_enum);
12552
12553	      /* Look for an asm-specification.  */
12554	      asm_specification = cp_parser_asm_specification_opt (parser);
12555	      /* Look for attributes that apply to the declaration.  */
12556	      attributes = cp_parser_attributes_opt (parser);
12557	      /* Remember which attributes are prefix attributes and
12558		 which are not.  */
12559	      first_attribute = attributes;
12560	      /* Combine the attributes.  */
12561	      attributes = chainon (prefix_attributes, attributes);
12562
12563	      /* If it's an `=', then we have a constant-initializer or a
12564		 pure-specifier.  It is not correct to parse the
12565		 initializer before registering the member declaration
12566		 since the member declaration should be in scope while
12567		 its initializer is processed.  However, the rest of the
12568		 front end does not yet provide an interface that allows
12569		 us to handle this correctly.  */
12570	      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12571		{
12572		  /* In [class.mem]:
12573
12574		     A pure-specifier shall be used only in the declaration of
12575		     a virtual function.
12576
12577		     A member-declarator can contain a constant-initializer
12578		     only if it declares a static member of integral or
12579		     enumeration type.
12580
12581		     Therefore, if the DECLARATOR is for a function, we look
12582		     for a pure-specifier; otherwise, we look for a
12583		     constant-initializer.  When we call `grokfield', it will
12584		     perform more stringent semantics checks.  */
12585		  if (TREE_CODE (declarator) == CALL_EXPR)
12586		    initializer = cp_parser_pure_specifier (parser);
12587		  else
12588		    /* Parse the initializer.  */
12589		    initializer = cp_parser_constant_initializer (parser);
12590		}
12591	      /* Otherwise, there is no initializer.  */
12592	      else
12593		initializer = NULL_TREE;
12594
12595	      /* See if we are probably looking at a function
12596		 definition.  We are certainly not looking at at a
12597		 member-declarator.  Calling `grokfield' has
12598		 side-effects, so we must not do it unless we are sure
12599		 that we are looking at a member-declarator.  */
12600	      if (cp_parser_token_starts_function_definition_p
12601		  (cp_lexer_peek_token (parser->lexer)))
12602		{
12603		  /* The grammar does not allow a pure-specifier to be
12604		     used when a member function is defined.  (It is
12605		     possible that this fact is an oversight in the
12606		     standard, since a pure function may be defined
12607		     outside of the class-specifier.  */
12608		  if (initializer)
12609		    error ("pure-specifier on function-definition");
12610		  decl = cp_parser_save_member_function_body (parser,
12611							      decl_specifiers,
12612							      declarator,
12613							      attributes);
12614		  /* If the member was not a friend, declare it here.  */
12615		  if (!friend_p)
12616		    finish_member_declaration (decl);
12617		  /* Peek at the next token.  */
12618		  token = cp_lexer_peek_token (parser->lexer);
12619		  /* If the next token is a semicolon, consume it.  */
12620		  if (token->type == CPP_SEMICOLON)
12621		    cp_lexer_consume_token (parser->lexer);
12622		  return;
12623		}
12624	      else
12625		{
12626		  /* Create the declaration.  */
12627		  decl = grokfield (declarator, decl_specifiers,
12628				    initializer, asm_specification,
12629				    attributes);
12630		  /* Any initialization must have been from a
12631		     constant-expression.  */
12632		  if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12633		    DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12634		}
12635	    }
12636
12637	  /* Reset PREFIX_ATTRIBUTES.  */
12638	  while (attributes && TREE_CHAIN (attributes) != first_attribute)
12639	    attributes = TREE_CHAIN (attributes);
12640	  if (attributes)
12641	    TREE_CHAIN (attributes) = NULL_TREE;
12642
12643	  /* If there is any qualification still in effect, clear it
12644	     now; we will be starting fresh with the next declarator.  */
12645	  parser->scope = NULL_TREE;
12646	  parser->qualifying_scope = NULL_TREE;
12647	  parser->object_scope = NULL_TREE;
12648	  /* If it's a `,', then there are more declarators.  */
12649	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12650	    cp_lexer_consume_token (parser->lexer);
12651	  /* If the next token isn't a `;', then we have a parse error.  */
12652	  else if (cp_lexer_next_token_is_not (parser->lexer,
12653					       CPP_SEMICOLON))
12654	    {
12655	      cp_parser_error (parser, "expected `;'");
12656	      /* Skip tokens until we find a `;'.  */
12657	      cp_parser_skip_to_end_of_statement (parser);
12658
12659	      break;
12660	    }
12661
12662	  if (decl)
12663	    {
12664	      /* Add DECL to the list of members.  */
12665	      if (!friend_p)
12666		finish_member_declaration (decl);
12667
12668	      if (TREE_CODE (decl) == FUNCTION_DECL)
12669		cp_parser_save_default_args (parser, decl);
12670	    }
12671	}
12672    }
12673
12674  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12675}
12676
12677/* Parse a pure-specifier.
12678
12679   pure-specifier:
12680     = 0
12681
12682   Returns INTEGER_ZERO_NODE if a pure specifier is found.
12683   Otherwise, ERROR_MARK_NODE is returned.  */
12684
12685static tree
12686cp_parser_pure_specifier (cp_parser* parser)
12687{
12688  cp_token *token;
12689
12690  /* Look for the `=' token.  */
12691  if (!cp_parser_require (parser, CPP_EQ, "`='"))
12692    return error_mark_node;
12693  /* Look for the `0' token.  */
12694  token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12695  /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
12696     to get information from the lexer about how the number was
12697     spelled in order to fix this problem.  */
12698  if (!token || !integer_zerop (token->value))
12699    return error_mark_node;
12700
12701  return integer_zero_node;
12702}
12703
12704/* Parse a constant-initializer.
12705
12706   constant-initializer:
12707     = constant-expression
12708
12709   Returns a representation of the constant-expression.  */
12710
12711static tree
12712cp_parser_constant_initializer (cp_parser* parser)
12713{
12714  /* Look for the `=' token.  */
12715  if (!cp_parser_require (parser, CPP_EQ, "`='"))
12716    return error_mark_node;
12717
12718  /* It is invalid to write:
12719
12720       struct S { static const int i = { 7 }; };
12721
12722     */
12723  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12724    {
12725      cp_parser_error (parser,
12726		       "a brace-enclosed initializer is not allowed here");
12727      /* Consume the opening brace.  */
12728      cp_lexer_consume_token (parser->lexer);
12729      /* Skip the initializer.  */
12730      cp_parser_skip_to_closing_brace (parser);
12731      /* Look for the trailing `}'.  */
12732      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12733
12734      return error_mark_node;
12735    }
12736
12737  return cp_parser_constant_expression (parser,
12738					/*allow_non_constant=*/false,
12739					NULL);
12740}
12741
12742/* Derived classes [gram.class.derived] */
12743
12744/* Parse a base-clause.
12745
12746   base-clause:
12747     : base-specifier-list
12748
12749   base-specifier-list:
12750     base-specifier
12751     base-specifier-list , base-specifier
12752
12753   Returns a TREE_LIST representing the base-classes, in the order in
12754   which they were declared.  The representation of each node is as
12755   described by cp_parser_base_specifier.
12756
12757   In the case that no bases are specified, this function will return
12758   NULL_TREE, not ERROR_MARK_NODE.  */
12759
12760static tree
12761cp_parser_base_clause (cp_parser* parser)
12762{
12763  tree bases = NULL_TREE;
12764
12765  /* Look for the `:' that begins the list.  */
12766  cp_parser_require (parser, CPP_COLON, "`:'");
12767
12768  /* Scan the base-specifier-list.  */
12769  while (true)
12770    {
12771      cp_token *token;
12772      tree base;
12773
12774      /* Look for the base-specifier.  */
12775      base = cp_parser_base_specifier (parser);
12776      /* Add BASE to the front of the list.  */
12777      if (base != error_mark_node)
12778	{
12779	  TREE_CHAIN (base) = bases;
12780	  bases = base;
12781	}
12782      /* Peek at the next token.  */
12783      token = cp_lexer_peek_token (parser->lexer);
12784      /* If it's not a comma, then the list is complete.  */
12785      if (token->type != CPP_COMMA)
12786	break;
12787      /* Consume the `,'.  */
12788      cp_lexer_consume_token (parser->lexer);
12789    }
12790
12791  /* PARSER->SCOPE may still be non-NULL at this point, if the last
12792     base class had a qualified name.  However, the next name that
12793     appears is certainly not qualified.  */
12794  parser->scope = NULL_TREE;
12795  parser->qualifying_scope = NULL_TREE;
12796  parser->object_scope = NULL_TREE;
12797
12798  return nreverse (bases);
12799}
12800
12801/* Parse a base-specifier.
12802
12803   base-specifier:
12804     :: [opt] nested-name-specifier [opt] class-name
12805     virtual access-specifier [opt] :: [opt] nested-name-specifier
12806       [opt] class-name
12807     access-specifier virtual [opt] :: [opt] nested-name-specifier
12808       [opt] class-name
12809
12810   Returns a TREE_LIST.  The TREE_PURPOSE will be one of
12811   ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12812   indicate the specifiers provided.  The TREE_VALUE will be a TYPE
12813   (or the ERROR_MARK_NODE) indicating the type that was specified.  */
12814
12815static tree
12816cp_parser_base_specifier (cp_parser* parser)
12817{
12818  cp_token *token;
12819  bool done = false;
12820  bool virtual_p = false;
12821  bool duplicate_virtual_error_issued_p = false;
12822  bool duplicate_access_error_issued_p = false;
12823  bool class_scope_p, template_p;
12824  tree access = access_default_node;
12825  tree type;
12826
12827  /* Process the optional `virtual' and `access-specifier'.  */
12828  while (!done)
12829    {
12830      /* Peek at the next token.  */
12831      token = cp_lexer_peek_token (parser->lexer);
12832      /* Process `virtual'.  */
12833      switch (token->keyword)
12834	{
12835	case RID_VIRTUAL:
12836	  /* If `virtual' appears more than once, issue an error.  */
12837	  if (virtual_p && !duplicate_virtual_error_issued_p)
12838	    {
12839	      cp_parser_error (parser,
12840			       "`virtual' specified more than once in base-specified");
12841	      duplicate_virtual_error_issued_p = true;
12842	    }
12843
12844	  virtual_p = true;
12845
12846	  /* Consume the `virtual' token.  */
12847	  cp_lexer_consume_token (parser->lexer);
12848
12849	  break;
12850
12851	case RID_PUBLIC:
12852	case RID_PROTECTED:
12853	case RID_PRIVATE:
12854	  /* If more than one access specifier appears, issue an
12855	     error.  */
12856	  if (access != access_default_node
12857	      && !duplicate_access_error_issued_p)
12858	    {
12859	      cp_parser_error (parser,
12860			       "more than one access specifier in base-specified");
12861	      duplicate_access_error_issued_p = true;
12862	    }
12863
12864	  access = ridpointers[(int) token->keyword];
12865
12866	  /* Consume the access-specifier.  */
12867	  cp_lexer_consume_token (parser->lexer);
12868
12869	  break;
12870
12871	default:
12872	  done = true;
12873	  break;
12874	}
12875    }
12876  /* It is not uncommon to see programs mechanically, errouneously, use
12877     the 'typename' keyword to denote (dependent) qualified types
12878     as base classes.  */
12879  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12880    {
12881      if (!processing_template_decl)
12882	error ("keyword `typename' not allowed outside of templates");
12883      else
12884	error ("keyword `typename' not allowed in this context "
12885	       "(the base class is implicitly a type)");
12886      cp_lexer_consume_token (parser->lexer);
12887    }
12888
12889  /* Look for the optional `::' operator.  */
12890  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12891  /* Look for the nested-name-specifier.  The simplest way to
12892     implement:
12893
12894       [temp.res]
12895
12896       The keyword `typename' is not permitted in a base-specifier or
12897       mem-initializer; in these contexts a qualified name that
12898       depends on a template-parameter is implicitly assumed to be a
12899       type name.
12900
12901     is to pretend that we have seen the `typename' keyword at this
12902     point.  */
12903  cp_parser_nested_name_specifier_opt (parser,
12904				       /*typename_keyword_p=*/true,
12905				       /*check_dependency_p=*/true,
12906				       /*type_p=*/true,
12907				       /*is_declaration=*/true);
12908  /* If the base class is given by a qualified name, assume that names
12909     we see are type names or templates, as appropriate.  */
12910  class_scope_p = (parser->scope && TYPE_P (parser->scope));
12911  template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12912
12913  /* Finally, look for the class-name.  */
12914  type = cp_parser_class_name (parser,
12915			       class_scope_p,
12916			       template_p,
12917			       /*type_p=*/true,
12918			       /*check_dependency_p=*/true,
12919			       /*class_head_p=*/false,
12920			       /*is_declaration=*/true);
12921
12922  if (type == error_mark_node)
12923    return error_mark_node;
12924
12925  return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12926}
12927
12928/* Exception handling [gram.exception] */
12929
12930/* Parse an (optional) exception-specification.
12931
12932   exception-specification:
12933     throw ( type-id-list [opt] )
12934
12935   Returns a TREE_LIST representing the exception-specification.  The
12936   TREE_VALUE of each node is a type.  */
12937
12938static tree
12939cp_parser_exception_specification_opt (cp_parser* parser)
12940{
12941  cp_token *token;
12942  tree type_id_list;
12943
12944  /* Peek at the next token.  */
12945  token = cp_lexer_peek_token (parser->lexer);
12946  /* If it's not `throw', then there's no exception-specification.  */
12947  if (!cp_parser_is_keyword (token, RID_THROW))
12948    return NULL_TREE;
12949
12950  /* Consume the `throw'.  */
12951  cp_lexer_consume_token (parser->lexer);
12952
12953  /* Look for the `('.  */
12954  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12955
12956  /* Peek at the next token.  */
12957  token = cp_lexer_peek_token (parser->lexer);
12958  /* If it's not a `)', then there is a type-id-list.  */
12959  if (token->type != CPP_CLOSE_PAREN)
12960    {
12961      const char *saved_message;
12962
12963      /* Types may not be defined in an exception-specification.  */
12964      saved_message = parser->type_definition_forbidden_message;
12965      parser->type_definition_forbidden_message
12966	= "types may not be defined in an exception-specification";
12967      /* Parse the type-id-list.  */
12968      type_id_list = cp_parser_type_id_list (parser);
12969      /* Restore the saved message.  */
12970      parser->type_definition_forbidden_message = saved_message;
12971    }
12972  else
12973    type_id_list = empty_except_spec;
12974
12975  /* Look for the `)'.  */
12976  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12977
12978  return type_id_list;
12979}
12980
12981/* Parse an (optional) type-id-list.
12982
12983   type-id-list:
12984     type-id
12985     type-id-list , type-id
12986
12987   Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
12988   in the order that the types were presented.  */
12989
12990static tree
12991cp_parser_type_id_list (cp_parser* parser)
12992{
12993  tree types = NULL_TREE;
12994
12995  while (true)
12996    {
12997      cp_token *token;
12998      tree type;
12999
13000      /* Get the next type-id.  */
13001      type = cp_parser_type_id (parser);
13002      /* Add it to the list.  */
13003      types = add_exception_specifier (types, type, /*complain=*/1);
13004      /* Peek at the next token.  */
13005      token = cp_lexer_peek_token (parser->lexer);
13006      /* If it is not a `,', we are done.  */
13007      if (token->type != CPP_COMMA)
13008	break;
13009      /* Consume the `,'.  */
13010      cp_lexer_consume_token (parser->lexer);
13011    }
13012
13013  return nreverse (types);
13014}
13015
13016/* Parse a try-block.
13017
13018   try-block:
13019     try compound-statement handler-seq  */
13020
13021static tree
13022cp_parser_try_block (cp_parser* parser)
13023{
13024  tree try_block;
13025
13026  cp_parser_require_keyword (parser, RID_TRY, "`try'");
13027  try_block = begin_try_block ();
13028  cp_parser_compound_statement (parser, false);
13029  finish_try_block (try_block);
13030  cp_parser_handler_seq (parser);
13031  finish_handler_sequence (try_block);
13032
13033  return try_block;
13034}
13035
13036/* Parse a function-try-block.
13037
13038   function-try-block:
13039     try ctor-initializer [opt] function-body handler-seq  */
13040
13041static bool
13042cp_parser_function_try_block (cp_parser* parser)
13043{
13044  tree try_block;
13045  bool ctor_initializer_p;
13046
13047  /* Look for the `try' keyword.  */
13048  if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13049    return false;
13050  /* Let the rest of the front-end know where we are.  */
13051  try_block = begin_function_try_block ();
13052  /* Parse the function-body.  */
13053  ctor_initializer_p
13054    = cp_parser_ctor_initializer_opt_and_function_body (parser);
13055  /* We're done with the `try' part.  */
13056  finish_function_try_block (try_block);
13057  /* Parse the handlers.  */
13058  cp_parser_handler_seq (parser);
13059  /* We're done with the handlers.  */
13060  finish_function_handler_sequence (try_block);
13061
13062  return ctor_initializer_p;
13063}
13064
13065/* Parse a handler-seq.
13066
13067   handler-seq:
13068     handler handler-seq [opt]  */
13069
13070static void
13071cp_parser_handler_seq (cp_parser* parser)
13072{
13073  while (true)
13074    {
13075      cp_token *token;
13076
13077      /* Parse the handler.  */
13078      cp_parser_handler (parser);
13079      /* Peek at the next token.  */
13080      token = cp_lexer_peek_token (parser->lexer);
13081      /* If it's not `catch' then there are no more handlers.  */
13082      if (!cp_parser_is_keyword (token, RID_CATCH))
13083	break;
13084    }
13085}
13086
13087/* Parse a handler.
13088
13089   handler:
13090     catch ( exception-declaration ) compound-statement  */
13091
13092static void
13093cp_parser_handler (cp_parser* parser)
13094{
13095  tree handler;
13096  tree declaration;
13097
13098  cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13099  handler = begin_handler ();
13100  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13101  declaration = cp_parser_exception_declaration (parser);
13102  finish_handler_parms (declaration, handler);
13103  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13104  cp_parser_compound_statement (parser, false);
13105  finish_handler (handler);
13106}
13107
13108/* Parse an exception-declaration.
13109
13110   exception-declaration:
13111     type-specifier-seq declarator
13112     type-specifier-seq abstract-declarator
13113     type-specifier-seq
13114     ...
13115
13116   Returns a VAR_DECL for the declaration, or NULL_TREE if the
13117   ellipsis variant is used.  */
13118
13119static tree
13120cp_parser_exception_declaration (cp_parser* parser)
13121{
13122  tree type_specifiers;
13123  tree declarator;
13124  const char *saved_message;
13125
13126  /* If it's an ellipsis, it's easy to handle.  */
13127  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13128    {
13129      /* Consume the `...' token.  */
13130      cp_lexer_consume_token (parser->lexer);
13131      return NULL_TREE;
13132    }
13133
13134  /* Types may not be defined in exception-declarations.  */
13135  saved_message = parser->type_definition_forbidden_message;
13136  parser->type_definition_forbidden_message
13137    = "types may not be defined in exception-declarations";
13138
13139  /* Parse the type-specifier-seq.  */
13140  type_specifiers = cp_parser_type_specifier_seq (parser);
13141  /* If it's a `)', then there is no declarator.  */
13142  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13143    declarator = NULL_TREE;
13144  else
13145    declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13146				       /*ctor_dtor_or_conv_p=*/NULL,
13147				       /*parenthesized_p=*/NULL);
13148
13149  /* Restore the saved message.  */
13150  parser->type_definition_forbidden_message = saved_message;
13151
13152  return start_handler_parms (type_specifiers, declarator);
13153}
13154
13155/* Parse a throw-expression.
13156
13157   throw-expression:
13158     throw assignment-expression [opt]
13159
13160   Returns a THROW_EXPR representing the throw-expression.  */
13161
13162static tree
13163cp_parser_throw_expression (cp_parser* parser)
13164{
13165  tree expression;
13166  cp_token* token;
13167
13168  cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13169  token = cp_lexer_peek_token (parser->lexer);
13170  /* Figure out whether or not there is an assignment-expression
13171     following the "throw" keyword.  */
13172  if (token->type == CPP_COMMA
13173      || token->type == CPP_SEMICOLON
13174      || token->type == CPP_CLOSE_PAREN
13175      || token->type == CPP_CLOSE_SQUARE
13176      || token->type == CPP_CLOSE_BRACE
13177      || token->type == CPP_COLON)
13178    expression = NULL_TREE;
13179  else
13180    expression = cp_parser_assignment_expression (parser);
13181
13182  return build_throw (expression);
13183}
13184
13185/* GNU Extensions */
13186
13187/* Parse an (optional) asm-specification.
13188
13189   asm-specification:
13190     asm ( string-literal )
13191
13192   If the asm-specification is present, returns a STRING_CST
13193   corresponding to the string-literal.  Otherwise, returns
13194   NULL_TREE.  */
13195
13196static tree
13197cp_parser_asm_specification_opt (cp_parser* parser)
13198{
13199  cp_token *token;
13200  tree asm_specification;
13201
13202  /* Peek at the next token.  */
13203  token = cp_lexer_peek_token (parser->lexer);
13204  /* If the next token isn't the `asm' keyword, then there's no
13205     asm-specification.  */
13206  if (!cp_parser_is_keyword (token, RID_ASM))
13207    return NULL_TREE;
13208
13209  /* Consume the `asm' token.  */
13210  cp_lexer_consume_token (parser->lexer);
13211  /* Look for the `('.  */
13212  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13213
13214  /* Look for the string-literal.  */
13215  token = cp_parser_require (parser, CPP_STRING, "string-literal");
13216  if (token)
13217    asm_specification = token->value;
13218  else
13219    asm_specification = NULL_TREE;
13220
13221  /* Look for the `)'.  */
13222  cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13223
13224  return asm_specification;
13225}
13226
13227/* Parse an asm-operand-list.
13228
13229   asm-operand-list:
13230     asm-operand
13231     asm-operand-list , asm-operand
13232
13233   asm-operand:
13234     string-literal ( expression )
13235     [ string-literal ] string-literal ( expression )
13236
13237   Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13238   each node is the expression.  The TREE_PURPOSE is itself a
13239   TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13240   string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13241   is a STRING_CST for the string literal before the parenthesis.  */
13242
13243static tree
13244cp_parser_asm_operand_list (cp_parser* parser)
13245{
13246  tree asm_operands = NULL_TREE;
13247
13248  while (true)
13249    {
13250      tree string_literal;
13251      tree expression;
13252      tree name;
13253      cp_token *token;
13254
13255      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13256	{
13257	  /* Consume the `[' token.  */
13258	  cp_lexer_consume_token (parser->lexer);
13259	  /* Read the operand name.  */
13260	  name = cp_parser_identifier (parser);
13261	  if (name != error_mark_node)
13262	    name = build_string (IDENTIFIER_LENGTH (name),
13263				 IDENTIFIER_POINTER (name));
13264	  /* Look for the closing `]'.  */
13265	  cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13266	}
13267      else
13268	name = NULL_TREE;
13269      /* Look for the string-literal.  */
13270      token = cp_parser_require (parser, CPP_STRING, "string-literal");
13271      string_literal = token ? token->value : error_mark_node;
13272      /* Look for the `('.  */
13273      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13274      /* Parse the expression.  */
13275      expression = cp_parser_expression (parser);
13276      /* Look for the `)'.  */
13277      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13278      /* Add this operand to the list.  */
13279      asm_operands = tree_cons (build_tree_list (name, string_literal),
13280				expression,
13281				asm_operands);
13282      /* If the next token is not a `,', there are no more
13283	 operands.  */
13284      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13285	break;
13286      /* Consume the `,'.  */
13287      cp_lexer_consume_token (parser->lexer);
13288    }
13289
13290  return nreverse (asm_operands);
13291}
13292
13293/* Parse an asm-clobber-list.
13294
13295   asm-clobber-list:
13296     string-literal
13297     asm-clobber-list , string-literal
13298
13299   Returns a TREE_LIST, indicating the clobbers in the order that they
13300   appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13301
13302static tree
13303cp_parser_asm_clobber_list (cp_parser* parser)
13304{
13305  tree clobbers = NULL_TREE;
13306
13307  while (true)
13308    {
13309      cp_token *token;
13310      tree string_literal;
13311
13312      /* Look for the string literal.  */
13313      token = cp_parser_require (parser, CPP_STRING, "string-literal");
13314      string_literal = token ? token->value : error_mark_node;
13315      /* Add it to the list.  */
13316      clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13317      /* If the next token is not a `,', then the list is
13318	 complete.  */
13319      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13320	break;
13321      /* Consume the `,' token.  */
13322      cp_lexer_consume_token (parser->lexer);
13323    }
13324
13325  return clobbers;
13326}
13327
13328/* Parse an (optional) series of attributes.
13329
13330   attributes:
13331     attributes attribute
13332
13333   attribute:
13334     __attribute__ (( attribute-list [opt] ))
13335
13336   The return value is as for cp_parser_attribute_list.  */
13337
13338static tree
13339cp_parser_attributes_opt (cp_parser* parser)
13340{
13341  tree attributes = NULL_TREE;
13342
13343  while (true)
13344    {
13345      cp_token *token;
13346      tree attribute_list;
13347
13348      /* Peek at the next token.  */
13349      token = cp_lexer_peek_token (parser->lexer);
13350      /* If it's not `__attribute__', then we're done.  */
13351      if (token->keyword != RID_ATTRIBUTE)
13352	break;
13353
13354      /* Consume the `__attribute__' keyword.  */
13355      cp_lexer_consume_token (parser->lexer);
13356      /* Look for the two `(' tokens.  */
13357      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13358      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13359
13360      /* Peek at the next token.  */
13361      token = cp_lexer_peek_token (parser->lexer);
13362      if (token->type != CPP_CLOSE_PAREN)
13363	/* Parse the attribute-list.  */
13364	attribute_list = cp_parser_attribute_list (parser);
13365      else
13366	/* If the next token is a `)', then there is no attribute
13367	   list.  */
13368	attribute_list = NULL;
13369
13370      /* Look for the two `)' tokens.  */
13371      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13372      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13373
13374      /* Add these new attributes to the list.  */
13375      attributes = chainon (attributes, attribute_list);
13376    }
13377
13378  return attributes;
13379}
13380
13381/* Parse an attribute-list.
13382
13383   attribute-list:
13384     attribute
13385     attribute-list , attribute
13386
13387   attribute:
13388     identifier
13389     identifier ( identifier )
13390     identifier ( identifier , expression-list )
13391     identifier ( expression-list )
13392
13393   Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13394   TREE_PURPOSE of each node is the identifier indicating which
13395   attribute is in use.  The TREE_VALUE represents the arguments, if
13396   any.  */
13397
13398static tree
13399cp_parser_attribute_list (cp_parser* parser)
13400{
13401  tree attribute_list = NULL_TREE;
13402
13403  while (true)
13404    {
13405      cp_token *token;
13406      tree identifier;
13407      tree attribute;
13408
13409      /* Look for the identifier.  We also allow keywords here; for
13410	 example `__attribute__ ((const))' is legal.  */
13411      token = cp_lexer_peek_token (parser->lexer);
13412      if (token->type != CPP_NAME
13413	  && token->type != CPP_KEYWORD)
13414	return error_mark_node;
13415      /* Consume the token.  */
13416      token = cp_lexer_consume_token (parser->lexer);
13417
13418      /* Save away the identifier that indicates which attribute this is.  */
13419      identifier = token->value;
13420      attribute = build_tree_list (identifier, NULL_TREE);
13421
13422      /* Peek at the next token.  */
13423      token = cp_lexer_peek_token (parser->lexer);
13424      /* If it's an `(', then parse the attribute arguments.  */
13425      if (token->type == CPP_OPEN_PAREN)
13426	{
13427	  tree arguments;
13428
13429	  arguments = (cp_parser_parenthesized_expression_list
13430		       (parser, true, /*non_constant_p=*/NULL));
13431	  /* Save the identifier and arguments away.  */
13432	  TREE_VALUE (attribute) = arguments;
13433	}
13434
13435      /* Add this attribute to the list.  */
13436      TREE_CHAIN (attribute) = attribute_list;
13437      attribute_list = attribute;
13438
13439      /* Now, look for more attributes.  */
13440      token = cp_lexer_peek_token (parser->lexer);
13441      /* If the next token isn't a `,', we're done.  */
13442      if (token->type != CPP_COMMA)
13443	break;
13444
13445      /* Consume the comma and keep going.  */
13446      cp_lexer_consume_token (parser->lexer);
13447    }
13448
13449  /* We built up the list in reverse order.  */
13450  return nreverse (attribute_list);
13451}
13452
13453/* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13454   present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13455   current value of the PEDANTIC flag, regardless of whether or not
13456   the `__extension__' keyword is present.  The caller is responsible
13457   for restoring the value of the PEDANTIC flag.  */
13458
13459static bool
13460cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13461{
13462  /* Save the old value of the PEDANTIC flag.  */
13463  *saved_pedantic = pedantic;
13464
13465  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13466    {
13467      /* Consume the `__extension__' token.  */
13468      cp_lexer_consume_token (parser->lexer);
13469      /* We're not being pedantic while the `__extension__' keyword is
13470	 in effect.  */
13471      pedantic = 0;
13472
13473      return true;
13474    }
13475
13476  return false;
13477}
13478
13479/* Parse a label declaration.
13480
13481   label-declaration:
13482     __label__ label-declarator-seq ;
13483
13484   label-declarator-seq:
13485     identifier , label-declarator-seq
13486     identifier  */
13487
13488static void
13489cp_parser_label_declaration (cp_parser* parser)
13490{
13491  /* Look for the `__label__' keyword.  */
13492  cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13493
13494  while (true)
13495    {
13496      tree identifier;
13497
13498      /* Look for an identifier.  */
13499      identifier = cp_parser_identifier (parser);
13500      /* Declare it as a lobel.  */
13501      finish_label_decl (identifier);
13502      /* If the next token is a `;', stop.  */
13503      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13504	break;
13505      /* Look for the `,' separating the label declarations.  */
13506      cp_parser_require (parser, CPP_COMMA, "`,'");
13507    }
13508
13509  /* Look for the final `;'.  */
13510  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13511}
13512
13513/* Support Functions */
13514
13515/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13516   NAME should have one of the representations used for an
13517   id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13518   is returned.  If PARSER->SCOPE is a dependent type, then a
13519   SCOPE_REF is returned.
13520
13521   If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13522   returned; the name was already resolved when the TEMPLATE_ID_EXPR
13523   was formed.  Abstractly, such entities should not be passed to this
13524   function, because they do not need to be looked up, but it is
13525   simpler to check for this special case here, rather than at the
13526   call-sites.
13527
13528   In cases not explicitly covered above, this function returns a
13529   DECL, OVERLOAD, or baselink representing the result of the lookup.
13530   If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13531   is returned.
13532
13533   If IS_TYPE is TRUE, bindings that do not refer to types are
13534   ignored.
13535
13536   If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13537   ignored.
13538
13539   If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13540   are ignored.
13541
13542   If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13543   types.  */
13544
13545static tree
13546cp_parser_lookup_name (cp_parser *parser, tree name,
13547		       bool is_type, bool is_template, bool is_namespace,
13548		       bool check_dependency)
13549{
13550  tree decl;
13551  tree object_type = parser->context->object_type;
13552
13553  /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13554     no longer valid.  Note that if we are parsing tentatively, and
13555     the parse fails, OBJECT_TYPE will be automatically restored.  */
13556  parser->context->object_type = NULL_TREE;
13557
13558  if (name == error_mark_node)
13559    return error_mark_node;
13560
13561  /* A template-id has already been resolved; there is no lookup to
13562     do.  */
13563  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13564    return name;
13565  if (BASELINK_P (name))
13566    {
13567      my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13568			   == TEMPLATE_ID_EXPR),
13569			  20020909);
13570      return name;
13571    }
13572
13573  /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
13574     it should already have been checked to make sure that the name
13575     used matches the type being destroyed.  */
13576  if (TREE_CODE (name) == BIT_NOT_EXPR)
13577    {
13578      tree type;
13579
13580      /* Figure out to which type this destructor applies.  */
13581      if (parser->scope)
13582	type = parser->scope;
13583      else if (object_type)
13584	type = object_type;
13585      else
13586	type = current_class_type;
13587      /* If that's not a class type, there is no destructor.  */
13588      if (!type || !CLASS_TYPE_P (type))
13589	return error_mark_node;
13590      if (!CLASSTYPE_DESTRUCTORS (type))
13591	  return error_mark_node;
13592      /* If it was a class type, return the destructor.  */
13593      return CLASSTYPE_DESTRUCTORS (type);
13594    }
13595
13596  /* By this point, the NAME should be an ordinary identifier.  If
13597     the id-expression was a qualified name, the qualifying scope is
13598     stored in PARSER->SCOPE at this point.  */
13599  my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13600		      20000619);
13601
13602  /* Perform the lookup.  */
13603  if (parser->scope)
13604    {
13605      bool dependent_p;
13606
13607      if (parser->scope == error_mark_node)
13608	return error_mark_node;
13609
13610      /* If the SCOPE is dependent, the lookup must be deferred until
13611	 the template is instantiated -- unless we are explicitly
13612	 looking up names in uninstantiated templates.  Even then, we
13613	 cannot look up the name if the scope is not a class type; it
13614	 might, for example, be a template type parameter.  */
13615      dependent_p = (TYPE_P (parser->scope)
13616		     && !(parser->in_declarator_p
13617			  && currently_open_class (parser->scope))
13618		     && dependent_type_p (parser->scope));
13619      if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13620	   && dependent_p)
13621	{
13622	  if (is_type)
13623	    /* The resolution to Core Issue 180 says that `struct A::B'
13624	       should be considered a type-name, even if `A' is
13625	       dependent.  */
13626	    decl = TYPE_NAME (make_typename_type (parser->scope,
13627						  name,
13628						  /*complain=*/1));
13629	  else if (is_template)
13630	    decl = make_unbound_class_template (parser->scope,
13631						name,
13632						/*complain=*/1);
13633	  else
13634	    decl = build_nt (SCOPE_REF, parser->scope, name);
13635	}
13636      else
13637	{
13638	  bool pop_p = false;
13639
13640	  /* If PARSER->SCOPE is a dependent type, then it must be a
13641	     class type, and we must not be checking dependencies;
13642	     otherwise, we would have processed this lookup above.  So
13643	     that PARSER->SCOPE is not considered a dependent base by
13644	     lookup_member, we must enter the scope here.  */
13645	  if (dependent_p)
13646	    pop_p = push_scope (parser->scope);
13647	  /* If the PARSER->SCOPE is a a template specialization, it
13648	     may be instantiated during name lookup.  In that case,
13649	     errors may be issued.  Even if we rollback the current
13650	     tentative parse, those errors are valid.  */
13651	  decl = lookup_qualified_name (parser->scope, name, is_type,
13652					/*complain=*/true);
13653	  if (pop_p)
13654	    pop_scope (parser->scope);
13655	}
13656      parser->qualifying_scope = parser->scope;
13657      parser->object_scope = NULL_TREE;
13658    }
13659  else if (object_type)
13660    {
13661      tree object_decl = NULL_TREE;
13662      /* Look up the name in the scope of the OBJECT_TYPE, unless the
13663	 OBJECT_TYPE is not a class.  */
13664      if (CLASS_TYPE_P (object_type))
13665	/* If the OBJECT_TYPE is a template specialization, it may
13666	   be instantiated during name lookup.  In that case, errors
13667	   may be issued.  Even if we rollback the current tentative
13668	   parse, those errors are valid.  */
13669	object_decl = lookup_member (object_type,
13670				     name,
13671				     /*protect=*/0, is_type);
13672      /* Look it up in the enclosing context, too.  */
13673      decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13674			       is_namespace,
13675			       /*flags=*/0);
13676      parser->object_scope = object_type;
13677      parser->qualifying_scope = NULL_TREE;
13678      if (object_decl)
13679	decl = object_decl;
13680    }
13681  else
13682    {
13683      decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13684			       is_namespace,
13685			       /*flags=*/0);
13686      parser->qualifying_scope = NULL_TREE;
13687      parser->object_scope = NULL_TREE;
13688    }
13689
13690  /* If the lookup failed, let our caller know.  */
13691  if (!decl
13692      || decl == error_mark_node
13693      || (TREE_CODE (decl) == FUNCTION_DECL
13694	  && DECL_ANTICIPATED (decl)))
13695    return error_mark_node;
13696
13697  /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
13698  if (TREE_CODE (decl) == TREE_LIST)
13699    {
13700      /* The error message we have to print is too complicated for
13701	 cp_parser_error, so we incorporate its actions directly.  */
13702      if (!cp_parser_simulate_error (parser))
13703	{
13704	  error ("reference to `%D' is ambiguous", name);
13705	  print_candidates (decl);
13706	}
13707      return error_mark_node;
13708    }
13709
13710  my_friendly_assert (DECL_P (decl)
13711		      || TREE_CODE (decl) == OVERLOAD
13712		      || TREE_CODE (decl) == SCOPE_REF
13713		      || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13714		      || BASELINK_P (decl),
13715		      20000619);
13716
13717  /* If we have resolved the name of a member declaration, check to
13718     see if the declaration is accessible.  When the name resolves to
13719     set of overloaded functions, accessibility is checked when
13720     overload resolution is done.
13721
13722     During an explicit instantiation, access is not checked at all,
13723     as per [temp.explicit].  */
13724  if (DECL_P (decl))
13725    check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13726
13727  return decl;
13728}
13729
13730/* Like cp_parser_lookup_name, but for use in the typical case where
13731   CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13732   IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
13733
13734static tree
13735cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13736{
13737  return cp_parser_lookup_name (parser, name,
13738				/*is_type=*/false,
13739				/*is_template=*/false,
13740				/*is_namespace=*/false,
13741				/*check_dependency=*/true);
13742}
13743
13744/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13745   the current context, return the TYPE_DECL.  If TAG_NAME_P is
13746   true, the DECL indicates the class being defined in a class-head,
13747   or declared in an elaborated-type-specifier.
13748
13749   Otherwise, return DECL.  */
13750
13751static tree
13752cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13753{
13754  /* If the TEMPLATE_DECL is being declared as part of a class-head,
13755     the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13756
13757       struct A {
13758         template <typename T> struct B;
13759       };
13760
13761       template <typename T> struct A::B {};
13762
13763     Similarly, in a elaborated-type-specifier:
13764
13765       namespace N { struct X{}; }
13766
13767       struct A {
13768         template <typename T> friend struct N::X;
13769       };
13770
13771     However, if the DECL refers to a class type, and we are in
13772     the scope of the class, then the name lookup automatically
13773     finds the TYPE_DECL created by build_self_reference rather
13774     than a TEMPLATE_DECL.  For example, in:
13775
13776       template <class T> struct S {
13777         S s;
13778       };
13779
13780     there is no need to handle such case.  */
13781
13782  if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13783    return DECL_TEMPLATE_RESULT (decl);
13784
13785  return decl;
13786}
13787
13788/* If too many, or too few, template-parameter lists apply to the
13789   declarator, issue an error message.  Returns TRUE if all went well,
13790   and FALSE otherwise.  */
13791
13792static bool
13793cp_parser_check_declarator_template_parameters (cp_parser* parser,
13794                                                tree declarator)
13795{
13796  unsigned num_templates;
13797
13798  /* We haven't seen any classes that involve template parameters yet.  */
13799  num_templates = 0;
13800
13801  switch (TREE_CODE (declarator))
13802    {
13803    case CALL_EXPR:
13804    case ARRAY_REF:
13805    case INDIRECT_REF:
13806    case ADDR_EXPR:
13807      {
13808	tree main_declarator = TREE_OPERAND (declarator, 0);
13809	return
13810	  cp_parser_check_declarator_template_parameters (parser,
13811							  main_declarator);
13812      }
13813
13814    case SCOPE_REF:
13815      {
13816	tree scope;
13817	tree member;
13818
13819	scope = TREE_OPERAND (declarator, 0);
13820	member = TREE_OPERAND (declarator, 1);
13821
13822	/* If this is a pointer-to-member, then we are not interested
13823	   in the SCOPE, because it does not qualify the thing that is
13824	   being declared.  */
13825	if (TREE_CODE (member) == INDIRECT_REF)
13826	  return (cp_parser_check_declarator_template_parameters
13827		  (parser, member));
13828
13829	while (scope && CLASS_TYPE_P (scope))
13830	  {
13831	    /* You're supposed to have one `template <...>'
13832	       for every template class, but you don't need one
13833	       for a full specialization.  For example:
13834
13835	       template <class T> struct S{};
13836	       template <> struct S<int> { void f(); };
13837	       void S<int>::f () {}
13838
13839	       is correct; there shouldn't be a `template <>' for
13840	       the definition of `S<int>::f'.  */
13841	    if (CLASSTYPE_TEMPLATE_INFO (scope)
13842		&& (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13843		    || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13844		&& PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13845	      ++num_templates;
13846
13847	    scope = TYPE_CONTEXT (scope);
13848	  }
13849      }
13850
13851      /* Fall through.  */
13852
13853    default:
13854      /* If the DECLARATOR has the form `X<y>' then it uses one
13855	 additional level of template parameters.  */
13856      if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13857	++num_templates;
13858
13859      return cp_parser_check_template_parameters (parser,
13860						  num_templates);
13861    }
13862}
13863
13864/* NUM_TEMPLATES were used in the current declaration.  If that is
13865   invalid, return FALSE and issue an error messages.  Otherwise,
13866   return TRUE.  */
13867
13868static bool
13869cp_parser_check_template_parameters (cp_parser* parser,
13870                                     unsigned num_templates)
13871{
13872  /* If there are more template classes than parameter lists, we have
13873     something like:
13874
13875       template <class T> void S<T>::R<T>::f ();  */
13876  if (parser->num_template_parameter_lists < num_templates)
13877    {
13878      error ("too few template-parameter-lists");
13879      return false;
13880    }
13881  /* If there are the same number of template classes and parameter
13882     lists, that's OK.  */
13883  if (parser->num_template_parameter_lists == num_templates)
13884    return true;
13885  /* If there are more, but only one more, then we are referring to a
13886     member template.  That's OK too.  */
13887  if (parser->num_template_parameter_lists == num_templates + 1)
13888      return true;
13889  /* Otherwise, there are too many template parameter lists.  We have
13890     something like:
13891
13892     template <class T> template <class U> void S::f();  */
13893  error ("too many template-parameter-lists");
13894  return false;
13895}
13896
13897/* Parse a binary-expression of the general form:
13898
13899   binary-expression:
13900     <expr>
13901     binary-expression <token> <expr>
13902
13903   The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
13904   to parser the <expr>s.  If the first production is used, then the
13905   value returned by FN is returned directly.  Otherwise, a node with
13906   the indicated EXPR_TYPE is returned, with operands corresponding to
13907   the two sub-expressions.  */
13908
13909static tree
13910cp_parser_binary_expression (cp_parser* parser,
13911                             const cp_parser_token_tree_map token_tree_map,
13912                             cp_parser_expression_fn fn)
13913{
13914  tree lhs;
13915
13916  /* Parse the first expression.  */
13917  lhs = (*fn) (parser);
13918  /* Now, look for more expressions.  */
13919  while (true)
13920    {
13921      cp_token *token;
13922      const cp_parser_token_tree_map_node *map_node;
13923      tree rhs;
13924
13925      /* Peek at the next token.  */
13926      token = cp_lexer_peek_token (parser->lexer);
13927      /* If the token is `>', and that's not an operator at the
13928	 moment, then we're done.  */
13929      if (token->type == CPP_GREATER
13930	  && !parser->greater_than_is_operator_p)
13931	break;
13932      /* If we find one of the tokens we want, build the corresponding
13933	 tree representation.  */
13934      for (map_node = token_tree_map;
13935	   map_node->token_type != CPP_EOF;
13936	   ++map_node)
13937	if (map_node->token_type == token->type)
13938	  {
13939	    /* Assume that an overloaded operator will not be used.  */
13940	    bool overloaded_p = false;
13941
13942	    /* Consume the operator token.  */
13943	    cp_lexer_consume_token (parser->lexer);
13944	    /* Parse the right-hand side of the expression.  */
13945	    rhs = (*fn) (parser);
13946	    /* Build the binary tree node.  */
13947	    lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
13948				     &overloaded_p);
13949	    /* If the binary operator required the use of an
13950	       overloaded operator, then this expression cannot be an
13951	       integral constant-expression.  An overloaded operator
13952	       can be used even if both operands are otherwise
13953	       permissible in an integral constant-expression if at
13954	       least one of the operands is of enumeration type.  */
13955	    if (overloaded_p
13956		&& (cp_parser_non_integral_constant_expression
13957		    (parser, "calls to overloaded operators")))
13958	      lhs = error_mark_node;
13959	    break;
13960	  }
13961
13962      /* If the token wasn't one of the ones we want, we're done.  */
13963      if (map_node->token_type == CPP_EOF)
13964	break;
13965    }
13966
13967  return lhs;
13968}
13969
13970/* Parse an optional `::' token indicating that the following name is
13971   from the global namespace.  If so, PARSER->SCOPE is set to the
13972   GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13973   unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13974   Returns the new value of PARSER->SCOPE, if the `::' token is
13975   present, and NULL_TREE otherwise.  */
13976
13977static tree
13978cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13979{
13980  cp_token *token;
13981
13982  /* Peek at the next token.  */
13983  token = cp_lexer_peek_token (parser->lexer);
13984  /* If we're looking at a `::' token then we're starting from the
13985     global namespace, not our current location.  */
13986  if (token->type == CPP_SCOPE)
13987    {
13988      /* Consume the `::' token.  */
13989      cp_lexer_consume_token (parser->lexer);
13990      /* Set the SCOPE so that we know where to start the lookup.  */
13991      parser->scope = global_namespace;
13992      parser->qualifying_scope = global_namespace;
13993      parser->object_scope = NULL_TREE;
13994
13995      return parser->scope;
13996    }
13997  else if (!current_scope_valid_p)
13998    {
13999      parser->scope = NULL_TREE;
14000      parser->qualifying_scope = NULL_TREE;
14001      parser->object_scope = NULL_TREE;
14002    }
14003
14004  return NULL_TREE;
14005}
14006
14007/* Returns TRUE if the upcoming token sequence is the start of a
14008   constructor declarator.  If FRIEND_P is true, the declarator is
14009   preceded by the `friend' specifier.  */
14010
14011static bool
14012cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14013{
14014  bool constructor_p;
14015  tree type_decl = NULL_TREE;
14016  bool nested_name_p;
14017  cp_token *next_token;
14018
14019  /* The common case is that this is not a constructor declarator, so
14020     try to avoid doing lots of work if at all possible.  It's not
14021     valid declare a constructor at function scope.  */
14022  if (at_function_scope_p ())
14023    return false;
14024  /* And only certain tokens can begin a constructor declarator.  */
14025  next_token = cp_lexer_peek_token (parser->lexer);
14026  if (next_token->type != CPP_NAME
14027      && next_token->type != CPP_SCOPE
14028      && next_token->type != CPP_NESTED_NAME_SPECIFIER
14029      && next_token->type != CPP_TEMPLATE_ID)
14030    return false;
14031
14032  /* Parse tentatively; we are going to roll back all of the tokens
14033     consumed here.  */
14034  cp_parser_parse_tentatively (parser);
14035  /* Assume that we are looking at a constructor declarator.  */
14036  constructor_p = true;
14037
14038  /* Look for the optional `::' operator.  */
14039  cp_parser_global_scope_opt (parser,
14040			      /*current_scope_valid_p=*/false);
14041  /* Look for the nested-name-specifier.  */
14042  nested_name_p
14043    = (cp_parser_nested_name_specifier_opt (parser,
14044					    /*typename_keyword_p=*/false,
14045					    /*check_dependency_p=*/false,
14046					    /*type_p=*/false,
14047					    /*is_declaration=*/false)
14048       != NULL_TREE);
14049  /* Outside of a class-specifier, there must be a
14050     nested-name-specifier.  */
14051  if (!nested_name_p &&
14052      (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14053       || friend_p))
14054    constructor_p = false;
14055  /* If we still think that this might be a constructor-declarator,
14056     look for a class-name.  */
14057  if (constructor_p)
14058    {
14059      /* If we have:
14060
14061	   template <typename T> struct S { S(); };
14062	   template <typename T> S<T>::S ();
14063
14064	 we must recognize that the nested `S' names a class.
14065	 Similarly, for:
14066
14067	   template <typename T> S<T>::S<T> ();
14068
14069	 we must recognize that the nested `S' names a template.  */
14070      type_decl = cp_parser_class_name (parser,
14071					/*typename_keyword_p=*/false,
14072					/*template_keyword_p=*/false,
14073					/*type_p=*/false,
14074					/*check_dependency_p=*/false,
14075					/*class_head_p=*/false,
14076					/*is_declaration=*/false);
14077      /* If there was no class-name, then this is not a constructor.  */
14078      constructor_p = !cp_parser_error_occurred (parser);
14079    }
14080
14081  /* If we're still considering a constructor, we have to see a `(',
14082     to begin the parameter-declaration-clause, followed by either a
14083     `)', an `...', or a decl-specifier.  We need to check for a
14084     type-specifier to avoid being fooled into thinking that:
14085
14086       S::S (f) (int);
14087
14088     is a constructor.  (It is actually a function named `f' that
14089     takes one parameter (of type `int') and returns a value of type
14090     `S::S'.  */
14091  if (constructor_p
14092      && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14093    {
14094      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14095	  && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14096	  /* A parameter declaration begins with a decl-specifier,
14097	     which is either the "attribute" keyword, a storage class
14098	     specifier, or (usually) a type-specifier.  */
14099	  && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14100	  && !cp_parser_storage_class_specifier_opt (parser))
14101	{
14102	  tree type;
14103	  bool pop_p = false;
14104	  unsigned saved_num_template_parameter_lists;
14105
14106	  /* Names appearing in the type-specifier should be looked up
14107	     in the scope of the class.  */
14108	  if (current_class_type)
14109	    type = NULL_TREE;
14110	  else
14111	    {
14112	      type = TREE_TYPE (type_decl);
14113	      if (TREE_CODE (type) == TYPENAME_TYPE)
14114		{
14115		  type = resolve_typename_type (type,
14116						/*only_current_p=*/false);
14117		  if (type == error_mark_node)
14118		    {
14119		      cp_parser_abort_tentative_parse (parser);
14120		      return false;
14121		    }
14122		}
14123	      pop_p = push_scope (type);
14124	    }
14125
14126	  /* Inside the constructor parameter list, surrounding
14127	     template-parameter-lists do not apply.  */
14128	  saved_num_template_parameter_lists
14129	    = parser->num_template_parameter_lists;
14130	  parser->num_template_parameter_lists = 0;
14131
14132	  /* Look for the type-specifier.  */
14133	  cp_parser_type_specifier (parser,
14134				    CP_PARSER_FLAGS_NONE,
14135				    /*is_friend=*/false,
14136				    /*is_declarator=*/true,
14137				    /*declares_class_or_enum=*/NULL,
14138				    /*is_cv_qualifier=*/NULL);
14139
14140	  parser->num_template_parameter_lists
14141	    = saved_num_template_parameter_lists;
14142
14143	  /* Leave the scope of the class.  */
14144	  if (pop_p)
14145	    pop_scope (type);
14146
14147	  constructor_p = !cp_parser_error_occurred (parser);
14148	}
14149    }
14150  else
14151    constructor_p = false;
14152  /* We did not really want to consume any tokens.  */
14153  cp_parser_abort_tentative_parse (parser);
14154
14155  return constructor_p;
14156}
14157
14158/* Parse the definition of the function given by the DECL_SPECIFIERS,
14159   ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14160   they must be performed once we are in the scope of the function.
14161
14162   Returns the function defined.  */
14163
14164static tree
14165cp_parser_function_definition_from_specifiers_and_declarator
14166  (cp_parser* parser,
14167   tree decl_specifiers,
14168   tree attributes,
14169   tree declarator)
14170{
14171  tree fn;
14172  bool success_p;
14173
14174  /* Begin the function-definition.  */
14175  success_p = begin_function_definition (decl_specifiers,
14176					 attributes,
14177					 declarator);
14178
14179  /* If there were names looked up in the decl-specifier-seq that we
14180     did not check, check them now.  We must wait until we are in the
14181     scope of the function to perform the checks, since the function
14182     might be a friend.  */
14183  perform_deferred_access_checks ();
14184
14185  if (!success_p)
14186    {
14187      /* If begin_function_definition didn't like the definition, skip
14188	 the entire function.  */
14189      error ("invalid function declaration");
14190      cp_parser_skip_to_end_of_block_or_statement (parser);
14191      fn = error_mark_node;
14192    }
14193  else
14194    fn = cp_parser_function_definition_after_declarator (parser,
14195							 /*inline_p=*/false);
14196
14197  return fn;
14198}
14199
14200/* Parse the part of a function-definition that follows the
14201   declarator.  INLINE_P is TRUE iff this function is an inline
14202   function defined with a class-specifier.
14203
14204   Returns the function defined.  */
14205
14206static tree
14207cp_parser_function_definition_after_declarator (cp_parser* parser,
14208						bool inline_p)
14209{
14210  tree fn;
14211  bool ctor_initializer_p = false;
14212  bool saved_in_unbraced_linkage_specification_p;
14213  unsigned saved_num_template_parameter_lists;
14214
14215  /* If the next token is `return', then the code may be trying to
14216     make use of the "named return value" extension that G++ used to
14217     support.  */
14218  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14219    {
14220      /* Consume the `return' keyword.  */
14221      cp_lexer_consume_token (parser->lexer);
14222      /* Look for the identifier that indicates what value is to be
14223	 returned.  */
14224      cp_parser_identifier (parser);
14225      /* Issue an error message.  */
14226      error ("named return values are no longer supported");
14227      /* Skip tokens until we reach the start of the function body.  */
14228      while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14229	     && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14230	cp_lexer_consume_token (parser->lexer);
14231    }
14232  /* The `extern' in `extern "C" void f () { ... }' does not apply to
14233     anything declared inside `f'.  */
14234  saved_in_unbraced_linkage_specification_p
14235    = parser->in_unbraced_linkage_specification_p;
14236  parser->in_unbraced_linkage_specification_p = false;
14237  /* Inside the function, surrounding template-parameter-lists do not
14238     apply.  */
14239  saved_num_template_parameter_lists
14240    = parser->num_template_parameter_lists;
14241  parser->num_template_parameter_lists = 0;
14242  /* If the next token is `try', then we are looking at a
14243     function-try-block.  */
14244  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14245    ctor_initializer_p = cp_parser_function_try_block (parser);
14246  /* A function-try-block includes the function-body, so we only do
14247     this next part if we're not processing a function-try-block.  */
14248  else
14249    ctor_initializer_p
14250      = cp_parser_ctor_initializer_opt_and_function_body (parser);
14251
14252  /* Finish the function.  */
14253  fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14254			(inline_p ? 2 : 0));
14255  /* Generate code for it, if necessary.  */
14256  expand_or_defer_fn (fn);
14257  /* Restore the saved values.  */
14258  parser->in_unbraced_linkage_specification_p
14259    = saved_in_unbraced_linkage_specification_p;
14260  parser->num_template_parameter_lists
14261    = saved_num_template_parameter_lists;
14262
14263  return fn;
14264}
14265
14266/* Parse a template-declaration, assuming that the `export' (and
14267   `extern') keywords, if present, has already been scanned.  MEMBER_P
14268   is as for cp_parser_template_declaration.  */
14269
14270static void
14271cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14272{
14273  tree decl = NULL_TREE;
14274  tree parameter_list;
14275  bool friend_p = false;
14276
14277  /* Look for the `template' keyword.  */
14278  if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14279    return;
14280
14281  /* And the `<'.  */
14282  if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14283    return;
14284
14285  /* If the next token is `>', then we have an invalid
14286     specialization.  Rather than complain about an invalid template
14287     parameter, issue an error message here.  */
14288  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14289    {
14290      cp_parser_error (parser, "invalid explicit specialization");
14291      begin_specialization ();
14292      parameter_list = NULL_TREE;
14293    }
14294  else
14295    {
14296      /* Parse the template parameters.  */
14297      begin_template_parm_list ();
14298      parameter_list = cp_parser_template_parameter_list (parser);
14299      parameter_list = end_template_parm_list (parameter_list);
14300    }
14301
14302  /* Look for the `>'.  */
14303  cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14304  /* We just processed one more parameter list.  */
14305  ++parser->num_template_parameter_lists;
14306  /* If the next token is `template', there are more template
14307     parameters.  */
14308  if (cp_lexer_next_token_is_keyword (parser->lexer,
14309				      RID_TEMPLATE))
14310    cp_parser_template_declaration_after_export (parser, member_p);
14311  else
14312    {
14313      decl = cp_parser_single_declaration (parser,
14314					   member_p,
14315					   &friend_p);
14316
14317      /* If this is a member template declaration, let the front
14318	 end know.  */
14319      if (member_p && !friend_p && decl)
14320	{
14321	  if (TREE_CODE (decl) == TYPE_DECL)
14322	    cp_parser_check_access_in_redeclaration (decl);
14323
14324	  decl = finish_member_template_decl (decl);
14325	}
14326      else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14327	make_friend_class (current_class_type, TREE_TYPE (decl),
14328			   /*complain=*/true);
14329    }
14330  /* We are done with the current parameter list.  */
14331  --parser->num_template_parameter_lists;
14332
14333  /* Finish up.  */
14334  finish_template_decl (parameter_list);
14335
14336  /* Register member declarations.  */
14337  if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14338    finish_member_declaration (decl);
14339
14340  /* If DECL is a function template, we must return to parse it later.
14341     (Even though there is no definition, there might be default
14342     arguments that need handling.)  */
14343  if (member_p && decl
14344      && (TREE_CODE (decl) == FUNCTION_DECL
14345	  || DECL_FUNCTION_TEMPLATE_P (decl)))
14346    TREE_VALUE (parser->unparsed_functions_queues)
14347      = tree_cons (NULL_TREE, decl,
14348		   TREE_VALUE (parser->unparsed_functions_queues));
14349}
14350
14351/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14352   `function-definition' sequence.  MEMBER_P is true, this declaration
14353   appears in a class scope.
14354
14355   Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14356   *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14357
14358static tree
14359cp_parser_single_declaration (cp_parser* parser,
14360			      bool member_p,
14361			      bool* friend_p)
14362{
14363  int declares_class_or_enum;
14364  tree decl = NULL_TREE;
14365  tree decl_specifiers;
14366  tree attributes;
14367  bool function_definition_p = false;
14368
14369  /* Defer access checks until we know what is being declared.  */
14370  push_deferring_access_checks (dk_deferred);
14371
14372  /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14373     alternative.  */
14374  decl_specifiers
14375    = cp_parser_decl_specifier_seq (parser,
14376				    CP_PARSER_FLAGS_OPTIONAL,
14377				    &attributes,
14378				    &declares_class_or_enum);
14379  if (friend_p)
14380    *friend_p = cp_parser_friend_p (decl_specifiers);
14381  /* Gather up the access checks that occurred the
14382     decl-specifier-seq.  */
14383  stop_deferring_access_checks ();
14384
14385  /* Check for the declaration of a template class.  */
14386  if (declares_class_or_enum)
14387    {
14388      if (cp_parser_declares_only_class_p (parser))
14389	{
14390	  decl = shadow_tag (decl_specifiers);
14391	  if (decl)
14392	    decl = TYPE_NAME (decl);
14393	  else
14394	    decl = error_mark_node;
14395	}
14396    }
14397  else
14398    decl = NULL_TREE;
14399  /* If it's not a template class, try for a template function.  If
14400     the next token is a `;', then this declaration does not declare
14401     anything.  But, if there were errors in the decl-specifiers, then
14402     the error might well have come from an attempted class-specifier.
14403     In that case, there's no need to warn about a missing declarator.  */
14404  if (!decl
14405      && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14406	  || !value_member (error_mark_node, decl_specifiers)))
14407    decl = cp_parser_init_declarator (parser,
14408				      decl_specifiers,
14409				      attributes,
14410				      /*function_definition_allowed_p=*/true,
14411				      member_p,
14412				      declares_class_or_enum,
14413				      &function_definition_p);
14414
14415  pop_deferring_access_checks ();
14416
14417  /* Clear any current qualification; whatever comes next is the start
14418     of something new.  */
14419  parser->scope = NULL_TREE;
14420  parser->qualifying_scope = NULL_TREE;
14421  parser->object_scope = NULL_TREE;
14422  /* Look for a trailing `;' after the declaration.  */
14423  if (!function_definition_p
14424      && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14425    cp_parser_skip_to_end_of_block_or_statement (parser);
14426
14427  return decl;
14428}
14429
14430/* Parse a cast-expression that is not the operand of a unary "&".  */
14431
14432static tree
14433cp_parser_simple_cast_expression (cp_parser *parser)
14434{
14435  return cp_parser_cast_expression (parser, /*address_p=*/false);
14436}
14437
14438/* Parse a functional cast to TYPE.  Returns an expression
14439   representing the cast.  */
14440
14441static tree
14442cp_parser_functional_cast (cp_parser* parser, tree type)
14443{
14444  tree expression_list;
14445  tree cast;
14446
14447  expression_list
14448    = cp_parser_parenthesized_expression_list (parser, false,
14449					       /*non_constant_p=*/NULL);
14450
14451  cast = build_functional_cast (type, expression_list);
14452  /* [expr.const]/1: In an integral constant expression "only type
14453     conversions to integral or enumeration type can be used".  */
14454  if (cast != error_mark_node && !type_dependent_expression_p (type)
14455      && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14456    {
14457      if (cp_parser_non_integral_constant_expression
14458	  (parser, "a call to a constructor"))
14459	return error_mark_node;
14460    }
14461  return cast;
14462}
14463
14464/* Save the tokens that make up the body of a member function defined
14465   in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
14466   already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
14467   specifiers applied to the declaration.  Returns the FUNCTION_DECL
14468   for the member function.  */
14469
14470static tree
14471cp_parser_save_member_function_body (cp_parser* parser,
14472				     tree decl_specifiers,
14473				     tree declarator,
14474				     tree attributes)
14475{
14476  cp_token_cache *cache;
14477  tree fn;
14478
14479  /* Create the function-declaration.  */
14480  fn = start_method (decl_specifiers, declarator, attributes);
14481  /* If something went badly wrong, bail out now.  */
14482  if (fn == error_mark_node)
14483    {
14484      /* If there's a function-body, skip it.  */
14485      if (cp_parser_token_starts_function_definition_p
14486	  (cp_lexer_peek_token (parser->lexer)))
14487	cp_parser_skip_to_end_of_block_or_statement (parser);
14488      return error_mark_node;
14489    }
14490
14491  /* Remember it, if there default args to post process.  */
14492  cp_parser_save_default_args (parser, fn);
14493
14494  /* Create a token cache.  */
14495  cache = cp_token_cache_new ();
14496  /* Save away the tokens that make up the body of the
14497     function.  */
14498  cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14499  /* Handle function try blocks.  */
14500  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14501    cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14502
14503  /* Save away the inline definition; we will process it when the
14504     class is complete.  */
14505  DECL_PENDING_INLINE_INFO (fn) = cache;
14506  DECL_PENDING_INLINE_P (fn) = 1;
14507
14508  /* We need to know that this was defined in the class, so that
14509     friend templates are handled correctly.  */
14510  DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14511
14512  /* We're done with the inline definition.  */
14513  finish_method (fn);
14514
14515  /* Add FN to the queue of functions to be parsed later.  */
14516  TREE_VALUE (parser->unparsed_functions_queues)
14517    = tree_cons (NULL_TREE, fn,
14518		 TREE_VALUE (parser->unparsed_functions_queues));
14519
14520  return fn;
14521}
14522
14523/* Parse a template-argument-list, as well as the trailing ">" (but
14524   not the opening ">").  See cp_parser_template_argument_list for the
14525   return value.  */
14526
14527static tree
14528cp_parser_enclosed_template_argument_list (cp_parser* parser)
14529{
14530  tree arguments;
14531  tree saved_scope;
14532  tree saved_qualifying_scope;
14533  tree saved_object_scope;
14534  bool saved_greater_than_is_operator_p;
14535
14536  /* [temp.names]
14537
14538     When parsing a template-id, the first non-nested `>' is taken as
14539     the end of the template-argument-list rather than a greater-than
14540     operator.  */
14541  saved_greater_than_is_operator_p
14542    = parser->greater_than_is_operator_p;
14543  parser->greater_than_is_operator_p = false;
14544  /* Parsing the argument list may modify SCOPE, so we save it
14545     here.  */
14546  saved_scope = parser->scope;
14547  saved_qualifying_scope = parser->qualifying_scope;
14548  saved_object_scope = parser->object_scope;
14549  /* Parse the template-argument-list itself.  */
14550  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14551    arguments = NULL_TREE;
14552  else
14553    arguments = cp_parser_template_argument_list (parser);
14554  /* Look for the `>' that ends the template-argument-list. If we find
14555     a '>>' instead, it's probably just a typo.  */
14556  if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14557    {
14558      if (!saved_greater_than_is_operator_p)
14559	{
14560	  /* If we're in a nested template argument list, the '>>' has to be
14561	    a typo for '> >'. We emit the error message, but we continue
14562	    parsing and we push a '>' as next token, so that the argument
14563	    list will be parsed correctly..  */
14564	  cp_token* token;
14565	  error ("`>>' should be `> >' within a nested template argument list");
14566	  token = cp_lexer_peek_token (parser->lexer);
14567	  token->type = CPP_GREATER;
14568	}
14569      else
14570	{
14571	  /* If this is not a nested template argument list, the '>>' is
14572	    a typo for '>'. Emit an error message and continue.  */
14573	  error ("spurious `>>', use `>' to terminate a template argument list");
14574	  cp_lexer_consume_token (parser->lexer);
14575	}
14576    }
14577  else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14578    error ("missing `>' to terminate the template argument list");
14579  /* The `>' token might be a greater-than operator again now.  */
14580  parser->greater_than_is_operator_p
14581    = saved_greater_than_is_operator_p;
14582  /* Restore the SAVED_SCOPE.  */
14583  parser->scope = saved_scope;
14584  parser->qualifying_scope = saved_qualifying_scope;
14585  parser->object_scope = saved_object_scope;
14586
14587  return arguments;
14588}
14589
14590/* MEMBER_FUNCTION is a member function, or a friend.  If default
14591   arguments, or the body of the function have not yet been parsed,
14592   parse them now.  */
14593
14594static void
14595cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14596{
14597  cp_lexer *saved_lexer;
14598
14599  /* If this member is a template, get the underlying
14600     FUNCTION_DECL.  */
14601  if (DECL_FUNCTION_TEMPLATE_P (member_function))
14602    member_function = DECL_TEMPLATE_RESULT (member_function);
14603
14604  /* There should not be any class definitions in progress at this
14605     point; the bodies of members are only parsed outside of all class
14606     definitions.  */
14607  my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14608  /* While we're parsing the member functions we might encounter more
14609     classes.  We want to handle them right away, but we don't want
14610     them getting mixed up with functions that are currently in the
14611     queue.  */
14612  parser->unparsed_functions_queues
14613    = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14614
14615  /* Make sure that any template parameters are in scope.  */
14616  maybe_begin_member_template_processing (member_function);
14617
14618  /* If the body of the function has not yet been parsed, parse it
14619     now.  */
14620  if (DECL_PENDING_INLINE_P (member_function))
14621    {
14622      tree function_scope;
14623      cp_token_cache *tokens;
14624
14625      /* The function is no longer pending; we are processing it.  */
14626      tokens = DECL_PENDING_INLINE_INFO (member_function);
14627      DECL_PENDING_INLINE_INFO (member_function) = NULL;
14628      DECL_PENDING_INLINE_P (member_function) = 0;
14629      /* If this was an inline function in a local class, enter the scope
14630	 of the containing function.  */
14631      function_scope = decl_function_context (member_function);
14632      if (function_scope)
14633	push_function_context_to (function_scope);
14634
14635      /* Save away the current lexer.  */
14636      saved_lexer = parser->lexer;
14637      /* Make a new lexer to feed us the tokens saved for this function.  */
14638      parser->lexer = cp_lexer_new_from_tokens (tokens);
14639      parser->lexer->next = saved_lexer;
14640
14641      /* Set the current source position to be the location of the first
14642	 token in the saved inline body.  */
14643      cp_lexer_peek_token (parser->lexer);
14644
14645      /* Let the front end know that we going to be defining this
14646	 function.  */
14647      start_function (NULL_TREE, member_function, NULL_TREE,
14648		      SF_PRE_PARSED | SF_INCLASS_INLINE);
14649
14650      /* Now, parse the body of the function.  */
14651      cp_parser_function_definition_after_declarator (parser,
14652						      /*inline_p=*/true);
14653
14654      /* Leave the scope of the containing function.  */
14655      if (function_scope)
14656	pop_function_context_from (function_scope);
14657      /* Restore the lexer.  */
14658      parser->lexer = saved_lexer;
14659    }
14660
14661  /* Remove any template parameters from the symbol table.  */
14662  maybe_end_member_template_processing ();
14663
14664  /* Restore the queue.  */
14665  parser->unparsed_functions_queues
14666    = TREE_CHAIN (parser->unparsed_functions_queues);
14667}
14668
14669/* If DECL contains any default args, remember it on the unparsed
14670   functions queue.  */
14671
14672static void
14673cp_parser_save_default_args (cp_parser* parser, tree decl)
14674{
14675  tree probe;
14676
14677  for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14678       probe;
14679       probe = TREE_CHAIN (probe))
14680    if (TREE_PURPOSE (probe))
14681      {
14682	TREE_PURPOSE (parser->unparsed_functions_queues)
14683	  = tree_cons (NULL_TREE, decl,
14684		       TREE_PURPOSE (parser->unparsed_functions_queues));
14685	break;
14686      }
14687  return;
14688}
14689
14690/* FN is a FUNCTION_DECL which may contains a parameter with an
14691   unparsed DEFAULT_ARG.  Parse the default args now.  */
14692
14693static void
14694cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14695{
14696  cp_lexer *saved_lexer;
14697  cp_token_cache *tokens;
14698  bool saved_local_variables_forbidden_p;
14699  tree parameters;
14700
14701  /* While we're parsing the default args, we might (due to the
14702     statement expression extension) encounter more classes.  We want
14703     to handle them right away, but we don't want them getting mixed
14704     up with default args that are currently in the queue.  */
14705  parser->unparsed_functions_queues
14706    = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14707
14708  for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14709       parameters;
14710       parameters = TREE_CHAIN (parameters))
14711    {
14712      if (!TREE_PURPOSE (parameters)
14713	  || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14714	continue;
14715
14716       /* Save away the current lexer.  */
14717      saved_lexer = parser->lexer;
14718       /* Create a new one, using the tokens we have saved.  */
14719      tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
14720      parser->lexer = cp_lexer_new_from_tokens (tokens);
14721
14722       /* Set the current source position to be the location of the
14723     	  first token in the default argument.  */
14724      cp_lexer_peek_token (parser->lexer);
14725
14726       /* Local variable names (and the `this' keyword) may not appear
14727     	  in a default argument.  */
14728      saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14729      parser->local_variables_forbidden_p = true;
14730       /* Parse the assignment-expression.  */
14731      if (DECL_CLASS_SCOPE_P (fn))
14732	push_nested_class (DECL_CONTEXT (fn));
14733      TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14734      if (DECL_CLASS_SCOPE_P (fn))
14735	pop_nested_class ();
14736
14737      /* If the token stream has not been completely used up, then
14738	 there was extra junk after the end of the default
14739	 argument.  */
14740      if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14741	cp_parser_error (parser, "expected `,'");
14742
14743       /* Restore saved state.  */
14744      parser->lexer = saved_lexer;
14745      parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14746    }
14747
14748  /* Restore the queue.  */
14749  parser->unparsed_functions_queues
14750    = TREE_CHAIN (parser->unparsed_functions_queues);
14751}
14752
14753/* Parse the operand of `sizeof' (or a similar operator).  Returns
14754   either a TYPE or an expression, depending on the form of the
14755   input.  The KEYWORD indicates which kind of expression we have
14756   encountered.  */
14757
14758static tree
14759cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14760{
14761  static const char *format;
14762  tree expr = NULL_TREE;
14763  const char *saved_message;
14764  bool saved_integral_constant_expression_p;
14765
14766  /* Initialize FORMAT the first time we get here.  */
14767  if (!format)
14768    format = "types may not be defined in `%s' expressions";
14769
14770  /* Types cannot be defined in a `sizeof' expression.  Save away the
14771     old message.  */
14772  saved_message = parser->type_definition_forbidden_message;
14773  /* And create the new one.  */
14774  parser->type_definition_forbidden_message
14775    = xmalloc (strlen (format)
14776	       + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14777	       + 1 /* `\0' */);
14778  sprintf ((char *) parser->type_definition_forbidden_message,
14779	   format, IDENTIFIER_POINTER (ridpointers[keyword]));
14780
14781  /* The restrictions on constant-expressions do not apply inside
14782     sizeof expressions.  */
14783  saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14784  parser->integral_constant_expression_p = false;
14785
14786  /* Do not actually evaluate the expression.  */
14787  ++skip_evaluation;
14788  /* If it's a `(', then we might be looking at the type-id
14789     construction.  */
14790  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14791    {
14792      tree type;
14793      bool saved_in_type_id_in_expr_p;
14794
14795      /* We can't be sure yet whether we're looking at a type-id or an
14796	 expression.  */
14797      cp_parser_parse_tentatively (parser);
14798      /* Consume the `('.  */
14799      cp_lexer_consume_token (parser->lexer);
14800      /* Parse the type-id.  */
14801      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14802      parser->in_type_id_in_expr_p = true;
14803      type = cp_parser_type_id (parser);
14804      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14805      /* Now, look for the trailing `)'.  */
14806      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14807      /* If all went well, then we're done.  */
14808      if (cp_parser_parse_definitely (parser))
14809	{
14810	  /* Build a list of decl-specifiers; right now, we have only
14811	     a single type-specifier.  */
14812	  type = build_tree_list (NULL_TREE,
14813				  type);
14814
14815	  /* Call grokdeclarator to figure out what type this is.  */
14816	  expr = grokdeclarator (NULL_TREE,
14817				 type,
14818				 TYPENAME,
14819				 /*initialized=*/0,
14820				 /*attrlist=*/NULL);
14821	}
14822    }
14823
14824  /* If the type-id production did not work out, then we must be
14825     looking at the unary-expression production.  */
14826  if (!expr)
14827    expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14828  /* Go back to evaluating expressions.  */
14829  --skip_evaluation;
14830
14831  /* Free the message we created.  */
14832  free ((char *) parser->type_definition_forbidden_message);
14833  /* And restore the old one.  */
14834  parser->type_definition_forbidden_message = saved_message;
14835  parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14836
14837  return expr;
14838}
14839
14840/* If the current declaration has no declarator, return true.  */
14841
14842static bool
14843cp_parser_declares_only_class_p (cp_parser *parser)
14844{
14845  /* If the next token is a `;' or a `,' then there is no
14846     declarator.  */
14847  return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14848	  || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14849}
14850
14851/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14852   Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
14853
14854static bool
14855cp_parser_friend_p (tree decl_specifiers)
14856{
14857  while (decl_specifiers)
14858    {
14859      /* See if this decl-specifier is `friend'.  */
14860      if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14861	  && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14862	return true;
14863
14864      /* Go on to the next decl-specifier.  */
14865      decl_specifiers = TREE_CHAIN (decl_specifiers);
14866    }
14867
14868  return false;
14869}
14870
14871/* If the next token is of the indicated TYPE, consume it.  Otherwise,
14872   issue an error message indicating that TOKEN_DESC was expected.
14873
14874   Returns the token consumed, if the token had the appropriate type.
14875   Otherwise, returns NULL.  */
14876
14877static cp_token *
14878cp_parser_require (cp_parser* parser,
14879                   enum cpp_ttype type,
14880                   const char* token_desc)
14881{
14882  if (cp_lexer_next_token_is (parser->lexer, type))
14883    return cp_lexer_consume_token (parser->lexer);
14884  else
14885    {
14886      /* Output the MESSAGE -- unless we're parsing tentatively.  */
14887      if (!cp_parser_simulate_error (parser))
14888	{
14889	  char *message = concat ("expected ", token_desc, NULL);
14890	  cp_parser_error (parser, message);
14891	  free (message);
14892	}
14893      return NULL;
14894    }
14895}
14896
14897/* Like cp_parser_require, except that tokens will be skipped until
14898   the desired token is found.  An error message is still produced if
14899   the next token is not as expected.  */
14900
14901static void
14902cp_parser_skip_until_found (cp_parser* parser,
14903                            enum cpp_ttype type,
14904                            const char* token_desc)
14905{
14906  cp_token *token;
14907  unsigned nesting_depth = 0;
14908
14909  if (cp_parser_require (parser, type, token_desc))
14910    return;
14911
14912  /* Skip tokens until the desired token is found.  */
14913  while (true)
14914    {
14915      /* Peek at the next token.  */
14916      token = cp_lexer_peek_token (parser->lexer);
14917      /* If we've reached the token we want, consume it and
14918	 stop.  */
14919      if (token->type == type && !nesting_depth)
14920	{
14921	  cp_lexer_consume_token (parser->lexer);
14922	  return;
14923	}
14924      /* If we've run out of tokens, stop.  */
14925      if (token->type == CPP_EOF)
14926	return;
14927      if (token->type == CPP_OPEN_BRACE
14928	  || token->type == CPP_OPEN_PAREN
14929	  || token->type == CPP_OPEN_SQUARE)
14930	++nesting_depth;
14931      else if (token->type == CPP_CLOSE_BRACE
14932	       || token->type == CPP_CLOSE_PAREN
14933	       || token->type == CPP_CLOSE_SQUARE)
14934	{
14935	  if (nesting_depth-- == 0)
14936	    return;
14937	}
14938      /* Consume this token.  */
14939      cp_lexer_consume_token (parser->lexer);
14940    }
14941}
14942
14943/* If the next token is the indicated keyword, consume it.  Otherwise,
14944   issue an error message indicating that TOKEN_DESC was expected.
14945
14946   Returns the token consumed, if the token had the appropriate type.
14947   Otherwise, returns NULL.  */
14948
14949static cp_token *
14950cp_parser_require_keyword (cp_parser* parser,
14951                           enum rid keyword,
14952                           const char* token_desc)
14953{
14954  cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14955
14956  if (token && token->keyword != keyword)
14957    {
14958      dyn_string_t error_msg;
14959
14960      /* Format the error message.  */
14961      error_msg = dyn_string_new (0);
14962      dyn_string_append_cstr (error_msg, "expected ");
14963      dyn_string_append_cstr (error_msg, token_desc);
14964      cp_parser_error (parser, error_msg->s);
14965      dyn_string_delete (error_msg);
14966      return NULL;
14967    }
14968
14969  return token;
14970}
14971
14972/* Returns TRUE iff TOKEN is a token that can begin the body of a
14973   function-definition.  */
14974
14975static bool
14976cp_parser_token_starts_function_definition_p (cp_token* token)
14977{
14978  return (/* An ordinary function-body begins with an `{'.  */
14979	  token->type == CPP_OPEN_BRACE
14980	  /* A ctor-initializer begins with a `:'.  */
14981	  || token->type == CPP_COLON
14982	  /* A function-try-block begins with `try'.  */
14983	  || token->keyword == RID_TRY
14984	  /* The named return value extension begins with `return'.  */
14985	  || token->keyword == RID_RETURN);
14986}
14987
14988/* Returns TRUE iff the next token is the ":" or "{" beginning a class
14989   definition.  */
14990
14991static bool
14992cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14993{
14994  cp_token *token;
14995
14996  token = cp_lexer_peek_token (parser->lexer);
14997  return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14998}
14999
15000/* Returns TRUE iff the next token is the "," or ">" ending a
15001   template-argument. ">>" is also accepted (after the full
15002   argument was parsed) because it's probably a typo for "> >",
15003   and there is a specific diagnostic for this.  */
15004
15005static bool
15006cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15007{
15008  cp_token *token;
15009
15010  token = cp_lexer_peek_token (parser->lexer);
15011  return (token->type == CPP_COMMA || token->type == CPP_GREATER
15012	  || token->type == CPP_RSHIFT);
15013}
15014
15015/* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15016   (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15017
15018static bool
15019cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15020						     size_t n)
15021{
15022  cp_token *token;
15023
15024  token = cp_lexer_peek_nth_token (parser->lexer, n);
15025  if (token->type == CPP_LESS)
15026    return true;
15027  /* Check for the sequence `<::' in the original code. It would be lexed as
15028     `[:', where `[' is a digraph, and there is no whitespace before
15029     `:'.  */
15030  if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15031    {
15032      cp_token *token2;
15033      token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15034      if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15035	return true;
15036    }
15037  return false;
15038}
15039
15040/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15041   or none_type otherwise.  */
15042
15043static enum tag_types
15044cp_parser_token_is_class_key (cp_token* token)
15045{
15046  switch (token->keyword)
15047    {
15048    case RID_CLASS:
15049      return class_type;
15050    case RID_STRUCT:
15051      return record_type;
15052    case RID_UNION:
15053      return union_type;
15054
15055    default:
15056      return none_type;
15057    }
15058}
15059
15060/* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15061
15062static void
15063cp_parser_check_class_key (enum tag_types class_key, tree type)
15064{
15065  if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15066    pedwarn ("`%s' tag used in naming `%#T'",
15067	    class_key == union_type ? "union"
15068	     : class_key == record_type ? "struct" : "class",
15069	     type);
15070}
15071
15072/* Issue an error message if DECL is redeclared with different
15073   access than its original declaration [class.access.spec/3].
15074   This applies to nested classes and nested class templates.
15075   [class.mem/1].  */
15076
15077static void cp_parser_check_access_in_redeclaration (tree decl)
15078{
15079  if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15080    return;
15081
15082  if ((TREE_PRIVATE (decl)
15083       != (current_access_specifier == access_private_node))
15084      || (TREE_PROTECTED (decl)
15085	  != (current_access_specifier == access_protected_node)))
15086    error ("%D redeclared with different access", decl);
15087}
15088
15089/* Look for the `template' keyword, as a syntactic disambiguator.
15090   Return TRUE iff it is present, in which case it will be
15091   consumed.  */
15092
15093static bool
15094cp_parser_optional_template_keyword (cp_parser *parser)
15095{
15096  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15097    {
15098      /* The `template' keyword can only be used within templates;
15099	 outside templates the parser can always figure out what is a
15100	 template and what is not.  */
15101      if (!processing_template_decl)
15102	{
15103	  error ("`template' (as a disambiguator) is only allowed "
15104		 "within templates");
15105	  /* If this part of the token stream is rescanned, the same
15106	     error message would be generated.  So, we purge the token
15107	     from the stream.  */
15108	  cp_lexer_purge_token (parser->lexer);
15109	  return false;
15110	}
15111      else
15112	{
15113	  /* Consume the `template' keyword.  */
15114	  cp_lexer_consume_token (parser->lexer);
15115	  return true;
15116	}
15117    }
15118
15119  return false;
15120}
15121
15122/* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15123   set PARSER->SCOPE, and perform other related actions.  */
15124
15125static void
15126cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15127{
15128  tree value;
15129  tree check;
15130
15131  /* Get the stored value.  */
15132  value = cp_lexer_consume_token (parser->lexer)->value;
15133  /* Perform any access checks that were deferred.  */
15134  for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15135    perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15136  /* Set the scope from the stored value.  */
15137  parser->scope = TREE_VALUE (value);
15138  parser->qualifying_scope = TREE_TYPE (value);
15139  parser->object_scope = NULL_TREE;
15140}
15141
15142/* Add tokens to CACHE until an non-nested END token appears.  */
15143
15144static void
15145cp_parser_cache_group (cp_parser *parser,
15146		       cp_token_cache *cache,
15147		       enum cpp_ttype end,
15148		       unsigned depth)
15149{
15150  while (true)
15151    {
15152      cp_token *token;
15153
15154      /* Abort a parenthesized expression if we encounter a brace.  */
15155      if ((end == CPP_CLOSE_PAREN || depth == 0)
15156	  && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15157	return;
15158      /* If we've reached the end of the file, stop.  */
15159      if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15160	return;
15161      /* Consume the next token.  */
15162      token = cp_lexer_consume_token (parser->lexer);
15163      /* Add this token to the tokens we are saving.  */
15164      cp_token_cache_push_token (cache, token);
15165      /* See if it starts a new group.  */
15166      if (token->type == CPP_OPEN_BRACE)
15167	{
15168	  cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15169	  if (depth == 0)
15170	    return;
15171	}
15172      else if (token->type == CPP_OPEN_PAREN)
15173	cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15174      else if (token->type == end)
15175	return;
15176    }
15177}
15178
15179/* Begin parsing tentatively.  We always save tokens while parsing
15180   tentatively so that if the tentative parsing fails we can restore the
15181   tokens.  */
15182
15183static void
15184cp_parser_parse_tentatively (cp_parser* parser)
15185{
15186  /* Enter a new parsing context.  */
15187  parser->context = cp_parser_context_new (parser->context);
15188  /* Begin saving tokens.  */
15189  cp_lexer_save_tokens (parser->lexer);
15190  /* In order to avoid repetitive access control error messages,
15191     access checks are queued up until we are no longer parsing
15192     tentatively.  */
15193  push_deferring_access_checks (dk_deferred);
15194}
15195
15196/* Commit to the currently active tentative parse.  */
15197
15198static void
15199cp_parser_commit_to_tentative_parse (cp_parser* parser)
15200{
15201  cp_parser_context *context;
15202  cp_lexer *lexer;
15203
15204  /* Mark all of the levels as committed.  */
15205  lexer = parser->lexer;
15206  for (context = parser->context; context->next; context = context->next)
15207    {
15208      if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15209	break;
15210      context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15211      while (!cp_lexer_saving_tokens (lexer))
15212	lexer = lexer->next;
15213      cp_lexer_commit_tokens (lexer);
15214    }
15215}
15216
15217/* Abort the currently active tentative parse.  All consumed tokens
15218   will be rolled back, and no diagnostics will be issued.  */
15219
15220static void
15221cp_parser_abort_tentative_parse (cp_parser* parser)
15222{
15223  cp_parser_simulate_error (parser);
15224  /* Now, pretend that we want to see if the construct was
15225     successfully parsed.  */
15226  cp_parser_parse_definitely (parser);
15227}
15228
15229/* Stop parsing tentatively.  If a parse error has occurred, restore the
15230   token stream.  Otherwise, commit to the tokens we have consumed.
15231   Returns true if no error occurred; false otherwise.  */
15232
15233static bool
15234cp_parser_parse_definitely (cp_parser* parser)
15235{
15236  bool error_occurred;
15237  cp_parser_context *context;
15238
15239  /* Remember whether or not an error occurred, since we are about to
15240     destroy that information.  */
15241  error_occurred = cp_parser_error_occurred (parser);
15242  /* Remove the topmost context from the stack.  */
15243  context = parser->context;
15244  parser->context = context->next;
15245  /* If no parse errors occurred, commit to the tentative parse.  */
15246  if (!error_occurred)
15247    {
15248      /* Commit to the tokens read tentatively, unless that was
15249	 already done.  */
15250      if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15251	cp_lexer_commit_tokens (parser->lexer);
15252
15253      pop_to_parent_deferring_access_checks ();
15254    }
15255  /* Otherwise, if errors occurred, roll back our state so that things
15256     are just as they were before we began the tentative parse.  */
15257  else
15258    {
15259      cp_lexer_rollback_tokens (parser->lexer);
15260      pop_deferring_access_checks ();
15261    }
15262  /* Add the context to the front of the free list.  */
15263  context->next = cp_parser_context_free_list;
15264  cp_parser_context_free_list = context;
15265
15266  return !error_occurred;
15267}
15268
15269/* Returns true if we are parsing tentatively -- but have decided that
15270   we will stick with this tentative parse, even if errors occur.  */
15271
15272static bool
15273cp_parser_committed_to_tentative_parse (cp_parser* parser)
15274{
15275  return (cp_parser_parsing_tentatively (parser)
15276	  && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15277}
15278
15279/* Returns nonzero iff an error has occurred during the most recent
15280   tentative parse.  */
15281
15282static bool
15283cp_parser_error_occurred (cp_parser* parser)
15284{
15285  return (cp_parser_parsing_tentatively (parser)
15286	  && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15287}
15288
15289/* Returns nonzero if GNU extensions are allowed.  */
15290
15291static bool
15292cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15293{
15294  return parser->allow_gnu_extensions_p;
15295}
15296
15297
15298
15299/* The parser.  */
15300
15301static GTY (()) cp_parser *the_parser;
15302
15303/* External interface.  */
15304
15305/* Parse one entire translation unit.  */
15306
15307void
15308c_parse_file (void)
15309{
15310  bool error_occurred;
15311
15312  the_parser = cp_parser_new ();
15313  push_deferring_access_checks (flag_access_control
15314				? dk_no_deferred : dk_no_check);
15315  error_occurred = cp_parser_translation_unit (the_parser);
15316  the_parser = NULL;
15317}
15318
15319/* This variable must be provided by every front end.  */
15320
15321int yydebug;
15322
15323#include "gt-cp-parser.h"
15324