Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / contrib / gcc-3.4 / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005 Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GCC; see the file COPYING.  If not, write to the Free
20    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21    02111-1307, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38
39 \f
40 /* The lexer.  */
41
42 /* Overview
43    --------
44
45    A cp_lexer represents a stream of cp_tokens.  It allows arbitrary
46    look-ahead.
47
48    Methodology
49    -----------
50
51    We use a circular buffer to store incoming tokens.
52
53    Some artifacts of the C++ language (such as the
54    expression/declaration ambiguity) require arbitrary look-ahead.
55    The strategy we adopt for dealing with these problems is to attempt
56    to parse one construct (e.g., the declaration) and fall back to the
57    other (e.g., the expression) if that attempt does not succeed.
58    Therefore, we must sometimes store an arbitrary number of tokens.
59
60    The parser routinely peeks at the next token, and then consumes it
61    later.  That also requires a buffer in which to store the tokens.
62      
63    In order to easily permit adding tokens to the end of the buffer,
64    while removing them from the beginning of the buffer, we use a
65    circular buffer.  */
66
67 /* A C++ token.  */
68
69 typedef struct cp_token GTY (())
70 {
71   /* The kind of token.  */
72   ENUM_BITFIELD (cpp_ttype) type : 8;
73   /* If this token is a keyword, this value indicates which keyword.
74      Otherwise, this value is RID_MAX.  */
75   ENUM_BITFIELD (rid) keyword : 8;
76   /* Token flags.  */
77   unsigned char flags;
78   /* The value associated with this token, if any.  */
79   tree value;
80   /* The location at which this token was found.  */
81   location_t location;
82 } cp_token;
83
84 /* The number of tokens in a single token block.
85    Computed so that cp_token_block fits in a 512B allocation unit.  */
86
87 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
88
89 /* A group of tokens.  These groups are chained together to store
90    large numbers of tokens.  (For example, a token block is created
91    when the body of an inline member function is first encountered;
92    the tokens are processed later after the class definition is
93    complete.)  
94
95    This somewhat ungainly data structure (as opposed to, say, a
96    variable-length array), is used due to constraints imposed by the
97    current garbage-collection methodology.  If it is made more
98    flexible, we could perhaps simplify the data structures involved.  */
99
100 typedef struct cp_token_block GTY (())
101 {
102   /* The tokens.  */
103   cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
104   /* The number of tokens in this block.  */
105   size_t num_tokens;
106   /* The next token block in the chain.  */
107   struct cp_token_block *next;
108   /* The previous block in the chain.  */
109   struct cp_token_block *prev;
110 } cp_token_block;
111
112 typedef struct cp_token_cache GTY (())
113 {
114   /* The first block in the cache.  NULL if there are no tokens in the
115      cache.  */
116   cp_token_block *first;
117   /* The last block in the cache.  NULL If there are no tokens in the
118      cache.  */
119   cp_token_block *last;
120 } cp_token_cache;
121
122 /* Prototypes.  */
123
124 static cp_token_cache *cp_token_cache_new 
125   (void);
126 static void cp_token_cache_push_token
127   (cp_token_cache *, cp_token *);
128
129 /* Create a new cp_token_cache.  */
130
131 static cp_token_cache *
132 cp_token_cache_new (void)
133 {
134   return ggc_alloc_cleared (sizeof (cp_token_cache));
135 }
136
137 /* Add *TOKEN to *CACHE.  */
138
139 static void
140 cp_token_cache_push_token (cp_token_cache *cache,
141                            cp_token *token)
142 {
143   cp_token_block *b = cache->last;
144
145   /* See if we need to allocate a new token block.  */
146   if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
147     {
148       b = ggc_alloc_cleared (sizeof (cp_token_block));
149       b->prev = cache->last;
150       if (cache->last)
151         {
152           cache->last->next = b;
153           cache->last = b;
154         }
155       else
156         cache->first = cache->last = b;
157     }
158   /* Add this token to the current token block.  */
159   b->tokens[b->num_tokens++] = *token;
160 }
161
162 /* The cp_lexer structure represents the C++ lexer.  It is responsible
163    for managing the token stream from the preprocessor and supplying
164    it to the parser.  */
165
166 typedef struct cp_lexer GTY (())
167 {
168   /* The memory allocated for the buffer.  Never NULL.  */
169   cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
170   /* A pointer just past the end of the memory allocated for the buffer.  */
171   cp_token * GTY ((skip (""))) buffer_end;
172   /* The first valid token in the buffer, or NULL if none.  */
173   cp_token * GTY ((skip (""))) first_token;
174   /* The next available token.  If NEXT_TOKEN is NULL, then there are
175      no more available tokens.  */
176   cp_token * GTY ((skip (""))) next_token;
177   /* A pointer just past the last available token.  If FIRST_TOKEN is
178      NULL, however, there are no available tokens, and then this
179      location is simply the place in which the next token read will be
180      placed.  If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
181      When the LAST_TOKEN == BUFFER, then the last token is at the
182      highest memory address in the BUFFER.  */
183   cp_token * GTY ((skip (""))) last_token;
184
185   /* A stack indicating positions at which cp_lexer_save_tokens was
186      called.  The top entry is the most recent position at which we
187      began saving tokens.  The entries are differences in token
188      position between FIRST_TOKEN and the first saved token.
189
190      If the stack is non-empty, we are saving tokens.  When a token is
191      consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
192      pointer will not.  The token stream will be preserved so that it
193      can be reexamined later.
194
195      If the stack is empty, then we are not saving tokens.  Whenever a
196      token is consumed, the FIRST_TOKEN pointer will be moved, and the
197      consumed token will be gone forever.  */
198   varray_type saved_tokens;
199
200   /* The STRING_CST tokens encountered while processing the current
201      string literal.  */
202   varray_type string_tokens;
203
204   /* True if we should obtain more tokens from the preprocessor; false
205      if we are processing a saved token cache.  */
206   bool main_lexer_p;
207
208   /* True if we should output debugging information.  */
209   bool debugging_p;
210
211   /* The next lexer in a linked list of lexers.  */
212   struct cp_lexer *next;
213 } cp_lexer;
214
215 /* Prototypes.  */
216
217 static cp_lexer *cp_lexer_new_main
218   (void);
219 static cp_lexer *cp_lexer_new_from_tokens
220   (struct cp_token_cache *);
221 static int cp_lexer_saving_tokens
222   (const cp_lexer *);
223 static cp_token *cp_lexer_next_token
224   (cp_lexer *, cp_token *);
225 static cp_token *cp_lexer_prev_token
226   (cp_lexer *, cp_token *);
227 static ptrdiff_t cp_lexer_token_difference 
228   (cp_lexer *, cp_token *, cp_token *);
229 static cp_token *cp_lexer_read_token
230   (cp_lexer *);
231 static void cp_lexer_maybe_grow_buffer
232   (cp_lexer *);
233 static void cp_lexer_get_preprocessor_token
234   (cp_lexer *, cp_token *);
235 static cp_token *cp_lexer_peek_token
236   (cp_lexer *);
237 static cp_token *cp_lexer_peek_nth_token
238   (cp_lexer *, size_t);
239 static inline bool cp_lexer_next_token_is
240   (cp_lexer *, enum cpp_ttype);
241 static bool cp_lexer_next_token_is_not
242   (cp_lexer *, enum cpp_ttype);
243 static bool cp_lexer_next_token_is_keyword
244   (cp_lexer *, enum rid);
245 static cp_token *cp_lexer_consume_token 
246   (cp_lexer *);
247 static void cp_lexer_purge_token
248   (cp_lexer *);
249 static void cp_lexer_purge_tokens_after
250   (cp_lexer *, cp_token *);
251 static void cp_lexer_save_tokens
252   (cp_lexer *);
253 static void cp_lexer_commit_tokens
254   (cp_lexer *);
255 static void cp_lexer_rollback_tokens
256   (cp_lexer *);
257 static inline void cp_lexer_set_source_position_from_token 
258   (cp_lexer *, const cp_token *);
259 static void cp_lexer_print_token
260   (FILE *, cp_token *);
261 static inline bool cp_lexer_debugging_p 
262   (cp_lexer *);
263 static void cp_lexer_start_debugging
264   (cp_lexer *) ATTRIBUTE_UNUSED;
265 static void cp_lexer_stop_debugging
266   (cp_lexer *) ATTRIBUTE_UNUSED;
267
268 /* Manifest constants.  */
269
270 #define CP_TOKEN_BUFFER_SIZE 5
271 #define CP_SAVED_TOKENS_SIZE 5
272
273 /* A token type for keywords, as opposed to ordinary identifiers.  */
274 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
275
276 /* A token type for template-ids.  If a template-id is processed while
277    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
278    the value of the CPP_TEMPLATE_ID is whatever was returned by
279    cp_parser_template_id.  */
280 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
281
282 /* A token type for nested-name-specifiers.  If a
283    nested-name-specifier is processed while parsing tentatively, it is
284    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
285    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
286    cp_parser_nested_name_specifier_opt.  */
287 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
288
289 /* A token type for tokens that are not tokens at all; these are used
290    to mark the end of a token block.  */
291 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
292
293 /* Variables.  */
294
295 /* The stream to which debugging output should be written.  */
296 static FILE *cp_lexer_debug_stream;
297
298 /* Create a new main C++ lexer, the lexer that gets tokens from the
299    preprocessor.  */
300
301 static cp_lexer *
302 cp_lexer_new_main (void)
303 {
304   cp_lexer *lexer;
305   cp_token first_token;
306
307   /* It's possible that lexing the first token will load a PCH file,
308      which is a GC collection point.  So we have to grab the first
309      token before allocating any memory.  */
310   cp_lexer_get_preprocessor_token (NULL, &first_token);
311   c_common_no_more_pch ();
312
313   /* Allocate the memory.  */
314   lexer = ggc_alloc_cleared (sizeof (cp_lexer));
315
316   /* Create the circular buffer.  */
317   lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
318   lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
319
320   /* There is one token in the buffer.  */
321   lexer->last_token = lexer->buffer + 1;
322   lexer->first_token = lexer->buffer;
323   lexer->next_token = lexer->buffer;
324   memcpy (lexer->buffer, &first_token, sizeof (cp_token));
325
326   /* This lexer obtains more tokens by calling c_lex.  */
327   lexer->main_lexer_p = true;
328
329   /* Create the SAVED_TOKENS stack.  */
330   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
331   
332   /* Create the STRINGS array.  */
333   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
334
335   /* Assume we are not debugging.  */
336   lexer->debugging_p = false;
337
338   return lexer;
339 }
340
341 /* Create a new lexer whose token stream is primed with the TOKENS.
342    When these tokens are exhausted, no new tokens will be read.  */
343
344 static cp_lexer *
345 cp_lexer_new_from_tokens (cp_token_cache *tokens)
346 {
347   cp_lexer *lexer;
348   cp_token *token;
349   cp_token_block *block;
350   ptrdiff_t num_tokens;
351
352   /* Allocate the memory.  */
353   lexer = ggc_alloc_cleared (sizeof (cp_lexer));
354
355   /* Create a new buffer, appropriately sized.  */
356   num_tokens = 0;
357   for (block = tokens->first; block != NULL; block = block->next)
358     num_tokens += block->num_tokens;
359   lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
360   lexer->buffer_end = lexer->buffer + num_tokens;
361   
362   /* Install the tokens.  */
363   token = lexer->buffer;
364   for (block = tokens->first; block != NULL; block = block->next)
365     {
366       memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
367       token += block->num_tokens;
368     }
369
370   /* The FIRST_TOKEN is the beginning of the buffer.  */
371   lexer->first_token = lexer->buffer;
372   /* The next available token is also at the beginning of the buffer.  */
373   lexer->next_token = lexer->buffer;
374   /* The buffer is full.  */
375   lexer->last_token = lexer->first_token;
376
377   /* This lexer doesn't obtain more tokens.  */
378   lexer->main_lexer_p = false;
379
380   /* Create the SAVED_TOKENS stack.  */
381   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
382   
383   /* Create the STRINGS array.  */
384   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
385
386   /* Assume we are not debugging.  */
387   lexer->debugging_p = false;
388
389   return lexer;
390 }
391
392 /* Returns nonzero if debugging information should be output.  */
393
394 static inline bool
395 cp_lexer_debugging_p (cp_lexer *lexer)
396 {
397   return lexer->debugging_p;
398 }
399
400 /* Set the current source position from the information stored in
401    TOKEN.  */
402
403 static inline void
404 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
405                                          const cp_token *token)
406 {
407   /* Ideally, the source position information would not be a global
408      variable, but it is.  */
409
410   /* Update the line number.  */
411   if (token->type != CPP_EOF)
412     input_location = token->location;
413 }
414
415 /* TOKEN points into the circular token buffer.  Return a pointer to
416    the next token in the buffer.  */
417
418 static inline cp_token *
419 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
420 {
421   token++;
422   if (token == lexer->buffer_end)
423     token = lexer->buffer;
424   return token;
425 }
426
427 /* TOKEN points into the circular token buffer.  Return a pointer to
428    the previous token in the buffer.  */
429
430 static inline cp_token *
431 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
432 {
433   if (token == lexer->buffer)
434     token = lexer->buffer_end;
435   return token - 1;
436 }
437
438 /* nonzero if we are presently saving tokens.  */
439
440 static int
441 cp_lexer_saving_tokens (const cp_lexer* lexer)
442 {
443   return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
444 }
445
446 /* Return a pointer to the token that is N tokens beyond TOKEN in the
447    buffer.  */
448
449 static cp_token *
450 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
451 {
452   token += n;
453   if (token >= lexer->buffer_end)
454     token = lexer->buffer + (token - lexer->buffer_end);
455   return token;
456 }
457
458 /* Returns the number of times that START would have to be incremented
459    to reach FINISH.  If START and FINISH are the same, returns zero.  */
460
461 static ptrdiff_t
462 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
463 {
464   if (finish >= start)
465     return finish - start;
466   else
467     return ((lexer->buffer_end - lexer->buffer)
468             - (start - finish));
469 }
470
471 /* Obtain another token from the C preprocessor and add it to the
472    token buffer.  Returns the newly read token.  */
473
474 static cp_token *
475 cp_lexer_read_token (cp_lexer* lexer)
476 {
477   cp_token *token;
478
479   /* Make sure there is room in the buffer.  */
480   cp_lexer_maybe_grow_buffer (lexer);
481
482   /* If there weren't any tokens, then this one will be the first.  */
483   if (!lexer->first_token)
484     lexer->first_token = lexer->last_token;
485   /* Similarly, if there were no available tokens, there is one now.  */
486   if (!lexer->next_token)
487     lexer->next_token = lexer->last_token;
488
489   /* Figure out where we're going to store the new token.  */
490   token = lexer->last_token;
491
492   /* Get a new token from the preprocessor.  */
493   cp_lexer_get_preprocessor_token (lexer, token);
494
495   /* Increment LAST_TOKEN.  */
496   lexer->last_token = cp_lexer_next_token (lexer, token);
497
498   /* Strings should have type `const char []'.  Right now, we will
499      have an ARRAY_TYPE that is constant rather than an array of
500      constant elements.
501      FIXME: Make fix_string_type get this right in the first place.  */
502   if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
503       && flag_const_strings)
504     {
505       tree type;
506
507       /* Get the current type.  It will be an ARRAY_TYPE.  */
508       type = TREE_TYPE (token->value);
509       /* Use build_cplus_array_type to rebuild the array, thereby
510          getting the right type.  */
511       type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
512       /* Reset the type of the token.  */
513       TREE_TYPE (token->value) = type;
514     }
515
516   return token;
517 }
518
519 /* If the circular buffer is full, make it bigger.  */
520
521 static void
522 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
523 {
524   /* If the buffer is full, enlarge it.  */
525   if (lexer->last_token == lexer->first_token)
526     {
527       cp_token *new_buffer;
528       cp_token *old_buffer;
529       cp_token *new_first_token;
530       ptrdiff_t buffer_length;
531       size_t num_tokens_to_copy;
532
533       /* Remember the current buffer pointer.  It will become invalid,
534          but we will need to do pointer arithmetic involving this
535          value.  */
536       old_buffer = lexer->buffer;
537       /* Compute the current buffer size.  */
538       buffer_length = lexer->buffer_end - lexer->buffer;
539       /* Allocate a buffer twice as big.  */
540       new_buffer = ggc_realloc (lexer->buffer, 
541                                 2 * buffer_length * sizeof (cp_token));
542       
543       /* Because the buffer is circular, logically consecutive tokens
544          are not necessarily placed consecutively in memory.
545          Therefore, we must keep move the tokens that were before
546          FIRST_TOKEN to the second half of the newly allocated
547          buffer.  */
548       num_tokens_to_copy = (lexer->first_token - old_buffer);
549       memcpy (new_buffer + buffer_length,
550               new_buffer,
551               num_tokens_to_copy * sizeof (cp_token));
552       /* Clear the rest of the buffer.  We never look at this storage,
553          but the garbage collector may.  */
554       memset (new_buffer + buffer_length + num_tokens_to_copy, 0, 
555               (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
556
557       /* Now recompute all of the buffer pointers.  */
558       new_first_token 
559         = new_buffer + (lexer->first_token - old_buffer);
560       if (lexer->next_token != NULL)
561         {
562           ptrdiff_t next_token_delta;
563
564           if (lexer->next_token > lexer->first_token)
565             next_token_delta = lexer->next_token - lexer->first_token;
566           else
567             next_token_delta = 
568               buffer_length - (lexer->first_token - lexer->next_token);
569           lexer->next_token = new_first_token + next_token_delta;
570         }
571       lexer->last_token = new_first_token + buffer_length;
572       lexer->buffer = new_buffer;
573       lexer->buffer_end = new_buffer + buffer_length * 2;
574       lexer->first_token = new_first_token;
575     }
576 }
577
578 /* Store the next token from the preprocessor in *TOKEN.  */
579
580 static void 
581 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
582                                  cp_token *token)
583 {
584   bool done;
585
586   /* If this not the main lexer, return a terminating CPP_EOF token.  */
587   if (lexer != NULL && !lexer->main_lexer_p)
588     {
589       token->type = CPP_EOF;
590       token->location.line = 0;
591       token->location.file = NULL;
592       token->value = NULL_TREE;
593       token->keyword = RID_MAX;
594
595       return;
596     }
597
598   done = false;
599   /* Keep going until we get a token we like.  */
600   while (!done)
601     {
602       /* Get a new token from the preprocessor.  */
603       token->type = c_lex_with_flags (&token->value, &token->flags);
604       /* Issue messages about tokens we cannot process.  */
605       switch (token->type)
606         {
607         case CPP_ATSIGN:
608         case CPP_HASH:
609         case CPP_PASTE:
610           error ("invalid token");
611           break;
612
613         default:
614           /* This is a good token, so we exit the loop.  */
615           done = true;
616           break;
617         }
618     }
619   /* Now we've got our token.  */
620   token->location = input_location;
621
622   /* Check to see if this token is a keyword.  */
623   if (token->type == CPP_NAME 
624       && C_IS_RESERVED_WORD (token->value))
625     {
626       /* Mark this token as a keyword.  */
627       token->type = CPP_KEYWORD;
628       /* Record which keyword.  */
629       token->keyword = C_RID_CODE (token->value);
630       /* Update the value.  Some keywords are mapped to particular
631          entities, rather than simply having the value of the
632          corresponding IDENTIFIER_NODE.  For example, `__const' is
633          mapped to `const'.  */
634       token->value = ridpointers[token->keyword];
635     }
636   else
637     token->keyword = RID_MAX;
638 }
639
640 /* Return a pointer to the next token in the token stream, but do not
641    consume it.  */
642
643 static cp_token *
644 cp_lexer_peek_token (cp_lexer* lexer)
645 {
646   cp_token *token;
647
648   /* If there are no tokens, read one now.  */
649   if (!lexer->next_token)
650     cp_lexer_read_token (lexer);
651
652   /* Provide debugging output.  */
653   if (cp_lexer_debugging_p (lexer))
654     {
655       fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
656       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
657       fprintf (cp_lexer_debug_stream, "\n");
658     }
659
660   token = lexer->next_token;
661   cp_lexer_set_source_position_from_token (lexer, token);
662   return token;
663 }
664
665 /* Return true if the next token has the indicated TYPE.  */
666
667 static bool
668 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
669 {
670   cp_token *token;
671
672   /* Peek at the next token.  */
673   token = cp_lexer_peek_token (lexer);
674   /* Check to see if it has the indicated TYPE.  */
675   return token->type == type;
676 }
677
678 /* Return true if the next token does not have the indicated TYPE.  */
679
680 static bool
681 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
682 {
683   return !cp_lexer_next_token_is (lexer, type);
684 }
685
686 /* Return true if the next token is the indicated KEYWORD.  */
687
688 static bool
689 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
690 {
691   cp_token *token;
692
693   /* Peek at the next token.  */
694   token = cp_lexer_peek_token (lexer);
695   /* Check to see if it is the indicated keyword.  */
696   return token->keyword == keyword;
697 }
698
699 /* Return a pointer to the Nth token in the token stream.  If N is 1,
700    then this is precisely equivalent to cp_lexer_peek_token.  */
701
702 static cp_token *
703 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
704 {
705   cp_token *token;
706
707   /* N is 1-based, not zero-based.  */
708   my_friendly_assert (n > 0, 20000224);
709
710   /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary.  */
711   token = lexer->next_token;
712   /* If there are no tokens in the buffer, get one now.  */
713   if (!token)
714     {
715       cp_lexer_read_token (lexer);
716       token = lexer->next_token;
717     }
718
719   /* Now, read tokens until we have enough.  */
720   while (--n > 0)
721     {
722       /* Advance to the next token.  */
723       token = cp_lexer_next_token (lexer, token);
724       /* If that's all the tokens we have, read a new one.  */
725       if (token == lexer->last_token)
726         token = cp_lexer_read_token (lexer);
727     }
728
729   return token;
730 }
731
732 /* Consume the next token.  The pointer returned is valid only until
733    another token is read.  Callers should preserve copy the token
734    explicitly if they will need its value for a longer period of
735    time.  */
736
737 static cp_token *
738 cp_lexer_consume_token (cp_lexer* lexer)
739 {
740   cp_token *token;
741
742   /* If there are no tokens, read one now.  */
743   if (!lexer->next_token)
744     cp_lexer_read_token (lexer);
745
746   /* Remember the token we'll be returning.  */
747   token = lexer->next_token;
748
749   /* Increment NEXT_TOKEN.  */
750   lexer->next_token = cp_lexer_next_token (lexer, 
751                                            lexer->next_token);
752   /* Check to see if we're all out of tokens.  */
753   if (lexer->next_token == lexer->last_token)
754     lexer->next_token = NULL;
755
756   /* If we're not saving tokens, then move FIRST_TOKEN too.  */
757   if (!cp_lexer_saving_tokens (lexer))
758     {
759       /* If there are no tokens available, set FIRST_TOKEN to NULL.  */
760       if (!lexer->next_token)
761         lexer->first_token = NULL;
762       else
763         lexer->first_token = lexer->next_token;
764     }
765
766   /* Provide debugging output.  */
767   if (cp_lexer_debugging_p (lexer))
768     {
769       fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
770       cp_lexer_print_token (cp_lexer_debug_stream, token);
771       fprintf (cp_lexer_debug_stream, "\n");
772     }
773
774   return token;
775 }
776
777 /* Permanently remove the next token from the token stream.  There
778    must be a valid next token already; this token never reads
779    additional tokens from the preprocessor.  */
780
781 static void
782 cp_lexer_purge_token (cp_lexer *lexer)
783 {
784   cp_token *token;
785   cp_token *next_token;
786
787   token = lexer->next_token;
788   while (true) 
789     {
790       next_token = cp_lexer_next_token (lexer, token);
791       if (next_token == lexer->last_token)
792         break;
793       *token = *next_token;
794       token = next_token;
795     }
796
797   lexer->last_token = token;
798   /* The token purged may have been the only token remaining; if so,
799      clear NEXT_TOKEN.  */
800   if (lexer->next_token == token)
801     lexer->next_token = NULL;
802 }
803
804 /* Permanently remove all tokens after TOKEN, up to, but not
805    including, the token that will be returned next by
806    cp_lexer_peek_token.  */
807
808 static void
809 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
810 {
811   cp_token *peek;
812   cp_token *t1;
813   cp_token *t2;
814
815   if (lexer->next_token)
816     {
817       /* Copy the tokens that have not yet been read to the location
818          immediately following TOKEN.  */
819       t1 = cp_lexer_next_token (lexer, token);
820       t2 = peek = cp_lexer_peek_token (lexer);
821       /* Move tokens into the vacant area between TOKEN and PEEK.  */
822       while (t2 != lexer->last_token)
823         {
824           *t1 = *t2;
825           t1 = cp_lexer_next_token (lexer, t1);
826           t2 = cp_lexer_next_token (lexer, t2);
827         }
828       /* Now, the next available token is right after TOKEN.  */
829       lexer->next_token = cp_lexer_next_token (lexer, token);
830       /* And the last token is wherever we ended up.  */
831       lexer->last_token = t1;
832     }
833   else
834     {
835       /* There are no tokens in the buffer, so there is nothing to
836          copy.  The last token in the buffer is TOKEN itself.  */
837       lexer->last_token = cp_lexer_next_token (lexer, token);
838     }
839 }
840
841 /* Begin saving tokens.  All tokens consumed after this point will be
842    preserved.  */
843
844 static void
845 cp_lexer_save_tokens (cp_lexer* lexer)
846 {
847   /* Provide debugging output.  */
848   if (cp_lexer_debugging_p (lexer))
849     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
850
851   /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
852      restore the tokens if required.  */
853   if (!lexer->next_token)
854     cp_lexer_read_token (lexer);
855
856   VARRAY_PUSH_INT (lexer->saved_tokens,
857                    cp_lexer_token_difference (lexer,
858                                               lexer->first_token,
859                                               lexer->next_token));
860 }
861
862 /* Commit to the portion of the token stream most recently saved.  */
863
864 static void
865 cp_lexer_commit_tokens (cp_lexer* lexer)
866 {
867   /* Provide debugging output.  */
868   if (cp_lexer_debugging_p (lexer))
869     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
870
871   VARRAY_POP (lexer->saved_tokens);
872 }
873
874 /* Return all tokens saved since the last call to cp_lexer_save_tokens
875    to the token stream.  Stop saving tokens.  */
876
877 static void
878 cp_lexer_rollback_tokens (cp_lexer* lexer)
879 {
880   size_t delta;
881
882   /* Provide debugging output.  */
883   if (cp_lexer_debugging_p (lexer))
884     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
885
886   /* Find the token that was the NEXT_TOKEN when we started saving
887      tokens.  */
888   delta = VARRAY_TOP_INT(lexer->saved_tokens);
889   /* Make it the next token again now.  */
890   lexer->next_token = cp_lexer_advance_token (lexer,
891                                               lexer->first_token, 
892                                               delta);
893   /* It might be the case that there were no tokens when we started
894      saving tokens, but that there are some tokens now.  */
895   if (!lexer->next_token && lexer->first_token)
896     lexer->next_token = lexer->first_token;
897
898   /* Stop saving tokens.  */
899   VARRAY_POP (lexer->saved_tokens);
900 }
901
902 /* Print a representation of the TOKEN on the STREAM.  */
903
904 static void
905 cp_lexer_print_token (FILE * stream, cp_token* token)
906 {
907   const char *token_type = NULL;
908
909   /* Figure out what kind of token this is.  */
910   switch (token->type)
911     {
912     case CPP_EQ:
913       token_type = "EQ";
914       break;
915
916     case CPP_COMMA:
917       token_type = "COMMA";
918       break;
919
920     case CPP_OPEN_PAREN:
921       token_type = "OPEN_PAREN";
922       break;
923
924     case CPP_CLOSE_PAREN:
925       token_type = "CLOSE_PAREN";
926       break;
927
928     case CPP_OPEN_BRACE:
929       token_type = "OPEN_BRACE";
930       break;
931
932     case CPP_CLOSE_BRACE:
933       token_type = "CLOSE_BRACE";
934       break;
935
936     case CPP_SEMICOLON:
937       token_type = "SEMICOLON";
938       break;
939
940     case CPP_NAME:
941       token_type = "NAME";
942       break;
943
944     case CPP_EOF:
945       token_type = "EOF";
946       break;
947
948     case CPP_KEYWORD:
949       token_type = "keyword";
950       break;
951
952       /* This is not a token that we know how to handle yet.  */
953     default:
954       break;
955     }
956
957   /* If we have a name for the token, print it out.  Otherwise, we
958      simply give the numeric code.  */
959   if (token_type)
960     fprintf (stream, "%s", token_type);
961   else
962     fprintf (stream, "%d", token->type);
963   /* And, for an identifier, print the identifier name.  */
964   if (token->type == CPP_NAME 
965       /* Some keywords have a value that is not an IDENTIFIER_NODE.
966          For example, `struct' is mapped to an INTEGER_CST.  */
967       || (token->type == CPP_KEYWORD 
968           && TREE_CODE (token->value) == IDENTIFIER_NODE))
969     fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
970 }
971
972 /* Start emitting debugging information.  */
973
974 static void
975 cp_lexer_start_debugging (cp_lexer* lexer)
976 {
977   ++lexer->debugging_p;
978 }
979   
980 /* Stop emitting debugging information.  */
981
982 static void
983 cp_lexer_stop_debugging (cp_lexer* lexer)
984 {
985   --lexer->debugging_p;
986 }
987
988 \f
989 /* The parser.  */
990
991 /* Overview
992    --------
993
994    A cp_parser parses the token stream as specified by the C++
995    grammar.  Its job is purely parsing, not semantic analysis.  For
996    example, the parser breaks the token stream into declarators,
997    expressions, statements, and other similar syntactic constructs.
998    It does not check that the types of the expressions on either side
999    of an assignment-statement are compatible, or that a function is
1000    not declared with a parameter of type `void'.
1001
1002    The parser invokes routines elsewhere in the compiler to perform
1003    semantic analysis and to build up the abstract syntax tree for the
1004    code processed.  
1005
1006    The parser (and the template instantiation code, which is, in a
1007    way, a close relative of parsing) are the only parts of the
1008    compiler that should be calling push_scope and pop_scope, or
1009    related functions.  The parser (and template instantiation code)
1010    keeps track of what scope is presently active; everything else
1011    should simply honor that.  (The code that generates static
1012    initializers may also need to set the scope, in order to check
1013    access control correctly when emitting the initializers.)
1014
1015    Methodology
1016    -----------
1017    
1018    The parser is of the standard recursive-descent variety.  Upcoming
1019    tokens in the token stream are examined in order to determine which
1020    production to use when parsing a non-terminal.  Some C++ constructs
1021    require arbitrary look ahead to disambiguate.  For example, it is
1022    impossible, in the general case, to tell whether a statement is an
1023    expression or declaration without scanning the entire statement.
1024    Therefore, the parser is capable of "parsing tentatively."  When the
1025    parser is not sure what construct comes next, it enters this mode.
1026    Then, while we attempt to parse the construct, the parser queues up
1027    error messages, rather than issuing them immediately, and saves the
1028    tokens it consumes.  If the construct is parsed successfully, the
1029    parser "commits", i.e., it issues any queued error messages and
1030    the tokens that were being preserved are permanently discarded.
1031    If, however, the construct is not parsed successfully, the parser
1032    rolls back its state completely so that it can resume parsing using
1033    a different alternative.
1034
1035    Future Improvements
1036    -------------------
1037    
1038    The performance of the parser could probably be improved
1039    substantially.  Some possible improvements include:
1040
1041      - The expression parser recurses through the various levels of
1042        precedence as specified in the grammar, rather than using an
1043        operator-precedence technique.  Therefore, parsing a simple
1044        identifier requires multiple recursive calls.
1045
1046      - We could often eliminate the need to parse tentatively by
1047        looking ahead a little bit.  In some places, this approach
1048        might not entirely eliminate the need to parse tentatively, but
1049        it might still speed up the average case.  */
1050
1051 /* Flags that are passed to some parsing functions.  These values can
1052    be bitwise-ored together.  */
1053
1054 typedef enum cp_parser_flags
1055 {
1056   /* No flags.  */
1057   CP_PARSER_FLAGS_NONE = 0x0,
1058   /* The construct is optional.  If it is not present, then no error
1059      should be issued.  */
1060   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1061   /* When parsing a type-specifier, do not allow user-defined types.  */
1062   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1063 } cp_parser_flags;
1064
1065 /* The different kinds of declarators we want to parse.  */
1066
1067 typedef enum cp_parser_declarator_kind
1068 {
1069   /* We want an abstract declartor.  */
1070   CP_PARSER_DECLARATOR_ABSTRACT,
1071   /* We want a named declarator.  */
1072   CP_PARSER_DECLARATOR_NAMED,
1073   /* We don't mind, but the name must be an unqualified-id.  */
1074   CP_PARSER_DECLARATOR_EITHER
1075 } cp_parser_declarator_kind;
1076
1077 /* A mapping from a token type to a corresponding tree node type.  */
1078
1079 typedef struct cp_parser_token_tree_map_node
1080 {
1081   /* The token type.  */
1082   ENUM_BITFIELD (cpp_ttype) token_type : 8;
1083   /* The corresponding tree code.  */
1084   ENUM_BITFIELD (tree_code) tree_type : 8;
1085 } cp_parser_token_tree_map_node;
1086
1087 /* A complete map consists of several ordinary entries, followed by a
1088    terminator.  The terminating entry has a token_type of CPP_EOF.  */
1089
1090 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1091
1092 /* The status of a tentative parse.  */
1093
1094 typedef enum cp_parser_status_kind
1095 {
1096   /* No errors have occurred.  */
1097   CP_PARSER_STATUS_KIND_NO_ERROR,
1098   /* An error has occurred.  */
1099   CP_PARSER_STATUS_KIND_ERROR,
1100   /* We are committed to this tentative parse, whether or not an error
1101      has occurred.  */
1102   CP_PARSER_STATUS_KIND_COMMITTED
1103 } cp_parser_status_kind;
1104
1105 /* Context that is saved and restored when parsing tentatively.  */
1106
1107 typedef struct cp_parser_context GTY (())
1108 {
1109   /* If this is a tentative parsing context, the status of the
1110      tentative parse.  */
1111   enum cp_parser_status_kind status;
1112   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1113      that are looked up in this context must be looked up both in the
1114      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1115      the context of the containing expression.  */
1116   tree object_type;
1117   /* The next parsing context in the stack.  */
1118   struct cp_parser_context *next;
1119 } cp_parser_context;
1120
1121 /* Prototypes.  */
1122
1123 /* Constructors and destructors.  */
1124
1125 static cp_parser_context *cp_parser_context_new
1126   (cp_parser_context *);
1127
1128 /* Class variables.  */
1129
1130 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1131
1132 /* Constructors and destructors.  */
1133
1134 /* Construct a new context.  The context below this one on the stack
1135    is given by NEXT.  */
1136
1137 static cp_parser_context *
1138 cp_parser_context_new (cp_parser_context* next)
1139 {
1140   cp_parser_context *context;
1141
1142   /* Allocate the storage.  */
1143   if (cp_parser_context_free_list != NULL)
1144     {
1145       /* Pull the first entry from the free list.  */
1146       context = cp_parser_context_free_list;
1147       cp_parser_context_free_list = context->next;
1148       memset (context, 0, sizeof (*context));
1149     }
1150   else
1151     context = ggc_alloc_cleared (sizeof (cp_parser_context));
1152   /* No errors have occurred yet in this context.  */
1153   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1154   /* If this is not the bottomost context, copy information that we
1155      need from the previous context.  */
1156   if (next)
1157     {
1158       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1159          expression, then we are parsing one in this context, too.  */
1160       context->object_type = next->object_type;
1161       /* Thread the stack.  */
1162       context->next = next;
1163     }
1164
1165   return context;
1166 }
1167
1168 /* The cp_parser structure represents the C++ parser.  */
1169
1170 typedef struct cp_parser GTY(())
1171 {
1172   /* The lexer from which we are obtaining tokens.  */
1173   cp_lexer *lexer;
1174
1175   /* The scope in which names should be looked up.  If NULL_TREE, then
1176      we look up names in the scope that is currently open in the
1177      source program.  If non-NULL, this is either a TYPE or
1178      NAMESPACE_DECL for the scope in which we should look.  
1179
1180      This value is not cleared automatically after a name is looked
1181      up, so we must be careful to clear it before starting a new look
1182      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1183      will look up `Z' in the scope of `X', rather than the current
1184      scope.)  Unfortunately, it is difficult to tell when name lookup
1185      is complete, because we sometimes peek at a token, look it up,
1186      and then decide not to consume it.  */
1187   tree scope;
1188
1189   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1190      last lookup took place.  OBJECT_SCOPE is used if an expression
1191      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1192      respectively.  QUALIFYING_SCOPE is used for an expression of the 
1193      form "X::Y"; it refers to X.  */
1194   tree object_scope;
1195   tree qualifying_scope;
1196
1197   /* A stack of parsing contexts.  All but the bottom entry on the
1198      stack will be tentative contexts.
1199
1200      We parse tentatively in order to determine which construct is in
1201      use in some situations.  For example, in order to determine
1202      whether a statement is an expression-statement or a
1203      declaration-statement we parse it tentatively as a
1204      declaration-statement.  If that fails, we then reparse the same
1205      token stream as an expression-statement.  */
1206   cp_parser_context *context;
1207
1208   /* True if we are parsing GNU C++.  If this flag is not set, then
1209      GNU extensions are not recognized.  */
1210   bool allow_gnu_extensions_p;
1211
1212   /* TRUE if the `>' token should be interpreted as the greater-than
1213      operator.  FALSE if it is the end of a template-id or
1214      template-parameter-list.  */
1215   bool greater_than_is_operator_p;
1216
1217   /* TRUE if default arguments are allowed within a parameter list
1218      that starts at this point. FALSE if only a gnu extension makes
1219      them permissible.  */
1220   bool default_arg_ok_p;
1221   
1222   /* TRUE if we are parsing an integral constant-expression.  See
1223      [expr.const] for a precise definition.  */
1224   bool integral_constant_expression_p;
1225
1226   /* TRUE if we are parsing an integral constant-expression -- but a
1227      non-constant expression should be permitted as well.  This flag
1228      is used when parsing an array bound so that GNU variable-length
1229      arrays are tolerated.  */
1230   bool allow_non_integral_constant_expression_p;
1231
1232   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1233      been seen that makes the expression non-constant.  */
1234   bool non_integral_constant_expression_p;
1235
1236   /* TRUE if we are parsing the argument to "__offsetof__".  */
1237   bool in_offsetof_p;
1238
1239   /* TRUE if local variable names and `this' are forbidden in the
1240      current context.  */
1241   bool local_variables_forbidden_p;
1242
1243   /* TRUE if the declaration we are parsing is part of a
1244      linkage-specification of the form `extern string-literal
1245      declaration'.  */
1246   bool in_unbraced_linkage_specification_p;
1247
1248   /* TRUE if we are presently parsing a declarator, after the
1249      direct-declarator.  */
1250   bool in_declarator_p;
1251
1252   /* TRUE if we are presently parsing a template-argument-list.  */
1253   bool in_template_argument_list_p;
1254
1255   /* TRUE if we are presently parsing the body of an
1256      iteration-statement.  */
1257   bool in_iteration_statement_p;
1258
1259   /* TRUE if we are presently parsing the body of a switch
1260      statement.  */
1261   bool in_switch_statement_p;
1262
1263   /* TRUE if we are parsing a type-id in an expression context.  In
1264      such a situation, both "type (expr)" and "type (type)" are valid
1265      alternatives.  */
1266   bool in_type_id_in_expr_p;
1267
1268   /* If non-NULL, then we are parsing a construct where new type
1269      definitions are not permitted.  The string stored here will be
1270      issued as an error message if a type is defined.  */
1271   const char *type_definition_forbidden_message;
1272
1273   /* A list of lists. The outer list is a stack, used for member
1274      functions of local classes. At each level there are two sub-list,
1275      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1276      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1277      TREE_VALUE's. The functions are chained in reverse declaration
1278      order.
1279
1280      The TREE_PURPOSE sublist contains those functions with default
1281      arguments that need post processing, and the TREE_VALUE sublist
1282      contains those functions with definitions that need post
1283      processing.
1284
1285      These lists can only be processed once the outermost class being
1286      defined is complete.  */
1287   tree unparsed_functions_queues;
1288
1289   /* The number of classes whose definitions are currently in
1290      progress.  */
1291   unsigned num_classes_being_defined;
1292
1293   /* The number of template parameter lists that apply directly to the
1294      current declaration.  */
1295   unsigned num_template_parameter_lists;
1296 } cp_parser;
1297
1298 /* The type of a function that parses some kind of expression.  */
1299 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1300
1301 /* Prototypes.  */
1302
1303 /* Constructors and destructors.  */
1304
1305 static cp_parser *cp_parser_new
1306   (void);
1307
1308 /* Routines to parse various constructs.  
1309
1310    Those that return `tree' will return the error_mark_node (rather
1311    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1312    Sometimes, they will return an ordinary node if error-recovery was
1313    attempted, even though a parse error occurred.  So, to check
1314    whether or not a parse error occurred, you should always use
1315    cp_parser_error_occurred.  If the construct is optional (indicated
1316    either by an `_opt' in the name of the function that does the
1317    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1318    the construct is not present.  */
1319
1320 /* Lexical conventions [gram.lex]  */
1321
1322 static tree cp_parser_identifier
1323   (cp_parser *);
1324
1325 /* Basic concepts [gram.basic]  */
1326
1327 static bool cp_parser_translation_unit
1328   (cp_parser *);
1329
1330 /* Expressions [gram.expr]  */
1331
1332 static tree cp_parser_primary_expression
1333   (cp_parser *, cp_id_kind *, tree *);
1334 static tree cp_parser_id_expression
1335   (cp_parser *, bool, bool, bool *, bool);
1336 static tree cp_parser_unqualified_id
1337   (cp_parser *, bool, bool, bool);
1338 static tree cp_parser_nested_name_specifier_opt
1339   (cp_parser *, bool, bool, bool, bool);
1340 static tree cp_parser_nested_name_specifier
1341   (cp_parser *, bool, bool, bool, bool);
1342 static tree cp_parser_class_or_namespace_name
1343   (cp_parser *, bool, bool, bool, bool, bool);
1344 static tree cp_parser_postfix_expression
1345   (cp_parser *, bool);
1346 static tree cp_parser_parenthesized_expression_list
1347   (cp_parser *, bool, bool *);
1348 static void cp_parser_pseudo_destructor_name
1349   (cp_parser *, tree *, tree *);
1350 static tree cp_parser_unary_expression
1351   (cp_parser *, bool);
1352 static enum tree_code cp_parser_unary_operator
1353   (cp_token *);
1354 static tree cp_parser_new_expression
1355   (cp_parser *);
1356 static tree cp_parser_new_placement
1357   (cp_parser *);
1358 static tree cp_parser_new_type_id
1359   (cp_parser *);
1360 static tree cp_parser_new_declarator_opt
1361   (cp_parser *);
1362 static tree cp_parser_direct_new_declarator
1363   (cp_parser *);
1364 static tree cp_parser_new_initializer
1365   (cp_parser *);
1366 static tree cp_parser_delete_expression
1367   (cp_parser *);
1368 static tree cp_parser_cast_expression 
1369   (cp_parser *, bool);
1370 static tree cp_parser_pm_expression
1371   (cp_parser *);
1372 static tree cp_parser_multiplicative_expression
1373   (cp_parser *);
1374 static tree cp_parser_additive_expression
1375   (cp_parser *);
1376 static tree cp_parser_shift_expression
1377   (cp_parser *);
1378 static tree cp_parser_relational_expression
1379   (cp_parser *);
1380 static tree cp_parser_equality_expression
1381   (cp_parser *);
1382 static tree cp_parser_and_expression
1383   (cp_parser *);
1384 static tree cp_parser_exclusive_or_expression
1385   (cp_parser *);
1386 static tree cp_parser_inclusive_or_expression
1387   (cp_parser *);
1388 static tree cp_parser_logical_and_expression
1389   (cp_parser *);
1390 static tree cp_parser_logical_or_expression 
1391   (cp_parser *);
1392 static tree cp_parser_question_colon_clause
1393   (cp_parser *, tree);
1394 static tree cp_parser_assignment_expression
1395   (cp_parser *);
1396 static enum tree_code cp_parser_assignment_operator_opt
1397   (cp_parser *);
1398 static tree cp_parser_expression
1399   (cp_parser *);
1400 static tree cp_parser_constant_expression
1401   (cp_parser *, bool, bool *);
1402
1403 /* Statements [gram.stmt.stmt]  */
1404
1405 static void cp_parser_statement
1406   (cp_parser *, bool);
1407 static tree cp_parser_labeled_statement
1408   (cp_parser *, bool);
1409 static tree cp_parser_expression_statement
1410   (cp_parser *, bool);
1411 static tree cp_parser_compound_statement
1412   (cp_parser *, bool);
1413 static void cp_parser_statement_seq_opt
1414   (cp_parser *, bool);
1415 static tree cp_parser_selection_statement
1416   (cp_parser *);
1417 static tree cp_parser_condition
1418   (cp_parser *);
1419 static tree cp_parser_iteration_statement
1420   (cp_parser *);
1421 static void cp_parser_for_init_statement
1422   (cp_parser *);
1423 static tree cp_parser_jump_statement
1424   (cp_parser *);
1425 static void cp_parser_declaration_statement
1426   (cp_parser *);
1427
1428 static tree cp_parser_implicitly_scoped_statement
1429   (cp_parser *);
1430 static void cp_parser_already_scoped_statement
1431   (cp_parser *);
1432
1433 /* Declarations [gram.dcl.dcl] */
1434
1435 static void cp_parser_declaration_seq_opt
1436   (cp_parser *);
1437 static void cp_parser_declaration
1438   (cp_parser *);
1439 static void cp_parser_block_declaration
1440   (cp_parser *, bool);
1441 static void cp_parser_simple_declaration
1442   (cp_parser *, bool);
1443 static tree cp_parser_decl_specifier_seq 
1444   (cp_parser *, cp_parser_flags, tree *, int *);
1445 static tree cp_parser_storage_class_specifier_opt
1446   (cp_parser *);
1447 static tree cp_parser_function_specifier_opt
1448   (cp_parser *);
1449 static tree cp_parser_type_specifier
1450   (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1451 static tree cp_parser_simple_type_specifier
1452   (cp_parser *, cp_parser_flags, bool);
1453 static tree cp_parser_type_name
1454   (cp_parser *);
1455 static tree cp_parser_elaborated_type_specifier
1456   (cp_parser *, bool, bool);
1457 static tree cp_parser_enum_specifier
1458   (cp_parser *);
1459 static void cp_parser_enumerator_list
1460   (cp_parser *, tree);
1461 static void cp_parser_enumerator_definition 
1462   (cp_parser *, tree);
1463 static tree cp_parser_namespace_name
1464   (cp_parser *);
1465 static void cp_parser_namespace_definition
1466   (cp_parser *);
1467 static void cp_parser_namespace_body
1468   (cp_parser *);
1469 static tree cp_parser_qualified_namespace_specifier
1470   (cp_parser *);
1471 static void cp_parser_namespace_alias_definition
1472   (cp_parser *);
1473 static void cp_parser_using_declaration
1474   (cp_parser *);
1475 static void cp_parser_using_directive
1476   (cp_parser *);
1477 static void cp_parser_asm_definition
1478   (cp_parser *);
1479 static void cp_parser_linkage_specification
1480   (cp_parser *);
1481
1482 /* Declarators [gram.dcl.decl] */
1483
1484 static tree cp_parser_init_declarator
1485   (cp_parser *, tree, tree, bool, bool, int, bool *);
1486 static tree cp_parser_declarator
1487   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1488 static tree cp_parser_direct_declarator
1489   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1490 static enum tree_code cp_parser_ptr_operator
1491   (cp_parser *, tree *, tree *);
1492 static tree cp_parser_cv_qualifier_seq_opt
1493   (cp_parser *);
1494 static tree cp_parser_cv_qualifier_opt
1495   (cp_parser *);
1496 static tree cp_parser_declarator_id
1497   (cp_parser *);
1498 static tree cp_parser_type_id
1499   (cp_parser *);
1500 static tree cp_parser_type_specifier_seq
1501   (cp_parser *);
1502 static tree cp_parser_parameter_declaration_clause
1503   (cp_parser *);
1504 static tree cp_parser_parameter_declaration_list
1505   (cp_parser *);
1506 static tree cp_parser_parameter_declaration
1507   (cp_parser *, bool, bool *);
1508 static void cp_parser_function_body
1509   (cp_parser *);
1510 static tree cp_parser_initializer
1511   (cp_parser *, bool *, bool *);
1512 static tree cp_parser_initializer_clause
1513   (cp_parser *, bool *);
1514 static tree cp_parser_initializer_list
1515   (cp_parser *, bool *);
1516
1517 static bool cp_parser_ctor_initializer_opt_and_function_body
1518   (cp_parser *);
1519
1520 /* Classes [gram.class] */
1521
1522 static tree cp_parser_class_name
1523   (cp_parser *, bool, bool, bool, bool, bool, bool);
1524 static tree cp_parser_class_specifier
1525   (cp_parser *);
1526 static tree cp_parser_class_head
1527   (cp_parser *, bool *, tree *);
1528 static enum tag_types cp_parser_class_key
1529   (cp_parser *);
1530 static void cp_parser_member_specification_opt
1531   (cp_parser *);
1532 static void cp_parser_member_declaration
1533   (cp_parser *);
1534 static tree cp_parser_pure_specifier
1535   (cp_parser *);
1536 static tree cp_parser_constant_initializer
1537   (cp_parser *);
1538
1539 /* Derived classes [gram.class.derived] */
1540
1541 static tree cp_parser_base_clause
1542   (cp_parser *);
1543 static tree cp_parser_base_specifier
1544   (cp_parser *);
1545
1546 /* Special member functions [gram.special] */
1547
1548 static tree cp_parser_conversion_function_id
1549   (cp_parser *);
1550 static tree cp_parser_conversion_type_id
1551   (cp_parser *);
1552 static tree cp_parser_conversion_declarator_opt
1553   (cp_parser *);
1554 static bool cp_parser_ctor_initializer_opt
1555   (cp_parser *);
1556 static void cp_parser_mem_initializer_list
1557   (cp_parser *);
1558 static tree cp_parser_mem_initializer
1559   (cp_parser *);
1560 static tree cp_parser_mem_initializer_id
1561   (cp_parser *);
1562
1563 /* Overloading [gram.over] */
1564
1565 static tree cp_parser_operator_function_id
1566   (cp_parser *);
1567 static tree cp_parser_operator
1568   (cp_parser *);
1569
1570 /* Templates [gram.temp] */
1571
1572 static void cp_parser_template_declaration
1573   (cp_parser *, bool);
1574 static tree cp_parser_template_parameter_list
1575   (cp_parser *);
1576 static tree cp_parser_template_parameter
1577   (cp_parser *);
1578 static tree cp_parser_type_parameter
1579   (cp_parser *);
1580 static tree cp_parser_template_id
1581   (cp_parser *, bool, bool, bool);
1582 static tree cp_parser_template_name
1583   (cp_parser *, bool, bool, bool, bool *);
1584 static tree cp_parser_template_argument_list
1585   (cp_parser *);
1586 static tree cp_parser_template_argument
1587   (cp_parser *);
1588 static void cp_parser_explicit_instantiation
1589   (cp_parser *);
1590 static void cp_parser_explicit_specialization
1591   (cp_parser *);
1592
1593 /* Exception handling [gram.exception] */
1594
1595 static tree cp_parser_try_block 
1596   (cp_parser *);
1597 static bool cp_parser_function_try_block
1598   (cp_parser *);
1599 static void cp_parser_handler_seq
1600   (cp_parser *);
1601 static void cp_parser_handler
1602   (cp_parser *);
1603 static tree cp_parser_exception_declaration
1604   (cp_parser *);
1605 static tree cp_parser_throw_expression
1606   (cp_parser *);
1607 static tree cp_parser_exception_specification_opt
1608   (cp_parser *);
1609 static tree cp_parser_type_id_list
1610   (cp_parser *);
1611
1612 /* GNU Extensions */
1613
1614 static tree cp_parser_asm_specification_opt
1615   (cp_parser *);
1616 static tree cp_parser_asm_operand_list
1617   (cp_parser *);
1618 static tree cp_parser_asm_clobber_list
1619   (cp_parser *);
1620 static tree cp_parser_attributes_opt
1621   (cp_parser *);
1622 static tree cp_parser_attribute_list
1623   (cp_parser *);
1624 static bool cp_parser_extension_opt
1625   (cp_parser *, int *);
1626 static void cp_parser_label_declaration
1627   (cp_parser *);
1628
1629 /* Utility Routines */
1630
1631 static tree cp_parser_lookup_name
1632   (cp_parser *, tree, bool, bool, bool, bool);
1633 static tree cp_parser_lookup_name_simple
1634   (cp_parser *, tree);
1635 static tree cp_parser_maybe_treat_template_as_class
1636   (tree, bool);
1637 static bool cp_parser_check_declarator_template_parameters
1638   (cp_parser *, tree);
1639 static bool cp_parser_check_template_parameters
1640   (cp_parser *, unsigned);
1641 static tree cp_parser_simple_cast_expression
1642   (cp_parser *);
1643 static tree cp_parser_binary_expression
1644   (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1645 static tree cp_parser_global_scope_opt
1646   (cp_parser *, bool);
1647 static bool cp_parser_constructor_declarator_p
1648   (cp_parser *, bool);
1649 static tree cp_parser_function_definition_from_specifiers_and_declarator
1650   (cp_parser *, tree, tree, tree);
1651 static tree cp_parser_function_definition_after_declarator
1652   (cp_parser *, bool);
1653 static void cp_parser_template_declaration_after_export
1654   (cp_parser *, bool);
1655 static tree cp_parser_single_declaration
1656   (cp_parser *, bool, bool *);
1657 static tree cp_parser_functional_cast
1658   (cp_parser *, tree);
1659 static tree cp_parser_save_member_function_body
1660   (cp_parser *, tree, tree, tree);
1661 static tree cp_parser_enclosed_template_argument_list
1662   (cp_parser *);
1663 static void cp_parser_save_default_args
1664   (cp_parser *, tree);
1665 static void cp_parser_late_parsing_for_member
1666   (cp_parser *, tree);
1667 static void cp_parser_late_parsing_default_args
1668   (cp_parser *, tree);
1669 static tree cp_parser_sizeof_operand
1670   (cp_parser *, enum rid);
1671 static bool cp_parser_declares_only_class_p
1672   (cp_parser *);
1673 static bool cp_parser_friend_p
1674   (tree);
1675 static bool cp_parser_typedef_p
1676   (tree);
1677 static cp_token *cp_parser_require
1678   (cp_parser *, enum cpp_ttype, const char *);
1679 static cp_token *cp_parser_require_keyword
1680   (cp_parser *, enum rid, const char *);
1681 static bool cp_parser_token_starts_function_definition_p 
1682   (cp_token *);
1683 static bool cp_parser_next_token_starts_class_definition_p
1684   (cp_parser *);
1685 static bool cp_parser_next_token_ends_template_argument_p
1686   (cp_parser *);
1687 static bool cp_parser_nth_token_starts_template_argument_list_p
1688   (cp_parser *, size_t);
1689 static enum tag_types cp_parser_token_is_class_key
1690   (cp_token *);
1691 static void cp_parser_check_class_key
1692   (enum tag_types, tree type);
1693 static void cp_parser_check_access_in_redeclaration
1694   (tree type);
1695 static bool cp_parser_optional_template_keyword
1696   (cp_parser *);
1697 static void cp_parser_pre_parsed_nested_name_specifier 
1698   (cp_parser *);
1699 static void cp_parser_cache_group
1700   (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1701 static void cp_parser_parse_tentatively 
1702   (cp_parser *);
1703 static void cp_parser_commit_to_tentative_parse
1704   (cp_parser *);
1705 static void cp_parser_abort_tentative_parse
1706   (cp_parser *);
1707 static bool cp_parser_parse_definitely
1708   (cp_parser *);
1709 static inline bool cp_parser_parsing_tentatively
1710   (cp_parser *);
1711 static bool cp_parser_committed_to_tentative_parse
1712   (cp_parser *);
1713 static void cp_parser_error
1714   (cp_parser *, const char *);
1715 static void cp_parser_name_lookup_error
1716   (cp_parser *, tree, tree, const char *);
1717 static bool cp_parser_simulate_error
1718   (cp_parser *);
1719 static void cp_parser_check_type_definition
1720   (cp_parser *);
1721 static void cp_parser_check_for_definition_in_return_type
1722   (tree, tree);
1723 static void cp_parser_check_for_invalid_template_id
1724   (cp_parser *, tree);
1725 static bool cp_parser_non_integral_constant_expression
1726   (cp_parser *, const char *);
1727 static bool cp_parser_diagnose_invalid_type_name
1728   (cp_parser *);
1729 static int cp_parser_skip_to_closing_parenthesis
1730   (cp_parser *, bool, bool, bool);
1731 static void cp_parser_skip_to_end_of_statement
1732   (cp_parser *);
1733 static void cp_parser_consume_semicolon_at_end_of_statement
1734   (cp_parser *);
1735 static void cp_parser_skip_to_end_of_block_or_statement
1736   (cp_parser *);
1737 static void cp_parser_skip_to_closing_brace
1738   (cp_parser *);
1739 static void cp_parser_skip_until_found
1740   (cp_parser *, enum cpp_ttype, const char *);
1741 static bool cp_parser_error_occurred
1742   (cp_parser *);
1743 static bool cp_parser_allow_gnu_extensions_p
1744   (cp_parser *);
1745 static bool cp_parser_is_string_literal
1746   (cp_token *);
1747 static bool cp_parser_is_keyword 
1748   (cp_token *, enum rid);
1749
1750 /* Returns nonzero if we are parsing tentatively.  */
1751
1752 static inline bool
1753 cp_parser_parsing_tentatively (cp_parser* parser)
1754 {
1755   return parser->context->next != NULL;
1756 }
1757
1758 /* Returns nonzero if TOKEN is a string literal.  */
1759
1760 static bool
1761 cp_parser_is_string_literal (cp_token* token)
1762 {
1763   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1764 }
1765
1766 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1767
1768 static bool
1769 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1770 {
1771   return token->keyword == keyword;
1772 }
1773
1774 /* Issue the indicated error MESSAGE.  */
1775
1776 static void
1777 cp_parser_error (cp_parser* parser, const char* message)
1778 {
1779   /* Output the MESSAGE -- unless we're parsing tentatively.  */
1780   if (!cp_parser_simulate_error (parser))
1781     {
1782       cp_token *token;
1783       token = cp_lexer_peek_token (parser->lexer);
1784       c_parse_error (message, 
1785                      /* Because c_parser_error does not understand
1786                         CPP_KEYWORD, keywords are treated like
1787                         identifiers.  */
1788                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type), 
1789                      token->value);
1790     }
1791 }
1792
1793 /* Issue an error about name-lookup failing.  NAME is the
1794    IDENTIFIER_NODE DECL is the result of
1795    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1796    the thing that we hoped to find.  */
1797
1798 static void
1799 cp_parser_name_lookup_error (cp_parser* parser,
1800                              tree name,
1801                              tree decl,
1802                              const char* desired)
1803 {
1804   /* If name lookup completely failed, tell the user that NAME was not
1805      declared.  */
1806   if (decl == error_mark_node)
1807     {
1808       if (parser->scope && parser->scope != global_namespace)
1809         error ("`%D::%D' has not been declared", 
1810                parser->scope, name);
1811       else if (parser->scope == global_namespace)
1812         error ("`::%D' has not been declared", name);
1813       else
1814         error ("`%D' has not been declared", name);
1815     }
1816   else if (parser->scope && parser->scope != global_namespace)
1817     error ("`%D::%D' %s", parser->scope, name, desired);
1818   else if (parser->scope == global_namespace)
1819     error ("`::%D' %s", name, desired);
1820   else
1821     error ("`%D' %s", name, desired);
1822 }
1823
1824 /* If we are parsing tentatively, remember that an error has occurred
1825    during this tentative parse.  Returns true if the error was
1826    simulated; false if a messgae should be issued by the caller.  */
1827
1828 static bool
1829 cp_parser_simulate_error (cp_parser* parser)
1830 {
1831   if (cp_parser_parsing_tentatively (parser)
1832       && !cp_parser_committed_to_tentative_parse (parser))
1833     {
1834       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1835       return true;
1836     }
1837   return false;
1838 }
1839
1840 /* This function is called when a type is defined.  If type
1841    definitions are forbidden at this point, an error message is
1842    issued.  */
1843
1844 static void
1845 cp_parser_check_type_definition (cp_parser* parser)
1846 {
1847   /* If types are forbidden here, issue a message.  */
1848   if (parser->type_definition_forbidden_message)
1849     /* Use `%s' to print the string in case there are any escape
1850        characters in the message.  */
1851     error ("%s", parser->type_definition_forbidden_message);
1852 }
1853
1854 /* This function is called when the DECLARATOR is processed.  The TYPE
1855    was a type defined in the decl-specifiers.  If it is invalid to
1856    define a type in the decl-specifiers for DECLARATOR, an error is
1857    issued.  */
1858
1859 static void
1860 cp_parser_check_for_definition_in_return_type (tree declarator, tree type)
1861 {
1862   /* [dcl.fct] forbids type definitions in return types.
1863      Unfortunately, it's not easy to know whether or not we are
1864      processing a return type until after the fact.  */
1865   while (declarator
1866          && (TREE_CODE (declarator) == INDIRECT_REF
1867              || TREE_CODE (declarator) == ADDR_EXPR))
1868     declarator = TREE_OPERAND (declarator, 0);
1869   if (declarator
1870       && TREE_CODE (declarator) == CALL_EXPR)
1871     {
1872       error ("new types may not be defined in a return type");
1873       inform ("(perhaps a semicolon is missing after the definition of `%T')",
1874               type);
1875     }
1876 }
1877
1878 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1879    "<" in any valid C++ program.  If the next token is indeed "<",
1880    issue a message warning the user about what appears to be an
1881    invalid attempt to form a template-id.  */
1882
1883 static void
1884 cp_parser_check_for_invalid_template_id (cp_parser* parser, 
1885                                          tree type)
1886 {
1887   ptrdiff_t start;
1888   cp_token *token;
1889
1890   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1891     {
1892       if (TYPE_P (type))
1893         error ("`%T' is not a template", type);
1894       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1895         error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1896       else
1897         error ("invalid template-id");
1898       /* Remember the location of the invalid "<".  */
1899       if (cp_parser_parsing_tentatively (parser)
1900           && !cp_parser_committed_to_tentative_parse (parser))
1901         {
1902           token = cp_lexer_peek_token (parser->lexer);
1903           token = cp_lexer_prev_token (parser->lexer, token);
1904           start = cp_lexer_token_difference (parser->lexer,
1905                                              parser->lexer->first_token,
1906                                              token);
1907         }
1908       else
1909         start = -1;
1910       /* Consume the "<".  */
1911       cp_lexer_consume_token (parser->lexer);
1912       /* Parse the template arguments.  */
1913       cp_parser_enclosed_template_argument_list (parser);
1914       /* Permanently remove the invalid template arguments so that
1915          this error message is not issued again.  */
1916       if (start >= 0)
1917         {
1918           token = cp_lexer_advance_token (parser->lexer,
1919                                           parser->lexer->first_token,
1920                                           start);
1921           cp_lexer_purge_tokens_after (parser->lexer, token);
1922         }
1923     }
1924 }
1925
1926 /* If parsing an integral constant-expression, issue an error message
1927    about the fact that THING appeared and return true.  Otherwise,
1928    return false, marking the current expression as non-constant.  */
1929
1930 static bool
1931 cp_parser_non_integral_constant_expression (cp_parser  *parser,
1932                                             const char *thing)
1933 {
1934   if (parser->integral_constant_expression_p)
1935     {
1936       if (!parser->allow_non_integral_constant_expression_p)
1937         {
1938           error ("%s cannot appear in a constant-expression", thing);
1939           return true;
1940         }
1941       parser->non_integral_constant_expression_p = true;
1942     }
1943   return false;
1944 }
1945
1946 /* Check for a common situation where a type-name should be present,
1947    but is not, and issue a sensible error message.  Returns true if an
1948    invalid type-name was detected.  */
1949
1950 static bool
1951 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1952 {
1953   /* If the next two tokens are both identifiers, the code is
1954      erroneous. The usual cause of this situation is code like:
1955
1956        T t;
1957
1958      where "T" should name a type -- but does not.  */
1959   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1960       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1961     {
1962       tree name;
1963
1964       /* If parsing tentatively, we should commit; we really are
1965          looking at a declaration.  */
1966       /* Consume the first identifier.  */
1967       name = cp_lexer_consume_token (parser->lexer)->value;
1968       /* Issue an error message.  */
1969       error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1970       /* If we're in a template class, it's possible that the user was
1971          referring to a type from a base class.  For example:
1972
1973            template <typename T> struct A { typedef T X; };
1974            template <typename T> struct B : public A<T> { X x; };
1975
1976          The user should have said "typename A<T>::X".  */
1977       if (processing_template_decl && current_class_type)
1978         {
1979           tree b;
1980
1981           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1982                b;
1983                b = TREE_CHAIN (b))
1984             {
1985               tree base_type = BINFO_TYPE (b);
1986               if (CLASS_TYPE_P (base_type) 
1987                   && dependent_type_p (base_type))
1988                 {
1989                   tree field;
1990                   /* Go from a particular instantiation of the
1991                      template (which will have an empty TYPE_FIELDs),
1992                      to the main version.  */
1993                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1994                   for (field = TYPE_FIELDS (base_type);
1995                        field;
1996                        field = TREE_CHAIN (field))
1997                     if (TREE_CODE (field) == TYPE_DECL
1998                         && DECL_NAME (field) == name)
1999                       {
2000                         error ("(perhaps `typename %T::%s' was intended)",
2001                                BINFO_TYPE (b), IDENTIFIER_POINTER (name));
2002                         break;
2003                       }
2004                   if (field)
2005                     break;
2006                 }
2007             }
2008         }
2009       /* Skip to the end of the declaration; there's no point in
2010          trying to process it.  */
2011       cp_parser_skip_to_end_of_statement (parser);
2012       
2013       return true;
2014     }
2015
2016   return false;
2017 }
2018
2019 /* Consume tokens up to, and including, the next non-nested closing `)'. 
2020    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2021    are doing error recovery. Returns -1 if OR_COMMA is true and we
2022    found an unnested comma.  */
2023
2024 static int
2025 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2026                                        bool recovering, 
2027                                        bool or_comma,
2028                                        bool consume_paren)
2029 {
2030   unsigned paren_depth = 0;
2031   unsigned brace_depth = 0;
2032
2033   if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2034       && !cp_parser_committed_to_tentative_parse (parser))
2035     return 0;
2036   
2037   while (true)
2038     {
2039       cp_token *token;
2040       
2041       /* If we've run out of tokens, then there is no closing `)'.  */
2042       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2043         return 0;
2044
2045       token = cp_lexer_peek_token (parser->lexer);
2046       
2047       /* This matches the processing in skip_to_end_of_statement.  */
2048       if (token->type == CPP_SEMICOLON && !brace_depth)
2049         return 0;
2050       if (token->type == CPP_OPEN_BRACE)
2051         ++brace_depth;
2052       if (token->type == CPP_CLOSE_BRACE)
2053         {
2054           if (!brace_depth--)
2055             return 0;
2056         }
2057       if (recovering && or_comma && token->type == CPP_COMMA
2058           && !brace_depth && !paren_depth)
2059         return -1;
2060       
2061       if (!brace_depth)
2062         {
2063           /* If it is an `(', we have entered another level of nesting.  */
2064           if (token->type == CPP_OPEN_PAREN)
2065             ++paren_depth;
2066           /* If it is a `)', then we might be done.  */
2067           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2068             {
2069               if (consume_paren)
2070                 cp_lexer_consume_token (parser->lexer);
2071               return 1;
2072             }
2073         }
2074       
2075       /* Consume the token.  */
2076       cp_lexer_consume_token (parser->lexer);
2077     }
2078 }
2079
2080 /* Consume tokens until we reach the end of the current statement.
2081    Normally, that will be just before consuming a `;'.  However, if a
2082    non-nested `}' comes first, then we stop before consuming that.  */
2083
2084 static void
2085 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2086 {
2087   unsigned nesting_depth = 0;
2088
2089   while (true)
2090     {
2091       cp_token *token;
2092
2093       /* Peek at the next token.  */
2094       token = cp_lexer_peek_token (parser->lexer);
2095       /* If we've run out of tokens, stop.  */
2096       if (token->type == CPP_EOF)
2097         break;
2098       /* If the next token is a `;', we have reached the end of the
2099          statement.  */
2100       if (token->type == CPP_SEMICOLON && !nesting_depth)
2101         break;
2102       /* If the next token is a non-nested `}', then we have reached
2103          the end of the current block.  */
2104       if (token->type == CPP_CLOSE_BRACE)
2105         {
2106           /* If this is a non-nested `}', stop before consuming it.
2107              That way, when confronted with something like:
2108
2109                { 3 + } 
2110
2111              we stop before consuming the closing `}', even though we
2112              have not yet reached a `;'.  */
2113           if (nesting_depth == 0)
2114             break;
2115           /* If it is the closing `}' for a block that we have
2116              scanned, stop -- but only after consuming the token.
2117              That way given:
2118
2119                 void f g () { ... }
2120                 typedef int I;
2121
2122              we will stop after the body of the erroneously declared
2123              function, but before consuming the following `typedef'
2124              declaration.  */
2125           if (--nesting_depth == 0)
2126             {
2127               cp_lexer_consume_token (parser->lexer);
2128               break;
2129             }
2130         }
2131       /* If it the next token is a `{', then we are entering a new
2132          block.  Consume the entire block.  */
2133       else if (token->type == CPP_OPEN_BRACE)
2134         ++nesting_depth;
2135       /* Consume the token.  */
2136       cp_lexer_consume_token (parser->lexer);
2137     }
2138 }
2139
2140 /* This function is called at the end of a statement or declaration.
2141    If the next token is a semicolon, it is consumed; otherwise, error
2142    recovery is attempted.  */
2143
2144 static void
2145 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2146 {
2147   /* Look for the trailing `;'.  */
2148   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2149     {
2150       /* If there is additional (erroneous) input, skip to the end of
2151          the statement.  */
2152       cp_parser_skip_to_end_of_statement (parser);
2153       /* If the next token is now a `;', consume it.  */
2154       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2155         cp_lexer_consume_token (parser->lexer);
2156     }
2157 }
2158
2159 /* Skip tokens until we have consumed an entire block, or until we
2160    have consumed a non-nested `;'.  */
2161
2162 static void
2163 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2164 {
2165   unsigned nesting_depth = 0;
2166
2167   while (true)
2168     {
2169       cp_token *token;
2170
2171       /* Peek at the next token.  */
2172       token = cp_lexer_peek_token (parser->lexer);
2173       /* If we've run out of tokens, stop.  */
2174       if (token->type == CPP_EOF)
2175         break;
2176       /* If the next token is a `;', we have reached the end of the
2177          statement.  */
2178       if (token->type == CPP_SEMICOLON && !nesting_depth)
2179         {
2180           /* Consume the `;'.  */
2181           cp_lexer_consume_token (parser->lexer);
2182           break;
2183         }
2184       /* Consume the token.  */
2185       token = cp_lexer_consume_token (parser->lexer);
2186       /* If the next token is a non-nested `}', then we have reached
2187          the end of the current block.  */
2188       if (token->type == CPP_CLOSE_BRACE 
2189           && (nesting_depth == 0 || --nesting_depth == 0))
2190         break;
2191       /* If it the next token is a `{', then we are entering a new
2192          block.  Consume the entire block.  */
2193       if (token->type == CPP_OPEN_BRACE)
2194         ++nesting_depth;
2195     }
2196 }
2197
2198 /* Skip tokens until a non-nested closing curly brace is the next
2199    token.  */
2200
2201 static void
2202 cp_parser_skip_to_closing_brace (cp_parser *parser)
2203 {
2204   unsigned nesting_depth = 0;
2205
2206   while (true)
2207     {
2208       cp_token *token;
2209
2210       /* Peek at the next token.  */
2211       token = cp_lexer_peek_token (parser->lexer);
2212       /* If we've run out of tokens, stop.  */
2213       if (token->type == CPP_EOF)
2214         break;
2215       /* If the next token is a non-nested `}', then we have reached
2216          the end of the current block.  */
2217       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2218         break;
2219       /* If it the next token is a `{', then we are entering a new
2220          block.  Consume the entire block.  */
2221       else if (token->type == CPP_OPEN_BRACE)
2222         ++nesting_depth;
2223       /* Consume the token.  */
2224       cp_lexer_consume_token (parser->lexer);
2225     }
2226 }
2227
2228 /* Create a new C++ parser.  */
2229
2230 static cp_parser *
2231 cp_parser_new (void)
2232 {
2233   cp_parser *parser;
2234   cp_lexer *lexer;
2235
2236   /* cp_lexer_new_main is called before calling ggc_alloc because
2237      cp_lexer_new_main might load a PCH file.  */
2238   lexer = cp_lexer_new_main ();
2239
2240   parser = ggc_alloc_cleared (sizeof (cp_parser));
2241   parser->lexer = lexer;
2242   parser->context = cp_parser_context_new (NULL);
2243
2244   /* For now, we always accept GNU extensions.  */
2245   parser->allow_gnu_extensions_p = 1;
2246
2247   /* The `>' token is a greater-than operator, not the end of a
2248      template-id.  */
2249   parser->greater_than_is_operator_p = true;
2250
2251   parser->default_arg_ok_p = true;
2252   
2253   /* We are not parsing a constant-expression.  */
2254   parser->integral_constant_expression_p = false;
2255   parser->allow_non_integral_constant_expression_p = false;
2256   parser->non_integral_constant_expression_p = false;
2257
2258   /* We are not parsing offsetof.  */
2259   parser->in_offsetof_p = false;
2260
2261   /* Local variable names are not forbidden.  */
2262   parser->local_variables_forbidden_p = false;
2263
2264   /* We are not processing an `extern "C"' declaration.  */
2265   parser->in_unbraced_linkage_specification_p = false;
2266
2267   /* We are not processing a declarator.  */
2268   parser->in_declarator_p = false;
2269
2270   /* We are not processing a template-argument-list.  */
2271   parser->in_template_argument_list_p = false;
2272
2273   /* We are not in an iteration statement.  */
2274   parser->in_iteration_statement_p = false;
2275
2276   /* We are not in a switch statement.  */
2277   parser->in_switch_statement_p = false;
2278
2279   /* We are not parsing a type-id inside an expression.  */
2280   parser->in_type_id_in_expr_p = false;
2281
2282   /* The unparsed function queue is empty.  */
2283   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2284
2285   /* There are no classes being defined.  */
2286   parser->num_classes_being_defined = 0;
2287
2288   /* No template parameters apply.  */
2289   parser->num_template_parameter_lists = 0;
2290
2291   return parser;
2292 }
2293
2294 /* Lexical conventions [gram.lex]  */
2295
2296 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2297    identifier.  */
2298
2299 static tree 
2300 cp_parser_identifier (cp_parser* parser)
2301 {
2302   cp_token *token;
2303
2304   /* Look for the identifier.  */
2305   token = cp_parser_require (parser, CPP_NAME, "identifier");
2306   /* Return the value.  */
2307   return token ? token->value : error_mark_node;
2308 }
2309
2310 /* Basic concepts [gram.basic]  */
2311
2312 /* Parse a translation-unit.
2313
2314    translation-unit:
2315      declaration-seq [opt]  
2316
2317    Returns TRUE if all went well.  */
2318
2319 static bool
2320 cp_parser_translation_unit (cp_parser* parser)
2321 {
2322   while (true)
2323     {
2324       cp_parser_declaration_seq_opt (parser);
2325
2326       /* If there are no tokens left then all went well.  */
2327       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2328         break;
2329       
2330       /* Otherwise, issue an error message.  */
2331       cp_parser_error (parser, "expected declaration");
2332       return false;
2333     }
2334
2335   /* Consume the EOF token.  */
2336   cp_parser_require (parser, CPP_EOF, "end-of-file");
2337   
2338   /* Finish up.  */
2339   finish_translation_unit ();
2340
2341   /* All went well.  */
2342   return true;
2343 }
2344
2345 /* Expressions [gram.expr] */
2346
2347 /* Parse a primary-expression.
2348
2349    primary-expression:
2350      literal
2351      this
2352      ( expression )
2353      id-expression
2354
2355    GNU Extensions:
2356
2357    primary-expression:
2358      ( compound-statement )
2359      __builtin_va_arg ( assignment-expression , type-id )
2360
2361    literal:
2362      __null
2363
2364    Returns a representation of the expression.  
2365
2366    *IDK indicates what kind of id-expression (if any) was present.  
2367
2368    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2369    used as the operand of a pointer-to-member.  In that case,
2370    *QUALIFYING_CLASS gives the class that is used as the qualifying
2371    class in the pointer-to-member.  */
2372
2373 static tree
2374 cp_parser_primary_expression (cp_parser *parser, 
2375                               cp_id_kind *idk,
2376                               tree *qualifying_class)
2377 {
2378   cp_token *token;
2379
2380   /* Assume the primary expression is not an id-expression.  */
2381   *idk = CP_ID_KIND_NONE;
2382   /* And that it cannot be used as pointer-to-member.  */
2383   *qualifying_class = NULL_TREE;
2384
2385   /* Peek at the next token.  */
2386   token = cp_lexer_peek_token (parser->lexer);
2387   switch (token->type)
2388     {
2389       /* literal:
2390            integer-literal
2391            character-literal
2392            floating-literal
2393            string-literal
2394            boolean-literal  */
2395     case CPP_CHAR:
2396     case CPP_WCHAR:
2397     case CPP_STRING:
2398     case CPP_WSTRING:
2399     case CPP_NUMBER:
2400       token = cp_lexer_consume_token (parser->lexer);
2401       return token->value;
2402
2403     case CPP_OPEN_PAREN:
2404       {
2405         tree expr;
2406         bool saved_greater_than_is_operator_p;
2407
2408         /* Consume the `('.  */
2409         cp_lexer_consume_token (parser->lexer);
2410         /* Within a parenthesized expression, a `>' token is always
2411            the greater-than operator.  */
2412         saved_greater_than_is_operator_p 
2413           = parser->greater_than_is_operator_p;
2414         parser->greater_than_is_operator_p = true;
2415         /* If we see `( { ' then we are looking at the beginning of
2416            a GNU statement-expression.  */
2417         if (cp_parser_allow_gnu_extensions_p (parser)
2418             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2419           {
2420             /* Statement-expressions are not allowed by the standard.  */
2421             if (pedantic)
2422               pedwarn ("ISO C++ forbids braced-groups within expressions");  
2423             
2424             /* And they're not allowed outside of a function-body; you
2425                cannot, for example, write:
2426                
2427                  int i = ({ int j = 3; j + 1; });
2428                
2429                at class or namespace scope.  */
2430             if (!at_function_scope_p ())
2431               error ("statement-expressions are allowed only inside functions");
2432             /* Start the statement-expression.  */
2433             expr = begin_stmt_expr ();
2434             /* Parse the compound-statement.  */
2435             cp_parser_compound_statement (parser, true);
2436             /* Finish up.  */
2437             expr = finish_stmt_expr (expr, false);
2438           }
2439         else
2440           {
2441             /* Parse the parenthesized expression.  */
2442             expr = cp_parser_expression (parser);
2443             /* Let the front end know that this expression was
2444                enclosed in parentheses. This matters in case, for
2445                example, the expression is of the form `A::B', since
2446                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2447                not.  */
2448             finish_parenthesized_expr (expr);
2449           }
2450         /* The `>' token might be the end of a template-id or
2451            template-parameter-list now.  */
2452         parser->greater_than_is_operator_p 
2453           = saved_greater_than_is_operator_p;
2454         /* Consume the `)'.  */
2455         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2456           cp_parser_skip_to_end_of_statement (parser);
2457
2458         return expr;
2459       }
2460
2461     case CPP_KEYWORD:
2462       switch (token->keyword)
2463         {
2464           /* These two are the boolean literals.  */
2465         case RID_TRUE:
2466           cp_lexer_consume_token (parser->lexer);
2467           return boolean_true_node;
2468         case RID_FALSE:
2469           cp_lexer_consume_token (parser->lexer);
2470           return boolean_false_node;
2471           
2472           /* The `__null' literal.  */
2473         case RID_NULL:
2474           cp_lexer_consume_token (parser->lexer);
2475           return null_node;
2476
2477           /* Recognize the `this' keyword.  */
2478         case RID_THIS:
2479           cp_lexer_consume_token (parser->lexer);
2480           if (parser->local_variables_forbidden_p)
2481             {
2482               error ("`this' may not be used in this context");
2483               return error_mark_node;
2484             }
2485           /* Pointers cannot appear in constant-expressions.  */
2486           if (cp_parser_non_integral_constant_expression (parser,
2487                                                           "`this'"))
2488             return error_mark_node;
2489           return finish_this_expr ();
2490
2491           /* The `operator' keyword can be the beginning of an
2492              id-expression.  */
2493         case RID_OPERATOR:
2494           goto id_expression;
2495
2496         case RID_FUNCTION_NAME:
2497         case RID_PRETTY_FUNCTION_NAME:
2498         case RID_C99_FUNCTION_NAME:
2499           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2500              __func__ are the names of variables -- but they are
2501              treated specially.  Therefore, they are handled here,
2502              rather than relying on the generic id-expression logic
2503              below.  Grammatically, these names are id-expressions.  
2504
2505              Consume the token.  */
2506           token = cp_lexer_consume_token (parser->lexer);
2507           /* Look up the name.  */
2508           return finish_fname (token->value);
2509
2510         case RID_VA_ARG:
2511           {
2512             tree expression;
2513             tree type;
2514
2515             /* The `__builtin_va_arg' construct is used to handle
2516                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2517             cp_lexer_consume_token (parser->lexer);
2518             /* Look for the opening `('.  */
2519             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2520             /* Now, parse the assignment-expression.  */
2521             expression = cp_parser_assignment_expression (parser);
2522             /* Look for the `,'.  */
2523             cp_parser_require (parser, CPP_COMMA, "`,'");
2524             /* Parse the type-id.  */
2525             type = cp_parser_type_id (parser);
2526             /* Look for the closing `)'.  */
2527             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2528             /* Using `va_arg' in a constant-expression is not
2529                allowed.  */
2530             if (cp_parser_non_integral_constant_expression (parser,
2531                                                             "`va_arg'"))
2532               return error_mark_node;
2533             return build_x_va_arg (expression, type);
2534           }
2535
2536         case RID_OFFSETOF:
2537           {
2538             tree expression;
2539             bool saved_in_offsetof_p;
2540
2541             /* Consume the "__offsetof__" token.  */
2542             cp_lexer_consume_token (parser->lexer);
2543             /* Consume the opening `('.  */
2544             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2545             /* Parse the parenthesized (almost) constant-expression.  */
2546             saved_in_offsetof_p = parser->in_offsetof_p;
2547             parser->in_offsetof_p = true;
2548             expression 
2549               = cp_parser_constant_expression (parser,
2550                                                /*allow_non_constant_p=*/false,
2551                                                /*non_constant_p=*/NULL);
2552             parser->in_offsetof_p = saved_in_offsetof_p;
2553             /* Consume the closing ')'.  */
2554             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2555
2556             return expression;
2557           }
2558
2559         default:
2560           cp_parser_error (parser, "expected primary-expression");
2561           return error_mark_node;
2562         }
2563
2564       /* An id-expression can start with either an identifier, a
2565          `::' as the beginning of a qualified-id, or the "operator"
2566          keyword.  */
2567     case CPP_NAME:
2568     case CPP_SCOPE:
2569     case CPP_TEMPLATE_ID:
2570     case CPP_NESTED_NAME_SPECIFIER:
2571       {
2572         tree id_expression;
2573         tree decl;
2574         const char *error_msg;
2575
2576       id_expression:
2577         /* Parse the id-expression.  */
2578         id_expression 
2579           = cp_parser_id_expression (parser, 
2580                                      /*template_keyword_p=*/false,
2581                                      /*check_dependency_p=*/true,
2582                                      /*template_p=*/NULL,
2583                                      /*declarator_p=*/false);
2584         if (id_expression == error_mark_node)
2585           return error_mark_node;
2586         /* If we have a template-id, then no further lookup is
2587            required.  If the template-id was for a template-class, we
2588            will sometimes have a TYPE_DECL at this point.  */
2589         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2590             || TREE_CODE (id_expression) == TYPE_DECL)
2591           decl = id_expression;
2592         /* Look up the name.  */
2593         else 
2594           {
2595             decl = cp_parser_lookup_name_simple (parser, id_expression);
2596             /* If name lookup gives us a SCOPE_REF, then the
2597                qualifying scope was dependent.  Just propagate the
2598                name.  */
2599             if (TREE_CODE (decl) == SCOPE_REF)
2600               {
2601                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2602                   *qualifying_class = TREE_OPERAND (decl, 0);
2603                 return decl;
2604               }
2605             /* Check to see if DECL is a local variable in a context
2606                where that is forbidden.  */
2607             if (parser->local_variables_forbidden_p
2608                 && local_variable_p (decl))
2609               {
2610                 /* It might be that we only found DECL because we are
2611                    trying to be generous with pre-ISO scoping rules.
2612                    For example, consider:
2613
2614                      int i;
2615                      void g() {
2616                        for (int i = 0; i < 10; ++i) {}
2617                        extern void f(int j = i);
2618                      }
2619
2620                    Here, name look up will originally find the out 
2621                    of scope `i'.  We need to issue a warning message,
2622                    but then use the global `i'.  */
2623                 decl = check_for_out_of_scope_variable (decl);
2624                 if (local_variable_p (decl))
2625                   {
2626                     error ("local variable `%D' may not appear in this context",
2627                            decl);
2628                     return error_mark_node;
2629                   }
2630               }
2631           }
2632         
2633         decl = finish_id_expression (id_expression, decl, parser->scope, 
2634                                      idk, qualifying_class,
2635                                      parser->integral_constant_expression_p,
2636                                      parser->allow_non_integral_constant_expression_p,
2637                                      &parser->non_integral_constant_expression_p,
2638                                      &error_msg);
2639         if (error_msg)
2640           cp_parser_error (parser, error_msg);
2641         return decl;
2642       }
2643
2644       /* Anything else is an error.  */
2645     default:
2646       cp_parser_error (parser, "expected primary-expression");
2647       return error_mark_node;
2648     }
2649 }
2650
2651 /* Parse an id-expression.
2652
2653    id-expression:
2654      unqualified-id
2655      qualified-id
2656
2657    qualified-id:
2658      :: [opt] nested-name-specifier template [opt] unqualified-id
2659      :: identifier
2660      :: operator-function-id
2661      :: template-id
2662
2663    Return a representation of the unqualified portion of the
2664    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2665    a `::' or nested-name-specifier.
2666
2667    Often, if the id-expression was a qualified-id, the caller will
2668    want to make a SCOPE_REF to represent the qualified-id.  This
2669    function does not do this in order to avoid wastefully creating
2670    SCOPE_REFs when they are not required.
2671
2672    If TEMPLATE_KEYWORD_P is true, then we have just seen the
2673    `template' keyword.
2674
2675    If CHECK_DEPENDENCY_P is false, then names are looked up inside
2676    uninstantiated templates.  
2677
2678    If *TEMPLATE_P is non-NULL, it is set to true iff the
2679    `template' keyword is used to explicitly indicate that the entity
2680    named is a template.  
2681
2682    If DECLARATOR_P is true, the id-expression is appearing as part of
2683    a declarator, rather than as part of an expression.  */
2684
2685 static tree
2686 cp_parser_id_expression (cp_parser *parser,
2687                          bool template_keyword_p,
2688                          bool check_dependency_p,
2689                          bool *template_p,
2690                          bool declarator_p)
2691 {
2692   bool global_scope_p;
2693   bool nested_name_specifier_p;
2694
2695   /* Assume the `template' keyword was not used.  */
2696   if (template_p)
2697     *template_p = false;
2698
2699   /* Look for the optional `::' operator.  */
2700   global_scope_p 
2701     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) 
2702        != NULL_TREE);
2703   /* Look for the optional nested-name-specifier.  */
2704   nested_name_specifier_p 
2705     = (cp_parser_nested_name_specifier_opt (parser,
2706                                             /*typename_keyword_p=*/false,
2707                                             check_dependency_p,
2708                                             /*type_p=*/false,
2709                                             declarator_p)
2710        != NULL_TREE);
2711   /* If there is a nested-name-specifier, then we are looking at
2712      the first qualified-id production.  */
2713   if (nested_name_specifier_p)
2714     {
2715       tree saved_scope;
2716       tree saved_object_scope;
2717       tree saved_qualifying_scope;
2718       tree unqualified_id;
2719       bool is_template;
2720
2721       /* See if the next token is the `template' keyword.  */
2722       if (!template_p)
2723         template_p = &is_template;
2724       *template_p = cp_parser_optional_template_keyword (parser);
2725       /* Name lookup we do during the processing of the
2726          unqualified-id might obliterate SCOPE.  */
2727       saved_scope = parser->scope;
2728       saved_object_scope = parser->object_scope;
2729       saved_qualifying_scope = parser->qualifying_scope;
2730       /* Process the final unqualified-id.  */
2731       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2732                                                  check_dependency_p,
2733                                                  declarator_p);
2734       /* Restore the SAVED_SCOPE for our caller.  */
2735       parser->scope = saved_scope;
2736       parser->object_scope = saved_object_scope;
2737       parser->qualifying_scope = saved_qualifying_scope;
2738
2739       return unqualified_id;
2740     }
2741   /* Otherwise, if we are in global scope, then we are looking at one
2742      of the other qualified-id productions.  */
2743   else if (global_scope_p)
2744     {
2745       cp_token *token;
2746       tree id;
2747
2748       /* Peek at the next token.  */
2749       token = cp_lexer_peek_token (parser->lexer);
2750
2751       /* If it's an identifier, and the next token is not a "<", then
2752          we can avoid the template-id case.  This is an optimization
2753          for this common case.  */
2754       if (token->type == CPP_NAME 
2755           && !cp_parser_nth_token_starts_template_argument_list_p 
2756                (parser, 2))
2757         return cp_parser_identifier (parser);
2758
2759       cp_parser_parse_tentatively (parser);
2760       /* Try a template-id.  */
2761       id = cp_parser_template_id (parser, 
2762                                   /*template_keyword_p=*/false,
2763                                   /*check_dependency_p=*/true,
2764                                   declarator_p);
2765       /* If that worked, we're done.  */
2766       if (cp_parser_parse_definitely (parser))
2767         return id;
2768
2769       /* Peek at the next token.  (Changes in the token buffer may
2770          have invalidated the pointer obtained above.)  */
2771       token = cp_lexer_peek_token (parser->lexer);
2772
2773       switch (token->type)
2774         {
2775         case CPP_NAME:
2776           return cp_parser_identifier (parser);
2777
2778         case CPP_KEYWORD:
2779           if (token->keyword == RID_OPERATOR)
2780             return cp_parser_operator_function_id (parser);
2781           /* Fall through.  */
2782           
2783         default:
2784           cp_parser_error (parser, "expected id-expression");
2785           return error_mark_node;
2786         }
2787     }
2788   else
2789     return cp_parser_unqualified_id (parser, template_keyword_p,
2790                                      /*check_dependency_p=*/true,
2791                                      declarator_p);
2792 }
2793
2794 /* Parse an unqualified-id.
2795
2796    unqualified-id:
2797      identifier
2798      operator-function-id
2799      conversion-function-id
2800      ~ class-name
2801      template-id
2802
2803    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2804    keyword, in a construct like `A::template ...'.
2805
2806    Returns a representation of unqualified-id.  For the `identifier'
2807    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
2808    production a BIT_NOT_EXPR is returned; the operand of the
2809    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
2810    other productions, see the documentation accompanying the
2811    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
2812    names are looked up in uninstantiated templates.  If DECLARATOR_P
2813    is true, the unqualified-id is appearing as part of a declarator,
2814    rather than as part of an expression.  */
2815
2816 static tree
2817 cp_parser_unqualified_id (cp_parser* parser, 
2818                           bool template_keyword_p,
2819                           bool check_dependency_p,
2820                           bool declarator_p)
2821 {
2822   cp_token *token;
2823
2824   /* Peek at the next token.  */
2825   token = cp_lexer_peek_token (parser->lexer);
2826   
2827   switch (token->type)
2828     {
2829     case CPP_NAME:
2830       {
2831         tree id;
2832
2833         /* We don't know yet whether or not this will be a
2834            template-id.  */
2835         cp_parser_parse_tentatively (parser);
2836         /* Try a template-id.  */
2837         id = cp_parser_template_id (parser, template_keyword_p,
2838                                     check_dependency_p,
2839                                     declarator_p);
2840         /* If it worked, we're done.  */
2841         if (cp_parser_parse_definitely (parser))
2842           return id;
2843         /* Otherwise, it's an ordinary identifier.  */
2844         return cp_parser_identifier (parser);
2845       }
2846
2847     case CPP_TEMPLATE_ID:
2848       return cp_parser_template_id (parser, template_keyword_p,
2849                                     check_dependency_p,
2850                                     declarator_p);
2851
2852     case CPP_COMPL:
2853       {
2854         tree type_decl;
2855         tree qualifying_scope;
2856         tree object_scope;
2857         tree scope;
2858         bool done;
2859
2860         /* Consume the `~' token.  */
2861         cp_lexer_consume_token (parser->lexer);
2862         /* Parse the class-name.  The standard, as written, seems to
2863            say that:
2864
2865              template <typename T> struct S { ~S (); };
2866              template <typename T> S<T>::~S() {}
2867
2868            is invalid, since `~' must be followed by a class-name, but
2869            `S<T>' is dependent, and so not known to be a class.
2870            That's not right; we need to look in uninstantiated
2871            templates.  A further complication arises from:
2872
2873              template <typename T> void f(T t) {
2874                t.T::~T();
2875              } 
2876
2877            Here, it is not possible to look up `T' in the scope of `T'
2878            itself.  We must look in both the current scope, and the
2879            scope of the containing complete expression.  
2880
2881            Yet another issue is:
2882
2883              struct S {
2884                int S;
2885                ~S();
2886              };
2887
2888              S::~S() {}
2889
2890            The standard does not seem to say that the `S' in `~S'
2891            should refer to the type `S' and not the data member
2892            `S::S'.  */
2893
2894         /* DR 244 says that we look up the name after the "~" in the
2895            same scope as we looked up the qualifying name.  That idea
2896            isn't fully worked out; it's more complicated than that.  */
2897         scope = parser->scope;
2898         object_scope = parser->object_scope;
2899         qualifying_scope = parser->qualifying_scope;
2900
2901         /* If the name is of the form "X::~X" it's OK.  */
2902         if (scope && TYPE_P (scope)
2903             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2904             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
2905                 == CPP_OPEN_PAREN)
2906             && (cp_lexer_peek_token (parser->lexer)->value 
2907                 == TYPE_IDENTIFIER (scope)))
2908           {
2909             cp_lexer_consume_token (parser->lexer);
2910             return build_nt (BIT_NOT_EXPR, scope);
2911           }
2912
2913         /* If there was an explicit qualification (S::~T), first look
2914            in the scope given by the qualification (i.e., S).  */
2915         done = false;
2916         type_decl = NULL_TREE;
2917         if (scope)
2918           {
2919             cp_parser_parse_tentatively (parser);
2920             type_decl = cp_parser_class_name (parser, 
2921                                               /*typename_keyword_p=*/false,
2922                                               /*template_keyword_p=*/false,
2923                                               /*type_p=*/false,
2924                                               /*check_dependency=*/false,
2925                                               /*class_head_p=*/false,
2926                                               declarator_p);
2927             if (cp_parser_parse_definitely (parser))
2928               done = true;
2929           }
2930         /* In "N::S::~S", look in "N" as well.  */
2931         if (!done && scope && qualifying_scope)
2932           {
2933             cp_parser_parse_tentatively (parser);
2934             parser->scope = qualifying_scope;
2935             parser->object_scope = NULL_TREE;
2936             parser->qualifying_scope = NULL_TREE;
2937             type_decl 
2938               = cp_parser_class_name (parser, 
2939                                       /*typename_keyword_p=*/false,
2940                                       /*template_keyword_p=*/false,
2941                                       /*type_p=*/false,
2942                                       /*check_dependency=*/false,
2943                                       /*class_head_p=*/false,
2944                                       declarator_p);
2945             if (cp_parser_parse_definitely (parser))
2946               done = true;
2947           }
2948         /* In "p->S::~T", look in the scope given by "*p" as well.  */
2949         else if (!done && object_scope)
2950           {
2951             cp_parser_parse_tentatively (parser);
2952             parser->scope = object_scope;
2953             parser->object_scope = NULL_TREE;
2954             parser->qualifying_scope = NULL_TREE;
2955             type_decl 
2956               = cp_parser_class_name (parser, 
2957                                       /*typename_keyword_p=*/false,
2958                                       /*template_keyword_p=*/false,
2959                                       /*type_p=*/false,
2960                                       /*check_dependency=*/false,
2961                                       /*class_head_p=*/false,
2962                                       declarator_p);
2963             if (cp_parser_parse_definitely (parser))
2964               done = true;
2965           }
2966         /* Look in the surrounding context.  */
2967         if (!done)
2968           {
2969             parser->scope = NULL_TREE;
2970             parser->object_scope = NULL_TREE;
2971             parser->qualifying_scope = NULL_TREE;
2972             type_decl 
2973               = cp_parser_class_name (parser, 
2974                                       /*typename_keyword_p=*/false,
2975                                       /*template_keyword_p=*/false,
2976                                       /*type_p=*/false,
2977                                       /*check_dependency=*/false,
2978                                       /*class_head_p=*/false,
2979                                       declarator_p);
2980           }
2981         /* If an error occurred, assume that the name of the
2982            destructor is the same as the name of the qualifying
2983            class.  That allows us to keep parsing after running
2984            into ill-formed destructor names.  */
2985         if (type_decl == error_mark_node && scope && TYPE_P (scope))
2986           return build_nt (BIT_NOT_EXPR, scope);
2987         else if (type_decl == error_mark_node)
2988           return error_mark_node;
2989
2990         /* [class.dtor]
2991
2992            A typedef-name that names a class shall not be used as the
2993            identifier in the declarator for a destructor declaration.  */
2994         if (declarator_p 
2995             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2996             && !DECL_SELF_REFERENCE_P (type_decl))
2997           error ("typedef-name `%D' used as destructor declarator",
2998                  type_decl);
2999
3000         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3001       }
3002
3003     case CPP_KEYWORD:
3004       if (token->keyword == RID_OPERATOR)
3005         {
3006           tree id;
3007
3008           /* This could be a template-id, so we try that first.  */
3009           cp_parser_parse_tentatively (parser);
3010           /* Try a template-id.  */
3011           id = cp_parser_template_id (parser, template_keyword_p,
3012                                       /*check_dependency_p=*/true,
3013                                       declarator_p);
3014           /* If that worked, we're done.  */
3015           if (cp_parser_parse_definitely (parser))
3016             return id;
3017           /* We still don't know whether we're looking at an
3018              operator-function-id or a conversion-function-id.  */
3019           cp_parser_parse_tentatively (parser);
3020           /* Try an operator-function-id.  */
3021           id = cp_parser_operator_function_id (parser);
3022           /* If that didn't work, try a conversion-function-id.  */
3023           if (!cp_parser_parse_definitely (parser))
3024             id = cp_parser_conversion_function_id (parser);
3025
3026           return id;
3027         }
3028       /* Fall through.  */
3029
3030     default:
3031       cp_parser_error (parser, "expected unqualified-id");
3032       return error_mark_node;
3033     }
3034 }
3035
3036 /* Parse an (optional) nested-name-specifier.
3037
3038    nested-name-specifier:
3039      class-or-namespace-name :: nested-name-specifier [opt]
3040      class-or-namespace-name :: template nested-name-specifier [opt]
3041
3042    PARSER->SCOPE should be set appropriately before this function is
3043    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3044    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3045    in name lookups.
3046
3047    Sets PARSER->SCOPE to the class (TYPE) or namespace
3048    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3049    it unchanged if there is no nested-name-specifier.  Returns the new
3050    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.  
3051
3052    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3053    part of a declaration and/or decl-specifier.  */
3054
3055 static tree
3056 cp_parser_nested_name_specifier_opt (cp_parser *parser, 
3057                                      bool typename_keyword_p, 
3058                                      bool check_dependency_p,
3059                                      bool type_p,
3060                                      bool is_declaration)
3061 {
3062   bool success = false;
3063   tree access_check = NULL_TREE;
3064   ptrdiff_t start;
3065   cp_token* token;
3066
3067   /* If the next token corresponds to a nested name specifier, there
3068      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3069      false, it may have been true before, in which case something 
3070      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3071      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3072      CHECK_DEPENDENCY_P is false, we have to fall through into the
3073      main loop.  */
3074   if (check_dependency_p
3075       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3076     {
3077       cp_parser_pre_parsed_nested_name_specifier (parser);
3078       return parser->scope;
3079     }
3080
3081   /* Remember where the nested-name-specifier starts.  */
3082   if (cp_parser_parsing_tentatively (parser)
3083       && !cp_parser_committed_to_tentative_parse (parser))
3084     {
3085       token = cp_lexer_peek_token (parser->lexer);
3086       start = cp_lexer_token_difference (parser->lexer,
3087                                          parser->lexer->first_token,
3088                                          token);
3089     }
3090   else
3091     start = -1;
3092
3093   push_deferring_access_checks (dk_deferred);
3094
3095   while (true)
3096     {
3097       tree new_scope;
3098       tree old_scope;
3099       tree saved_qualifying_scope;
3100       bool template_keyword_p;
3101
3102       /* Spot cases that cannot be the beginning of a
3103          nested-name-specifier.  */
3104       token = cp_lexer_peek_token (parser->lexer);
3105
3106       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3107          the already parsed nested-name-specifier.  */
3108       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3109         {
3110           /* Grab the nested-name-specifier and continue the loop.  */
3111           cp_parser_pre_parsed_nested_name_specifier (parser);
3112           success = true;
3113           continue;
3114         }
3115
3116       /* Spot cases that cannot be the beginning of a
3117          nested-name-specifier.  On the second and subsequent times
3118          through the loop, we look for the `template' keyword.  */
3119       if (success && token->keyword == RID_TEMPLATE)
3120         ;
3121       /* A template-id can start a nested-name-specifier.  */
3122       else if (token->type == CPP_TEMPLATE_ID)
3123         ;
3124       else
3125         {
3126           /* If the next token is not an identifier, then it is
3127              definitely not a class-or-namespace-name.  */
3128           if (token->type != CPP_NAME)
3129             break;
3130           /* If the following token is neither a `<' (to begin a
3131              template-id), nor a `::', then we are not looking at a
3132              nested-name-specifier.  */
3133           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3134           if (token->type != CPP_SCOPE
3135               && !cp_parser_nth_token_starts_template_argument_list_p
3136                   (parser, 2))
3137             break;
3138         }
3139
3140       /* The nested-name-specifier is optional, so we parse
3141          tentatively.  */
3142       cp_parser_parse_tentatively (parser);
3143
3144       /* Look for the optional `template' keyword, if this isn't the
3145          first time through the loop.  */
3146       if (success)
3147         template_keyword_p = cp_parser_optional_template_keyword (parser);
3148       else
3149         template_keyword_p = false;
3150
3151       /* Save the old scope since the name lookup we are about to do
3152          might destroy it.  */
3153       old_scope = parser->scope;
3154       saved_qualifying_scope = parser->qualifying_scope;
3155       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3156          look up names in "X<T>::I" in order to determine that "Y" is
3157          a template.  So, if we have a typename at this point, we make
3158          an effort to look through it.  */
3159       if (is_declaration 
3160           && !typename_keyword_p
3161           && parser->scope 
3162           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3163         parser->scope = resolve_typename_type (parser->scope, 
3164                                                /*only_current_p=*/false);
3165       /* Parse the qualifying entity.  */
3166       new_scope 
3167         = cp_parser_class_or_namespace_name (parser,
3168                                              typename_keyword_p,
3169                                              template_keyword_p,
3170                                              check_dependency_p,
3171                                              type_p,
3172                                              is_declaration);
3173       /* Look for the `::' token.  */
3174       cp_parser_require (parser, CPP_SCOPE, "`::'");
3175
3176       /* If we found what we wanted, we keep going; otherwise, we're
3177          done.  */
3178       if (!cp_parser_parse_definitely (parser))
3179         {
3180           bool error_p = false;
3181
3182           /* Restore the OLD_SCOPE since it was valid before the
3183              failed attempt at finding the last
3184              class-or-namespace-name.  */
3185           parser->scope = old_scope;
3186           parser->qualifying_scope = saved_qualifying_scope;
3187           /* If the next token is an identifier, and the one after
3188              that is a `::', then any valid interpretation would have
3189              found a class-or-namespace-name.  */
3190           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3191                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
3192                      == CPP_SCOPE)
3193                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
3194                      != CPP_COMPL))
3195             {
3196               token = cp_lexer_consume_token (parser->lexer);
3197               if (!error_p) 
3198                 {
3199                   tree decl;
3200
3201                   decl = cp_parser_lookup_name_simple (parser, token->value);
3202                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3203                     error ("`%D' used without template parameters",
3204                            decl);
3205                   else
3206                     cp_parser_name_lookup_error 
3207                       (parser, token->value, decl, 
3208                        "is not a class or namespace");
3209                   parser->scope = NULL_TREE;
3210                   error_p = true;
3211                   /* Treat this as a successful nested-name-specifier
3212                      due to:
3213
3214                      [basic.lookup.qual]
3215
3216                      If the name found is not a class-name (clause
3217                      _class_) or namespace-name (_namespace.def_), the
3218                      program is ill-formed.  */
3219                   success = true;
3220                 }
3221               cp_lexer_consume_token (parser->lexer);
3222             }
3223           break;
3224         }
3225
3226       /* We've found one valid nested-name-specifier.  */
3227       success = true;
3228       /* Make sure we look in the right scope the next time through
3229          the loop.  */
3230       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL 
3231                        ? TREE_TYPE (new_scope)
3232                        : new_scope);
3233       /* If it is a class scope, try to complete it; we are about to
3234          be looking up names inside the class.  */
3235       if (TYPE_P (parser->scope)
3236           /* Since checking types for dependency can be expensive,
3237              avoid doing it if the type is already complete.  */
3238           && !COMPLETE_TYPE_P (parser->scope)
3239           /* Do not try to complete dependent types.  */
3240           && !dependent_type_p (parser->scope))
3241         complete_type (parser->scope);
3242     }
3243
3244   /* Retrieve any deferred checks.  Do not pop this access checks yet
3245      so the memory will not be reclaimed during token replacing below.  */
3246   access_check = get_deferred_access_checks ();
3247
3248   /* If parsing tentatively, replace the sequence of tokens that makes
3249      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3250      token.  That way, should we re-parse the token stream, we will
3251      not have to repeat the effort required to do the parse, nor will
3252      we issue duplicate error messages.  */
3253   if (success && start >= 0)
3254     {
3255       /* Find the token that corresponds to the start of the
3256          template-id.  */
3257       token = cp_lexer_advance_token (parser->lexer, 
3258                                       parser->lexer->first_token,
3259                                       start);
3260
3261       /* Reset the contents of the START token.  */
3262       token->type = CPP_NESTED_NAME_SPECIFIER;
3263       token->value = build_tree_list (access_check, parser->scope);
3264       TREE_TYPE (token->value) = parser->qualifying_scope;
3265       token->keyword = RID_MAX;
3266       /* Purge all subsequent tokens.  */
3267       cp_lexer_purge_tokens_after (parser->lexer, token);
3268     }
3269
3270   pop_deferring_access_checks ();
3271   return success ? parser->scope : NULL_TREE;
3272 }
3273
3274 /* Parse a nested-name-specifier.  See
3275    cp_parser_nested_name_specifier_opt for details.  This function
3276    behaves identically, except that it will an issue an error if no
3277    nested-name-specifier is present, and it will return
3278    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3279    is present.  */
3280
3281 static tree
3282 cp_parser_nested_name_specifier (cp_parser *parser, 
3283                                  bool typename_keyword_p, 
3284                                  bool check_dependency_p,
3285                                  bool type_p,
3286                                  bool is_declaration)
3287 {
3288   tree scope;
3289
3290   /* Look for the nested-name-specifier.  */
3291   scope = cp_parser_nested_name_specifier_opt (parser,
3292                                                typename_keyword_p,
3293                                                check_dependency_p,
3294                                                type_p,
3295                                                is_declaration);
3296   /* If it was not present, issue an error message.  */
3297   if (!scope)
3298     {
3299       cp_parser_error (parser, "expected nested-name-specifier");
3300       parser->scope = NULL_TREE;
3301       return error_mark_node;
3302     }
3303
3304   return scope;
3305 }
3306
3307 /* Parse a class-or-namespace-name.
3308
3309    class-or-namespace-name:
3310      class-name
3311      namespace-name
3312
3313    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3314    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3315    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3316    TYPE_P is TRUE iff the next name should be taken as a class-name,
3317    even the same name is declared to be another entity in the same
3318    scope.
3319
3320    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3321    specified by the class-or-namespace-name.  If neither is found the
3322    ERROR_MARK_NODE is returned.  */
3323
3324 static tree
3325 cp_parser_class_or_namespace_name (cp_parser *parser, 
3326                                    bool typename_keyword_p,
3327                                    bool template_keyword_p,
3328                                    bool check_dependency_p,
3329                                    bool type_p,
3330                                    bool is_declaration)
3331 {
3332   tree saved_scope;
3333   tree saved_qualifying_scope;
3334   tree saved_object_scope;
3335   tree scope;
3336   bool only_class_p;
3337
3338   /* Before we try to parse the class-name, we must save away the
3339      current PARSER->SCOPE since cp_parser_class_name will destroy
3340      it.  */
3341   saved_scope = parser->scope;
3342   saved_qualifying_scope = parser->qualifying_scope;
3343   saved_object_scope = parser->object_scope;
3344   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3345      there is no need to look for a namespace-name.  */
3346   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3347   if (!only_class_p)
3348     cp_parser_parse_tentatively (parser);
3349   scope = cp_parser_class_name (parser, 
3350                                 typename_keyword_p,
3351                                 template_keyword_p,
3352                                 type_p,
3353                                 check_dependency_p,
3354                                 /*class_head_p=*/false,
3355                                 is_declaration);
3356   /* If that didn't work, try for a namespace-name.  */
3357   if (!only_class_p && !cp_parser_parse_definitely (parser))
3358     {
3359       /* Restore the saved scope.  */
3360       parser->scope = saved_scope;
3361       parser->qualifying_scope = saved_qualifying_scope;
3362       parser->object_scope = saved_object_scope;
3363       /* If we are not looking at an identifier followed by the scope
3364          resolution operator, then this is not part of a
3365          nested-name-specifier.  (Note that this function is only used
3366          to parse the components of a nested-name-specifier.)  */
3367       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3368           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3369         return error_mark_node;
3370       scope = cp_parser_namespace_name (parser);
3371     }
3372
3373   return scope;
3374 }
3375
3376 /* Parse a postfix-expression.
3377
3378    postfix-expression:
3379      primary-expression
3380      postfix-expression [ expression ]
3381      postfix-expression ( expression-list [opt] )
3382      simple-type-specifier ( expression-list [opt] )
3383      typename :: [opt] nested-name-specifier identifier 
3384        ( expression-list [opt] )
3385      typename :: [opt] nested-name-specifier template [opt] template-id
3386        ( expression-list [opt] )
3387      postfix-expression . template [opt] id-expression
3388      postfix-expression -> template [opt] id-expression
3389      postfix-expression . pseudo-destructor-name
3390      postfix-expression -> pseudo-destructor-name
3391      postfix-expression ++
3392      postfix-expression --
3393      dynamic_cast < type-id > ( expression )
3394      static_cast < type-id > ( expression )
3395      reinterpret_cast < type-id > ( expression )
3396      const_cast < type-id > ( expression )
3397      typeid ( expression )
3398      typeid ( type-id )
3399
3400    GNU Extension:
3401      
3402    postfix-expression:
3403      ( type-id ) { initializer-list , [opt] }
3404
3405    This extension is a GNU version of the C99 compound-literal
3406    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3407    but they are essentially the same concept.)
3408
3409    If ADDRESS_P is true, the postfix expression is the operand of the
3410    `&' operator.
3411
3412    Returns a representation of the expression.  */
3413
3414 static tree
3415 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3416 {
3417   cp_token *token;
3418   enum rid keyword;
3419   cp_id_kind idk = CP_ID_KIND_NONE;
3420   tree postfix_expression = NULL_TREE;
3421   /* Non-NULL only if the current postfix-expression can be used to
3422      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3423      class used to qualify the member.  */
3424   tree qualifying_class = NULL_TREE;
3425
3426   /* Peek at the next token.  */
3427   token = cp_lexer_peek_token (parser->lexer);
3428   /* Some of the productions are determined by keywords.  */
3429   keyword = token->keyword;
3430   switch (keyword)
3431     {
3432     case RID_DYNCAST:
3433     case RID_STATCAST:
3434     case RID_REINTCAST:
3435     case RID_CONSTCAST:
3436       {
3437         tree type;
3438         tree expression;
3439         const char *saved_message;
3440
3441         /* All of these can be handled in the same way from the point
3442            of view of parsing.  Begin by consuming the token
3443            identifying the cast.  */
3444         cp_lexer_consume_token (parser->lexer);
3445         
3446         /* New types cannot be defined in the cast.  */
3447         saved_message = parser->type_definition_forbidden_message;
3448         parser->type_definition_forbidden_message
3449           = "types may not be defined in casts";
3450
3451         /* Look for the opening `<'.  */
3452         cp_parser_require (parser, CPP_LESS, "`<'");
3453         /* Parse the type to which we are casting.  */
3454         type = cp_parser_type_id (parser);
3455         /* Look for the closing `>'.  */
3456         cp_parser_require (parser, CPP_GREATER, "`>'");
3457         /* Restore the old message.  */
3458         parser->type_definition_forbidden_message = saved_message;
3459
3460         /* And the expression which is being cast.  */
3461         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3462         expression = cp_parser_expression (parser);
3463         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3464
3465         /* Only type conversions to integral or enumeration types
3466            can be used in constant-expressions.  */
3467         if (parser->integral_constant_expression_p
3468             && !dependent_type_p (type)
3469             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3470             /* A cast to pointer or reference type is allowed in the
3471                implementation of "offsetof".  */
3472             && !(parser->in_offsetof_p && POINTER_TYPE_P (type))
3473             && (cp_parser_non_integral_constant_expression 
3474                 (parser,
3475                  "a cast to a type other than an integral or "
3476                  "enumeration type")))
3477           return error_mark_node;
3478
3479         switch (keyword)
3480           {
3481           case RID_DYNCAST:
3482             postfix_expression
3483               = build_dynamic_cast (type, expression);
3484             break;
3485           case RID_STATCAST:
3486             postfix_expression
3487               = build_static_cast (type, expression);
3488             break;
3489           case RID_REINTCAST:
3490             postfix_expression
3491               = build_reinterpret_cast (type, expression);
3492             break;
3493           case RID_CONSTCAST:
3494             postfix_expression
3495               = build_const_cast (type, expression);
3496             break;
3497           default:
3498             abort ();
3499           }
3500       }
3501       break;
3502
3503     case RID_TYPEID:
3504       {
3505         tree type;
3506         const char *saved_message;
3507         bool saved_in_type_id_in_expr_p;
3508
3509         /* Consume the `typeid' token.  */
3510         cp_lexer_consume_token (parser->lexer);
3511         /* Look for the `(' token.  */
3512         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3513         /* Types cannot be defined in a `typeid' expression.  */
3514         saved_message = parser->type_definition_forbidden_message;
3515         parser->type_definition_forbidden_message
3516           = "types may not be defined in a `typeid\' expression";
3517         /* We can't be sure yet whether we're looking at a type-id or an
3518            expression.  */
3519         cp_parser_parse_tentatively (parser);
3520         /* Try a type-id first.  */
3521         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3522         parser->in_type_id_in_expr_p = true;
3523         type = cp_parser_type_id (parser);
3524         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3525         /* Look for the `)' token.  Otherwise, we can't be sure that
3526            we're not looking at an expression: consider `typeid (int
3527            (3))', for example.  */
3528         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3529         /* If all went well, simply lookup the type-id.  */
3530         if (cp_parser_parse_definitely (parser))
3531           postfix_expression = get_typeid (type);
3532         /* Otherwise, fall back to the expression variant.  */
3533         else
3534           {
3535             tree expression;
3536
3537             /* Look for an expression.  */
3538             expression = cp_parser_expression (parser);
3539             /* Compute its typeid.  */
3540             postfix_expression = build_typeid (expression);
3541             /* Look for the `)' token.  */
3542             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3543           }
3544         /* `typeid' may not appear in an integral constant expression.  */
3545         if (cp_parser_non_integral_constant_expression(parser, 
3546                                                        "`typeid' operator"))
3547           return error_mark_node;
3548         /* Restore the saved message.  */
3549         parser->type_definition_forbidden_message = saved_message;
3550       }
3551       break;
3552       
3553     case RID_TYPENAME:
3554       {
3555         tree type;
3556
3557         /* The syntax permitted here is the same permitted for an
3558            elaborated-type-specifier.  */
3559         type = cp_parser_elaborated_type_specifier (parser,
3560                                                     /*is_friend=*/false,
3561                                                     /*is_declaration=*/false);
3562         postfix_expression = cp_parser_functional_cast (parser, type);
3563       }
3564       break;
3565
3566     default:
3567       {
3568         tree type;
3569
3570         /* If the next thing is a simple-type-specifier, we may be
3571            looking at a functional cast.  We could also be looking at
3572            an id-expression.  So, we try the functional cast, and if
3573            that doesn't work we fall back to the primary-expression.  */
3574         cp_parser_parse_tentatively (parser);
3575         /* Look for the simple-type-specifier.  */
3576         type = cp_parser_simple_type_specifier (parser, 
3577                                                 CP_PARSER_FLAGS_NONE,
3578                                                 /*identifier_p=*/false);
3579         /* Parse the cast itself.  */
3580         if (!cp_parser_error_occurred (parser))
3581           postfix_expression 
3582             = cp_parser_functional_cast (parser, type);
3583         /* If that worked, we're done.  */
3584         if (cp_parser_parse_definitely (parser))
3585           break;
3586
3587         /* If the functional-cast didn't work out, try a
3588            compound-literal.  */
3589         if (cp_parser_allow_gnu_extensions_p (parser)
3590             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3591           {
3592             tree initializer_list = NULL_TREE;
3593             bool saved_in_type_id_in_expr_p;
3594
3595             cp_parser_parse_tentatively (parser);
3596             /* Consume the `('.  */
3597             cp_lexer_consume_token (parser->lexer);
3598             /* Parse the type.  */
3599             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3600             parser->in_type_id_in_expr_p = true;
3601             type = cp_parser_type_id (parser);
3602             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3603             /* Look for the `)'.  */
3604             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3605             /* Look for the `{'.  */
3606             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3607             /* If things aren't going well, there's no need to
3608                keep going.  */
3609             if (!cp_parser_error_occurred (parser))
3610               {
3611                 bool non_constant_p;
3612                 /* Parse the initializer-list.  */
3613                 initializer_list 
3614                   = cp_parser_initializer_list (parser, &non_constant_p);
3615                 /* Allow a trailing `,'.  */
3616                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3617                   cp_lexer_consume_token (parser->lexer);
3618                 /* Look for the final `}'.  */
3619                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3620               }
3621             /* If that worked, we're definitely looking at a
3622                compound-literal expression.  */
3623             if (cp_parser_parse_definitely (parser))
3624               {
3625                 /* Warn the user that a compound literal is not
3626                    allowed in standard C++.  */
3627                 if (pedantic)
3628                   pedwarn ("ISO C++ forbids compound-literals");
3629                 /* Form the representation of the compound-literal.  */
3630                 postfix_expression 
3631                   = finish_compound_literal (type, initializer_list);
3632                 break;
3633               }
3634           }
3635
3636         /* It must be a primary-expression.  */
3637         postfix_expression = cp_parser_primary_expression (parser, 
3638                                                            &idk,
3639                                                            &qualifying_class);
3640       }
3641       break;
3642     }
3643
3644   /* If we were avoiding committing to the processing of a
3645      qualified-id until we knew whether or not we had a
3646      pointer-to-member, we now know.  */
3647   if (qualifying_class)
3648     {
3649       bool done;
3650
3651       /* Peek at the next token.  */
3652       token = cp_lexer_peek_token (parser->lexer);
3653       done = (token->type != CPP_OPEN_SQUARE
3654               && token->type != CPP_OPEN_PAREN
3655               && token->type != CPP_DOT
3656               && token->type != CPP_DEREF
3657               && token->type != CPP_PLUS_PLUS
3658               && token->type != CPP_MINUS_MINUS);
3659
3660       postfix_expression = finish_qualified_id_expr (qualifying_class,
3661                                                      postfix_expression,
3662                                                      done,
3663                                                      address_p);
3664       if (done)
3665         return postfix_expression;
3666     }
3667
3668   /* Keep looping until the postfix-expression is complete.  */
3669   while (true)
3670     {
3671       if (idk == CP_ID_KIND_UNQUALIFIED
3672           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3673           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3674         /* It is not a Koenig lookup function call.  */
3675         postfix_expression 
3676           = unqualified_name_lookup_error (postfix_expression);
3677       
3678       /* Peek at the next token.  */
3679       token = cp_lexer_peek_token (parser->lexer);
3680
3681       switch (token->type)
3682         {
3683         case CPP_OPEN_SQUARE:
3684           /* postfix-expression [ expression ] */
3685           {
3686             tree index;
3687
3688             /* Consume the `[' token.  */
3689             cp_lexer_consume_token (parser->lexer);
3690             /* Parse the index expression.  */
3691             index = cp_parser_expression (parser);
3692             /* Look for the closing `]'.  */
3693             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3694
3695             /* Build the ARRAY_REF.  */
3696             postfix_expression 
3697               = grok_array_decl (postfix_expression, index);
3698             idk = CP_ID_KIND_NONE;
3699             /* Array references are not permitted in
3700                constant-expressions (but they are allowed
3701                in offsetof).  */
3702             if (!parser->in_offsetof_p
3703                 && cp_parser_non_integral_constant_expression
3704                     (parser, "an array reference"))
3705               postfix_expression = error_mark_node;
3706           }
3707           break;
3708
3709         case CPP_OPEN_PAREN:
3710           /* postfix-expression ( expression-list [opt] ) */
3711           {
3712             bool koenig_p;
3713             tree args = (cp_parser_parenthesized_expression_list 
3714                          (parser, false, /*non_constant_p=*/NULL));
3715
3716             if (args == error_mark_node)
3717               {
3718                 postfix_expression = error_mark_node;
3719                 break;
3720               }
3721             
3722             /* Function calls are not permitted in
3723                constant-expressions.  */
3724             if (cp_parser_non_integral_constant_expression (parser,
3725                                                             "a function call"))
3726               {
3727                 postfix_expression = error_mark_node;
3728                 break;
3729               }
3730
3731             koenig_p = false;
3732             if (idk == CP_ID_KIND_UNQUALIFIED)
3733               {
3734                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3735                   {
3736                     if (args)
3737                       {
3738                         koenig_p = true;
3739                         postfix_expression
3740                           = perform_koenig_lookup (postfix_expression, args);
3741                       }
3742                     else
3743                       postfix_expression
3744                         = unqualified_fn_lookup_error (postfix_expression);
3745                   }
3746                 /* We do not perform argument-dependent lookup if
3747                    normal lookup finds a non-function, in accordance
3748                    with the expected resolution of DR 218.  */
3749                 else if (args && is_overloaded_fn (postfix_expression))
3750                   {
3751                     tree fn = get_first_fn (postfix_expression);
3752                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3753                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
3754                     /* Only do argument dependent lookup if regular
3755                        lookup does not find a set of member functions.
3756                        [basic.lookup.koenig]/2a  */
3757                     if (!DECL_FUNCTION_MEMBER_P (fn))
3758                       {
3759                         koenig_p = true;
3760                         postfix_expression
3761                           = perform_koenig_lookup (postfix_expression, args);
3762                       }
3763                   }
3764               }
3765           
3766             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3767               {
3768                 tree instance = TREE_OPERAND (postfix_expression, 0);
3769                 tree fn = TREE_OPERAND (postfix_expression, 1);
3770
3771                 if (processing_template_decl
3772                     && (type_dependent_expression_p (instance)
3773                         || (!BASELINK_P (fn)
3774                             && TREE_CODE (fn) != FIELD_DECL)
3775                         || type_dependent_expression_p (fn)
3776                         || any_type_dependent_arguments_p (args)))
3777                   {
3778                     postfix_expression
3779                       = build_min_nt (CALL_EXPR, postfix_expression, args);
3780                     break;
3781                   }
3782
3783                 if (BASELINK_P (fn))
3784                   postfix_expression
3785                     = (build_new_method_call 
3786                        (instance, fn, args, NULL_TREE, 
3787                         (idk == CP_ID_KIND_QUALIFIED 
3788                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3789                 else
3790                   postfix_expression
3791                     = finish_call_expr (postfix_expression, args,
3792                                         /*disallow_virtual=*/false,
3793                                         /*koenig_p=*/false);
3794               }
3795             else if (TREE_CODE (postfix_expression) == OFFSET_REF
3796                      || TREE_CODE (postfix_expression) == MEMBER_REF
3797                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3798               postfix_expression = (build_offset_ref_call_from_tree
3799                                     (postfix_expression, args));
3800             else if (idk == CP_ID_KIND_QUALIFIED)
3801               /* A call to a static class member, or a namespace-scope
3802                  function.  */
3803               postfix_expression
3804                 = finish_call_expr (postfix_expression, args,
3805                                     /*disallow_virtual=*/true,
3806                                     koenig_p);
3807             else
3808               /* All other function calls.  */
3809               postfix_expression 
3810                 = finish_call_expr (postfix_expression, args, 
3811                                     /*disallow_virtual=*/false,
3812                                     koenig_p);
3813
3814             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
3815             idk = CP_ID_KIND_NONE;
3816           }
3817           break;
3818           
3819         case CPP_DOT:
3820         case CPP_DEREF:
3821           /* postfix-expression . template [opt] id-expression  
3822              postfix-expression . pseudo-destructor-name 
3823              postfix-expression -> template [opt] id-expression
3824              postfix-expression -> pseudo-destructor-name */
3825           {
3826             tree name;
3827             bool dependent_p;
3828             bool template_p;
3829             bool pseudo_destructor_p;
3830             tree scope = NULL_TREE;
3831             enum cpp_ttype token_type = token->type;
3832
3833             /* If this is a `->' operator, dereference the pointer.  */
3834             if (token->type == CPP_DEREF)
3835               postfix_expression = build_x_arrow (postfix_expression);
3836             /* Check to see whether or not the expression is
3837                type-dependent.  */
3838             dependent_p = type_dependent_expression_p (postfix_expression);
3839             /* The identifier following the `->' or `.' is not
3840                qualified.  */
3841             parser->scope = NULL_TREE;
3842             parser->qualifying_scope = NULL_TREE;
3843             parser->object_scope = NULL_TREE;
3844             idk = CP_ID_KIND_NONE;
3845             /* Enter the scope corresponding to the type of the object
3846                given by the POSTFIX_EXPRESSION.  */
3847             if (!dependent_p 
3848                 && TREE_TYPE (postfix_expression) != NULL_TREE)
3849               {
3850                 scope = TREE_TYPE (postfix_expression);
3851                 /* According to the standard, no expression should
3852                    ever have reference type.  Unfortunately, we do not
3853                    currently match the standard in this respect in
3854                    that our internal representation of an expression
3855                    may have reference type even when the standard says
3856                    it does not.  Therefore, we have to manually obtain
3857                    the underlying type here.  */
3858                 scope = non_reference (scope);
3859                 /* The type of the POSTFIX_EXPRESSION must be
3860                    complete.  */
3861                 scope = complete_type_or_else (scope, NULL_TREE);
3862                 /* Let the name lookup machinery know that we are
3863                    processing a class member access expression.  */
3864                 parser->context->object_type = scope;
3865                 /* If something went wrong, we want to be able to
3866                    discern that case, as opposed to the case where
3867                    there was no SCOPE due to the type of expression
3868                    being dependent.  */
3869                 if (!scope)
3870                   scope = error_mark_node;
3871                 /* If the SCOPE was erroneous, make the various
3872                    semantic analysis functions exit quickly -- and
3873                    without issuing additional error messages.  */
3874                 if (scope == error_mark_node)
3875                   postfix_expression = error_mark_node;
3876               }
3877
3878             /* Consume the `.' or `->' operator.  */
3879             cp_lexer_consume_token (parser->lexer);
3880             
3881             /* Assume this expression is not a pseudo-destructor access.  */
3882             pseudo_destructor_p = false;
3883
3884             /* If the SCOPE is a scalar type, then, if this is a valid program,
3885                we must be looking at a pseudo-destructor-name.  */
3886             if (scope && SCALAR_TYPE_P (scope))
3887               {
3888                 tree s = NULL_TREE;
3889                 tree type;
3890
3891                 cp_parser_parse_tentatively (parser);
3892                 /* Parse the pseudo-destructor-name.  */
3893                 cp_parser_pseudo_destructor_name (parser, &s, &type);
3894                 if (cp_parser_parse_definitely (parser))
3895                   {
3896                     pseudo_destructor_p = true;
3897                     postfix_expression
3898                       = finish_pseudo_destructor_expr (postfix_expression,
3899                                                        s, TREE_TYPE (type));
3900                   }
3901               }
3902
3903             if (!pseudo_destructor_p)
3904               {
3905                 /* If the SCOPE is not a scalar type, we are looking
3906                    at an ordinary class member access expression,
3907                    rather than a pseudo-destructor-name.  */
3908                 template_p = cp_parser_optional_template_keyword (parser);
3909                 /* Parse the id-expression.  */
3910                 name = cp_parser_id_expression (parser,
3911                                                 template_p,
3912                                                 /*check_dependency_p=*/true,
3913                                                 /*template_p=*/NULL,
3914                                                 /*declarator_p=*/false);
3915                 /* In general, build a SCOPE_REF if the member name is
3916                    qualified.  However, if the name was not dependent
3917                    and has already been resolved; there is no need to
3918                    build the SCOPE_REF.  For example;
3919
3920                      struct X { void f(); };
3921                      template <typename T> void f(T* t) { t->X::f(); }
3922  
3923                    Even though "t" is dependent, "X::f" is not and has
3924                    been resolved to a BASELINK; there is no need to
3925                    include scope information.  */
3926
3927                 /* But we do need to remember that there was an explicit
3928                    scope for virtual function calls.  */
3929                 if (parser->scope)
3930                   idk = CP_ID_KIND_QUALIFIED;
3931
3932                 /* If the name is a template-id that names a type, we will
3933                    get a TYPE_DECL here.  That is invalid code.  */
3934                 if (TREE_CODE (name) == TYPE_DECL)
3935                   {
3936                     error ("invalid use of `%D'", name);
3937                     postfix_expression = error_mark_node;
3938                   }
3939                 else
3940                   {
3941                     if (name != error_mark_node && !BASELINK_P (name)
3942                         && parser->scope)
3943                       {
3944                         name = build_nt (SCOPE_REF, parser->scope, name);
3945                         parser->scope = NULL_TREE;
3946                         parser->qualifying_scope = NULL_TREE;
3947                         parser->object_scope = NULL_TREE;
3948                       }
3949                     if (scope && name && BASELINK_P (name))
3950                       adjust_result_of_qualified_name_lookup
3951                         (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
3952                     postfix_expression = finish_class_member_access_expr
3953                                            (postfix_expression, name);
3954                   }
3955               }
3956
3957             /* We no longer need to look up names in the scope of the
3958                object on the left-hand side of the `.' or `->'
3959                operator.  */
3960             parser->context->object_type = NULL_TREE;
3961             /* These operators may not appear in constant-expressions.  */
3962             if (/* The "->" operator is allowed in the implementation
3963                    of "offsetof".  The "." operator may appear in the
3964                    name of the member.  */
3965                 !parser->in_offsetof_p
3966                 && (cp_parser_non_integral_constant_expression 
3967                     (parser,
3968                      token_type == CPP_DEREF ? "'->'" : "`.'")))
3969               postfix_expression = error_mark_node;
3970           }
3971           break;
3972
3973         case CPP_PLUS_PLUS:
3974           /* postfix-expression ++  */
3975           /* Consume the `++' token.  */
3976           cp_lexer_consume_token (parser->lexer);
3977           /* Generate a representation for the complete expression.  */
3978           postfix_expression 
3979             = finish_increment_expr (postfix_expression, 
3980                                      POSTINCREMENT_EXPR);
3981           /* Increments may not appear in constant-expressions.  */
3982           if (cp_parser_non_integral_constant_expression (parser,
3983                                                           "an increment"))
3984             postfix_expression = error_mark_node;
3985           idk = CP_ID_KIND_NONE;
3986           break;
3987
3988         case CPP_MINUS_MINUS:
3989           /* postfix-expression -- */
3990           /* Consume the `--' token.  */
3991           cp_lexer_consume_token (parser->lexer);
3992           /* Generate a representation for the complete expression.  */
3993           postfix_expression 
3994             = finish_increment_expr (postfix_expression, 
3995                                      POSTDECREMENT_EXPR);
3996           /* Decrements may not appear in constant-expressions.  */
3997           if (cp_parser_non_integral_constant_expression (parser,
3998                                                           "a decrement"))
3999             postfix_expression = error_mark_node;
4000           idk = CP_ID_KIND_NONE;
4001           break;
4002
4003         default:
4004           return postfix_expression;
4005         }
4006     }
4007
4008   /* We should never get here.  */
4009   abort ();
4010   return error_mark_node;
4011 }
4012
4013 /* Parse a parenthesized expression-list.
4014
4015    expression-list:
4016      assignment-expression
4017      expression-list, assignment-expression
4018
4019    attribute-list:
4020      expression-list
4021      identifier
4022      identifier, expression-list
4023
4024    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4025    representation of an assignment-expression.  Note that a TREE_LIST
4026    is returned even if there is only a single expression in the list.
4027    error_mark_node is returned if the ( and or ) are
4028    missing. NULL_TREE is returned on no expressions. The parentheses
4029    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4030    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4031    indicates whether or not all of the expressions in the list were
4032    constant.  */
4033
4034 static tree
4035 cp_parser_parenthesized_expression_list (cp_parser* parser, 
4036                                          bool is_attribute_list,
4037                                          bool *non_constant_p)
4038 {
4039   tree expression_list = NULL_TREE;
4040   tree identifier = NULL_TREE;
4041
4042   /* Assume all the expressions will be constant.  */
4043   if (non_constant_p)
4044     *non_constant_p = false;
4045
4046   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4047     return error_mark_node;
4048   
4049   /* Consume expressions until there are no more.  */
4050   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4051     while (true)
4052       {
4053         tree expr;
4054         
4055         /* At the beginning of attribute lists, check to see if the
4056            next token is an identifier.  */
4057         if (is_attribute_list
4058             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4059           {
4060             cp_token *token;
4061             
4062             /* Consume the identifier.  */
4063             token = cp_lexer_consume_token (parser->lexer);
4064             /* Save the identifier.  */
4065             identifier = token->value;
4066           }
4067         else
4068           {
4069             /* Parse the next assignment-expression.  */
4070             if (non_constant_p)
4071               {
4072                 bool expr_non_constant_p;
4073                 expr = (cp_parser_constant_expression 
4074                         (parser, /*allow_non_constant_p=*/true,
4075                          &expr_non_constant_p));
4076                 if (expr_non_constant_p)
4077                   *non_constant_p = true;
4078               }
4079             else
4080               expr = cp_parser_assignment_expression (parser);
4081
4082              /* Add it to the list.  We add error_mark_node
4083                 expressions to the list, so that we can still tell if
4084                 the correct form for a parenthesized expression-list
4085                 is found. That gives better errors.  */
4086             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4087
4088             if (expr == error_mark_node)
4089               goto skip_comma;
4090           }
4091
4092         /* After the first item, attribute lists look the same as
4093            expression lists.  */
4094         is_attribute_list = false;
4095         
4096       get_comma:;
4097         /* If the next token isn't a `,', then we are done.  */
4098         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4099           break;
4100
4101         /* Otherwise, consume the `,' and keep going.  */
4102         cp_lexer_consume_token (parser->lexer);
4103       }
4104   
4105   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4106     {
4107       int ending;
4108       
4109     skip_comma:;
4110       /* We try and resync to an unnested comma, as that will give the
4111          user better diagnostics.  */
4112       ending = cp_parser_skip_to_closing_parenthesis (parser, 
4113                                                       /*recovering=*/true, 
4114                                                       /*or_comma=*/true,
4115                                                       /*consume_paren=*/true);
4116       if (ending < 0)
4117         goto get_comma;
4118       if (!ending)
4119         return error_mark_node;
4120     }
4121
4122   /* We built up the list in reverse order so we must reverse it now.  */
4123   expression_list = nreverse (expression_list);
4124   if (identifier)
4125     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4126   
4127   return expression_list;
4128 }
4129
4130 /* Parse a pseudo-destructor-name.
4131
4132    pseudo-destructor-name:
4133      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4134      :: [opt] nested-name-specifier template template-id :: ~ type-name
4135      :: [opt] nested-name-specifier [opt] ~ type-name
4136
4137    If either of the first two productions is used, sets *SCOPE to the
4138    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4139    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4140    or ERROR_MARK_NODE if the parse fails.  */
4141
4142 static void
4143 cp_parser_pseudo_destructor_name (cp_parser* parser, 
4144                                   tree* scope, 
4145                                   tree* type)
4146 {
4147   bool nested_name_specifier_p;
4148
4149   /* Look for the optional `::' operator.  */
4150   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4151   /* Look for the optional nested-name-specifier.  */
4152   nested_name_specifier_p 
4153     = (cp_parser_nested_name_specifier_opt (parser,
4154                                             /*typename_keyword_p=*/false,
4155                                             /*check_dependency_p=*/true,
4156                                             /*type_p=*/false,
4157                                             /*is_declaration=*/true) 
4158        != NULL_TREE);
4159   /* Now, if we saw a nested-name-specifier, we might be doing the
4160      second production.  */
4161   if (nested_name_specifier_p 
4162       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4163     {
4164       /* Consume the `template' keyword.  */
4165       cp_lexer_consume_token (parser->lexer);
4166       /* Parse the template-id.  */
4167       cp_parser_template_id (parser, 
4168                              /*template_keyword_p=*/true,
4169                              /*check_dependency_p=*/false,
4170                              /*is_declaration=*/true);
4171       /* Look for the `::' token.  */
4172       cp_parser_require (parser, CPP_SCOPE, "`::'");
4173     }
4174   /* If the next token is not a `~', then there might be some
4175      additional qualification.  */
4176   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4177     {
4178       /* Look for the type-name.  */
4179       *scope = TREE_TYPE (cp_parser_type_name (parser));
4180
4181       /* If we didn't get an aggregate type, or we don't have ::~,
4182          then something has gone wrong.  Since the only caller of this
4183          function is looking for something after `.' or `->' after a
4184          scalar type, most likely the program is trying to get a
4185          member of a non-aggregate type.  */
4186       if (*scope == error_mark_node
4187           || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4188           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4189         {
4190           cp_parser_error (parser, "request for member of non-aggregate type");
4191           *type = error_mark_node;
4192           return;
4193         }
4194
4195       /* Look for the `::' token.  */
4196       cp_parser_require (parser, CPP_SCOPE, "`::'");
4197     }
4198   else
4199     *scope = NULL_TREE;
4200
4201   /* Look for the `~'.  */
4202   cp_parser_require (parser, CPP_COMPL, "`~'");
4203   /* Look for the type-name again.  We are not responsible for
4204      checking that it matches the first type-name.  */
4205   *type = cp_parser_type_name (parser);
4206 }
4207
4208 /* Parse a unary-expression.
4209
4210    unary-expression:
4211      postfix-expression
4212      ++ cast-expression
4213      -- cast-expression
4214      unary-operator cast-expression
4215      sizeof unary-expression
4216      sizeof ( type-id )
4217      new-expression
4218      delete-expression
4219
4220    GNU Extensions:
4221
4222    unary-expression:
4223      __extension__ cast-expression
4224      __alignof__ unary-expression
4225      __alignof__ ( type-id )
4226      __real__ cast-expression
4227      __imag__ cast-expression
4228      && identifier
4229
4230    ADDRESS_P is true iff the unary-expression is appearing as the
4231    operand of the `&' operator.
4232
4233    Returns a representation of the expression.  */
4234
4235 static tree
4236 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4237 {
4238   cp_token *token;
4239   enum tree_code unary_operator;
4240
4241   /* Peek at the next token.  */
4242   token = cp_lexer_peek_token (parser->lexer);
4243   /* Some keywords give away the kind of expression.  */
4244   if (token->type == CPP_KEYWORD)
4245     {
4246       enum rid keyword = token->keyword;
4247
4248       switch (keyword)
4249         {
4250         case RID_ALIGNOF:
4251         case RID_SIZEOF:
4252           {
4253             tree operand;
4254             enum tree_code op;
4255             
4256             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4257             /* Consume the token.  */
4258             cp_lexer_consume_token (parser->lexer);
4259             /* Parse the operand.  */
4260             operand = cp_parser_sizeof_operand (parser, keyword);
4261
4262             if (TYPE_P (operand))
4263               return cxx_sizeof_or_alignof_type (operand, op, true);
4264             else
4265               return cxx_sizeof_or_alignof_expr (operand, op);
4266           }
4267
4268         case RID_NEW:
4269           return cp_parser_new_expression (parser);
4270
4271         case RID_DELETE:
4272           return cp_parser_delete_expression (parser);
4273           
4274         case RID_EXTENSION:
4275           {
4276             /* The saved value of the PEDANTIC flag.  */
4277             int saved_pedantic;
4278             tree expr;
4279
4280             /* Save away the PEDANTIC flag.  */
4281             cp_parser_extension_opt (parser, &saved_pedantic);
4282             /* Parse the cast-expression.  */
4283             expr = cp_parser_simple_cast_expression (parser);
4284             /* Restore the PEDANTIC flag.  */
4285             pedantic = saved_pedantic;
4286
4287             return expr;
4288           }
4289
4290         case RID_REALPART:
4291         case RID_IMAGPART:
4292           {
4293             tree expression;
4294
4295             /* Consume the `__real__' or `__imag__' token.  */
4296             cp_lexer_consume_token (parser->lexer);
4297             /* Parse the cast-expression.  */
4298             expression = cp_parser_simple_cast_expression (parser);
4299             /* Create the complete representation.  */
4300             return build_x_unary_op ((keyword == RID_REALPART
4301                                       ? REALPART_EXPR : IMAGPART_EXPR),
4302                                      expression);
4303           }
4304           break;
4305
4306         default:
4307           break;
4308         }
4309     }
4310
4311   /* Look for the `:: new' and `:: delete', which also signal the
4312      beginning of a new-expression, or delete-expression,
4313      respectively.  If the next token is `::', then it might be one of
4314      these.  */
4315   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4316     {
4317       enum rid keyword;
4318
4319       /* See if the token after the `::' is one of the keywords in
4320          which we're interested.  */
4321       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4322       /* If it's `new', we have a new-expression.  */
4323       if (keyword == RID_NEW)
4324         return cp_parser_new_expression (parser);
4325       /* Similarly, for `delete'.  */
4326       else if (keyword == RID_DELETE)
4327         return cp_parser_delete_expression (parser);
4328     }
4329
4330   /* Look for a unary operator.  */
4331   unary_operator = cp_parser_unary_operator (token);
4332   /* The `++' and `--' operators can be handled similarly, even though
4333      they are not technically unary-operators in the grammar.  */
4334   if (unary_operator == ERROR_MARK)
4335     {
4336       if (token->type == CPP_PLUS_PLUS)
4337         unary_operator = PREINCREMENT_EXPR;
4338       else if (token->type == CPP_MINUS_MINUS)
4339         unary_operator = PREDECREMENT_EXPR;
4340       /* Handle the GNU address-of-label extension.  */
4341       else if (cp_parser_allow_gnu_extensions_p (parser)
4342                && token->type == CPP_AND_AND)
4343         {
4344           tree identifier;
4345
4346           /* Consume the '&&' token.  */
4347           cp_lexer_consume_token (parser->lexer);
4348           /* Look for the identifier.  */
4349           identifier = cp_parser_identifier (parser);
4350           /* Create an expression representing the address.  */
4351           return finish_label_address_expr (identifier);
4352         }
4353     }
4354   if (unary_operator != ERROR_MARK)
4355     {
4356       tree cast_expression;
4357       tree expression = error_mark_node;
4358       const char *non_constant_p = NULL;
4359
4360       /* Consume the operator token.  */
4361       token = cp_lexer_consume_token (parser->lexer);
4362       /* Parse the cast-expression.  */
4363       cast_expression 
4364         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4365       /* Now, build an appropriate representation.  */
4366       switch (unary_operator)
4367         {
4368         case INDIRECT_REF:
4369           non_constant_p = "`*'";
4370           expression = build_x_indirect_ref (cast_expression, "unary *");
4371           break;
4372
4373         case ADDR_EXPR:
4374           /* The "&" operator is allowed in the implementation of
4375              "offsetof".  */
4376           if (!parser->in_offsetof_p)
4377             non_constant_p = "`&'";
4378           /* Fall through.  */
4379         case BIT_NOT_EXPR:
4380           expression = build_x_unary_op (unary_operator, cast_expression);
4381           break;
4382
4383         case PREINCREMENT_EXPR:
4384         case PREDECREMENT_EXPR:
4385           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4386                             ? "`++'" : "`--'");
4387           /* Fall through.  */
4388         case CONVERT_EXPR:
4389         case NEGATE_EXPR:
4390         case TRUTH_NOT_EXPR:
4391           expression = finish_unary_op_expr (unary_operator, cast_expression);
4392           break;
4393
4394         default:
4395           abort ();
4396         }
4397
4398       if (non_constant_p 
4399           && cp_parser_non_integral_constant_expression (parser,
4400                                                          non_constant_p))
4401         expression = error_mark_node;
4402
4403       return expression;
4404     }
4405
4406   return cp_parser_postfix_expression (parser, address_p);
4407 }
4408
4409 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4410    unary-operator, the corresponding tree code is returned.  */
4411
4412 static enum tree_code
4413 cp_parser_unary_operator (cp_token* token)
4414 {
4415   switch (token->type)
4416     {
4417     case CPP_MULT:
4418       return INDIRECT_REF;
4419
4420     case CPP_AND:
4421       return ADDR_EXPR;
4422
4423     case CPP_PLUS:
4424       return CONVERT_EXPR;
4425
4426     case CPP_MINUS:
4427       return NEGATE_EXPR;
4428
4429     case CPP_NOT:
4430       return TRUTH_NOT_EXPR;
4431       
4432     case CPP_COMPL:
4433       return BIT_NOT_EXPR;
4434
4435     default:
4436       return ERROR_MARK;
4437     }
4438 }
4439
4440 /* Parse a new-expression.
4441
4442    new-expression:
4443      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4444      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4445
4446    Returns a representation of the expression.  */
4447
4448 static tree
4449 cp_parser_new_expression (cp_parser* parser)
4450 {
4451   bool global_scope_p;
4452   tree placement;
4453   tree type;
4454   tree initializer;
4455
4456   /* Look for the optional `::' operator.  */
4457   global_scope_p 
4458     = (cp_parser_global_scope_opt (parser,
4459                                    /*current_scope_valid_p=*/false)
4460        != NULL_TREE);
4461   /* Look for the `new' operator.  */
4462   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4463   /* There's no easy way to tell a new-placement from the
4464      `( type-id )' construct.  */
4465   cp_parser_parse_tentatively (parser);
4466   /* Look for a new-placement.  */
4467   placement = cp_parser_new_placement (parser);
4468   /* If that didn't work out, there's no new-placement.  */
4469   if (!cp_parser_parse_definitely (parser))
4470     placement = NULL_TREE;
4471
4472   /* If the next token is a `(', then we have a parenthesized
4473      type-id.  */
4474   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4475     {
4476       /* Consume the `('.  */
4477       cp_lexer_consume_token (parser->lexer);
4478       /* Parse the type-id.  */
4479       type = cp_parser_type_id (parser);
4480       /* Look for the closing `)'.  */
4481       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4482       /* There should not be a direct-new-declarator in this production, 
4483          but GCC used to allowed this, so we check and emit a sensible error
4484          message for this case.  */
4485       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4486         {
4487           error ("array bound forbidden after parenthesized type-id");
4488           inform ("try removing the parentheses around the type-id");
4489           cp_parser_direct_new_declarator (parser);
4490         }
4491     }
4492   /* Otherwise, there must be a new-type-id.  */
4493   else
4494     type = cp_parser_new_type_id (parser);
4495
4496   /* If the next token is a `(', then we have a new-initializer.  */
4497   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4498     initializer = cp_parser_new_initializer (parser);
4499   else
4500     initializer = NULL_TREE;
4501
4502   /* A new-expression may not appear in an integral constant
4503      expression.  */
4504   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4505     return error_mark_node;
4506
4507   /* Create a representation of the new-expression.  */
4508   return build_new (placement, type, initializer, global_scope_p);
4509 }
4510
4511 /* Parse a new-placement.
4512
4513    new-placement:
4514      ( expression-list )
4515
4516    Returns the same representation as for an expression-list.  */
4517
4518 static tree
4519 cp_parser_new_placement (cp_parser* parser)
4520 {
4521   tree expression_list;
4522
4523   /* Parse the expression-list.  */
4524   expression_list = (cp_parser_parenthesized_expression_list 
4525                      (parser, false, /*non_constant_p=*/NULL));
4526
4527   return expression_list;
4528 }
4529
4530 /* Parse a new-type-id.
4531
4532    new-type-id:
4533      type-specifier-seq new-declarator [opt]
4534
4535    Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4536    and whose TREE_VALUE is the new-declarator.  */
4537
4538 static tree
4539 cp_parser_new_type_id (cp_parser* parser)
4540 {
4541   tree type_specifier_seq;
4542   tree declarator;
4543   const char *saved_message;
4544
4545   /* The type-specifier sequence must not contain type definitions.
4546      (It cannot contain declarations of new types either, but if they
4547      are not definitions we will catch that because they are not
4548      complete.)  */
4549   saved_message = parser->type_definition_forbidden_message;
4550   parser->type_definition_forbidden_message
4551     = "types may not be defined in a new-type-id";
4552   /* Parse the type-specifier-seq.  */
4553   type_specifier_seq = cp_parser_type_specifier_seq (parser);
4554   /* Restore the old message.  */
4555   parser->type_definition_forbidden_message = saved_message;
4556   /* Parse the new-declarator.  */
4557   declarator = cp_parser_new_declarator_opt (parser);
4558
4559   return build_tree_list (type_specifier_seq, declarator);
4560 }
4561
4562 /* Parse an (optional) new-declarator.
4563
4564    new-declarator:
4565      ptr-operator new-declarator [opt]
4566      direct-new-declarator
4567
4568    Returns a representation of the declarator.  See
4569    cp_parser_declarator for the representations used.  */
4570
4571 static tree
4572 cp_parser_new_declarator_opt (cp_parser* parser)
4573 {
4574   enum tree_code code;
4575   tree type;
4576   tree cv_qualifier_seq;
4577
4578   /* We don't know if there's a ptr-operator next, or not.  */
4579   cp_parser_parse_tentatively (parser);
4580   /* Look for a ptr-operator.  */
4581   code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4582   /* If that worked, look for more new-declarators.  */
4583   if (cp_parser_parse_definitely (parser))
4584     {
4585       tree declarator;
4586
4587       /* Parse another optional declarator.  */
4588       declarator = cp_parser_new_declarator_opt (parser);
4589
4590       /* Create the representation of the declarator.  */
4591       if (code == INDIRECT_REF)
4592         declarator = make_pointer_declarator (cv_qualifier_seq,
4593                                               declarator);
4594       else
4595         declarator = make_reference_declarator (cv_qualifier_seq,
4596                                                 declarator);
4597
4598      /* Handle the pointer-to-member case.  */
4599      if (type)
4600        declarator = build_nt (SCOPE_REF, type, declarator);
4601
4602       return declarator;
4603     }
4604
4605   /* If the next token is a `[', there is a direct-new-declarator.  */
4606   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4607     return cp_parser_direct_new_declarator (parser);
4608
4609   return NULL_TREE;
4610 }
4611
4612 /* Parse a direct-new-declarator.
4613
4614    direct-new-declarator:
4615      [ expression ]
4616      direct-new-declarator [constant-expression]  
4617
4618    Returns an ARRAY_REF, following the same conventions as are
4619    documented for cp_parser_direct_declarator.  */
4620
4621 static tree
4622 cp_parser_direct_new_declarator (cp_parser* parser)
4623 {
4624   tree declarator = NULL_TREE;
4625
4626   while (true)
4627     {
4628       tree expression;
4629
4630       /* Look for the opening `['.  */
4631       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4632       /* The first expression is not required to be constant.  */
4633       if (!declarator)
4634         {
4635           expression = cp_parser_expression (parser);
4636           /* The standard requires that the expression have integral
4637              type.  DR 74 adds enumeration types.  We believe that the
4638              real intent is that these expressions be handled like the
4639              expression in a `switch' condition, which also allows
4640              classes with a single conversion to integral or
4641              enumeration type.  */
4642           if (!processing_template_decl)
4643             {
4644               expression 
4645                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4646                                               expression,
4647                                               /*complain=*/true);
4648               if (!expression)
4649                 {
4650                   error ("expression in new-declarator must have integral or enumeration type");
4651                   expression = error_mark_node;
4652                 }
4653             }
4654         }
4655       /* But all the other expressions must be.  */
4656       else
4657         expression 
4658           = cp_parser_constant_expression (parser, 
4659                                            /*allow_non_constant=*/false,
4660                                            NULL);
4661       /* Look for the closing `]'.  */
4662       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4663
4664       /* Add this bound to the declarator.  */
4665       declarator = build_nt (ARRAY_REF, declarator, expression);
4666
4667       /* If the next token is not a `[', then there are no more
4668          bounds.  */
4669       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4670         break;
4671     }
4672
4673   return declarator;
4674 }
4675
4676 /* Parse a new-initializer.
4677
4678    new-initializer:
4679      ( expression-list [opt] )
4680
4681    Returns a representation of the expression-list.  If there is no
4682    expression-list, VOID_ZERO_NODE is returned.  */
4683
4684 static tree
4685 cp_parser_new_initializer (cp_parser* parser)
4686 {
4687   tree expression_list;
4688
4689   expression_list = (cp_parser_parenthesized_expression_list 
4690                      (parser, false, /*non_constant_p=*/NULL));
4691   if (!expression_list)
4692     expression_list = void_zero_node;
4693
4694   return expression_list;
4695 }
4696
4697 /* Parse a delete-expression.
4698
4699    delete-expression:
4700      :: [opt] delete cast-expression
4701      :: [opt] delete [ ] cast-expression
4702
4703    Returns a representation of the expression.  */
4704
4705 static tree
4706 cp_parser_delete_expression (cp_parser* parser)
4707 {
4708   bool global_scope_p;
4709   bool array_p;
4710   tree expression;
4711
4712   /* Look for the optional `::' operator.  */
4713   global_scope_p
4714     = (cp_parser_global_scope_opt (parser,
4715                                    /*current_scope_valid_p=*/false)
4716        != NULL_TREE);
4717   /* Look for the `delete' keyword.  */
4718   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4719   /* See if the array syntax is in use.  */
4720   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4721     {
4722       /* Consume the `[' token.  */
4723       cp_lexer_consume_token (parser->lexer);
4724       /* Look for the `]' token.  */
4725       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4726       /* Remember that this is the `[]' construct.  */
4727       array_p = true;
4728     }
4729   else
4730     array_p = false;
4731
4732   /* Parse the cast-expression.  */
4733   expression = cp_parser_simple_cast_expression (parser);
4734
4735   /* A delete-expression may not appear in an integral constant
4736      expression.  */
4737   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4738     return error_mark_node;
4739
4740   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4741 }
4742
4743 /* Parse a cast-expression.
4744
4745    cast-expression:
4746      unary-expression
4747      ( type-id ) cast-expression
4748
4749    Returns a representation of the expression.  */
4750
4751 static tree
4752 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4753 {
4754   /* If it's a `(', then we might be looking at a cast.  */
4755   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4756     {
4757       tree type = NULL_TREE;
4758       tree expr = NULL_TREE;
4759       bool compound_literal_p;
4760       const char *saved_message;
4761
4762       /* There's no way to know yet whether or not this is a cast.
4763          For example, `(int (3))' is a unary-expression, while `(int)
4764          3' is a cast.  So, we resort to parsing tentatively.  */
4765       cp_parser_parse_tentatively (parser);
4766       /* Types may not be defined in a cast.  */
4767       saved_message = parser->type_definition_forbidden_message;
4768       parser->type_definition_forbidden_message
4769         = "types may not be defined in casts";
4770       /* Consume the `('.  */
4771       cp_lexer_consume_token (parser->lexer);
4772       /* A very tricky bit is that `(struct S) { 3 }' is a
4773          compound-literal (which we permit in C++ as an extension).
4774          But, that construct is not a cast-expression -- it is a
4775          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
4776          is legal; if the compound-literal were a cast-expression,
4777          you'd need an extra set of parentheses.)  But, if we parse
4778          the type-id, and it happens to be a class-specifier, then we
4779          will commit to the parse at that point, because we cannot
4780          undo the action that is done when creating a new class.  So,
4781          then we cannot back up and do a postfix-expression.  
4782
4783          Therefore, we scan ahead to the closing `)', and check to see
4784          if the token after the `)' is a `{'.  If so, we are not
4785          looking at a cast-expression.  
4786
4787          Save tokens so that we can put them back.  */
4788       cp_lexer_save_tokens (parser->lexer);
4789       /* Skip tokens until the next token is a closing parenthesis.
4790          If we find the closing `)', and the next token is a `{', then
4791          we are looking at a compound-literal.  */
4792       compound_literal_p 
4793         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4794                                                   /*consume_paren=*/true)
4795            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4796       /* Roll back the tokens we skipped.  */
4797       cp_lexer_rollback_tokens (parser->lexer);
4798       /* If we were looking at a compound-literal, simulate an error
4799          so that the call to cp_parser_parse_definitely below will
4800          fail.  */
4801       if (compound_literal_p)
4802         cp_parser_simulate_error (parser);
4803       else
4804         {
4805           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4806           parser->in_type_id_in_expr_p = true;
4807           /* Look for the type-id.  */
4808           type = cp_parser_type_id (parser);
4809           /* Look for the closing `)'.  */
4810           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4811           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4812         }
4813
4814       /* Restore the saved message.  */
4815       parser->type_definition_forbidden_message = saved_message;
4816
4817       /* If ok so far, parse the dependent expression. We cannot be
4818          sure it is a cast. Consider `(T ())'.  It is a parenthesized
4819          ctor of T, but looks like a cast to function returning T
4820          without a dependent expression.  */
4821       if (!cp_parser_error_occurred (parser))
4822         expr = cp_parser_simple_cast_expression (parser);
4823
4824       if (cp_parser_parse_definitely (parser))
4825         {
4826           /* Warn about old-style casts, if so requested.  */
4827           if (warn_old_style_cast 
4828               && !in_system_header 
4829               && !VOID_TYPE_P (type) 
4830               && current_lang_name != lang_name_c)
4831             warning ("use of old-style cast");
4832
4833           /* Only type conversions to integral or enumeration types
4834              can be used in constant-expressions.  */
4835           if (parser->integral_constant_expression_p
4836               && !dependent_type_p (type)
4837               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4838               && (cp_parser_non_integral_constant_expression 
4839                   (parser,
4840                    "a casts to a type other than an integral or "
4841                    "enumeration type")))
4842             return error_mark_node;
4843
4844           /* Perform the cast.  */
4845           expr = build_c_cast (type, expr);
4846           return expr;
4847         }
4848     }
4849
4850   /* If we get here, then it's not a cast, so it must be a
4851      unary-expression.  */
4852   return cp_parser_unary_expression (parser, address_p);
4853 }
4854
4855 /* Parse a pm-expression.
4856
4857    pm-expression:
4858      cast-expression
4859      pm-expression .* cast-expression
4860      pm-expression ->* cast-expression
4861
4862      Returns a representation of the expression.  */
4863
4864 static tree
4865 cp_parser_pm_expression (cp_parser* parser)
4866 {
4867   static const cp_parser_token_tree_map map = {
4868     { CPP_DEREF_STAR, MEMBER_REF },
4869     { CPP_DOT_STAR, DOTSTAR_EXPR },
4870     { CPP_EOF, ERROR_MARK }
4871   };
4872
4873   return cp_parser_binary_expression (parser, map, 
4874                                       cp_parser_simple_cast_expression);
4875 }
4876
4877 /* Parse a multiplicative-expression.
4878
4879    mulitplicative-expression:
4880      pm-expression
4881      multiplicative-expression * pm-expression
4882      multiplicative-expression / pm-expression
4883      multiplicative-expression % pm-expression
4884
4885    Returns a representation of the expression.  */
4886
4887 static tree
4888 cp_parser_multiplicative_expression (cp_parser* parser)
4889 {
4890   static const cp_parser_token_tree_map map = {
4891     { CPP_MULT, MULT_EXPR },
4892     { CPP_DIV, TRUNC_DIV_EXPR },
4893     { CPP_MOD, TRUNC_MOD_EXPR },
4894     { CPP_EOF, ERROR_MARK }
4895   };
4896
4897   return cp_parser_binary_expression (parser,
4898                                       map,
4899                                       cp_parser_pm_expression);
4900 }
4901
4902 /* Parse an additive-expression.
4903
4904    additive-expression:
4905      multiplicative-expression
4906      additive-expression + multiplicative-expression
4907      additive-expression - multiplicative-expression
4908
4909    Returns a representation of the expression.  */
4910
4911 static tree
4912 cp_parser_additive_expression (cp_parser* parser)
4913 {
4914   static const cp_parser_token_tree_map map = {
4915     { CPP_PLUS, PLUS_EXPR },
4916     { CPP_MINUS, MINUS_EXPR },
4917     { CPP_EOF, ERROR_MARK }
4918   };
4919
4920   return cp_parser_binary_expression (parser,
4921                                       map,
4922                                       cp_parser_multiplicative_expression);
4923 }
4924
4925 /* Parse a shift-expression.
4926
4927    shift-expression:
4928      additive-expression
4929      shift-expression << additive-expression
4930      shift-expression >> additive-expression
4931
4932    Returns a representation of the expression.  */
4933
4934 static tree
4935 cp_parser_shift_expression (cp_parser* parser)
4936 {
4937   static const cp_parser_token_tree_map map = {
4938     { CPP_LSHIFT, LSHIFT_EXPR },
4939     { CPP_RSHIFT, RSHIFT_EXPR },
4940     { CPP_EOF, ERROR_MARK }
4941   };
4942
4943   return cp_parser_binary_expression (parser,
4944                                       map,
4945                                       cp_parser_additive_expression);
4946 }
4947
4948 /* Parse a relational-expression.
4949
4950    relational-expression:
4951      shift-expression
4952      relational-expression < shift-expression
4953      relational-expression > shift-expression
4954      relational-expression <= shift-expression
4955      relational-expression >= shift-expression
4956
4957    GNU Extension:
4958
4959    relational-expression:
4960      relational-expression <? shift-expression
4961      relational-expression >? shift-expression
4962
4963    Returns a representation of the expression.  */
4964
4965 static tree
4966 cp_parser_relational_expression (cp_parser* parser)
4967 {
4968   static const cp_parser_token_tree_map map = {
4969     { CPP_LESS, LT_EXPR },
4970     { CPP_GREATER, GT_EXPR },
4971     { CPP_LESS_EQ, LE_EXPR },
4972     { CPP_GREATER_EQ, GE_EXPR },
4973     { CPP_MIN, MIN_EXPR },
4974     { CPP_MAX, MAX_EXPR },
4975     { CPP_EOF, ERROR_MARK }
4976   };
4977
4978   return cp_parser_binary_expression (parser,
4979                                       map,
4980                                       cp_parser_shift_expression);
4981 }
4982
4983 /* Parse an equality-expression.
4984
4985    equality-expression:
4986      relational-expression
4987      equality-expression == relational-expression
4988      equality-expression != relational-expression
4989
4990    Returns a representation of the expression.  */
4991
4992 static tree
4993 cp_parser_equality_expression (cp_parser* parser)
4994 {
4995   static const cp_parser_token_tree_map map = {
4996     { CPP_EQ_EQ, EQ_EXPR },
4997     { CPP_NOT_EQ, NE_EXPR },
4998     { CPP_EOF, ERROR_MARK }
4999   };
5000
5001   return cp_parser_binary_expression (parser,
5002                                       map,
5003                                       cp_parser_relational_expression);
5004 }
5005
5006 /* Parse an and-expression.
5007
5008    and-expression:
5009      equality-expression
5010      and-expression & equality-expression
5011
5012    Returns a representation of the expression.  */
5013
5014 static tree
5015 cp_parser_and_expression (cp_parser* parser)
5016 {
5017   static const cp_parser_token_tree_map map = {
5018     { CPP_AND, BIT_AND_EXPR },
5019     { CPP_EOF, ERROR_MARK }
5020   };
5021
5022   return cp_parser_binary_expression (parser,
5023                                       map,
5024                                       cp_parser_equality_expression);
5025 }
5026
5027 /* Parse an exclusive-or-expression.
5028
5029    exclusive-or-expression:
5030      and-expression
5031      exclusive-or-expression ^ and-expression
5032
5033    Returns a representation of the expression.  */
5034
5035 static tree
5036 cp_parser_exclusive_or_expression (cp_parser* parser)
5037 {
5038   static const cp_parser_token_tree_map map = {
5039     { CPP_XOR, BIT_XOR_EXPR },
5040     { CPP_EOF, ERROR_MARK }
5041   };
5042
5043   return cp_parser_binary_expression (parser,
5044                                       map,
5045                                       cp_parser_and_expression);
5046 }
5047
5048
5049 /* Parse an inclusive-or-expression.
5050
5051    inclusive-or-expression:
5052      exclusive-or-expression
5053      inclusive-or-expression | exclusive-or-expression
5054
5055    Returns a representation of the expression.  */
5056
5057 static tree
5058 cp_parser_inclusive_or_expression (cp_parser* parser)
5059 {
5060   static const cp_parser_token_tree_map map = {
5061     { CPP_OR, BIT_IOR_EXPR },
5062     { CPP_EOF, ERROR_MARK }
5063   };
5064
5065   return cp_parser_binary_expression (parser,
5066                                       map,
5067                                       cp_parser_exclusive_or_expression);
5068 }
5069
5070 /* Parse a logical-and-expression.
5071
5072    logical-and-expression:
5073      inclusive-or-expression
5074      logical-and-expression && inclusive-or-expression
5075
5076    Returns a representation of the expression.  */
5077
5078 static tree
5079 cp_parser_logical_and_expression (cp_parser* parser)
5080 {
5081   static const cp_parser_token_tree_map map = {
5082     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5083     { CPP_EOF, ERROR_MARK }
5084   };
5085
5086   return cp_parser_binary_expression (parser,
5087                                       map,
5088                                       cp_parser_inclusive_or_expression);
5089 }
5090
5091 /* Parse a logical-or-expression.
5092
5093    logical-or-expression:
5094      logical-and-expression
5095      logical-or-expression || logical-and-expression
5096
5097    Returns a representation of the expression.  */
5098
5099 static tree
5100 cp_parser_logical_or_expression (cp_parser* parser)
5101 {
5102   static const cp_parser_token_tree_map map = {
5103     { CPP_OR_OR, TRUTH_ORIF_EXPR },
5104     { CPP_EOF, ERROR_MARK }
5105   };
5106
5107   return cp_parser_binary_expression (parser,
5108                                       map,
5109                                       cp_parser_logical_and_expression);
5110 }
5111
5112 /* Parse the `? expression : assignment-expression' part of a
5113    conditional-expression.  The LOGICAL_OR_EXPR is the
5114    logical-or-expression that started the conditional-expression.
5115    Returns a representation of the entire conditional-expression.
5116
5117    This routine is used by cp_parser_assignment_expression.
5118
5119      ? expression : assignment-expression
5120    
5121    GNU Extensions:
5122    
5123      ? : assignment-expression */
5124
5125 static tree
5126 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5127 {
5128   tree expr;
5129   tree assignment_expr;
5130
5131   /* Consume the `?' token.  */
5132   cp_lexer_consume_token (parser->lexer);
5133   if (cp_parser_allow_gnu_extensions_p (parser)
5134       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5135     /* Implicit true clause.  */
5136     expr = NULL_TREE;
5137   else
5138     /* Parse the expression.  */
5139     expr = cp_parser_expression (parser);
5140   
5141   /* The next token should be a `:'.  */
5142   cp_parser_require (parser, CPP_COLON, "`:'");
5143   /* Parse the assignment-expression.  */
5144   assignment_expr = cp_parser_assignment_expression (parser);
5145
5146   /* Build the conditional-expression.  */
5147   return build_x_conditional_expr (logical_or_expr,
5148                                    expr,
5149                                    assignment_expr);
5150 }
5151
5152 /* Parse an assignment-expression.
5153
5154    assignment-expression:
5155      conditional-expression
5156      logical-or-expression assignment-operator assignment_expression
5157      throw-expression
5158
5159    Returns a representation for the expression.  */
5160
5161 static tree
5162 cp_parser_assignment_expression (cp_parser* parser)
5163 {
5164   tree expr;
5165
5166   /* If the next token is the `throw' keyword, then we're looking at
5167      a throw-expression.  */
5168   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5169     expr = cp_parser_throw_expression (parser);
5170   /* Otherwise, it must be that we are looking at a
5171      logical-or-expression.  */
5172   else
5173     {
5174       /* Parse the logical-or-expression.  */
5175       expr = cp_parser_logical_or_expression (parser);
5176       /* If the next token is a `?' then we're actually looking at a
5177          conditional-expression.  */
5178       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5179         return cp_parser_question_colon_clause (parser, expr);
5180       else 
5181         {
5182           enum tree_code assignment_operator;
5183
5184           /* If it's an assignment-operator, we're using the second
5185              production.  */
5186           assignment_operator 
5187             = cp_parser_assignment_operator_opt (parser);
5188           if (assignment_operator != ERROR_MARK)
5189             {
5190               tree rhs;
5191
5192               /* Parse the right-hand side of the assignment.  */
5193               rhs = cp_parser_assignment_expression (parser);
5194               /* An assignment may not appear in a
5195                  constant-expression.  */
5196               if (cp_parser_non_integral_constant_expression (parser,
5197                                                               "an assignment"))
5198                 return error_mark_node;
5199               /* Build the assignment expression.  */
5200               expr = build_x_modify_expr (expr, 
5201                                           assignment_operator, 
5202                                           rhs);
5203             }
5204         }
5205     }
5206
5207   return expr;
5208 }
5209
5210 /* Parse an (optional) assignment-operator.
5211
5212    assignment-operator: one of 
5213      = *= /= %= += -= >>= <<= &= ^= |=  
5214
5215    GNU Extension:
5216    
5217    assignment-operator: one of
5218      <?= >?=
5219
5220    If the next token is an assignment operator, the corresponding tree
5221    code is returned, and the token is consumed.  For example, for
5222    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5223    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5224    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5225    operator, ERROR_MARK is returned.  */
5226
5227 static enum tree_code
5228 cp_parser_assignment_operator_opt (cp_parser* parser)
5229 {
5230   enum tree_code op;
5231   cp_token *token;
5232
5233   /* Peek at the next toen.  */
5234   token = cp_lexer_peek_token (parser->lexer);
5235
5236   switch (token->type)
5237     {
5238     case CPP_EQ:
5239       op = NOP_EXPR;
5240       break;
5241
5242     case CPP_MULT_EQ:
5243       op = MULT_EXPR;
5244       break;
5245
5246     case CPP_DIV_EQ:
5247       op = TRUNC_DIV_EXPR;
5248       break;
5249
5250     case CPP_MOD_EQ:
5251       op = TRUNC_MOD_EXPR;
5252       break;
5253
5254     case CPP_PLUS_EQ:
5255       op = PLUS_EXPR;
5256       break;
5257
5258     case CPP_MINUS_EQ:
5259       op = MINUS_EXPR;
5260       break;
5261
5262     case CPP_RSHIFT_EQ:
5263       op = RSHIFT_EXPR;
5264       break;
5265
5266     case CPP_LSHIFT_EQ:
5267       op = LSHIFT_EXPR;
5268       break;
5269
5270     case CPP_AND_EQ:
5271       op = BIT_AND_EXPR;
5272       break;
5273
5274     case CPP_XOR_EQ:
5275       op = BIT_XOR_EXPR;
5276       break;
5277
5278     case CPP_OR_EQ:
5279       op = BIT_IOR_EXPR;
5280       break;
5281
5282     case CPP_MIN_EQ:
5283       op = MIN_EXPR;
5284       break;
5285
5286     case CPP_MAX_EQ:
5287       op = MAX_EXPR;
5288       break;
5289
5290     default: 
5291       /* Nothing else is an assignment operator.  */
5292       op = ERROR_MARK;
5293     }
5294
5295   /* If it was an assignment operator, consume it.  */
5296   if (op != ERROR_MARK)
5297     cp_lexer_consume_token (parser->lexer);
5298
5299   return op;
5300 }
5301
5302 /* Parse an expression.
5303
5304    expression:
5305      assignment-expression
5306      expression , assignment-expression
5307
5308    Returns a representation of the expression.  */
5309
5310 static tree
5311 cp_parser_expression (cp_parser* parser)
5312 {
5313   tree expression = NULL_TREE;
5314
5315   while (true)
5316     {
5317       tree assignment_expression;
5318
5319       /* Parse the next assignment-expression.  */
5320       assignment_expression 
5321         = cp_parser_assignment_expression (parser);
5322       /* If this is the first assignment-expression, we can just
5323          save it away.  */
5324       if (!expression)
5325         expression = assignment_expression;
5326       else
5327         expression = build_x_compound_expr (expression,
5328                                             assignment_expression);
5329       /* If the next token is not a comma, then we are done with the
5330          expression.  */
5331       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5332         break;
5333       /* Consume the `,'.  */
5334       cp_lexer_consume_token (parser->lexer);
5335       /* A comma operator cannot appear in a constant-expression.  */
5336       if (cp_parser_non_integral_constant_expression (parser,
5337                                                       "a comma operator"))
5338         expression = error_mark_node;
5339     }
5340
5341   return expression;
5342 }
5343
5344 /* Parse a constant-expression. 
5345
5346    constant-expression:
5347      conditional-expression  
5348
5349   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5350   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5351   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5352   is false, NON_CONSTANT_P should be NULL.  */
5353
5354 static tree
5355 cp_parser_constant_expression (cp_parser* parser, 
5356                                bool allow_non_constant_p,
5357                                bool *non_constant_p)
5358 {
5359   bool saved_integral_constant_expression_p;
5360   bool saved_allow_non_integral_constant_expression_p;
5361   bool saved_non_integral_constant_expression_p;
5362   tree expression;
5363
5364   /* It might seem that we could simply parse the
5365      conditional-expression, and then check to see if it were
5366      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5367      one that the compiler can figure out is constant, possibly after
5368      doing some simplifications or optimizations.  The standard has a
5369      precise definition of constant-expression, and we must honor
5370      that, even though it is somewhat more restrictive.
5371
5372      For example:
5373
5374        int i[(2, 3)];
5375
5376      is not a legal declaration, because `(2, 3)' is not a
5377      constant-expression.  The `,' operator is forbidden in a
5378      constant-expression.  However, GCC's constant-folding machinery
5379      will fold this operation to an INTEGER_CST for `3'.  */
5380
5381   /* Save the old settings.  */
5382   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5383   saved_allow_non_integral_constant_expression_p 
5384     = parser->allow_non_integral_constant_expression_p;
5385   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5386   /* We are now parsing a constant-expression.  */
5387   parser->integral_constant_expression_p = true;
5388   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5389   parser->non_integral_constant_expression_p = false;
5390   /* Although the grammar says "conditional-expression", we parse an
5391      "assignment-expression", which also permits "throw-expression"
5392      and the use of assignment operators.  In the case that
5393      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5394      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5395      actually essential that we look for an assignment-expression.
5396      For example, cp_parser_initializer_clauses uses this function to
5397      determine whether a particular assignment-expression is in fact
5398      constant.  */
5399   expression = cp_parser_assignment_expression (parser);
5400   /* Restore the old settings.  */
5401   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5402   parser->allow_non_integral_constant_expression_p 
5403     = saved_allow_non_integral_constant_expression_p;
5404   if (allow_non_constant_p)
5405     *non_constant_p = parser->non_integral_constant_expression_p;
5406   parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5407
5408   return expression;
5409 }
5410
5411 /* Statements [gram.stmt.stmt]  */
5412
5413 /* Parse a statement.  
5414
5415    statement:
5416      labeled-statement
5417      expression-statement
5418      compound-statement
5419      selection-statement
5420      iteration-statement
5421      jump-statement
5422      declaration-statement
5423      try-block  */
5424
5425 static void
5426 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5427 {
5428   tree statement;
5429   cp_token *token;
5430   int statement_line_number;
5431
5432   /* There is no statement yet.  */
5433   statement = NULL_TREE;
5434   /* Peek at the next token.  */
5435   token = cp_lexer_peek_token (parser->lexer);
5436   /* Remember the line number of the first token in the statement.  */
5437   statement_line_number = token->location.line;
5438   /* If this is a keyword, then that will often determine what kind of
5439      statement we have.  */
5440   if (token->type == CPP_KEYWORD)
5441     {
5442       enum rid keyword = token->keyword;
5443
5444       switch (keyword)
5445         {
5446         case RID_CASE:
5447         case RID_DEFAULT:
5448           statement = cp_parser_labeled_statement (parser,
5449                                                    in_statement_expr_p);
5450           break;
5451
5452         case RID_IF:
5453         case RID_SWITCH:
5454           statement = cp_parser_selection_statement (parser);
5455           break;
5456
5457         case RID_WHILE:
5458         case RID_DO:
5459         case RID_FOR:
5460           statement = cp_parser_iteration_statement (parser);
5461           break;
5462
5463         case RID_BREAK:
5464         case RID_CONTINUE:
5465         case RID_RETURN:
5466         case RID_GOTO:
5467           statement = cp_parser_jump_statement (parser);
5468           break;
5469
5470         case RID_TRY:
5471           statement = cp_parser_try_block (parser);
5472           break;
5473
5474         default:
5475           /* It might be a keyword like `int' that can start a
5476              declaration-statement.  */
5477           break;
5478         }
5479     }
5480   else if (token->type == CPP_NAME)
5481     {
5482       /* If the next token is a `:', then we are looking at a
5483          labeled-statement.  */
5484       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5485       if (token->type == CPP_COLON)
5486         statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5487     }
5488   /* Anything that starts with a `{' must be a compound-statement.  */
5489   else if (token->type == CPP_OPEN_BRACE)
5490     statement = cp_parser_compound_statement (parser, false);
5491
5492   /* Everything else must be a declaration-statement or an
5493      expression-statement.  Try for the declaration-statement 
5494      first, unless we are looking at a `;', in which case we know that
5495      we have an expression-statement.  */
5496   if (!statement)
5497     {
5498       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5499         {
5500           cp_parser_parse_tentatively (parser);
5501           /* Try to parse the declaration-statement.  */
5502           cp_parser_declaration_statement (parser);
5503           /* If that worked, we're done.  */
5504           if (cp_parser_parse_definitely (parser))
5505             return;
5506         }
5507       /* Look for an expression-statement instead.  */
5508       statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5509     }
5510
5511   /* Set the line number for the statement.  */
5512   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5513     STMT_LINENO (statement) = statement_line_number;
5514 }
5515
5516 /* Parse a labeled-statement.
5517
5518    labeled-statement:
5519      identifier : statement
5520      case constant-expression : statement
5521      default : statement
5522
5523    GNU Extension:
5524    
5525    labeled-statement:
5526      case constant-expression ... constant-expression : statement
5527
5528    Returns the new CASE_LABEL, for a `case' or `default' label.  For
5529    an ordinary label, returns a LABEL_STMT.  */
5530
5531 static tree
5532 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5533 {
5534   cp_token *token;
5535   tree statement = error_mark_node;
5536
5537   /* The next token should be an identifier.  */
5538   token = cp_lexer_peek_token (parser->lexer);
5539   if (token->type != CPP_NAME
5540       && token->type != CPP_KEYWORD)
5541     {
5542       cp_parser_error (parser, "expected labeled-statement");
5543       return error_mark_node;
5544     }
5545
5546   switch (token->keyword)
5547     {
5548     case RID_CASE:
5549       {
5550         tree expr, expr_hi;
5551         cp_token *ellipsis;
5552
5553         /* Consume the `case' token.  */
5554         cp_lexer_consume_token (parser->lexer);
5555         /* Parse the constant-expression.  */
5556         expr = cp_parser_constant_expression (parser, 
5557                                               /*allow_non_constant_p=*/false,
5558                                               NULL);
5559
5560         ellipsis = cp_lexer_peek_token (parser->lexer);
5561         if (ellipsis->type == CPP_ELLIPSIS)
5562           {
5563             /* Consume the `...' token.  */
5564             cp_lexer_consume_token (parser->lexer);
5565             expr_hi =
5566               cp_parser_constant_expression (parser,
5567                                              /*allow_non_constant_p=*/false,
5568                                              NULL);
5569             /* We don't need to emit warnings here, as the common code
5570                will do this for us.  */
5571           }
5572         else
5573           expr_hi = NULL_TREE;
5574
5575         if (!parser->in_switch_statement_p)
5576           error ("case label `%E' not within a switch statement", expr);
5577         else
5578           statement = finish_case_label (expr, expr_hi);
5579       }
5580       break;
5581
5582     case RID_DEFAULT:
5583       /* Consume the `default' token.  */
5584       cp_lexer_consume_token (parser->lexer);
5585       if (!parser->in_switch_statement_p)
5586         error ("case label not within a switch statement");
5587       else
5588         statement = finish_case_label (NULL_TREE, NULL_TREE);
5589       break;
5590
5591     default:
5592       /* Anything else must be an ordinary label.  */
5593       statement = finish_label_stmt (cp_parser_identifier (parser));
5594       break;
5595     }
5596
5597   /* Require the `:' token.  */
5598   cp_parser_require (parser, CPP_COLON, "`:'");
5599   /* Parse the labeled statement.  */
5600   cp_parser_statement (parser, in_statement_expr_p);
5601
5602   /* Return the label, in the case of a `case' or `default' label.  */
5603   return statement;
5604 }
5605
5606 /* Parse an expression-statement.
5607
5608    expression-statement:
5609      expression [opt] ;
5610
5611    Returns the new EXPR_STMT -- or NULL_TREE if the expression
5612    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5613    indicates whether this expression-statement is part of an
5614    expression statement.  */
5615
5616 static tree
5617 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5618 {
5619   tree statement = NULL_TREE;
5620
5621   /* If the next token is a ';', then there is no expression
5622      statement.  */
5623   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5624     statement = cp_parser_expression (parser);
5625   
5626   /* Consume the final `;'.  */
5627   cp_parser_consume_semicolon_at_end_of_statement (parser);
5628
5629   if (in_statement_expr_p
5630       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5631     {
5632       /* This is the final expression statement of a statement
5633          expression.  */
5634       statement = finish_stmt_expr_expr (statement);
5635     }
5636   else if (statement)
5637     statement = finish_expr_stmt (statement);
5638   else
5639     finish_stmt ();
5640   
5641   return statement;
5642 }
5643
5644 /* Parse a compound-statement.
5645
5646    compound-statement:
5647      { statement-seq [opt] }
5648      
5649    Returns a COMPOUND_STMT representing the statement.  */
5650
5651 static tree
5652 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5653 {
5654   tree compound_stmt;
5655
5656   /* Consume the `{'.  */
5657   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5658     return error_mark_node;
5659   /* Begin the compound-statement.  */
5660   compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5661   /* Parse an (optional) statement-seq.  */
5662   cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5663   /* Finish the compound-statement.  */
5664   finish_compound_stmt (compound_stmt);
5665   /* Consume the `}'.  */
5666   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5667
5668   return compound_stmt;
5669 }
5670
5671 /* Parse an (optional) statement-seq.
5672
5673    statement-seq:
5674      statement
5675      statement-seq [opt] statement  */
5676
5677 static void
5678 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5679 {
5680   /* Scan statements until there aren't any more.  */
5681   while (true)
5682     {
5683       /* If we're looking at a `}', then we've run out of statements.  */
5684       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5685           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5686         break;
5687
5688       /* Parse the statement.  */
5689       cp_parser_statement (parser, in_statement_expr_p);
5690     }
5691 }
5692
5693 /* Parse a selection-statement.
5694
5695    selection-statement:
5696      if ( condition ) statement
5697      if ( condition ) statement else statement
5698      switch ( condition ) statement  
5699
5700    Returns the new IF_STMT or SWITCH_STMT.  */
5701
5702 static tree
5703 cp_parser_selection_statement (cp_parser* parser)
5704 {
5705   cp_token *token;
5706   enum rid keyword;
5707
5708   /* Peek at the next token.  */
5709   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5710
5711   /* See what kind of keyword it is.  */
5712   keyword = token->keyword;
5713   switch (keyword)
5714     {
5715     case RID_IF:
5716     case RID_SWITCH:
5717       {
5718         tree statement;
5719         tree condition;
5720
5721         /* Look for the `('.  */
5722         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5723           {
5724             cp_parser_skip_to_end_of_statement (parser);
5725             return error_mark_node;
5726           }
5727
5728         /* Begin the selection-statement.  */
5729         if (keyword == RID_IF)
5730           statement = begin_if_stmt ();
5731         else
5732           statement = begin_switch_stmt ();
5733
5734         /* Parse the condition.  */
5735         condition = cp_parser_condition (parser);
5736         /* Look for the `)'.  */
5737         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5738           cp_parser_skip_to_closing_parenthesis (parser, true, false,
5739                                                  /*consume_paren=*/true);
5740
5741         if (keyword == RID_IF)
5742           {
5743             tree then_stmt;
5744
5745             /* Add the condition.  */
5746             finish_if_stmt_cond (condition, statement);
5747
5748             /* Parse the then-clause.  */
5749             then_stmt = cp_parser_implicitly_scoped_statement (parser);
5750             finish_then_clause (statement);
5751
5752             /* If the next token is `else', parse the else-clause.  */
5753             if (cp_lexer_next_token_is_keyword (parser->lexer,
5754                                                 RID_ELSE))
5755               {
5756                 tree else_stmt;
5757
5758                 /* Consume the `else' keyword.  */
5759                 cp_lexer_consume_token (parser->lexer);
5760                 /* Parse the else-clause.  */
5761                 else_stmt 
5762                   = cp_parser_implicitly_scoped_statement (parser);
5763                 finish_else_clause (statement);
5764               }
5765
5766             /* Now we're all done with the if-statement.  */
5767             finish_if_stmt ();
5768           }
5769         else
5770           {
5771             tree body;
5772             bool in_switch_statement_p;
5773
5774             /* Add the condition.  */
5775             finish_switch_cond (condition, statement);
5776
5777             /* Parse the body of the switch-statement.  */
5778             in_switch_statement_p = parser->in_switch_statement_p;
5779             parser->in_switch_statement_p = true;
5780             body = cp_parser_implicitly_scoped_statement (parser);
5781             parser->in_switch_statement_p = in_switch_statement_p;
5782
5783             /* Now we're all done with the switch-statement.  */
5784             finish_switch_stmt (statement);
5785           }
5786
5787         return statement;
5788       }
5789       break;
5790
5791     default:
5792       cp_parser_error (parser, "expected selection-statement");
5793       return error_mark_node;
5794     }
5795 }
5796
5797 /* Parse a condition. 
5798
5799    condition:
5800      expression
5801      type-specifier-seq declarator = assignment-expression  
5802
5803    GNU Extension:
5804    
5805    condition:
5806      type-specifier-seq declarator asm-specification [opt] 
5807        attributes [opt] = assignment-expression
5808  
5809    Returns the expression that should be tested.  */
5810
5811 static tree
5812 cp_parser_condition (cp_parser* parser)
5813 {
5814   tree type_specifiers;
5815   const char *saved_message;
5816
5817   /* Try the declaration first.  */
5818   cp_parser_parse_tentatively (parser);
5819   /* New types are not allowed in the type-specifier-seq for a
5820      condition.  */
5821   saved_message = parser->type_definition_forbidden_message;
5822   parser->type_definition_forbidden_message
5823     = "types may not be defined in conditions";
5824   /* Parse the type-specifier-seq.  */
5825   type_specifiers = cp_parser_type_specifier_seq (parser);
5826   /* Restore the saved message.  */
5827   parser->type_definition_forbidden_message = saved_message;
5828   /* If all is well, we might be looking at a declaration.  */
5829   if (!cp_parser_error_occurred (parser))
5830     {
5831       tree decl;
5832       tree asm_specification;
5833       tree attributes;
5834       tree declarator;
5835       tree initializer = NULL_TREE;
5836       
5837       /* Parse the declarator.  */
5838       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5839                                          /*ctor_dtor_or_conv_p=*/NULL,
5840                                          /*parenthesized_p=*/NULL,
5841                                          /*member_p=*/false);
5842       /* Parse the attributes.  */
5843       attributes = cp_parser_attributes_opt (parser);
5844       /* Parse the asm-specification.  */
5845       asm_specification = cp_parser_asm_specification_opt (parser);
5846       /* If the next token is not an `=', then we might still be
5847          looking at an expression.  For example:
5848          
5849            if (A(a).x)
5850           
5851          looks like a decl-specifier-seq and a declarator -- but then
5852          there is no `=', so this is an expression.  */
5853       cp_parser_require (parser, CPP_EQ, "`='");
5854       /* If we did see an `=', then we are looking at a declaration
5855          for sure.  */
5856       if (cp_parser_parse_definitely (parser))
5857         {
5858           /* Create the declaration.  */
5859           decl = start_decl (declarator, type_specifiers, 
5860                              /*initialized_p=*/true,
5861                              attributes, /*prefix_attributes=*/NULL_TREE);
5862           /* Parse the assignment-expression.  */
5863           initializer = cp_parser_assignment_expression (parser);
5864           
5865           /* Process the initializer.  */
5866           cp_finish_decl (decl, 
5867                           initializer, 
5868                           asm_specification, 
5869                           LOOKUP_ONLYCONVERTING);
5870           
5871           return convert_from_reference (decl);
5872         }
5873     }
5874   /* If we didn't even get past the declarator successfully, we are
5875      definitely not looking at a declaration.  */
5876   else
5877     cp_parser_abort_tentative_parse (parser);
5878
5879   /* Otherwise, we are looking at an expression.  */
5880   return cp_parser_expression (parser);
5881 }
5882
5883 /* Parse an iteration-statement.
5884
5885    iteration-statement:
5886      while ( condition ) statement
5887      do statement while ( expression ) ;
5888      for ( for-init-statement condition [opt] ; expression [opt] )
5889        statement
5890
5891    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
5892
5893 static tree
5894 cp_parser_iteration_statement (cp_parser* parser)
5895 {
5896   cp_token *token;
5897   enum rid keyword;
5898   tree statement;
5899   bool in_iteration_statement_p;
5900
5901
5902   /* Peek at the next token.  */
5903   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5904   if (!token)
5905     return error_mark_node;
5906
5907   /* Remember whether or not we are already within an iteration
5908      statement.  */ 
5909   in_iteration_statement_p = parser->in_iteration_statement_p;
5910
5911   /* See what kind of keyword it is.  */
5912   keyword = token->keyword;
5913   switch (keyword)
5914     {
5915     case RID_WHILE:
5916       {
5917         tree condition;
5918
5919         /* Begin the while-statement.  */
5920         statement = begin_while_stmt ();
5921         /* Look for the `('.  */
5922         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5923         /* Parse the condition.  */
5924         condition = cp_parser_condition (parser);
5925         finish_while_stmt_cond (condition, statement);
5926         /* Look for the `)'.  */
5927         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5928         /* Parse the dependent statement.  */
5929         parser->in_iteration_statement_p = true;
5930         cp_parser_already_scoped_statement (parser);
5931         parser->in_iteration_statement_p = in_iteration_statement_p;
5932         /* We're done with the while-statement.  */
5933         finish_while_stmt (statement);
5934       }
5935       break;
5936
5937     case RID_DO:
5938       {
5939         tree expression;
5940
5941         /* Begin the do-statement.  */
5942         statement = begin_do_stmt ();
5943         /* Parse the body of the do-statement.  */
5944         parser->in_iteration_statement_p = true;
5945         cp_parser_implicitly_scoped_statement (parser);
5946         parser->in_iteration_statement_p = in_iteration_statement_p;
5947         finish_do_body (statement);
5948         /* Look for the `while' keyword.  */
5949         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5950         /* Look for the `('.  */
5951         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5952         /* Parse the expression.  */
5953         expression = cp_parser_expression (parser);
5954         /* We're done with the do-statement.  */
5955         finish_do_stmt (expression, statement);
5956         /* Look for the `)'.  */
5957         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5958         /* Look for the `;'.  */
5959         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5960       }
5961       break;
5962
5963     case RID_FOR:
5964       {
5965         tree condition = NULL_TREE;
5966         tree expression = NULL_TREE;
5967
5968         /* Begin the for-statement.  */
5969         statement = begin_for_stmt ();
5970         /* Look for the `('.  */
5971         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5972         /* Parse the initialization.  */
5973         cp_parser_for_init_statement (parser);
5974         finish_for_init_stmt (statement);
5975
5976         /* If there's a condition, process it.  */
5977         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5978           condition = cp_parser_condition (parser);
5979         finish_for_cond (condition, statement);
5980         /* Look for the `;'.  */
5981         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5982
5983         /* If there's an expression, process it.  */
5984         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5985           expression = cp_parser_expression (parser);
5986         finish_for_expr (expression, statement);
5987         /* Look for the `)'.  */
5988         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5989         
5990         /* Parse the body of the for-statement.  */
5991         parser->in_iteration_statement_p = true;
5992         cp_parser_already_scoped_statement (parser);
5993         parser->in_iteration_statement_p = in_iteration_statement_p;
5994
5995         /* We're done with the for-statement.  */
5996         finish_for_stmt (statement);
5997       }
5998       break;
5999
6000     default:
6001       cp_parser_error (parser, "expected iteration-statement");
6002       statement = error_mark_node;
6003       break;
6004     }
6005
6006   return statement;
6007 }
6008
6009 /* Parse a for-init-statement.
6010
6011    for-init-statement:
6012      expression-statement
6013      simple-declaration  */
6014
6015 static void
6016 cp_parser_for_init_statement (cp_parser* parser)
6017 {
6018   /* If the next token is a `;', then we have an empty
6019      expression-statement.  Grammatically, this is also a
6020      simple-declaration, but an invalid one, because it does not
6021      declare anything.  Therefore, if we did not handle this case
6022      specially, we would issue an error message about an invalid
6023      declaration.  */
6024   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6025     {
6026       /* We're going to speculatively look for a declaration, falling back
6027          to an expression, if necessary.  */
6028       cp_parser_parse_tentatively (parser);
6029       /* Parse the declaration.  */
6030       cp_parser_simple_declaration (parser,
6031                                     /*function_definition_allowed_p=*/false);
6032       /* If the tentative parse failed, then we shall need to look for an
6033          expression-statement.  */
6034       if (cp_parser_parse_definitely (parser))
6035         return;
6036     }
6037
6038   cp_parser_expression_statement (parser, false);
6039 }
6040
6041 /* Parse a jump-statement.
6042
6043    jump-statement:
6044      break ;
6045      continue ;
6046      return expression [opt] ;
6047      goto identifier ;  
6048
6049    GNU extension:
6050
6051    jump-statement:
6052      goto * expression ;
6053
6054    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6055    GOTO_STMT.  */
6056
6057 static tree
6058 cp_parser_jump_statement (cp_parser* parser)
6059 {
6060   tree statement = error_mark_node;
6061   cp_token *token;
6062   enum rid keyword;
6063
6064   /* Peek at the next token.  */
6065   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6066   if (!token)
6067     return error_mark_node;
6068
6069   /* See what kind of keyword it is.  */
6070   keyword = token->keyword;
6071   switch (keyword)
6072     {
6073     case RID_BREAK:
6074       if (!parser->in_switch_statement_p
6075           && !parser->in_iteration_statement_p)
6076         {
6077           error ("break statement not within loop or switch");
6078           statement = error_mark_node;
6079         }
6080       else
6081         statement = finish_break_stmt ();
6082       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6083       break;
6084
6085     case RID_CONTINUE:
6086       if (!parser->in_iteration_statement_p)
6087         {
6088           error ("continue statement not within a loop");
6089           statement = error_mark_node;
6090         }
6091       else
6092         statement = finish_continue_stmt ();
6093       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6094       break;
6095
6096     case RID_RETURN:
6097       {
6098         tree expr;
6099
6100         /* If the next token is a `;', then there is no 
6101            expression.  */
6102         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6103           expr = cp_parser_expression (parser);
6104         else
6105           expr = NULL_TREE;
6106         /* Build the return-statement.  */
6107         statement = finish_return_stmt (expr);
6108         /* Look for the final `;'.  */
6109         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6110       }
6111       break;
6112
6113     case RID_GOTO:
6114       /* Create the goto-statement.  */
6115       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6116         {
6117           /* Issue a warning about this use of a GNU extension.  */
6118           if (pedantic)
6119             pedwarn ("ISO C++ forbids computed gotos");
6120           /* Consume the '*' token.  */
6121           cp_lexer_consume_token (parser->lexer);
6122           /* Parse the dependent expression.  */
6123           finish_goto_stmt (cp_parser_expression (parser));
6124         }
6125       else
6126         finish_goto_stmt (cp_parser_identifier (parser));
6127       /* Look for the final `;'.  */
6128       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6129       break;
6130
6131     default:
6132       cp_parser_error (parser, "expected jump-statement");
6133       break;
6134     }
6135
6136   return statement;
6137 }
6138
6139 /* Parse a declaration-statement.
6140
6141    declaration-statement:
6142      block-declaration  */
6143
6144 static void
6145 cp_parser_declaration_statement (cp_parser* parser)
6146 {
6147   /* Parse the block-declaration.  */
6148   cp_parser_block_declaration (parser, /*statement_p=*/true);
6149
6150   /* Finish off the statement.  */
6151   finish_stmt ();
6152 }
6153
6154 /* Some dependent statements (like `if (cond) statement'), are
6155    implicitly in their own scope.  In other words, if the statement is
6156    a single statement (as opposed to a compound-statement), it is
6157    none-the-less treated as if it were enclosed in braces.  Any
6158    declarations appearing in the dependent statement are out of scope
6159    after control passes that point.  This function parses a statement,
6160    but ensures that is in its own scope, even if it is not a
6161    compound-statement.  
6162
6163    Returns the new statement.  */
6164
6165 static tree
6166 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6167 {
6168   tree statement;
6169
6170   /* If the token is not a `{', then we must take special action.  */
6171   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6172     {
6173       /* Create a compound-statement.  */
6174       statement = begin_compound_stmt (/*has_no_scope=*/false);
6175       /* Parse the dependent-statement.  */
6176       cp_parser_statement (parser, false);
6177       /* Finish the dummy compound-statement.  */
6178       finish_compound_stmt (statement);
6179     }
6180   /* Otherwise, we simply parse the statement directly.  */
6181   else
6182     statement = cp_parser_compound_statement (parser, false);
6183
6184   /* Return the statement.  */
6185   return statement;
6186 }
6187
6188 /* For some dependent statements (like `while (cond) statement'), we
6189    have already created a scope.  Therefore, even if the dependent
6190    statement is a compound-statement, we do not want to create another
6191    scope.  */
6192
6193 static void
6194 cp_parser_already_scoped_statement (cp_parser* parser)
6195 {
6196   /* If the token is not a `{', then we must take special action.  */
6197   if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6198     {
6199       tree statement;
6200
6201       /* Create a compound-statement.  */
6202       statement = begin_compound_stmt (/*has_no_scope=*/true);
6203       /* Parse the dependent-statement.  */
6204       cp_parser_statement (parser, false);
6205       /* Finish the dummy compound-statement.  */
6206       finish_compound_stmt (statement);
6207     }
6208   /* Otherwise, we simply parse the statement directly.  */
6209   else
6210     cp_parser_statement (parser, false);
6211 }
6212
6213 /* Declarations [gram.dcl.dcl] */
6214
6215 /* Parse an optional declaration-sequence.
6216
6217    declaration-seq:
6218      declaration
6219      declaration-seq declaration  */
6220
6221 static void
6222 cp_parser_declaration_seq_opt (cp_parser* parser)
6223 {
6224   while (true)
6225     {
6226       cp_token *token;
6227
6228       token = cp_lexer_peek_token (parser->lexer);
6229
6230       if (token->type == CPP_CLOSE_BRACE
6231           || token->type == CPP_EOF)
6232         break;
6233
6234       if (token->type == CPP_SEMICOLON) 
6235         {
6236           /* A declaration consisting of a single semicolon is
6237              invalid.  Allow it unless we're being pedantic.  */
6238           if (pedantic && !in_system_header)
6239             pedwarn ("extra `;'");
6240           cp_lexer_consume_token (parser->lexer);
6241           continue;
6242         }
6243
6244       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6245          parser to enter or exit implicit `extern "C"' blocks.  */
6246       while (pending_lang_change > 0)
6247         {
6248           push_lang_context (lang_name_c);
6249           --pending_lang_change;
6250         }
6251       while (pending_lang_change < 0)
6252         {
6253           pop_lang_context ();
6254           ++pending_lang_change;
6255         }
6256
6257       /* Parse the declaration itself.  */
6258       cp_parser_declaration (parser);
6259     }
6260 }
6261
6262 /* Parse a declaration.
6263
6264    declaration:
6265      block-declaration
6266      function-definition
6267      template-declaration
6268      explicit-instantiation
6269      explicit-specialization
6270      linkage-specification
6271      namespace-definition    
6272
6273    GNU extension:
6274
6275    declaration:
6276       __extension__ declaration */
6277
6278 static void
6279 cp_parser_declaration (cp_parser* parser)
6280 {
6281   cp_token token1;
6282   cp_token token2;
6283   int saved_pedantic;
6284
6285   /* Check for the `__extension__' keyword.  */
6286   if (cp_parser_extension_opt (parser, &saved_pedantic))
6287     {
6288       /* Parse the qualified declaration.  */
6289       cp_parser_declaration (parser);
6290       /* Restore the PEDANTIC flag.  */
6291       pedantic = saved_pedantic;
6292
6293       return;
6294     }
6295
6296   /* Try to figure out what kind of declaration is present.  */
6297   token1 = *cp_lexer_peek_token (parser->lexer);
6298   if (token1.type != CPP_EOF)
6299     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6300
6301   /* If the next token is `extern' and the following token is a string
6302      literal, then we have a linkage specification.  */
6303   if (token1.keyword == RID_EXTERN
6304       && cp_parser_is_string_literal (&token2))
6305     cp_parser_linkage_specification (parser);
6306   /* If the next token is `template', then we have either a template
6307      declaration, an explicit instantiation, or an explicit
6308      specialization.  */
6309   else if (token1.keyword == RID_TEMPLATE)
6310     {
6311       /* `template <>' indicates a template specialization.  */
6312       if (token2.type == CPP_LESS
6313           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6314         cp_parser_explicit_specialization (parser);
6315       /* `template <' indicates a template declaration.  */
6316       else if (token2.type == CPP_LESS)
6317         cp_parser_template_declaration (parser, /*member_p=*/false);
6318       /* Anything else must be an explicit instantiation.  */
6319       else
6320         cp_parser_explicit_instantiation (parser);
6321     }
6322   /* If the next token is `export', then we have a template
6323      declaration.  */
6324   else if (token1.keyword == RID_EXPORT)
6325     cp_parser_template_declaration (parser, /*member_p=*/false);
6326   /* If the next token is `extern', 'static' or 'inline' and the one
6327      after that is `template', we have a GNU extended explicit
6328      instantiation directive.  */
6329   else if (cp_parser_allow_gnu_extensions_p (parser)
6330            && (token1.keyword == RID_EXTERN
6331                || token1.keyword == RID_STATIC
6332                || token1.keyword == RID_INLINE)
6333            && token2.keyword == RID_TEMPLATE)
6334     cp_parser_explicit_instantiation (parser);
6335   /* If the next token is `namespace', check for a named or unnamed
6336      namespace definition.  */
6337   else if (token1.keyword == RID_NAMESPACE
6338            && (/* A named namespace definition.  */
6339                (token2.type == CPP_NAME
6340                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
6341                     == CPP_OPEN_BRACE))
6342                /* An unnamed namespace definition.  */
6343                || token2.type == CPP_OPEN_BRACE))
6344     cp_parser_namespace_definition (parser);
6345   /* We must have either a block declaration or a function
6346      definition.  */
6347   else
6348     /* Try to parse a block-declaration, or a function-definition.  */
6349     cp_parser_block_declaration (parser, /*statement_p=*/false);
6350 }
6351
6352 /* Parse a block-declaration.  
6353
6354    block-declaration:
6355      simple-declaration
6356      asm-definition
6357      namespace-alias-definition
6358      using-declaration
6359      using-directive  
6360
6361    GNU Extension:
6362
6363    block-declaration:
6364      __extension__ block-declaration 
6365      label-declaration
6366
6367    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6368    part of a declaration-statement.  */
6369
6370 static void
6371 cp_parser_block_declaration (cp_parser *parser, 
6372                              bool      statement_p)
6373 {
6374   cp_token *token1;
6375   int saved_pedantic;
6376
6377   /* Check for the `__extension__' keyword.  */
6378   if (cp_parser_extension_opt (parser, &saved_pedantic))
6379     {
6380       /* Parse the qualified declaration.  */
6381       cp_parser_block_declaration (parser, statement_p);
6382       /* Restore the PEDANTIC flag.  */
6383       pedantic = saved_pedantic;
6384
6385       return;
6386     }
6387
6388   /* Peek at the next token to figure out which kind of declaration is
6389      present.  */
6390   token1 = cp_lexer_peek_token (parser->lexer);
6391
6392   /* If the next keyword is `asm', we have an asm-definition.  */
6393   if (token1->keyword == RID_ASM)
6394     {
6395       if (statement_p)
6396         cp_parser_commit_to_tentative_parse (parser);
6397       cp_parser_asm_definition (parser);
6398     }
6399   /* If the next keyword is `namespace', we have a
6400      namespace-alias-definition.  */
6401   else if (token1->keyword == RID_NAMESPACE)
6402     cp_parser_namespace_alias_definition (parser);
6403   /* If the next keyword is `using', we have either a
6404      using-declaration or a using-directive.  */
6405   else if (token1->keyword == RID_USING)
6406     {
6407       cp_token *token2;
6408
6409       if (statement_p)
6410         cp_parser_commit_to_tentative_parse (parser);
6411       /* If the token after `using' is `namespace', then we have a
6412          using-directive.  */
6413       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6414       if (token2->keyword == RID_NAMESPACE)
6415         cp_parser_using_directive (parser);
6416       /* Otherwise, it's a using-declaration.  */
6417       else
6418         cp_parser_using_declaration (parser);
6419     }
6420   /* If the next keyword is `__label__' we have a label declaration.  */
6421   else if (token1->keyword == RID_LABEL)
6422     {
6423       if (statement_p)
6424         cp_parser_commit_to_tentative_parse (parser);
6425       cp_parser_label_declaration (parser);
6426     }
6427   /* Anything else must be a simple-declaration.  */
6428   else
6429     cp_parser_simple_declaration (parser, !statement_p);
6430 }
6431
6432 /* Parse a simple-declaration.
6433
6434    simple-declaration:
6435      decl-specifier-seq [opt] init-declarator-list [opt] ;  
6436
6437    init-declarator-list:
6438      init-declarator
6439      init-declarator-list , init-declarator 
6440
6441    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6442    function-definition as a simple-declaration.  */
6443
6444 static void
6445 cp_parser_simple_declaration (cp_parser* parser, 
6446                               bool function_definition_allowed_p)
6447 {
6448   tree decl_specifiers;
6449   tree attributes;
6450   int declares_class_or_enum;
6451   bool saw_declarator;
6452
6453   /* Defer access checks until we know what is being declared; the
6454      checks for names appearing in the decl-specifier-seq should be
6455      done as if we were in the scope of the thing being declared.  */
6456   push_deferring_access_checks (dk_deferred);
6457
6458   /* Parse the decl-specifier-seq.  We have to keep track of whether
6459      or not the decl-specifier-seq declares a named class or
6460      enumeration type, since that is the only case in which the
6461      init-declarator-list is allowed to be empty.  
6462
6463      [dcl.dcl]
6464
6465      In a simple-declaration, the optional init-declarator-list can be
6466      omitted only when declaring a class or enumeration, that is when
6467      the decl-specifier-seq contains either a class-specifier, an
6468      elaborated-type-specifier, or an enum-specifier.  */
6469   decl_specifiers
6470     = cp_parser_decl_specifier_seq (parser, 
6471                                     CP_PARSER_FLAGS_OPTIONAL,
6472                                     &attributes,
6473                                     &declares_class_or_enum);
6474   /* We no longer need to defer access checks.  */
6475   stop_deferring_access_checks ();
6476
6477   /* In a block scope, a valid declaration must always have a
6478      decl-specifier-seq.  By not trying to parse declarators, we can
6479      resolve the declaration/expression ambiguity more quickly.  */
6480   if (!function_definition_allowed_p && !decl_specifiers)
6481     {
6482       cp_parser_error (parser, "expected declaration");
6483       goto done;
6484     }
6485
6486   /* If the next two tokens are both identifiers, the code is
6487      erroneous. The usual cause of this situation is code like:
6488
6489        T t;
6490
6491      where "T" should name a type -- but does not.  */
6492   if (cp_parser_diagnose_invalid_type_name (parser))
6493     {
6494       /* If parsing tentatively, we should commit; we really are
6495          looking at a declaration.  */
6496       cp_parser_commit_to_tentative_parse (parser);
6497       /* Give up.  */
6498       goto done;
6499     }
6500   
6501   /* If we have seen at least one decl-specifier, and the next token
6502      is not a parenthesis, then we must be looking at a declaration.
6503      (After "int (" we might be looking at a functional cast.)  */
6504   if (decl_specifiers
6505       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6506     cp_parser_commit_to_tentative_parse (parser);
6507
6508   /* Keep going until we hit the `;' at the end of the simple
6509      declaration.  */
6510   saw_declarator = false;
6511   while (cp_lexer_next_token_is_not (parser->lexer, 
6512                                      CPP_SEMICOLON))
6513     {
6514       cp_token *token;
6515       bool function_definition_p;
6516       tree decl;
6517
6518       saw_declarator = true;
6519       /* Parse the init-declarator.  */
6520       decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6521                                         function_definition_allowed_p,
6522                                         /*member_p=*/false,
6523                                         declares_class_or_enum,
6524                                         &function_definition_p);
6525       /* If an error occurred while parsing tentatively, exit quickly.
6526          (That usually happens when in the body of a function; each
6527          statement is treated as a declaration-statement until proven
6528          otherwise.)  */
6529       if (cp_parser_error_occurred (parser))
6530         goto done;
6531       /* Handle function definitions specially.  */
6532       if (function_definition_p)
6533         {
6534           /* If the next token is a `,', then we are probably
6535              processing something like:
6536
6537                void f() {}, *p;
6538
6539              which is erroneous.  */
6540           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6541             error ("mixing declarations and function-definitions is forbidden");
6542           /* Otherwise, we're done with the list of declarators.  */
6543           else
6544             {
6545               pop_deferring_access_checks ();
6546               return;
6547             }
6548         }
6549       /* The next token should be either a `,' or a `;'.  */
6550       token = cp_lexer_peek_token (parser->lexer);
6551       /* If it's a `,', there are more declarators to come.  */
6552       if (token->type == CPP_COMMA)
6553         cp_lexer_consume_token (parser->lexer);
6554       /* If it's a `;', we are done.  */
6555       else if (token->type == CPP_SEMICOLON)
6556         break;
6557       /* Anything else is an error.  */
6558       else
6559         {
6560           /* If we have already issued an error message we don't need
6561              to issue another one.  */
6562           if (decl != error_mark_node
6563               || (cp_parser_parsing_tentatively (parser)
6564                   && !cp_parser_committed_to_tentative_parse (parser)))
6565             cp_parser_error (parser, "expected `,' or `;'");
6566           /* Skip tokens until we reach the end of the statement.  */
6567           cp_parser_skip_to_end_of_statement (parser);
6568           /* If the next token is now a `;', consume it.  */
6569           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6570             cp_lexer_consume_token (parser->lexer);
6571           goto done;
6572         }
6573       /* After the first time around, a function-definition is not
6574          allowed -- even if it was OK at first.  For example:
6575
6576            int i, f() {}
6577
6578          is not valid.  */
6579       function_definition_allowed_p = false;
6580     }
6581
6582   /* Issue an error message if no declarators are present, and the
6583      decl-specifier-seq does not itself declare a class or
6584      enumeration.  */
6585   if (!saw_declarator)
6586     {
6587       if (cp_parser_declares_only_class_p (parser))
6588         shadow_tag (decl_specifiers);
6589       /* Perform any deferred access checks.  */
6590       perform_deferred_access_checks ();
6591     }
6592
6593   /* Consume the `;'.  */
6594   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6595
6596  done:
6597   pop_deferring_access_checks ();
6598 }
6599
6600 /* Parse a decl-specifier-seq.
6601
6602    decl-specifier-seq:
6603      decl-specifier-seq [opt] decl-specifier
6604
6605    decl-specifier:
6606      storage-class-specifier
6607      type-specifier
6608      function-specifier
6609      friend
6610      typedef  
6611
6612    GNU Extension:
6613
6614    decl-specifier:
6615      attributes
6616
6617    Returns a TREE_LIST, giving the decl-specifiers in the order they
6618    appear in the source code.  The TREE_VALUE of each node is the
6619    decl-specifier.  For a keyword (such as `auto' or `friend'), the
6620    TREE_VALUE is simply the corresponding TREE_IDENTIFIER.  For the
6621    representation of a type-specifier, see cp_parser_type_specifier.  
6622
6623    If there are attributes, they will be stored in *ATTRIBUTES,
6624    represented as described above cp_parser_attributes.  
6625
6626    If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6627    appears, and the entity that will be a friend is not going to be a
6628    class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
6629    even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6630    friendship is granted might not be a class.  
6631
6632    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6633    flags:
6634
6635      1: one of the decl-specifiers is an elaborated-type-specifier
6636         (i.e., a type declaration)
6637      2: one of the decl-specifiers is an enum-specifier or a
6638         class-specifier (i.e., a type definition)
6639
6640    */
6641
6642 static tree
6643 cp_parser_decl_specifier_seq (cp_parser* parser, 
6644                               cp_parser_flags flags, 
6645                               tree* attributes,
6646                               int* declares_class_or_enum)
6647 {
6648   tree decl_specs = NULL_TREE;
6649   bool friend_p = false;
6650   bool constructor_possible_p = !parser->in_declarator_p;
6651   
6652   /* Assume no class or enumeration type is declared.  */
6653   *declares_class_or_enum = 0;
6654
6655   /* Assume there are no attributes.  */
6656   *attributes = NULL_TREE;
6657
6658   /* Keep reading specifiers until there are no more to read.  */
6659   while (true)
6660     {
6661       tree decl_spec = NULL_TREE;
6662       bool constructor_p;
6663       cp_token *token;
6664
6665       /* Peek at the next token.  */
6666       token = cp_lexer_peek_token (parser->lexer);
6667       /* Handle attributes.  */
6668       if (token->keyword == RID_ATTRIBUTE)
6669         {
6670           /* Parse the attributes.  */
6671           decl_spec = cp_parser_attributes_opt (parser);
6672           /* Add them to the list.  */
6673           *attributes = chainon (*attributes, decl_spec);
6674           continue;
6675         }
6676       /* If the next token is an appropriate keyword, we can simply
6677          add it to the list.  */
6678       switch (token->keyword)
6679         {
6680         case RID_FRIEND:
6681           /* decl-specifier:
6682                friend  */
6683           if (friend_p)
6684             error ("duplicate `friend'");
6685           else
6686             friend_p = true;
6687           /* The representation of the specifier is simply the
6688              appropriate TREE_IDENTIFIER node.  */
6689           decl_spec = token->value;
6690           /* Consume the token.  */
6691           cp_lexer_consume_token (parser->lexer);
6692           break;
6693
6694           /* function-specifier:
6695                inline
6696                virtual
6697                explicit  */
6698         case RID_INLINE:
6699         case RID_VIRTUAL:
6700         case RID_EXPLICIT:
6701           decl_spec = cp_parser_function_specifier_opt (parser);
6702           break;
6703           
6704           /* decl-specifier:
6705                typedef  */
6706         case RID_TYPEDEF:
6707           /* The representation of the specifier is simply the
6708              appropriate TREE_IDENTIFIER node.  */
6709           decl_spec = token->value;
6710           /* Consume the token.  */
6711           cp_lexer_consume_token (parser->lexer);
6712           /* A constructor declarator cannot appear in a typedef.  */
6713           constructor_possible_p = false;
6714           /* The "typedef" keyword can only occur in a declaration; we
6715              may as well commit at this point.  */
6716           cp_parser_commit_to_tentative_parse (parser);
6717           break;
6718
6719           /* storage-class-specifier:
6720                auto
6721                register
6722                static
6723                extern
6724                mutable  
6725
6726              GNU Extension:
6727                thread  */
6728         case RID_AUTO:
6729         case RID_REGISTER:
6730         case RID_STATIC:
6731         case RID_EXTERN:
6732         case RID_MUTABLE:
6733         case RID_THREAD:
6734           decl_spec = cp_parser_storage_class_specifier_opt (parser);
6735           break;
6736           
6737         default:
6738           break;
6739         }
6740
6741       /* Constructors are a special case.  The `S' in `S()' is not a
6742          decl-specifier; it is the beginning of the declarator.  */
6743       constructor_p = (!decl_spec 
6744                        && constructor_possible_p
6745                        && cp_parser_constructor_declarator_p (parser,
6746                                                               friend_p));
6747
6748       /* If we don't have a DECL_SPEC yet, then we must be looking at
6749          a type-specifier.  */
6750       if (!decl_spec && !constructor_p)
6751         {
6752           int decl_spec_declares_class_or_enum;
6753           bool is_cv_qualifier;
6754
6755           decl_spec
6756             = cp_parser_type_specifier (parser, flags,
6757                                         friend_p,
6758                                         /*is_declaration=*/true,
6759                                         &decl_spec_declares_class_or_enum,
6760                                         &is_cv_qualifier);
6761
6762           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6763
6764           /* If this type-specifier referenced a user-defined type
6765              (a typedef, class-name, etc.), then we can't allow any
6766              more such type-specifiers henceforth.
6767
6768              [dcl.spec]
6769
6770              The longest sequence of decl-specifiers that could
6771              possibly be a type name is taken as the
6772              decl-specifier-seq of a declaration.  The sequence shall
6773              be self-consistent as described below.
6774
6775              [dcl.type]
6776
6777              As a general rule, at most one type-specifier is allowed
6778              in the complete decl-specifier-seq of a declaration.  The
6779              only exceptions are the following:
6780
6781              -- const or volatile can be combined with any other
6782                 type-specifier. 
6783
6784              -- signed or unsigned can be combined with char, long,
6785                 short, or int.
6786
6787              -- ..
6788
6789              Example:
6790
6791                typedef char* Pc;
6792                void g (const int Pc);
6793
6794              Here, Pc is *not* part of the decl-specifier seq; it's
6795              the declarator.  Therefore, once we see a type-specifier
6796              (other than a cv-qualifier), we forbid any additional
6797              user-defined types.  We *do* still allow things like `int
6798              int' to be considered a decl-specifier-seq, and issue the
6799              error message later.  */
6800           if (decl_spec && !is_cv_qualifier)
6801             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6802           /* A constructor declarator cannot follow a type-specifier.  */
6803           if (decl_spec)
6804             constructor_possible_p = false;
6805         }
6806
6807       /* If we still do not have a DECL_SPEC, then there are no more
6808          decl-specifiers.  */
6809       if (!decl_spec)
6810         {
6811           /* Issue an error message, unless the entire construct was
6812              optional.  */
6813           if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6814             {
6815               cp_parser_error (parser, "expected decl specifier");
6816               return error_mark_node;
6817             }
6818
6819           break;
6820         }
6821
6822       /* Add the DECL_SPEC to the list of specifiers.  */
6823       if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6824         decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6825
6826       /* After we see one decl-specifier, further decl-specifiers are
6827          always optional.  */
6828       flags |= CP_PARSER_FLAGS_OPTIONAL;
6829     }
6830
6831   /* Don't allow a friend specifier with a class definition.  */
6832   if (friend_p && (*declares_class_or_enum & 2))
6833     error ("class definition may not be declared a friend");
6834
6835   /* We have built up the DECL_SPECS in reverse order.  Return them in
6836      the correct order.  */
6837   return nreverse (decl_specs);
6838 }
6839
6840 /* Parse an (optional) storage-class-specifier. 
6841
6842    storage-class-specifier:
6843      auto
6844      register
6845      static
6846      extern
6847      mutable  
6848
6849    GNU Extension:
6850
6851    storage-class-specifier:
6852      thread
6853
6854    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6855    
6856 static tree
6857 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6858 {
6859   switch (cp_lexer_peek_token (parser->lexer)->keyword)
6860     {
6861     case RID_AUTO:
6862     case RID_REGISTER:
6863     case RID_STATIC:
6864     case RID_EXTERN:
6865     case RID_MUTABLE:
6866     case RID_THREAD:
6867       /* Consume the token.  */
6868       return cp_lexer_consume_token (parser->lexer)->value;
6869
6870     default:
6871       return NULL_TREE;
6872     }
6873 }
6874
6875 /* Parse an (optional) function-specifier. 
6876
6877    function-specifier:
6878      inline
6879      virtual
6880      explicit
6881
6882    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6883    
6884 static tree
6885 cp_parser_function_specifier_opt (cp_parser* parser)
6886 {
6887   switch (cp_lexer_peek_token (parser->lexer)->keyword)
6888     {
6889     case RID_INLINE:
6890     case RID_VIRTUAL:
6891     case RID_EXPLICIT:
6892       /* Consume the token.  */
6893       return cp_lexer_consume_token (parser->lexer)->value;
6894
6895     default:
6896       return NULL_TREE;
6897     }
6898 }
6899
6900 /* Parse a linkage-specification.
6901
6902    linkage-specification:
6903      extern string-literal { declaration-seq [opt] }
6904      extern string-literal declaration  */
6905
6906 static void
6907 cp_parser_linkage_specification (cp_parser* parser)
6908 {
6909   cp_token *token;
6910   tree linkage;
6911
6912   /* Look for the `extern' keyword.  */
6913   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6914
6915   /* Peek at the next token.  */
6916   token = cp_lexer_peek_token (parser->lexer);
6917   /* If it's not a string-literal, then there's a problem.  */
6918   if (!cp_parser_is_string_literal (token))
6919     {
6920       cp_parser_error (parser, "expected language-name");
6921       return;
6922     }
6923   /* Consume the token.  */
6924   cp_lexer_consume_token (parser->lexer);
6925
6926   /* Transform the literal into an identifier.  If the literal is a
6927      wide-character string, or contains embedded NULs, then we can't
6928      handle it as the user wants.  */
6929   if (token->type == CPP_WSTRING
6930       || (strlen (TREE_STRING_POINTER (token->value))
6931           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6932     {
6933       cp_parser_error (parser, "invalid linkage-specification");
6934       /* Assume C++ linkage.  */
6935       linkage = get_identifier ("c++");
6936     }
6937   /* If it's a simple string constant, things are easier.  */
6938   else
6939     linkage = get_identifier (TREE_STRING_POINTER (token->value));
6940
6941   /* We're now using the new linkage.  */
6942   push_lang_context (linkage);
6943
6944   /* If the next token is a `{', then we're using the first
6945      production.  */
6946   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6947     {
6948       /* Consume the `{' token.  */
6949       cp_lexer_consume_token (parser->lexer);
6950       /* Parse the declarations.  */
6951       cp_parser_declaration_seq_opt (parser);
6952       /* Look for the closing `}'.  */
6953       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6954     }
6955   /* Otherwise, there's just one declaration.  */
6956   else
6957     {
6958       bool saved_in_unbraced_linkage_specification_p;
6959
6960       saved_in_unbraced_linkage_specification_p 
6961         = parser->in_unbraced_linkage_specification_p;
6962       parser->in_unbraced_linkage_specification_p = true;
6963       have_extern_spec = true;
6964       cp_parser_declaration (parser);
6965       have_extern_spec = false;
6966       parser->in_unbraced_linkage_specification_p 
6967         = saved_in_unbraced_linkage_specification_p;
6968     }
6969
6970   /* We're done with the linkage-specification.  */
6971   pop_lang_context ();
6972 }
6973
6974 /* Special member functions [gram.special] */
6975
6976 /* Parse a conversion-function-id.
6977
6978    conversion-function-id:
6979      operator conversion-type-id  
6980
6981    Returns an IDENTIFIER_NODE representing the operator.  */
6982
6983 static tree 
6984 cp_parser_conversion_function_id (cp_parser* parser)
6985 {
6986   tree type;
6987   tree saved_scope;
6988   tree saved_qualifying_scope;
6989   tree saved_object_scope;
6990   bool pop_p = false;
6991
6992   /* Look for the `operator' token.  */
6993   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6994     return error_mark_node;
6995   /* When we parse the conversion-type-id, the current scope will be
6996      reset.  However, we need that information in able to look up the
6997      conversion function later, so we save it here.  */
6998   saved_scope = parser->scope;
6999   saved_qualifying_scope = parser->qualifying_scope;
7000   saved_object_scope = parser->object_scope;
7001   /* We must enter the scope of the class so that the names of
7002      entities declared within the class are available in the
7003      conversion-type-id.  For example, consider:
7004
7005        struct S { 
7006          typedef int I;
7007          operator I();
7008        };
7009
7010        S::operator I() { ... }
7011
7012      In order to see that `I' is a type-name in the definition, we
7013      must be in the scope of `S'.  */
7014   if (saved_scope)
7015     pop_p = push_scope (saved_scope);
7016   /* Parse the conversion-type-id.  */
7017   type = cp_parser_conversion_type_id (parser);
7018   /* Leave the scope of the class, if any.  */
7019   if (pop_p)
7020     pop_scope (saved_scope);
7021   /* Restore the saved scope.  */
7022   parser->scope = saved_scope;
7023   parser->qualifying_scope = saved_qualifying_scope;
7024   parser->object_scope = saved_object_scope;
7025   /* If the TYPE is invalid, indicate failure.  */
7026   if (type == error_mark_node)
7027     return error_mark_node;
7028   return mangle_conv_op_name_for_type (type);
7029 }
7030
7031 /* Parse a conversion-type-id:
7032
7033    conversion-type-id:
7034      type-specifier-seq conversion-declarator [opt]
7035
7036    Returns the TYPE specified.  */
7037
7038 static tree
7039 cp_parser_conversion_type_id (cp_parser* parser)
7040 {
7041   tree attributes;
7042   tree type_specifiers;
7043   tree declarator;
7044
7045   /* Parse the attributes.  */
7046   attributes = cp_parser_attributes_opt (parser);
7047   /* Parse the type-specifiers.  */
7048   type_specifiers = cp_parser_type_specifier_seq (parser);
7049   /* If that didn't work, stop.  */
7050   if (type_specifiers == error_mark_node)
7051     return error_mark_node;
7052   /* Parse the conversion-declarator.  */
7053   declarator = cp_parser_conversion_declarator_opt (parser);
7054
7055   return grokdeclarator (declarator, type_specifiers, TYPENAME,
7056                          /*initialized=*/0, &attributes);
7057 }
7058
7059 /* Parse an (optional) conversion-declarator.
7060
7061    conversion-declarator:
7062      ptr-operator conversion-declarator [opt]  
7063
7064    Returns a representation of the declarator.  See
7065    cp_parser_declarator for details.  */
7066
7067 static tree
7068 cp_parser_conversion_declarator_opt (cp_parser* parser)
7069 {
7070   enum tree_code code;
7071   tree class_type;
7072   tree cv_qualifier_seq;
7073
7074   /* We don't know if there's a ptr-operator next, or not.  */
7075   cp_parser_parse_tentatively (parser);
7076   /* Try the ptr-operator.  */
7077   code = cp_parser_ptr_operator (parser, &class_type, 
7078                                  &cv_qualifier_seq);
7079   /* If it worked, look for more conversion-declarators.  */
7080   if (cp_parser_parse_definitely (parser))
7081     {
7082      tree declarator;
7083
7084      /* Parse another optional declarator.  */
7085      declarator = cp_parser_conversion_declarator_opt (parser);
7086
7087      /* Create the representation of the declarator.  */
7088      if (code == INDIRECT_REF)
7089        declarator = make_pointer_declarator (cv_qualifier_seq,
7090                                              declarator);
7091      else
7092        declarator =  make_reference_declarator (cv_qualifier_seq,
7093                                                 declarator);
7094
7095      /* Handle the pointer-to-member case.  */
7096      if (class_type)
7097        declarator = build_nt (SCOPE_REF, class_type, declarator);
7098
7099      return declarator;
7100    }
7101
7102   return NULL_TREE;
7103 }
7104
7105 /* Parse an (optional) ctor-initializer.
7106
7107    ctor-initializer:
7108      : mem-initializer-list  
7109
7110    Returns TRUE iff the ctor-initializer was actually present.  */
7111
7112 static bool
7113 cp_parser_ctor_initializer_opt (cp_parser* parser)
7114 {
7115   /* If the next token is not a `:', then there is no
7116      ctor-initializer.  */
7117   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7118     {
7119       /* Do default initialization of any bases and members.  */
7120       if (DECL_CONSTRUCTOR_P (current_function_decl))
7121         finish_mem_initializers (NULL_TREE);
7122
7123       return false;
7124     }
7125
7126   /* Consume the `:' token.  */
7127   cp_lexer_consume_token (parser->lexer);
7128   /* And the mem-initializer-list.  */
7129   cp_parser_mem_initializer_list (parser);
7130
7131   return true;
7132 }
7133
7134 /* Parse a mem-initializer-list.
7135
7136    mem-initializer-list:
7137      mem-initializer
7138      mem-initializer , mem-initializer-list  */
7139
7140 static void
7141 cp_parser_mem_initializer_list (cp_parser* parser)
7142 {
7143   tree mem_initializer_list = NULL_TREE;
7144
7145   /* Let the semantic analysis code know that we are starting the
7146      mem-initializer-list.  */
7147   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7148     error ("only constructors take base initializers");
7149
7150   /* Loop through the list.  */
7151   while (true)
7152     {
7153       tree mem_initializer;
7154
7155       /* Parse the mem-initializer.  */
7156       mem_initializer = cp_parser_mem_initializer (parser);
7157       /* Add it to the list, unless it was erroneous.  */
7158       if (mem_initializer)
7159         {
7160           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7161           mem_initializer_list = mem_initializer;
7162         }
7163       /* If the next token is not a `,', we're done.  */
7164       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7165         break;
7166       /* Consume the `,' token.  */
7167       cp_lexer_consume_token (parser->lexer);
7168     }
7169
7170   /* Perform semantic analysis.  */
7171   if (DECL_CONSTRUCTOR_P (current_function_decl))
7172     finish_mem_initializers (mem_initializer_list);
7173 }
7174
7175 /* Parse a mem-initializer.
7176
7177    mem-initializer:
7178      mem-initializer-id ( expression-list [opt] )  
7179
7180    GNU extension:
7181   
7182    mem-initializer:
7183      ( expression-list [opt] )
7184
7185    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7186    class) or FIELD_DECL (for a non-static data member) to initialize;
7187    the TREE_VALUE is the expression-list.  */
7188
7189 static tree
7190 cp_parser_mem_initializer (cp_parser* parser)
7191 {
7192   tree mem_initializer_id;
7193   tree expression_list;
7194   tree member;
7195   
7196   /* Find out what is being initialized.  */
7197   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7198     {
7199       pedwarn ("anachronistic old-style base class initializer");
7200       mem_initializer_id = NULL_TREE;
7201     }
7202   else
7203     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7204   member = expand_member_init (mem_initializer_id);
7205   if (member && !DECL_P (member))
7206     in_base_initializer = 1;
7207
7208   expression_list 
7209     = cp_parser_parenthesized_expression_list (parser, false,
7210                                                /*non_constant_p=*/NULL);
7211   if (!expression_list)
7212     expression_list = void_type_node;
7213
7214   in_base_initializer = 0;
7215   
7216   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7217 }
7218
7219 /* Parse a mem-initializer-id.
7220
7221    mem-initializer-id:
7222      :: [opt] nested-name-specifier [opt] class-name
7223      identifier  
7224
7225    Returns a TYPE indicating the class to be initializer for the first
7226    production.  Returns an IDENTIFIER_NODE indicating the data member
7227    to be initialized for the second production.  */
7228
7229 static tree
7230 cp_parser_mem_initializer_id (cp_parser* parser)
7231 {
7232   bool global_scope_p;
7233   bool nested_name_specifier_p;
7234   bool template_p = false;
7235   tree id;
7236
7237   /* `typename' is not allowed in this context ([temp.res]).  */
7238   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7239     {
7240       error ("keyword `typename' not allowed in this context (a qualified "
7241              "member initializer is implicitly a type)");
7242       cp_lexer_consume_token (parser->lexer);
7243     }
7244   /* Look for the optional `::' operator.  */
7245   global_scope_p 
7246     = (cp_parser_global_scope_opt (parser, 
7247                                    /*current_scope_valid_p=*/false) 
7248        != NULL_TREE);
7249   /* Look for the optional nested-name-specifier.  The simplest way to
7250      implement:
7251
7252        [temp.res]
7253
7254        The keyword `typename' is not permitted in a base-specifier or
7255        mem-initializer; in these contexts a qualified name that
7256        depends on a template-parameter is implicitly assumed to be a
7257        type name.
7258
7259      is to assume that we have seen the `typename' keyword at this
7260      point.  */
7261   nested_name_specifier_p 
7262     = (cp_parser_nested_name_specifier_opt (parser,
7263                                             /*typename_keyword_p=*/true,
7264                                             /*check_dependency_p=*/true,
7265                                             /*type_p=*/true,
7266                                             /*is_declaration=*/true)
7267        != NULL_TREE);
7268   if (nested_name_specifier_p)
7269     template_p = cp_parser_optional_template_keyword (parser);
7270   /* If there is a `::' operator or a nested-name-specifier, then we
7271      are definitely looking for a class-name.  */
7272   if (global_scope_p || nested_name_specifier_p)
7273     return cp_parser_class_name (parser,
7274                                  /*typename_keyword_p=*/true,
7275                                  /*template_keyword_p=*/template_p,
7276                                  /*type_p=*/false,
7277                                  /*check_dependency_p=*/true,
7278                                  /*class_head_p=*/false,
7279                                  /*is_declaration=*/true);
7280   /* Otherwise, we could also be looking for an ordinary identifier.  */
7281   cp_parser_parse_tentatively (parser);
7282   /* Try a class-name.  */
7283   id = cp_parser_class_name (parser, 
7284                              /*typename_keyword_p=*/true,
7285                              /*template_keyword_p=*/false,
7286                              /*type_p=*/false,
7287                              /*check_dependency_p=*/true,
7288                              /*class_head_p=*/false,
7289                              /*is_declaration=*/true);
7290   /* If we found one, we're done.  */
7291   if (cp_parser_parse_definitely (parser))
7292     return id;
7293   /* Otherwise, look for an ordinary identifier.  */
7294   return cp_parser_identifier (parser);
7295 }
7296
7297 /* Overloading [gram.over] */
7298
7299 /* Parse an operator-function-id.
7300
7301    operator-function-id:
7302      operator operator  
7303
7304    Returns an IDENTIFIER_NODE for the operator which is a
7305    human-readable spelling of the identifier, e.g., `operator +'.  */
7306
7307 static tree 
7308 cp_parser_operator_function_id (cp_parser* parser)
7309 {
7310   /* Look for the `operator' keyword.  */
7311   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7312     return error_mark_node;
7313   /* And then the name of the operator itself.  */
7314   return cp_parser_operator (parser);
7315 }
7316
7317 /* Parse an operator.
7318
7319    operator:
7320      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7321      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7322      || ++ -- , ->* -> () []
7323
7324    GNU Extensions:
7325    
7326    operator:
7327      <? >? <?= >?=
7328
7329    Returns an IDENTIFIER_NODE for the operator which is a
7330    human-readable spelling of the identifier, e.g., `operator +'.  */
7331    
7332 static tree
7333 cp_parser_operator (cp_parser* parser)
7334 {
7335   tree id = NULL_TREE;
7336   cp_token *token;
7337
7338   /* Peek at the next token.  */
7339   token = cp_lexer_peek_token (parser->lexer);
7340   /* Figure out which operator we have.  */
7341   switch (token->type)
7342     {
7343     case CPP_KEYWORD:
7344       {
7345         enum tree_code op;
7346
7347         /* The keyword should be either `new' or `delete'.  */
7348         if (token->keyword == RID_NEW)
7349           op = NEW_EXPR;
7350         else if (token->keyword == RID_DELETE)
7351           op = DELETE_EXPR;
7352         else
7353           break;
7354
7355         /* Consume the `new' or `delete' token.  */
7356         cp_lexer_consume_token (parser->lexer);
7357
7358         /* Peek at the next token.  */
7359         token = cp_lexer_peek_token (parser->lexer);
7360         /* If it's a `[' token then this is the array variant of the
7361            operator.  */
7362         if (token->type == CPP_OPEN_SQUARE)
7363           {
7364             /* Consume the `[' token.  */
7365             cp_lexer_consume_token (parser->lexer);
7366             /* Look for the `]' token.  */
7367             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7368             id = ansi_opname (op == NEW_EXPR 
7369                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7370           }
7371         /* Otherwise, we have the non-array variant.  */
7372         else
7373           id = ansi_opname (op);
7374
7375         return id;
7376       }
7377
7378     case CPP_PLUS:
7379       id = ansi_opname (PLUS_EXPR);
7380       break;
7381
7382     case CPP_MINUS:
7383       id = ansi_opname (MINUS_EXPR);
7384       break;
7385
7386     case CPP_MULT:
7387       id = ansi_opname (MULT_EXPR);
7388       break;
7389
7390     case CPP_DIV:
7391       id = ansi_opname (TRUNC_DIV_EXPR);
7392       break;
7393
7394     case CPP_MOD:
7395       id = ansi_opname (TRUNC_MOD_EXPR);
7396       break;
7397
7398     case CPP_XOR:
7399       id = ansi_opname (BIT_XOR_EXPR);
7400       break;
7401
7402     case CPP_AND:
7403       id = ansi_opname (BIT_AND_EXPR);
7404       break;
7405
7406     case CPP_OR:
7407       id = ansi_opname (BIT_IOR_EXPR);
7408       break;
7409
7410     case CPP_COMPL:
7411       id = ansi_opname (BIT_NOT_EXPR);
7412       break;
7413       
7414     case CPP_NOT:
7415       id = ansi_opname (TRUTH_NOT_EXPR);
7416       break;
7417
7418     case CPP_EQ:
7419       id = ansi_assopname (NOP_EXPR);
7420       break;
7421
7422     case CPP_LESS:
7423       id = ansi_opname (LT_EXPR);
7424       break;
7425
7426     case CPP_GREATER:
7427       id = ansi_opname (GT_EXPR);
7428       break;
7429
7430     case CPP_PLUS_EQ:
7431       id = ansi_assopname (PLUS_EXPR);
7432       break;
7433
7434     case CPP_MINUS_EQ:
7435       id = ansi_assopname (MINUS_EXPR);
7436       break;
7437
7438     case CPP_MULT_EQ:
7439       id = ansi_assopname (MULT_EXPR);
7440       break;
7441
7442     case CPP_DIV_EQ:
7443       id = ansi_assopname (TRUNC_DIV_EXPR);
7444       break;
7445
7446     case CPP_MOD_EQ:
7447       id = ansi_assopname (TRUNC_MOD_EXPR);
7448       break;
7449
7450     case CPP_XOR_EQ:
7451       id = ansi_assopname (BIT_XOR_EXPR);
7452       break;
7453
7454     case CPP_AND_EQ:
7455       id = ansi_assopname (BIT_AND_EXPR);
7456       break;
7457
7458     case CPP_OR_EQ:
7459       id = ansi_assopname (BIT_IOR_EXPR);
7460       break;
7461
7462     case CPP_LSHIFT:
7463       id = ansi_opname (LSHIFT_EXPR);
7464       break;
7465
7466     case CPP_RSHIFT:
7467       id = ansi_opname (RSHIFT_EXPR);
7468       break;
7469
7470     case CPP_LSHIFT_EQ:
7471       id = ansi_assopname (LSHIFT_EXPR);
7472       break;
7473
7474     case CPP_RSHIFT_EQ:
7475       id = ansi_assopname (RSHIFT_EXPR);
7476       break;
7477
7478     case CPP_EQ_EQ:
7479       id = ansi_opname (EQ_EXPR);
7480       break;
7481
7482     case CPP_NOT_EQ:
7483       id = ansi_opname (NE_EXPR);
7484       break;
7485
7486     case CPP_LESS_EQ:
7487       id = ansi_opname (LE_EXPR);
7488       break;
7489
7490     case CPP_GREATER_EQ:
7491       id = ansi_opname (GE_EXPR);
7492       break;
7493
7494     case CPP_AND_AND:
7495       id = ansi_opname (TRUTH_ANDIF_EXPR);
7496       break;
7497
7498     case CPP_OR_OR:
7499       id = ansi_opname (TRUTH_ORIF_EXPR);
7500       break;
7501       
7502     case CPP_PLUS_PLUS:
7503       id = ansi_opname (POSTINCREMENT_EXPR);
7504       break;
7505
7506     case CPP_MINUS_MINUS:
7507       id = ansi_opname (PREDECREMENT_EXPR);
7508       break;
7509
7510     case CPP_COMMA:
7511       id = ansi_opname (COMPOUND_EXPR);
7512       break;
7513
7514     case CPP_DEREF_STAR:
7515       id = ansi_opname (MEMBER_REF);
7516       break;
7517
7518     case CPP_DEREF:
7519       id = ansi_opname (COMPONENT_REF);
7520       break;
7521
7522     case CPP_OPEN_PAREN:
7523       /* Consume the `('.  */
7524       cp_lexer_consume_token (parser->lexer);
7525       /* Look for the matching `)'.  */
7526       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7527       return ansi_opname (CALL_EXPR);
7528
7529     case CPP_OPEN_SQUARE:
7530       /* Consume the `['.  */
7531       cp_lexer_consume_token (parser->lexer);
7532       /* Look for the matching `]'.  */
7533       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7534       return ansi_opname (ARRAY_REF);
7535
7536       /* Extensions.  */
7537     case CPP_MIN:
7538       id = ansi_opname (MIN_EXPR);
7539       break;
7540
7541     case CPP_MAX:
7542       id = ansi_opname (MAX_EXPR);
7543       break;
7544
7545     case CPP_MIN_EQ:
7546       id = ansi_assopname (MIN_EXPR);
7547       break;
7548
7549     case CPP_MAX_EQ:
7550       id = ansi_assopname (MAX_EXPR);
7551       break;
7552
7553     default:
7554       /* Anything else is an error.  */
7555       break;
7556     }
7557
7558   /* If we have selected an identifier, we need to consume the
7559      operator token.  */
7560   if (id)
7561     cp_lexer_consume_token (parser->lexer);
7562   /* Otherwise, no valid operator name was present.  */
7563   else
7564     {
7565       cp_parser_error (parser, "expected operator");
7566       id = error_mark_node;
7567     }
7568
7569   return id;
7570 }
7571
7572 /* Parse a template-declaration.
7573
7574    template-declaration:
7575      export [opt] template < template-parameter-list > declaration  
7576
7577    If MEMBER_P is TRUE, this template-declaration occurs within a
7578    class-specifier.  
7579
7580    The grammar rule given by the standard isn't correct.  What
7581    is really meant is:
7582
7583    template-declaration:
7584      export [opt] template-parameter-list-seq 
7585        decl-specifier-seq [opt] init-declarator [opt] ;
7586      export [opt] template-parameter-list-seq 
7587        function-definition
7588
7589    template-parameter-list-seq:
7590      template-parameter-list-seq [opt]
7591      template < template-parameter-list >  */
7592
7593 static void
7594 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7595 {
7596   /* Check for `export'.  */
7597   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7598     {
7599       /* Consume the `export' token.  */
7600       cp_lexer_consume_token (parser->lexer);
7601       /* Warn that we do not support `export'.  */
7602       warning ("keyword `export' not implemented, and will be ignored");
7603     }
7604
7605   cp_parser_template_declaration_after_export (parser, member_p);
7606 }
7607
7608 /* Parse a template-parameter-list.
7609
7610    template-parameter-list:
7611      template-parameter
7612      template-parameter-list , template-parameter
7613
7614    Returns a TREE_LIST.  Each node represents a template parameter.
7615    The nodes are connected via their TREE_CHAINs.  */
7616
7617 static tree
7618 cp_parser_template_parameter_list (cp_parser* parser)
7619 {
7620   tree parameter_list = NULL_TREE;
7621
7622   while (true)
7623     {
7624       tree parameter;
7625       cp_token *token;
7626
7627       /* Parse the template-parameter.  */
7628       parameter = cp_parser_template_parameter (parser);
7629       /* Add it to the list.  */
7630       parameter_list = process_template_parm (parameter_list,
7631                                               parameter);
7632
7633       /* Peek at the next token.  */
7634       token = cp_lexer_peek_token (parser->lexer);
7635       /* If it's not a `,', we're done.  */
7636       if (token->type != CPP_COMMA)
7637         break;
7638       /* Otherwise, consume the `,' token.  */
7639       cp_lexer_consume_token (parser->lexer);
7640     }
7641
7642   return parameter_list;
7643 }
7644
7645 /* Parse a template-parameter.
7646
7647    template-parameter:
7648      type-parameter
7649      parameter-declaration
7650
7651    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
7652    TREE_PURPOSE is the default value, if any.  */
7653
7654 static tree
7655 cp_parser_template_parameter (cp_parser* parser)
7656 {
7657   cp_token *token;
7658
7659   /* Peek at the next token.  */
7660   token = cp_lexer_peek_token (parser->lexer);
7661   /* If it is `class' or `template', we have a type-parameter.  */
7662   if (token->keyword == RID_TEMPLATE)
7663     return cp_parser_type_parameter (parser);
7664   /* If it is `class' or `typename' we do not know yet whether it is a
7665      type parameter or a non-type parameter.  Consider:
7666
7667        template <typename T, typename T::X X> ...
7668
7669      or:
7670      
7671        template <class C, class D*> ...
7672
7673      Here, the first parameter is a type parameter, and the second is
7674      a non-type parameter.  We can tell by looking at the token after
7675      the identifier -- if it is a `,', `=', or `>' then we have a type
7676      parameter.  */
7677   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7678     {
7679       /* Peek at the token after `class' or `typename'.  */
7680       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7681       /* If it's an identifier, skip it.  */
7682       if (token->type == CPP_NAME)
7683         token = cp_lexer_peek_nth_token (parser->lexer, 3);
7684       /* Now, see if the token looks like the end of a template
7685          parameter.  */
7686       if (token->type == CPP_COMMA 
7687           || token->type == CPP_EQ
7688           || token->type == CPP_GREATER)
7689         return cp_parser_type_parameter (parser);
7690     }
7691
7692   /* Otherwise, it is a non-type parameter.  
7693
7694      [temp.param]
7695
7696      When parsing a default template-argument for a non-type
7697      template-parameter, the first non-nested `>' is taken as the end
7698      of the template parameter-list rather than a greater-than
7699      operator.  */
7700   return 
7701     cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7702                                      /*parenthesized_p=*/NULL);
7703 }
7704
7705 /* Parse a type-parameter.
7706
7707    type-parameter:
7708      class identifier [opt]
7709      class identifier [opt] = type-id
7710      typename identifier [opt]
7711      typename identifier [opt] = type-id
7712      template < template-parameter-list > class identifier [opt]
7713      template < template-parameter-list > class identifier [opt] 
7714        = id-expression  
7715
7716    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
7717    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
7718    the declaration of the parameter.  */
7719
7720 static tree
7721 cp_parser_type_parameter (cp_parser* parser)
7722 {
7723   cp_token *token;
7724   tree parameter;
7725
7726   /* Look for a keyword to tell us what kind of parameter this is.  */
7727   token = cp_parser_require (parser, CPP_KEYWORD, 
7728                              "`class', `typename', or `template'");
7729   if (!token)
7730     return error_mark_node;
7731
7732   switch (token->keyword)
7733     {
7734     case RID_CLASS:
7735     case RID_TYPENAME:
7736       {
7737         tree identifier;
7738         tree default_argument;
7739
7740         /* If the next token is an identifier, then it names the
7741            parameter.  */
7742         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7743           identifier = cp_parser_identifier (parser);
7744         else
7745           identifier = NULL_TREE;
7746
7747         /* Create the parameter.  */
7748         parameter = finish_template_type_parm (class_type_node, identifier);
7749
7750         /* If the next token is an `=', we have a default argument.  */
7751         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7752           {
7753             /* Consume the `=' token.  */
7754             cp_lexer_consume_token (parser->lexer);
7755             /* Parse the default-argument.  */
7756             default_argument = cp_parser_type_id (parser);
7757           }
7758         else
7759           default_argument = NULL_TREE;
7760
7761         /* Create the combined representation of the parameter and the
7762            default argument.  */
7763         parameter = build_tree_list (default_argument, parameter);
7764       }
7765       break;
7766
7767     case RID_TEMPLATE:
7768       {
7769         tree parameter_list;
7770         tree identifier;
7771         tree default_argument;
7772
7773         /* Look for the `<'.  */
7774         cp_parser_require (parser, CPP_LESS, "`<'");
7775         /* Parse the template-parameter-list.  */
7776         begin_template_parm_list ();
7777         parameter_list 
7778           = cp_parser_template_parameter_list (parser);
7779         parameter_list = end_template_parm_list (parameter_list);
7780         /* Look for the `>'.  */
7781         cp_parser_require (parser, CPP_GREATER, "`>'");
7782         /* Look for the `class' keyword.  */
7783         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7784         /* If the next token is an `=', then there is a
7785            default-argument.  If the next token is a `>', we are at
7786            the end of the parameter-list.  If the next token is a `,',
7787            then we are at the end of this parameter.  */
7788         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7789             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7790             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7791           {
7792             identifier = cp_parser_identifier (parser);
7793             /* Treat invalid names as if the parameter were nameless. */
7794             if (identifier == error_mark_node)
7795               identifier = NULL_TREE;
7796           }
7797         else
7798           identifier = NULL_TREE;
7799
7800         /* Create the template parameter.  */
7801         parameter = finish_template_template_parm (class_type_node,
7802                                                    identifier);
7803                                                    
7804         /* If the next token is an `=', then there is a
7805            default-argument.  */
7806         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7807           {
7808             bool is_template;
7809
7810             /* Consume the `='.  */
7811             cp_lexer_consume_token (parser->lexer);
7812             /* Parse the id-expression.  */
7813             default_argument 
7814               = cp_parser_id_expression (parser,
7815                                          /*template_keyword_p=*/false,
7816                                          /*check_dependency_p=*/true,
7817                                          /*template_p=*/&is_template,
7818                                          /*declarator_p=*/false);
7819             if (TREE_CODE (default_argument) == TYPE_DECL)
7820               /* If the id-expression was a template-id that refers to
7821                  a template-class, we already have the declaration here,
7822                  so no further lookup is needed.  */
7823                  ;
7824             else
7825               /* Look up the name.  */
7826               default_argument 
7827                 = cp_parser_lookup_name (parser, default_argument,
7828                                         /*is_type=*/false,
7829                                         /*is_template=*/is_template,
7830                                         /*is_namespace=*/false,
7831                                         /*check_dependency=*/true);
7832             /* See if the default argument is valid.  */
7833             default_argument
7834               = check_template_template_default_arg (default_argument);
7835           }
7836         else
7837           default_argument = NULL_TREE;
7838
7839         /* Create the combined representation of the parameter and the
7840            default argument.  */
7841         parameter = build_tree_list (default_argument, parameter);
7842       }
7843       break;
7844
7845     default:
7846       abort ();
7847       break;
7848     }
7849   
7850   return parameter;
7851 }
7852
7853 /* Parse a template-id.
7854
7855    template-id:
7856      template-name < template-argument-list [opt] >
7857
7858    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7859    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
7860    returned.  Otherwise, if the template-name names a function, or set
7861    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
7862    names a class, returns a TYPE_DECL for the specialization.  
7863
7864    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7865    uninstantiated templates.  */
7866
7867 static tree
7868 cp_parser_template_id (cp_parser *parser, 
7869                        bool template_keyword_p, 
7870                        bool check_dependency_p,
7871                        bool is_declaration)
7872 {
7873   tree template;
7874   tree arguments;
7875   tree template_id;
7876   ptrdiff_t start_of_id;
7877   tree access_check = NULL_TREE;
7878   cp_token *next_token, *next_token_2;
7879   bool is_identifier;
7880
7881   /* If the next token corresponds to a template-id, there is no need
7882      to reparse it.  */
7883   next_token = cp_lexer_peek_token (parser->lexer);
7884   if (next_token->type == CPP_TEMPLATE_ID)
7885     {
7886       tree value;
7887       tree check;
7888
7889       /* Get the stored value.  */
7890       value = cp_lexer_consume_token (parser->lexer)->value;
7891       /* Perform any access checks that were deferred.  */
7892       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7893         perform_or_defer_access_check (TREE_PURPOSE (check),
7894                                        TREE_VALUE (check));
7895       /* Return the stored value.  */
7896       return TREE_VALUE (value);
7897     }
7898
7899   /* Avoid performing name lookup if there is no possibility of
7900      finding a template-id.  */
7901   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7902       || (next_token->type == CPP_NAME
7903           && !cp_parser_nth_token_starts_template_argument_list_p 
7904                (parser, 2)))
7905     {
7906       cp_parser_error (parser, "expected template-id");
7907       return error_mark_node;
7908     }
7909
7910   /* Remember where the template-id starts.  */
7911   if (cp_parser_parsing_tentatively (parser)
7912       && !cp_parser_committed_to_tentative_parse (parser))
7913     {
7914       next_token = cp_lexer_peek_token (parser->lexer);
7915       start_of_id = cp_lexer_token_difference (parser->lexer,
7916                                                parser->lexer->first_token,
7917                                                next_token);
7918     }
7919   else
7920     start_of_id = -1;
7921
7922   push_deferring_access_checks (dk_deferred);
7923
7924   /* Parse the template-name.  */
7925   is_identifier = false;
7926   template = cp_parser_template_name (parser, template_keyword_p,
7927                                       check_dependency_p,
7928                                       is_declaration,
7929                                       &is_identifier);
7930   if (template == error_mark_node || is_identifier)
7931     {
7932       pop_deferring_access_checks ();
7933       return template;
7934     }
7935
7936   /* If we find the sequence `[:' after a template-name, it's probably 
7937      a digraph-typo for `< ::'. Substitute the tokens and check if we can
7938      parse correctly the argument list.  */
7939   next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7940   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7941   if (next_token->type == CPP_OPEN_SQUARE 
7942       && next_token->flags & DIGRAPH
7943       && next_token_2->type == CPP_COLON 
7944       && !(next_token_2->flags & PREV_WHITE))
7945     {
7946       cp_parser_parse_tentatively (parser);
7947       /* Change `:' into `::'.  */
7948       next_token_2->type = CPP_SCOPE;
7949       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7950          CPP_LESS.  */
7951       cp_lexer_consume_token (parser->lexer);
7952       /* Parse the arguments.  */
7953       arguments = cp_parser_enclosed_template_argument_list (parser);
7954       if (!cp_parser_parse_definitely (parser))
7955         {
7956           /* If we couldn't parse an argument list, then we revert our changes
7957              and return simply an error. Maybe this is not a template-id
7958              after all.  */
7959           next_token_2->type = CPP_COLON;
7960           cp_parser_error (parser, "expected `<'");
7961           pop_deferring_access_checks ();
7962           return error_mark_node;
7963         }
7964       /* Otherwise, emit an error about the invalid digraph, but continue
7965          parsing because we got our argument list.  */
7966       pedwarn ("`<::' cannot begin a template-argument list");
7967       inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7968               "between `<' and `::'");
7969       if (!flag_permissive)
7970         {
7971           static bool hint;
7972           if (!hint)
7973             {
7974               inform ("(if you use `-fpermissive' G++ will accept your code)");
7975               hint = true;
7976             }
7977         }
7978     }
7979   else
7980     {
7981       /* Look for the `<' that starts the template-argument-list.  */
7982       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7983         {
7984           pop_deferring_access_checks ();
7985           return error_mark_node;
7986         }
7987       /* Parse the arguments.  */
7988       arguments = cp_parser_enclosed_template_argument_list (parser);
7989     }
7990
7991   /* Build a representation of the specialization.  */
7992   if (TREE_CODE (template) == IDENTIFIER_NODE)
7993     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7994   else if (DECL_CLASS_TEMPLATE_P (template)
7995            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7996     template_id 
7997       = finish_template_type (template, arguments, 
7998                               cp_lexer_next_token_is (parser->lexer, 
7999                                                       CPP_SCOPE));
8000   else
8001     {
8002       /* If it's not a class-template or a template-template, it should be
8003          a function-template.  */
8004       my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8005                            || TREE_CODE (template) == OVERLOAD
8006                            || BASELINK_P (template)),
8007                           20010716);
8008       
8009       template_id = lookup_template_function (template, arguments);
8010     }
8011   
8012   /* Retrieve any deferred checks.  Do not pop this access checks yet
8013      so the memory will not be reclaimed during token replacing below.  */
8014   access_check = get_deferred_access_checks ();
8015
8016   /* If parsing tentatively, replace the sequence of tokens that makes
8017      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8018      should we re-parse the token stream, we will not have to repeat
8019      the effort required to do the parse, nor will we issue duplicate
8020      error messages about problems during instantiation of the
8021      template.  */
8022   if (start_of_id >= 0)
8023     {
8024       cp_token *token;
8025
8026       /* Find the token that corresponds to the start of the
8027          template-id.  */
8028       token = cp_lexer_advance_token (parser->lexer, 
8029                                       parser->lexer->first_token,
8030                                       start_of_id);
8031
8032       /* Reset the contents of the START_OF_ID token.  */
8033       token->type = CPP_TEMPLATE_ID;
8034       token->value = build_tree_list (access_check, template_id);
8035       token->keyword = RID_MAX;
8036       /* Purge all subsequent tokens.  */
8037       cp_lexer_purge_tokens_after (parser->lexer, token);
8038
8039       /* ??? Can we actually assume that, if template_id ==
8040          error_mark_node, we will have issued a diagnostic to the
8041          user, as opposed to simply marking the tentative parse as
8042          failed?  */
8043       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8044         error ("parse error in template argument list");
8045     }
8046
8047   pop_deferring_access_checks ();
8048   return template_id;
8049 }
8050
8051 /* Parse a template-name.
8052
8053    template-name:
8054      identifier
8055  
8056    The standard should actually say:
8057
8058    template-name:
8059      identifier
8060      operator-function-id
8061
8062    A defect report has been filed about this issue.
8063
8064    A conversion-function-id cannot be a template name because they cannot
8065    be part of a template-id. In fact, looking at this code:
8066
8067    a.operator K<int>()
8068
8069    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8070    It is impossible to call a templated conversion-function-id with an 
8071    explicit argument list, since the only allowed template parameter is
8072    the type to which it is converting.
8073
8074    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8075    `template' keyword, in a construction like:
8076
8077      T::template f<3>()
8078
8079    In that case `f' is taken to be a template-name, even though there
8080    is no way of knowing for sure.
8081
8082    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8083    name refers to a set of overloaded functions, at least one of which
8084    is a template, or an IDENTIFIER_NODE with the name of the template,
8085    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8086    names are looked up inside uninstantiated templates.  */
8087
8088 static tree
8089 cp_parser_template_name (cp_parser* parser, 
8090                          bool template_keyword_p, 
8091                          bool check_dependency_p,
8092                          bool is_declaration,
8093                          bool *is_identifier)
8094 {
8095   tree identifier;
8096   tree decl;
8097   tree fns;
8098
8099   /* If the next token is `operator', then we have either an
8100      operator-function-id or a conversion-function-id.  */
8101   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8102     {
8103       /* We don't know whether we're looking at an
8104          operator-function-id or a conversion-function-id.  */
8105       cp_parser_parse_tentatively (parser);
8106       /* Try an operator-function-id.  */
8107       identifier = cp_parser_operator_function_id (parser);
8108       /* If that didn't work, try a conversion-function-id.  */
8109       if (!cp_parser_parse_definitely (parser))
8110         {
8111           cp_parser_error (parser, "expected template-name");
8112           return error_mark_node;
8113         }
8114     }
8115   /* Look for the identifier.  */
8116   else
8117     identifier = cp_parser_identifier (parser);
8118   
8119   /* If we didn't find an identifier, we don't have a template-id.  */
8120   if (identifier == error_mark_node)
8121     return error_mark_node;
8122
8123   /* If the name immediately followed the `template' keyword, then it
8124      is a template-name.  However, if the next token is not `<', then
8125      we do not treat it as a template-name, since it is not being used
8126      as part of a template-id.  This enables us to handle constructs
8127      like:
8128
8129        template <typename T> struct S { S(); };
8130        template <typename T> S<T>::S();
8131
8132      correctly.  We would treat `S' as a template -- if it were `S<T>'
8133      -- but we do not if there is no `<'.  */
8134
8135   if (processing_template_decl
8136       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8137     {
8138       /* In a declaration, in a dependent context, we pretend that the
8139          "template" keyword was present in order to improve error
8140          recovery.  For example, given:
8141          
8142            template <typename T> void f(T::X<int>);
8143          
8144          we want to treat "X<int>" as a template-id.  */
8145       if (is_declaration 
8146           && !template_keyword_p 
8147           && parser->scope && TYPE_P (parser->scope)
8148           && check_dependency_p
8149           && dependent_type_p (parser->scope)
8150           /* Do not do this for dtors (or ctors), since they never
8151              need the template keyword before their name.  */
8152           && !constructor_name_p (identifier, parser->scope))
8153         {
8154           ptrdiff_t start;
8155           cp_token* token;
8156           /* Explain what went wrong.  */
8157           error ("non-template `%D' used as template", identifier);
8158           inform ("use `%T::template %D' to indicate that it is a template",
8159                   parser->scope, identifier);
8160           /* If parsing tentatively, find the location of the "<"
8161              token.  */
8162           if (cp_parser_parsing_tentatively (parser)
8163               && !cp_parser_committed_to_tentative_parse (parser))
8164             {
8165               cp_parser_simulate_error (parser);
8166               token = cp_lexer_peek_token (parser->lexer);
8167               token = cp_lexer_prev_token (parser->lexer, token);
8168               start = cp_lexer_token_difference (parser->lexer,
8169                                                  parser->lexer->first_token,
8170                                                  token);
8171             }
8172           else
8173             start = -1;
8174           /* Parse the template arguments so that we can issue error
8175              messages about them.  */
8176           cp_lexer_consume_token (parser->lexer);
8177           cp_parser_enclosed_template_argument_list (parser);
8178           /* Skip tokens until we find a good place from which to
8179              continue parsing.  */
8180           cp_parser_skip_to_closing_parenthesis (parser,
8181                                                  /*recovering=*/true,
8182                                                  /*or_comma=*/true,
8183                                                  /*consume_paren=*/false);
8184           /* If parsing tentatively, permanently remove the
8185              template argument list.  That will prevent duplicate
8186              error messages from being issued about the missing
8187              "template" keyword.  */
8188           if (start >= 0)
8189             {
8190               token = cp_lexer_advance_token (parser->lexer,
8191                                               parser->lexer->first_token,
8192                                               start);
8193               cp_lexer_purge_tokens_after (parser->lexer, token);
8194             }
8195           if (is_identifier)
8196             *is_identifier = true;
8197           return identifier;
8198         }
8199
8200       /* If the "template" keyword is present, then there is generally
8201          no point in doing name-lookup, so we just return IDENTIFIER.
8202          But, if the qualifying scope is non-dependent then we can
8203          (and must) do name-lookup normally.  */
8204       if (template_keyword_p
8205           && (!parser->scope
8206               || (TYPE_P (parser->scope) 
8207                   && dependent_type_p (parser->scope))))
8208         return identifier;
8209     }
8210
8211   /* Look up the name.  */
8212   decl = cp_parser_lookup_name (parser, identifier,
8213                                 /*is_type=*/false,
8214                                 /*is_template=*/false,
8215                                 /*is_namespace=*/false,
8216                                 check_dependency_p);
8217   decl = maybe_get_template_decl_from_type_decl (decl);
8218
8219   /* If DECL is a template, then the name was a template-name.  */
8220   if (TREE_CODE (decl) == TEMPLATE_DECL)
8221     ;
8222   else 
8223     {
8224       tree fn = NULL_TREE;
8225
8226       /* The standard does not explicitly indicate whether a name that
8227          names a set of overloaded declarations, some of which are
8228          templates, is a template-name.  However, such a name should
8229          be a template-name; otherwise, there is no way to form a
8230          template-id for the overloaded templates.  */
8231       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8232       if (TREE_CODE (fns) == OVERLOAD)
8233         for (fn = fns; fn; fn = OVL_NEXT (fn))
8234           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8235             break;
8236
8237       if (!fn)
8238         {
8239           /* Otherwise, the name does not name a template.  */
8240           cp_parser_error (parser, "expected template-name");
8241           return error_mark_node;
8242         }
8243     }
8244
8245   /* If DECL is dependent, and refers to a function, then just return
8246      its name; we will look it up again during template instantiation.  */
8247   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8248     {
8249       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8250       if (TYPE_P (scope) && dependent_type_p (scope))
8251         return identifier;
8252     }
8253
8254   return decl;
8255 }
8256
8257 /* Parse a template-argument-list.
8258
8259    template-argument-list:
8260      template-argument
8261      template-argument-list , template-argument
8262
8263    Returns a TREE_VEC containing the arguments.  */
8264
8265 static tree
8266 cp_parser_template_argument_list (cp_parser* parser)
8267 {
8268   tree fixed_args[10];
8269   unsigned n_args = 0;
8270   unsigned alloced = 10;
8271   tree *arg_ary = fixed_args;
8272   tree vec;
8273   bool saved_in_template_argument_list_p;
8274
8275   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8276   parser->in_template_argument_list_p = true;
8277   do
8278     {
8279       tree argument;
8280
8281       if (n_args)
8282         /* Consume the comma.  */
8283         cp_lexer_consume_token (parser->lexer);
8284       
8285       /* Parse the template-argument.  */
8286       argument = cp_parser_template_argument (parser);
8287       if (n_args == alloced)
8288         {
8289           alloced *= 2;
8290           
8291           if (arg_ary == fixed_args)
8292             {
8293               arg_ary = xmalloc (sizeof (tree) * alloced);
8294               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8295             }
8296           else
8297             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8298         }
8299       arg_ary[n_args++] = argument;
8300     }
8301   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8302
8303   vec = make_tree_vec (n_args);
8304
8305   while (n_args--)
8306     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8307   
8308   if (arg_ary != fixed_args)
8309     free (arg_ary);
8310   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8311   return vec;
8312 }
8313
8314 /* Parse a template-argument.
8315
8316    template-argument:
8317      assignment-expression
8318      type-id
8319      id-expression
8320
8321    The representation is that of an assignment-expression, type-id, or
8322    id-expression -- except that the qualified id-expression is
8323    evaluated, so that the value returned is either a DECL or an
8324    OVERLOAD.  
8325
8326    Although the standard says "assignment-expression", it forbids
8327    throw-expressions or assignments in the template argument.
8328    Therefore, we use "conditional-expression" instead.  */
8329
8330 static tree
8331 cp_parser_template_argument (cp_parser* parser)
8332 {
8333   tree argument;
8334   bool template_p;
8335   bool address_p;
8336   bool maybe_type_id = false;
8337   cp_token *token;
8338   cp_id_kind idk;
8339   tree qualifying_class;
8340
8341   /* There's really no way to know what we're looking at, so we just
8342      try each alternative in order.  
8343
8344        [temp.arg]
8345
8346        In a template-argument, an ambiguity between a type-id and an
8347        expression is resolved to a type-id, regardless of the form of
8348        the corresponding template-parameter.  
8349
8350      Therefore, we try a type-id first.  */
8351   cp_parser_parse_tentatively (parser);
8352   argument = cp_parser_type_id (parser);
8353   /* If there was no error parsing the type-id but the next token is a '>>',
8354      we probably found a typo for '> >'. But there are type-id which are 
8355      also valid expressions. For instance:
8356
8357      struct X { int operator >> (int); };
8358      template <int V> struct Foo {};
8359      Foo<X () >> 5> r;
8360
8361      Here 'X()' is a valid type-id of a function type, but the user just
8362      wanted to write the expression "X() >> 5". Thus, we remember that we
8363      found a valid type-id, but we still try to parse the argument as an
8364      expression to see what happens.  */
8365   if (!cp_parser_error_occurred (parser)
8366       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8367     {
8368       maybe_type_id = true;
8369       cp_parser_abort_tentative_parse (parser);
8370     }
8371   else
8372     {
8373       /* If the next token isn't a `,' or a `>', then this argument wasn't
8374       really finished. This means that the argument is not a valid
8375       type-id.  */
8376       if (!cp_parser_next_token_ends_template_argument_p (parser))
8377         cp_parser_error (parser, "expected template-argument");
8378       /* If that worked, we're done.  */
8379       if (cp_parser_parse_definitely (parser))
8380         return argument;
8381     }
8382   /* We're still not sure what the argument will be.  */
8383   cp_parser_parse_tentatively (parser);
8384   /* Try a template.  */
8385   argument = cp_parser_id_expression (parser, 
8386                                       /*template_keyword_p=*/false,
8387                                       /*check_dependency_p=*/true,
8388                                       &template_p,
8389                                       /*declarator_p=*/false);
8390   /* If the next token isn't a `,' or a `>', then this argument wasn't
8391      really finished.  */
8392   if (!cp_parser_next_token_ends_template_argument_p (parser))
8393     cp_parser_error (parser, "expected template-argument");
8394   if (!cp_parser_error_occurred (parser))
8395     {
8396       /* Figure out what is being referred to.  If the id-expression
8397          was for a class template specialization, then we will have a
8398          TYPE_DECL at this point.  There is no need to do name lookup
8399          at this point in that case.  */
8400       if (TREE_CODE (argument) != TYPE_DECL)
8401         argument = cp_parser_lookup_name (parser, argument,
8402                                           /*is_type=*/false,
8403                                           /*is_template=*/template_p,
8404                                           /*is_namespace=*/false,
8405                                           /*check_dependency=*/true);
8406       if (TREE_CODE (argument) != TEMPLATE_DECL
8407           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8408         cp_parser_error (parser, "expected template-name");
8409     }
8410   if (cp_parser_parse_definitely (parser))
8411     return argument;
8412   /* It must be a non-type argument.  There permitted cases are given
8413      in [temp.arg.nontype]:
8414
8415      -- an integral constant-expression of integral or enumeration
8416         type; or
8417
8418      -- the name of a non-type template-parameter; or
8419
8420      -- the name of an object or function with external linkage...
8421
8422      -- the address of an object or function with external linkage...
8423
8424      -- a pointer to member...  */
8425   /* Look for a non-type template parameter.  */
8426   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8427     {
8428       cp_parser_parse_tentatively (parser);
8429       argument = cp_parser_primary_expression (parser,
8430                                                &idk,
8431                                                &qualifying_class);
8432       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8433           || !cp_parser_next_token_ends_template_argument_p (parser))
8434         cp_parser_simulate_error (parser);
8435       if (cp_parser_parse_definitely (parser))
8436         return argument;
8437     }
8438   /* If the next token is "&", the argument must be the address of an
8439      object or function with external linkage.  */
8440   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8441   if (address_p)
8442     cp_lexer_consume_token (parser->lexer);
8443   /* See if we might have an id-expression.  */
8444   token = cp_lexer_peek_token (parser->lexer);
8445   if (token->type == CPP_NAME
8446       || token->keyword == RID_OPERATOR
8447       || token->type == CPP_SCOPE
8448       || token->type == CPP_TEMPLATE_ID
8449       || token->type == CPP_NESTED_NAME_SPECIFIER)
8450     {
8451       cp_parser_parse_tentatively (parser);
8452       argument = cp_parser_primary_expression (parser,
8453                                                &idk,
8454                                                &qualifying_class);
8455       if (cp_parser_error_occurred (parser)
8456           || !cp_parser_next_token_ends_template_argument_p (parser))
8457         cp_parser_abort_tentative_parse (parser);
8458       else
8459         {
8460           if (qualifying_class)
8461             argument = finish_qualified_id_expr (qualifying_class,
8462                                                  argument,
8463                                                  /*done=*/true,
8464                                                  address_p);
8465           if (TREE_CODE (argument) == VAR_DECL)
8466             {
8467               /* A variable without external linkage might still be a
8468                  valid constant-expression, so no error is issued here
8469                  if the external-linkage check fails.  */
8470               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8471                 cp_parser_simulate_error (parser);
8472             }
8473           else if (is_overloaded_fn (argument))
8474             /* All overloaded functions are allowed; if the external
8475                linkage test does not pass, an error will be issued
8476                later.  */
8477             ;
8478           else if (address_p
8479                    && (TREE_CODE (argument) == OFFSET_REF 
8480                        || TREE_CODE (argument) == SCOPE_REF))
8481             /* A pointer-to-member.  */
8482             ;
8483           else
8484             cp_parser_simulate_error (parser);
8485
8486           if (cp_parser_parse_definitely (parser))
8487             {
8488               if (address_p)
8489                 argument = build_x_unary_op (ADDR_EXPR, argument);
8490               return argument;
8491             }
8492         }
8493     }
8494   /* If the argument started with "&", there are no other valid
8495      alternatives at this point.  */
8496   if (address_p)
8497     {
8498       cp_parser_error (parser, "invalid non-type template argument");
8499       return error_mark_node;
8500     }
8501   /* If the argument wasn't successfully parsed as a type-id followed
8502      by '>>', the argument can only be a constant expression now.  
8503      Otherwise, we try parsing the constant-expression tentatively,
8504      because the argument could really be a type-id.  */
8505   if (maybe_type_id)
8506     cp_parser_parse_tentatively (parser);
8507   argument = cp_parser_constant_expression (parser, 
8508                                             /*allow_non_constant_p=*/false,
8509                                             /*non_constant_p=*/NULL);
8510   argument = fold_non_dependent_expr (argument);
8511   if (!maybe_type_id)
8512     return argument;
8513   if (!cp_parser_next_token_ends_template_argument_p (parser))
8514     cp_parser_error (parser, "expected template-argument");
8515   if (cp_parser_parse_definitely (parser))
8516     return argument;
8517   /* We did our best to parse the argument as a non type-id, but that
8518      was the only alternative that matched (albeit with a '>' after
8519      it). We can assume it's just a typo from the user, and a 
8520      diagnostic will then be issued.  */
8521   return cp_parser_type_id (parser);
8522 }
8523
8524 /* Parse an explicit-instantiation.
8525
8526    explicit-instantiation:
8527      template declaration  
8528
8529    Although the standard says `declaration', what it really means is:
8530
8531    explicit-instantiation:
8532      template decl-specifier-seq [opt] declarator [opt] ; 
8533
8534    Things like `template int S<int>::i = 5, int S<double>::j;' are not
8535    supposed to be allowed.  A defect report has been filed about this
8536    issue.  
8537
8538    GNU Extension:
8539   
8540    explicit-instantiation:
8541      storage-class-specifier template 
8542        decl-specifier-seq [opt] declarator [opt] ;
8543      function-specifier template 
8544        decl-specifier-seq [opt] declarator [opt] ;  */
8545
8546 static void
8547 cp_parser_explicit_instantiation (cp_parser* parser)
8548 {
8549   int declares_class_or_enum;
8550   tree decl_specifiers;
8551   tree attributes;
8552   tree extension_specifier = NULL_TREE;
8553
8554   /* Look for an (optional) storage-class-specifier or
8555      function-specifier.  */
8556   if (cp_parser_allow_gnu_extensions_p (parser))
8557     {
8558       extension_specifier 
8559         = cp_parser_storage_class_specifier_opt (parser);
8560       if (!extension_specifier)
8561         extension_specifier = cp_parser_function_specifier_opt (parser);
8562     }
8563
8564   /* Look for the `template' keyword.  */
8565   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8566   /* Let the front end know that we are processing an explicit
8567      instantiation.  */
8568   begin_explicit_instantiation ();
8569   /* [temp.explicit] says that we are supposed to ignore access
8570      control while processing explicit instantiation directives.  */
8571   push_deferring_access_checks (dk_no_check);
8572   /* Parse a decl-specifier-seq.  */
8573   decl_specifiers 
8574     = cp_parser_decl_specifier_seq (parser,
8575                                     CP_PARSER_FLAGS_OPTIONAL,
8576                                     &attributes,
8577                                     &declares_class_or_enum);
8578   /* If there was exactly one decl-specifier, and it declared a class,
8579      and there's no declarator, then we have an explicit type
8580      instantiation.  */
8581   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8582     {
8583       tree type;
8584
8585       type = check_tag_decl (decl_specifiers);
8586       /* Turn access control back on for names used during
8587          template instantiation.  */
8588       pop_deferring_access_checks ();
8589       if (type)
8590         do_type_instantiation (type, extension_specifier, /*complain=*/1);
8591     }
8592   else
8593     {
8594       tree declarator;
8595       tree decl;
8596
8597       /* Parse the declarator.  */
8598       declarator 
8599         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8600                                 /*ctor_dtor_or_conv_p=*/NULL,
8601                                 /*parenthesized_p=*/NULL,
8602                                 /*member_p=*/false);
8603       if (declares_class_or_enum & 2)
8604         cp_parser_check_for_definition_in_return_type
8605           (declarator, TREE_VALUE (decl_specifiers));
8606       if (declarator != error_mark_node)
8607         {
8608           decl = grokdeclarator (declarator, decl_specifiers, 
8609                                  NORMAL, 0, NULL);
8610           /* Turn access control back on for names used during
8611              template instantiation.  */
8612           pop_deferring_access_checks ();
8613           /* Do the explicit instantiation.  */
8614           do_decl_instantiation (decl, extension_specifier);
8615         }
8616       else
8617         {
8618           pop_deferring_access_checks ();
8619           /* Skip the body of the explicit instantiation.  */
8620           cp_parser_skip_to_end_of_statement (parser);
8621         }
8622     }
8623   /* We're done with the instantiation.  */
8624   end_explicit_instantiation ();
8625
8626   cp_parser_consume_semicolon_at_end_of_statement (parser);
8627 }
8628
8629 /* Parse an explicit-specialization.
8630
8631    explicit-specialization:
8632      template < > declaration  
8633
8634    Although the standard says `declaration', what it really means is:
8635
8636    explicit-specialization:
8637      template <> decl-specifier [opt] init-declarator [opt] ;
8638      template <> function-definition 
8639      template <> explicit-specialization
8640      template <> template-declaration  */
8641
8642 static void
8643 cp_parser_explicit_specialization (cp_parser* parser)
8644 {
8645   /* Look for the `template' keyword.  */
8646   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8647   /* Look for the `<'.  */
8648   cp_parser_require (parser, CPP_LESS, "`<'");
8649   /* Look for the `>'.  */
8650   cp_parser_require (parser, CPP_GREATER, "`>'");
8651   /* We have processed another parameter list.  */
8652   ++parser->num_template_parameter_lists;
8653   /* Let the front end know that we are beginning a specialization.  */
8654   begin_specialization ();
8655
8656   /* If the next keyword is `template', we need to figure out whether
8657      or not we're looking a template-declaration.  */
8658   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8659     {
8660       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8661           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8662         cp_parser_template_declaration_after_export (parser,
8663                                                      /*member_p=*/false);
8664       else
8665         cp_parser_explicit_specialization (parser);
8666     }
8667   else
8668     /* Parse the dependent declaration.  */
8669     cp_parser_single_declaration (parser, 
8670                                   /*member_p=*/false,
8671                                   /*friend_p=*/NULL);
8672
8673   /* We're done with the specialization.  */
8674   end_specialization ();
8675   /* We're done with this parameter list.  */
8676   --parser->num_template_parameter_lists;
8677 }
8678
8679 /* Parse a type-specifier.
8680
8681    type-specifier:
8682      simple-type-specifier
8683      class-specifier
8684      enum-specifier
8685      elaborated-type-specifier
8686      cv-qualifier
8687
8688    GNU Extension:
8689
8690    type-specifier:
8691      __complex__
8692
8693    Returns a representation of the type-specifier.  If the
8694    type-specifier is a keyword (like `int' or `const', or
8695    `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8696    For a class-specifier, enum-specifier, or elaborated-type-specifier
8697    a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8698
8699    If IS_FRIEND is TRUE then this type-specifier is being declared a
8700    `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
8701    appearing in a decl-specifier-seq.
8702
8703    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8704    class-specifier, enum-specifier, or elaborated-type-specifier, then
8705    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
8706    if a type is declared; 2 if it is defined.  Otherwise, it is set to
8707    zero.
8708
8709    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8710    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
8711    is set to FALSE.  */
8712
8713 static tree
8714 cp_parser_type_specifier (cp_parser* parser, 
8715                           cp_parser_flags flags, 
8716                           bool is_friend,
8717                           bool is_declaration,
8718                           int* declares_class_or_enum,
8719                           bool* is_cv_qualifier)
8720 {
8721   tree type_spec = NULL_TREE;
8722   cp_token *token;
8723   enum rid keyword;
8724
8725   /* Assume this type-specifier does not declare a new type.  */
8726   if (declares_class_or_enum)
8727     *declares_class_or_enum = 0;
8728   /* And that it does not specify a cv-qualifier.  */
8729   if (is_cv_qualifier)
8730     *is_cv_qualifier = false;
8731   /* Peek at the next token.  */
8732   token = cp_lexer_peek_token (parser->lexer);
8733
8734   /* If we're looking at a keyword, we can use that to guide the
8735      production we choose.  */
8736   keyword = token->keyword;
8737   switch (keyword)
8738     {
8739     case RID_ENUM:
8740       /* 'enum' [identifier] '{' introduces an enum-specifier;
8741          'enum' <anything else> introduces an elaborated-type-specifier.  */
8742       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
8743           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
8744               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
8745                  == CPP_OPEN_BRACE))
8746         {
8747           if (parser->num_template_parameter_lists)
8748             {
8749               error ("template declaration of `enum'");
8750               cp_parser_skip_to_end_of_block_or_statement (parser);
8751               type_spec = error_mark_node;
8752             }
8753           else
8754             type_spec = cp_parser_enum_specifier (parser);
8755
8756           if (declares_class_or_enum)
8757             *declares_class_or_enum = 2;
8758           return type_spec;
8759         }
8760       else
8761         goto elaborated_type_specifier;
8762
8763       /* Any of these indicate either a class-specifier, or an
8764          elaborated-type-specifier.  */
8765     case RID_CLASS:
8766     case RID_STRUCT:
8767     case RID_UNION:
8768       /* Parse tentatively so that we can back up if we don't find a
8769          class-specifier or enum-specifier.  */
8770       cp_parser_parse_tentatively (parser);
8771       /* Look for the class-specifier.  */
8772       type_spec = cp_parser_class_specifier (parser);
8773       /* If that worked, we're done.  */
8774       if (cp_parser_parse_definitely (parser))
8775         {
8776           if (declares_class_or_enum)
8777             *declares_class_or_enum = 2;
8778           return type_spec;
8779         }
8780
8781       /* Fall through.  */
8782
8783     case RID_TYPENAME:
8784     elaborated_type_specifier:
8785       /* Look for an elaborated-type-specifier.  */
8786       type_spec = cp_parser_elaborated_type_specifier (parser,
8787                                                        is_friend,
8788                                                        is_declaration);
8789       /* We're declaring a class or enum -- unless we're using
8790          `typename'.  */
8791       if (declares_class_or_enum && keyword != RID_TYPENAME)
8792         *declares_class_or_enum = 1;
8793       return type_spec;
8794
8795     case RID_CONST:
8796     case RID_VOLATILE:
8797     case RID_RESTRICT:
8798       type_spec = cp_parser_cv_qualifier_opt (parser);
8799       /* Even though we call a routine that looks for an optional
8800          qualifier, we know that there should be one.  */
8801       my_friendly_assert (type_spec != NULL, 20000328);
8802       /* This type-specifier was a cv-qualified.  */
8803       if (is_cv_qualifier)
8804         *is_cv_qualifier = true;
8805
8806       return type_spec;
8807
8808     case RID_COMPLEX:
8809       /* The `__complex__' keyword is a GNU extension.  */
8810       return cp_lexer_consume_token (parser->lexer)->value;
8811
8812     default:
8813       break;
8814     }
8815
8816   /* If we do not already have a type-specifier, assume we are looking
8817      at a simple-type-specifier.  */
8818   type_spec = cp_parser_simple_type_specifier (parser, flags, 
8819                                                /*identifier_p=*/true);
8820
8821   /* If we didn't find a type-specifier, and a type-specifier was not
8822      optional in this context, issue an error message.  */
8823   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8824     {
8825       cp_parser_error (parser, "expected type specifier");
8826       return error_mark_node;
8827     }
8828
8829   return type_spec;
8830 }
8831
8832 /* Parse a simple-type-specifier.
8833
8834    simple-type-specifier:
8835      :: [opt] nested-name-specifier [opt] type-name
8836      :: [opt] nested-name-specifier template template-id
8837      char
8838      wchar_t
8839      bool
8840      short
8841      int
8842      long
8843      signed
8844      unsigned
8845      float
8846      double
8847      void  
8848
8849    GNU Extension:
8850
8851    simple-type-specifier:
8852      __typeof__ unary-expression
8853      __typeof__ ( type-id )
8854
8855    For the various keywords, the value returned is simply the
8856    TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8857    For the first two productions, and if IDENTIFIER_P is false, the
8858    value returned is the indicated TYPE_DECL.  */
8859
8860 static tree
8861 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8862                                  bool identifier_p)
8863 {
8864   tree type = NULL_TREE;
8865   cp_token *token;
8866
8867   /* Peek at the next token.  */
8868   token = cp_lexer_peek_token (parser->lexer);
8869
8870   /* If we're looking at a keyword, things are easy.  */
8871   switch (token->keyword)
8872     {
8873     case RID_CHAR:
8874       type = char_type_node;
8875       break;
8876     case RID_WCHAR:
8877       type = wchar_type_node;
8878       break;
8879     case RID_BOOL:
8880       type = boolean_type_node;
8881       break;
8882     case RID_SHORT:
8883       type = short_integer_type_node;
8884       break;
8885     case RID_INT:
8886       type = integer_type_node;
8887       break;
8888     case RID_LONG:
8889       type = long_integer_type_node;
8890       break;
8891     case RID_SIGNED:
8892       type = integer_type_node;
8893       break;
8894     case RID_UNSIGNED:
8895       type = unsigned_type_node;
8896       break;
8897     case RID_FLOAT:
8898       type = float_type_node;
8899       break;
8900     case RID_DOUBLE:
8901       type = double_type_node;
8902       break;
8903     case RID_VOID:
8904       type = void_type_node;
8905       break;
8906
8907     case RID_TYPEOF:
8908       {
8909         tree operand;
8910
8911         /* Consume the `typeof' token.  */
8912         cp_lexer_consume_token (parser->lexer);
8913         /* Parse the operand to `typeof'.  */
8914         operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8915         /* If it is not already a TYPE, take its type.  */
8916         if (!TYPE_P (operand))
8917           operand = finish_typeof (operand);
8918
8919         return operand;
8920       }
8921
8922     default:
8923       break;
8924     }
8925
8926   /* If the type-specifier was for a built-in type, we're done.  */
8927   if (type)
8928     {
8929       tree id;
8930
8931       /* Consume the token.  */
8932       id = cp_lexer_consume_token (parser->lexer)->value;
8933
8934       /* There is no valid C++ program where a non-template type is
8935          followed by a "<".  That usually indicates that the user thought
8936          that the type was a template.  */
8937       cp_parser_check_for_invalid_template_id (parser, type);
8938
8939       return identifier_p ? id : TYPE_NAME (type);
8940     }
8941
8942   /* The type-specifier must be a user-defined type.  */
8943   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) 
8944     {
8945       bool qualified_p;
8946       bool global_p;
8947
8948       /* Don't gobble tokens or issue error messages if this is an
8949          optional type-specifier.  */
8950       if (flags & CP_PARSER_FLAGS_OPTIONAL)
8951         cp_parser_parse_tentatively (parser);
8952
8953       /* Look for the optional `::' operator.  */
8954       global_p
8955         = (cp_parser_global_scope_opt (parser,
8956                                        /*current_scope_valid_p=*/false)
8957            != NULL_TREE);
8958       /* Look for the nested-name specifier.  */
8959       qualified_p
8960         = (cp_parser_nested_name_specifier_opt (parser,
8961                                                 /*typename_keyword_p=*/false,
8962                                                 /*check_dependency_p=*/true,
8963                                                 /*type_p=*/false,
8964                                                 /*is_declaration=*/false)
8965            != NULL_TREE);
8966       /* If we have seen a nested-name-specifier, and the next token
8967          is `template', then we are using the template-id production.  */
8968       if (parser->scope 
8969           && cp_parser_optional_template_keyword (parser))
8970         {
8971           /* Look for the template-id.  */
8972           type = cp_parser_template_id (parser, 
8973                                         /*template_keyword_p=*/true,
8974                                         /*check_dependency_p=*/true,
8975                                         /*is_declaration=*/false);
8976           /* If the template-id did not name a type, we are out of
8977              luck.  */
8978           if (TREE_CODE (type) != TYPE_DECL)
8979             {
8980               cp_parser_error (parser, "expected template-id for type");
8981               type = NULL_TREE;
8982             }
8983         }
8984       /* Otherwise, look for a type-name.  */
8985       else
8986         type = cp_parser_type_name (parser);
8987       /* Keep track of all name-lookups performed in class scopes.  */
8988       if (type  
8989           && !global_p
8990           && !qualified_p
8991           && TREE_CODE (type) == TYPE_DECL 
8992           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
8993         maybe_note_name_used_in_class (DECL_NAME (type), type);
8994       /* If it didn't work out, we don't have a TYPE.  */
8995       if ((flags & CP_PARSER_FLAGS_OPTIONAL) 
8996           && !cp_parser_parse_definitely (parser))
8997         type = NULL_TREE;
8998     }
8999
9000   /* If we didn't get a type-name, issue an error message.  */
9001   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9002     {
9003       cp_parser_error (parser, "expected type-name");
9004       return error_mark_node;
9005     }
9006
9007   /* There is no valid C++ program where a non-template type is
9008      followed by a "<".  That usually indicates that the user thought
9009      that the type was a template.  */
9010   if (type && type != error_mark_node)
9011     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9012
9013   return type;
9014 }
9015
9016 /* Parse a type-name.
9017
9018    type-name:
9019      class-name
9020      enum-name
9021      typedef-name  
9022
9023    enum-name:
9024      identifier
9025
9026    typedef-name:
9027      identifier 
9028
9029    Returns a TYPE_DECL for the the type.  */
9030
9031 static tree
9032 cp_parser_type_name (cp_parser* parser)
9033 {
9034   tree type_decl;
9035   tree identifier;
9036
9037   /* We can't know yet whether it is a class-name or not.  */
9038   cp_parser_parse_tentatively (parser);
9039   /* Try a class-name.  */
9040   type_decl = cp_parser_class_name (parser, 
9041                                     /*typename_keyword_p=*/false,
9042                                     /*template_keyword_p=*/false,
9043                                     /*type_p=*/false,
9044                                     /*check_dependency_p=*/true,
9045                                     /*class_head_p=*/false,
9046                                     /*is_declaration=*/false);
9047   /* If it's not a class-name, keep looking.  */
9048   if (!cp_parser_parse_definitely (parser))
9049     {
9050       /* It must be a typedef-name or an enum-name.  */
9051       identifier = cp_parser_identifier (parser);
9052       if (identifier == error_mark_node)
9053         return error_mark_node;
9054       
9055       /* Look up the type-name.  */
9056       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9057       /* Issue an error if we did not find a type-name.  */
9058       if (TREE_CODE (type_decl) != TYPE_DECL)
9059         {
9060           if (!cp_parser_simulate_error (parser))
9061             cp_parser_name_lookup_error (parser, identifier, type_decl, 
9062                                          "is not a type");
9063           type_decl = error_mark_node;
9064         }
9065       /* Remember that the name was used in the definition of the
9066          current class so that we can check later to see if the
9067          meaning would have been different after the class was
9068          entirely defined.  */
9069       else if (type_decl != error_mark_node
9070                && !parser->scope)
9071         maybe_note_name_used_in_class (identifier, type_decl);
9072     }
9073   
9074   return type_decl;
9075 }
9076
9077
9078 /* Parse an elaborated-type-specifier.  Note that the grammar given
9079    here incorporates the resolution to DR68.
9080
9081    elaborated-type-specifier:
9082      class-key :: [opt] nested-name-specifier [opt] identifier
9083      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9084      enum :: [opt] nested-name-specifier [opt] identifier
9085      typename :: [opt] nested-name-specifier identifier
9086      typename :: [opt] nested-name-specifier template [opt] 
9087        template-id 
9088
9089    GNU extension:
9090
9091    elaborated-type-specifier:
9092      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9093      class-key attributes :: [opt] nested-name-specifier [opt] 
9094                template [opt] template-id
9095      enum attributes :: [opt] nested-name-specifier [opt] identifier
9096
9097    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9098    declared `friend'.  If IS_DECLARATION is TRUE, then this
9099    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9100    something is being declared.
9101
9102    Returns the TYPE specified.  */
9103
9104 static tree
9105 cp_parser_elaborated_type_specifier (cp_parser* parser, 
9106                                      bool is_friend, 
9107                                      bool is_declaration)
9108 {
9109   enum tag_types tag_type;
9110   tree identifier;
9111   tree type = NULL_TREE;
9112   tree attributes = NULL_TREE;
9113
9114   /* See if we're looking at the `enum' keyword.  */
9115   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9116     {
9117       /* Consume the `enum' token.  */
9118       cp_lexer_consume_token (parser->lexer);
9119       /* Remember that it's an enumeration type.  */
9120       tag_type = enum_type;
9121       /* Parse the attributes.  */
9122       attributes = cp_parser_attributes_opt (parser);
9123     }
9124   /* Or, it might be `typename'.  */
9125   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9126                                            RID_TYPENAME))
9127     {
9128       /* Consume the `typename' token.  */
9129       cp_lexer_consume_token (parser->lexer);
9130       /* Remember that it's a `typename' type.  */
9131       tag_type = typename_type;
9132       /* The `typename' keyword is only allowed in templates.  */
9133       if (!processing_template_decl)
9134         pedwarn ("using `typename' outside of template");
9135     }
9136   /* Otherwise it must be a class-key.  */
9137   else
9138     {
9139       tag_type = cp_parser_class_key (parser);
9140       if (tag_type == none_type)
9141         return error_mark_node;
9142       /* Parse the attributes.  */
9143       attributes = cp_parser_attributes_opt (parser);
9144     }
9145
9146   /* Look for the `::' operator.  */
9147   cp_parser_global_scope_opt (parser, 
9148                               /*current_scope_valid_p=*/false);
9149   /* Look for the nested-name-specifier.  */
9150   if (tag_type == typename_type)
9151     {
9152       if (cp_parser_nested_name_specifier (parser,
9153                                            /*typename_keyword_p=*/true,
9154                                            /*check_dependency_p=*/true,
9155                                            /*type_p=*/true,
9156                                            is_declaration) 
9157           == error_mark_node)
9158         return error_mark_node;
9159     }
9160   else
9161     /* Even though `typename' is not present, the proposed resolution
9162        to Core Issue 180 says that in `class A<T>::B', `B' should be
9163        considered a type-name, even if `A<T>' is dependent.  */
9164     cp_parser_nested_name_specifier_opt (parser,
9165                                          /*typename_keyword_p=*/true,
9166                                          /*check_dependency_p=*/true,
9167                                          /*type_p=*/true,
9168                                          is_declaration);
9169   /* For everything but enumeration types, consider a template-id.  */
9170   if (tag_type != enum_type)
9171     {
9172       bool template_p = false;
9173       tree decl;
9174
9175       /* Allow the `template' keyword.  */
9176       template_p = cp_parser_optional_template_keyword (parser);
9177       /* If we didn't see `template', we don't know if there's a
9178          template-id or not.  */
9179       if (!template_p)
9180         cp_parser_parse_tentatively (parser);
9181       /* Parse the template-id.  */
9182       decl = cp_parser_template_id (parser, template_p,
9183                                     /*check_dependency_p=*/true,
9184                                     is_declaration);
9185       /* If we didn't find a template-id, look for an ordinary
9186          identifier.  */
9187       if (!template_p && !cp_parser_parse_definitely (parser))
9188         ;
9189       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9190          in effect, then we must assume that, upon instantiation, the
9191          template will correspond to a class.  */
9192       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9193                && tag_type == typename_type)
9194         type = make_typename_type (parser->scope, decl,
9195                                    /*complain=*/1);
9196       else 
9197         type = TREE_TYPE (decl);
9198     }
9199
9200   /* For an enumeration type, consider only a plain identifier.  */
9201   if (!type)
9202     {
9203       identifier = cp_parser_identifier (parser);
9204
9205       if (identifier == error_mark_node)
9206         {
9207           parser->scope = NULL_TREE;
9208           return error_mark_node;
9209         }
9210
9211       /* For a `typename', we needn't call xref_tag.  */
9212       if (tag_type == typename_type
9213           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9214         return make_typename_type (parser->scope, identifier, 
9215                                    /*complain=*/1);
9216       /* Look up a qualified name in the usual way.  */
9217       if (parser->scope)
9218         {
9219           tree decl;
9220
9221           /* In an elaborated-type-specifier, names are assumed to name
9222              types, so we set IS_TYPE to TRUE when calling
9223              cp_parser_lookup_name.  */
9224           decl = cp_parser_lookup_name (parser, identifier, 
9225                                         /*is_type=*/true,
9226                                         /*is_template=*/false,
9227                                         /*is_namespace=*/false,
9228                                         /*check_dependency=*/true);
9229
9230           /* If we are parsing friend declaration, DECL may be a
9231              TEMPLATE_DECL tree node here.  However, we need to check
9232              whether this TEMPLATE_DECL results in valid code.  Consider
9233              the following example:
9234
9235                namespace N {
9236                  template <class T> class C {};
9237                }
9238                class X {
9239                  template <class T> friend class N::C; // #1, valid code
9240                };
9241                template <class T> class Y {
9242                  friend class N::C;                    // #2, invalid code
9243                };
9244
9245              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9246              name lookup of `N::C'.  We see that friend declaration must
9247              be template for the code to be valid.  Note that
9248              processing_template_decl does not work here since it is
9249              always 1 for the above two cases.  */
9250
9251           decl = (cp_parser_maybe_treat_template_as_class 
9252                   (decl, /*tag_name_p=*/is_friend
9253                          && parser->num_template_parameter_lists));
9254
9255           if (TREE_CODE (decl) != TYPE_DECL)
9256             {
9257               cp_parser_diagnose_invalid_type_name (parser);
9258               return error_mark_node;
9259             }
9260
9261           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9262             check_elaborated_type_specifier 
9263               (tag_type, decl,
9264                (parser->num_template_parameter_lists
9265                 || DECL_SELF_REFERENCE_P (decl)));
9266
9267           type = TREE_TYPE (decl);
9268         }
9269       else 
9270         {
9271           /* An elaborated-type-specifier sometimes introduces a new type and
9272              sometimes names an existing type.  Normally, the rule is that it
9273              introduces a new type only if there is not an existing type of
9274              the same name already in scope.  For example, given:
9275
9276                struct S {};
9277                void f() { struct S s; }
9278
9279              the `struct S' in the body of `f' is the same `struct S' as in
9280              the global scope; the existing definition is used.  However, if
9281              there were no global declaration, this would introduce a new 
9282              local class named `S'.
9283
9284              An exception to this rule applies to the following code:
9285
9286                namespace N { struct S; }
9287
9288              Here, the elaborated-type-specifier names a new type
9289              unconditionally; even if there is already an `S' in the
9290              containing scope this declaration names a new type.
9291              This exception only applies if the elaborated-type-specifier
9292              forms the complete declaration:
9293
9294                [class.name] 
9295
9296                A declaration consisting solely of `class-key identifier ;' is
9297                either a redeclaration of the name in the current scope or a
9298                forward declaration of the identifier as a class name.  It
9299                introduces the name into the current scope.
9300
9301              We are in this situation precisely when the next token is a `;'.
9302
9303              An exception to the exception is that a `friend' declaration does
9304              *not* name a new type; i.e., given:
9305
9306                struct S { friend struct T; };
9307
9308              `T' is not a new type in the scope of `S'.  
9309
9310              Also, `new struct S' or `sizeof (struct S)' never results in the
9311              definition of a new type; a new type can only be declared in a
9312              declaration context.  */
9313
9314           /* Warn about attributes. They are ignored.  */
9315           if (attributes)
9316             warning ("type attributes are honored only at type definition");
9317
9318           type = xref_tag (tag_type, identifier, 
9319                            (is_friend 
9320                             || !is_declaration
9321                             || cp_lexer_next_token_is_not (parser->lexer, 
9322                                                            CPP_SEMICOLON)),
9323                            parser->num_template_parameter_lists);
9324         }
9325     }
9326   if (tag_type != enum_type)
9327     cp_parser_check_class_key (tag_type, type);
9328
9329   /* A "<" cannot follow an elaborated type specifier.  If that
9330      happens, the user was probably trying to form a template-id.  */
9331   cp_parser_check_for_invalid_template_id (parser, type);
9332
9333   return type;
9334 }
9335
9336 /* Parse an enum-specifier.
9337
9338    enum-specifier:
9339      enum identifier [opt] { enumerator-list [opt] }
9340
9341    Returns an ENUM_TYPE representing the enumeration.  */
9342
9343 static tree
9344 cp_parser_enum_specifier (cp_parser* parser)
9345 {
9346   cp_token *token;
9347   tree identifier = NULL_TREE;
9348   tree type;
9349
9350   /* Look for the `enum' keyword.  */
9351   if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9352     return error_mark_node;
9353   /* Peek at the next token.  */
9354   token = cp_lexer_peek_token (parser->lexer);
9355
9356   /* See if it is an identifier.  */
9357   if (token->type == CPP_NAME)
9358     identifier = cp_parser_identifier (parser);
9359
9360   /* Look for the `{'.  */
9361   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9362     return error_mark_node;
9363
9364   /* At this point, we're going ahead with the enum-specifier, even
9365      if some other problem occurs.  */
9366   cp_parser_commit_to_tentative_parse (parser);
9367
9368   /* Issue an error message if type-definitions are forbidden here.  */
9369   cp_parser_check_type_definition (parser);
9370
9371   /* Create the new type.  */
9372   type = start_enum (identifier ? identifier : make_anon_name ());
9373
9374   /* Peek at the next token.  */
9375   token = cp_lexer_peek_token (parser->lexer);
9376   /* If it's not a `}', then there are some enumerators.  */
9377   if (token->type != CPP_CLOSE_BRACE)
9378     cp_parser_enumerator_list (parser, type);
9379   /* Look for the `}'.  */
9380   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9381
9382   /* Finish up the enumeration.  */
9383   finish_enum (type);
9384
9385   return type;
9386 }
9387
9388 /* Parse an enumerator-list.  The enumerators all have the indicated
9389    TYPE.  
9390
9391    enumerator-list:
9392      enumerator-definition
9393      enumerator-list , enumerator-definition  */
9394
9395 static void
9396 cp_parser_enumerator_list (cp_parser* parser, tree type)
9397 {
9398   while (true)
9399     {
9400       cp_token *token;
9401
9402       /* Parse an enumerator-definition.  */
9403       cp_parser_enumerator_definition (parser, type);
9404       /* Peek at the next token.  */
9405       token = cp_lexer_peek_token (parser->lexer);
9406       /* If it's not a `,', then we've reached the end of the 
9407          list.  */
9408       if (token->type != CPP_COMMA)
9409         break;
9410       /* Otherwise, consume the `,' and keep going.  */
9411       cp_lexer_consume_token (parser->lexer);
9412       /* If the next token is a `}', there is a trailing comma.  */
9413       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9414         {
9415           if (pedantic && !in_system_header)
9416             pedwarn ("comma at end of enumerator list");
9417           break;
9418         }
9419     }
9420 }
9421
9422 /* Parse an enumerator-definition.  The enumerator has the indicated
9423    TYPE.
9424
9425    enumerator-definition:
9426      enumerator
9427      enumerator = constant-expression
9428     
9429    enumerator:
9430      identifier  */
9431
9432 static void
9433 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9434 {
9435   cp_token *token;
9436   tree identifier;
9437   tree value;
9438
9439   /* Look for the identifier.  */
9440   identifier = cp_parser_identifier (parser);
9441   if (identifier == error_mark_node)
9442     return;
9443   
9444   /* Peek at the next token.  */
9445   token = cp_lexer_peek_token (parser->lexer);
9446   /* If it's an `=', then there's an explicit value.  */
9447   if (token->type == CPP_EQ)
9448     {
9449       /* Consume the `=' token.  */
9450       cp_lexer_consume_token (parser->lexer);
9451       /* Parse the value.  */
9452       value = cp_parser_constant_expression (parser, 
9453                                              /*allow_non_constant_p=*/false,
9454                                              NULL);
9455     }
9456   else
9457     value = NULL_TREE;
9458
9459   /* Create the enumerator.  */
9460   build_enumerator (identifier, value, type);
9461 }
9462
9463 /* Parse a namespace-name.
9464
9465    namespace-name:
9466      original-namespace-name
9467      namespace-alias
9468
9469    Returns the NAMESPACE_DECL for the namespace.  */
9470
9471 static tree
9472 cp_parser_namespace_name (cp_parser* parser)
9473 {
9474   tree identifier;
9475   tree namespace_decl;
9476
9477   /* Get the name of the namespace.  */
9478   identifier = cp_parser_identifier (parser);
9479   if (identifier == error_mark_node)
9480     return error_mark_node;
9481
9482   /* Look up the identifier in the currently active scope.  Look only
9483      for namespaces, due to:
9484
9485        [basic.lookup.udir]
9486
9487        When looking up a namespace-name in a using-directive or alias
9488        definition, only namespace names are considered.  
9489
9490      And:
9491
9492        [basic.lookup.qual]
9493
9494        During the lookup of a name preceding the :: scope resolution
9495        operator, object, function, and enumerator names are ignored.  
9496
9497      (Note that cp_parser_class_or_namespace_name only calls this
9498      function if the token after the name is the scope resolution
9499      operator.)  */
9500   namespace_decl = cp_parser_lookup_name (parser, identifier,
9501                                           /*is_type=*/false,
9502                                           /*is_template=*/false,
9503                                           /*is_namespace=*/true,
9504                                           /*check_dependency=*/true);
9505   /* If it's not a namespace, issue an error.  */
9506   if (namespace_decl == error_mark_node
9507       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9508     {
9509       if (!cp_parser_parsing_tentatively (parser)
9510           || cp_parser_committed_to_tentative_parse (parser))
9511         error ("`%D' is not a namespace-name", identifier);
9512       cp_parser_error (parser, "expected namespace-name");
9513       namespace_decl = error_mark_node;
9514     }
9515   
9516   return namespace_decl;
9517 }
9518
9519 /* Parse a namespace-definition.
9520
9521    namespace-definition:
9522      named-namespace-definition
9523      unnamed-namespace-definition  
9524
9525    named-namespace-definition:
9526      original-namespace-definition
9527      extension-namespace-definition
9528
9529    original-namespace-definition:
9530      namespace identifier { namespace-body }
9531    
9532    extension-namespace-definition:
9533      namespace original-namespace-name { namespace-body }
9534  
9535    unnamed-namespace-definition:
9536      namespace { namespace-body } */
9537
9538 static void
9539 cp_parser_namespace_definition (cp_parser* parser)
9540 {
9541   tree identifier;
9542
9543   /* Look for the `namespace' keyword.  */
9544   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9545
9546   /* Get the name of the namespace.  We do not attempt to distinguish
9547      between an original-namespace-definition and an
9548      extension-namespace-definition at this point.  The semantic
9549      analysis routines are responsible for that.  */
9550   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9551     identifier = cp_parser_identifier (parser);
9552   else
9553     identifier = NULL_TREE;
9554
9555   /* Look for the `{' to start the namespace.  */
9556   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9557   /* Start the namespace.  */
9558   push_namespace (identifier);
9559   /* Parse the body of the namespace.  */
9560   cp_parser_namespace_body (parser);
9561   /* Finish the namespace.  */
9562   pop_namespace ();
9563   /* Look for the final `}'.  */
9564   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9565 }
9566
9567 /* Parse a namespace-body.
9568
9569    namespace-body:
9570      declaration-seq [opt]  */
9571
9572 static void
9573 cp_parser_namespace_body (cp_parser* parser)
9574 {
9575   cp_parser_declaration_seq_opt (parser);
9576 }
9577
9578 /* Parse a namespace-alias-definition.
9579
9580    namespace-alias-definition:
9581      namespace identifier = qualified-namespace-specifier ;  */
9582
9583 static void
9584 cp_parser_namespace_alias_definition (cp_parser* parser)
9585 {
9586   tree identifier;
9587   tree namespace_specifier;
9588
9589   /* Look for the `namespace' keyword.  */
9590   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9591   /* Look for the identifier.  */
9592   identifier = cp_parser_identifier (parser);
9593   if (identifier == error_mark_node)
9594     return;
9595   /* Look for the `=' token.  */
9596   cp_parser_require (parser, CPP_EQ, "`='");
9597   /* Look for the qualified-namespace-specifier.  */
9598   namespace_specifier 
9599     = cp_parser_qualified_namespace_specifier (parser);
9600   /* Look for the `;' token.  */
9601   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9602
9603   /* Register the alias in the symbol table.  */
9604   do_namespace_alias (identifier, namespace_specifier);
9605 }
9606
9607 /* Parse a qualified-namespace-specifier.
9608
9609    qualified-namespace-specifier:
9610      :: [opt] nested-name-specifier [opt] namespace-name
9611
9612    Returns a NAMESPACE_DECL corresponding to the specified
9613    namespace.  */
9614
9615 static tree
9616 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9617 {
9618   /* Look for the optional `::'.  */
9619   cp_parser_global_scope_opt (parser, 
9620                               /*current_scope_valid_p=*/false);
9621
9622   /* Look for the optional nested-name-specifier.  */
9623   cp_parser_nested_name_specifier_opt (parser,
9624                                        /*typename_keyword_p=*/false,
9625                                        /*check_dependency_p=*/true,
9626                                        /*type_p=*/false,
9627                                        /*is_declaration=*/true);
9628
9629   return cp_parser_namespace_name (parser);
9630 }
9631
9632 /* Parse a using-declaration.
9633
9634    using-declaration:
9635      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9636      using :: unqualified-id ;  */
9637
9638 static void
9639 cp_parser_using_declaration (cp_parser* parser)
9640 {
9641   cp_token *token;
9642   bool typename_p = false;
9643   bool global_scope_p;
9644   tree decl;
9645   tree identifier;
9646   tree scope;
9647   tree qscope;
9648
9649   /* Look for the `using' keyword.  */
9650   cp_parser_require_keyword (parser, RID_USING, "`using'");
9651   
9652   /* Peek at the next token.  */
9653   token = cp_lexer_peek_token (parser->lexer);
9654   /* See if it's `typename'.  */
9655   if (token->keyword == RID_TYPENAME)
9656     {
9657       /* Remember that we've seen it.  */
9658       typename_p = true;
9659       /* Consume the `typename' token.  */
9660       cp_lexer_consume_token (parser->lexer);
9661     }
9662
9663   /* Look for the optional global scope qualification.  */
9664   global_scope_p 
9665     = (cp_parser_global_scope_opt (parser,
9666                                    /*current_scope_valid_p=*/false) 
9667        != NULL_TREE);
9668
9669   /* If we saw `typename', or didn't see `::', then there must be a
9670      nested-name-specifier present.  */
9671   if (typename_p || !global_scope_p)
9672     qscope = cp_parser_nested_name_specifier (parser, typename_p, 
9673                                               /*check_dependency_p=*/true,
9674                                               /*type_p=*/false,
9675                                               /*is_declaration=*/true);
9676   /* Otherwise, we could be in either of the two productions.  In that
9677      case, treat the nested-name-specifier as optional.  */
9678   else
9679     qscope = cp_parser_nested_name_specifier_opt (parser,
9680                                                   /*typename_keyword_p=*/false,
9681                                                   /*check_dependency_p=*/true,
9682                                                   /*type_p=*/false,
9683                                                   /*is_declaration=*/true);
9684   if (!qscope)
9685     qscope = global_namespace;
9686
9687   /* Parse the unqualified-id.  */
9688   identifier = cp_parser_unqualified_id (parser, 
9689                                          /*template_keyword_p=*/false,
9690                                          /*check_dependency_p=*/true,
9691                                          /*declarator_p=*/true);
9692
9693   /* The function we call to handle a using-declaration is different
9694      depending on what scope we are in.  */
9695   if (identifier == error_mark_node)
9696     ;
9697   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9698            && TREE_CODE (identifier) != BIT_NOT_EXPR)
9699     /* [namespace.udecl]
9700
9701        A using declaration shall not name a template-id.  */
9702     error ("a template-id may not appear in a using-declaration");
9703   else
9704     {
9705       scope = current_scope ();
9706       if (scope && TYPE_P (scope))
9707         {
9708           /* Create the USING_DECL.  */
9709           decl = do_class_using_decl (build_nt (SCOPE_REF,
9710                                                 parser->scope,
9711                                                 identifier));
9712           /* Add it to the list of members in this class.  */
9713           finish_member_declaration (decl);
9714         }
9715       else
9716         {
9717           decl = cp_parser_lookup_name_simple (parser, identifier);
9718           if (decl == error_mark_node)
9719             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9720           else if (scope)
9721             do_local_using_decl (decl, qscope, identifier);
9722           else
9723             do_toplevel_using_decl (decl, qscope, identifier);
9724         }
9725     }
9726
9727   /* Look for the final `;'.  */
9728   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9729 }
9730
9731 /* Parse a using-directive.  
9732  
9733    using-directive:
9734      using namespace :: [opt] nested-name-specifier [opt]
9735        namespace-name ;  */
9736
9737 static void
9738 cp_parser_using_directive (cp_parser* parser)
9739 {
9740   tree namespace_decl;
9741   tree attribs;
9742
9743   /* Look for the `using' keyword.  */
9744   cp_parser_require_keyword (parser, RID_USING, "`using'");
9745   /* And the `namespace' keyword.  */
9746   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9747   /* Look for the optional `::' operator.  */
9748   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9749   /* And the optional nested-name-specifier.  */
9750   cp_parser_nested_name_specifier_opt (parser,
9751                                        /*typename_keyword_p=*/false,
9752                                        /*check_dependency_p=*/true,
9753                                        /*type_p=*/false,
9754                                        /*is_declaration=*/true);
9755   /* Get the namespace being used.  */
9756   namespace_decl = cp_parser_namespace_name (parser);
9757   /* And any specified attributes.  */
9758   attribs = cp_parser_attributes_opt (parser);
9759   /* Update the symbol table.  */
9760   parse_using_directive (namespace_decl, attribs);
9761   /* Look for the final `;'.  */
9762   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9763 }
9764
9765 /* Parse an asm-definition.
9766
9767    asm-definition:
9768      asm ( string-literal ) ;  
9769
9770    GNU Extension:
9771
9772    asm-definition:
9773      asm volatile [opt] ( string-literal ) ;
9774      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9775      asm volatile [opt] ( string-literal : asm-operand-list [opt]
9776                           : asm-operand-list [opt] ) ;
9777      asm volatile [opt] ( string-literal : asm-operand-list [opt] 
9778                           : asm-operand-list [opt] 
9779                           : asm-operand-list [opt] ) ;  */
9780
9781 static void
9782 cp_parser_asm_definition (cp_parser* parser)
9783 {
9784   cp_token *token;
9785   tree string;
9786   tree outputs = NULL_TREE;
9787   tree inputs = NULL_TREE;
9788   tree clobbers = NULL_TREE;
9789   tree asm_stmt;
9790   bool volatile_p = false;
9791   bool extended_p = false;
9792
9793   /* Look for the `asm' keyword.  */
9794   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9795   /* See if the next token is `volatile'.  */
9796   if (cp_parser_allow_gnu_extensions_p (parser)
9797       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9798     {
9799       /* Remember that we saw the `volatile' keyword.  */
9800       volatile_p = true;
9801       /* Consume the token.  */
9802       cp_lexer_consume_token (parser->lexer);
9803     }
9804   /* Look for the opening `('.  */
9805   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9806   /* Look for the string.  */
9807   token = cp_parser_require (parser, CPP_STRING, "asm body");
9808   if (!token)
9809     return;
9810   string = token->value;
9811   /* If we're allowing GNU extensions, check for the extended assembly
9812      syntax.  Unfortunately, the `:' tokens need not be separated by 
9813      a space in C, and so, for compatibility, we tolerate that here
9814      too.  Doing that means that we have to treat the `::' operator as
9815      two `:' tokens.  */
9816   if (cp_parser_allow_gnu_extensions_p (parser)
9817       && at_function_scope_p ()
9818       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9819           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9820     {
9821       bool inputs_p = false;
9822       bool clobbers_p = false;
9823
9824       /* The extended syntax was used.  */
9825       extended_p = true;
9826
9827       /* Look for outputs.  */
9828       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9829         {
9830           /* Consume the `:'.  */
9831           cp_lexer_consume_token (parser->lexer);
9832           /* Parse the output-operands.  */
9833           if (cp_lexer_next_token_is_not (parser->lexer, 
9834                                           CPP_COLON)
9835               && cp_lexer_next_token_is_not (parser->lexer,
9836                                              CPP_SCOPE)
9837               && cp_lexer_next_token_is_not (parser->lexer,
9838                                              CPP_CLOSE_PAREN))
9839             outputs = cp_parser_asm_operand_list (parser);
9840         }
9841       /* If the next token is `::', there are no outputs, and the
9842          next token is the beginning of the inputs.  */
9843       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9844         /* The inputs are coming next.  */
9845         inputs_p = true;
9846
9847       /* Look for inputs.  */
9848       if (inputs_p
9849           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9850         {
9851           /* Consume the `:' or `::'.  */
9852           cp_lexer_consume_token (parser->lexer);
9853           /* Parse the output-operands.  */
9854           if (cp_lexer_next_token_is_not (parser->lexer, 
9855                                           CPP_COLON)
9856               && cp_lexer_next_token_is_not (parser->lexer,
9857                                              CPP_CLOSE_PAREN))
9858             inputs = cp_parser_asm_operand_list (parser);
9859         }
9860       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9861         /* The clobbers are coming next.  */
9862         clobbers_p = true;
9863
9864       /* Look for clobbers.  */
9865       if (clobbers_p 
9866           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9867         {
9868           /* Consume the `:' or `::'.  */
9869           cp_lexer_consume_token (parser->lexer);
9870           /* Parse the clobbers.  */
9871           if (cp_lexer_next_token_is_not (parser->lexer,
9872                                           CPP_CLOSE_PAREN))
9873             clobbers = cp_parser_asm_clobber_list (parser);
9874         }
9875     }
9876   /* Look for the closing `)'.  */
9877   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9878     cp_parser_skip_to_closing_parenthesis (parser, true, false,
9879                                            /*consume_paren=*/true);
9880   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9881
9882   /* Create the ASM_STMT.  */
9883   if (at_function_scope_p ())
9884     {
9885       asm_stmt = 
9886         finish_asm_stmt (volatile_p 
9887                          ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9888                          string, outputs, inputs, clobbers);
9889       /* If the extended syntax was not used, mark the ASM_STMT.  */
9890       if (!extended_p)
9891         ASM_INPUT_P (asm_stmt) = 1;
9892     }
9893   else
9894     assemble_asm (string);
9895 }
9896
9897 /* Declarators [gram.dcl.decl] */
9898
9899 /* Parse an init-declarator.
9900
9901    init-declarator:
9902      declarator initializer [opt]
9903
9904    GNU Extension:
9905
9906    init-declarator:
9907      declarator asm-specification [opt] attributes [opt] initializer [opt]
9908
9909    function-definition:
9910      decl-specifier-seq [opt] declarator ctor-initializer [opt]
9911        function-body 
9912      decl-specifier-seq [opt] declarator function-try-block  
9913
9914    GNU Extension:
9915
9916    function-definition:
9917      __extension__ function-definition 
9918
9919    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9920    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
9921    then this declarator appears in a class scope.  The new DECL created
9922    by this declarator is returned.
9923
9924    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9925    for a function-definition here as well.  If the declarator is a
9926    declarator for a function-definition, *FUNCTION_DEFINITION_P will
9927    be TRUE upon return.  By that point, the function-definition will
9928    have been completely parsed.
9929
9930    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9931    is FALSE.  */
9932
9933 static tree
9934 cp_parser_init_declarator (cp_parser* parser, 
9935                            tree decl_specifiers, 
9936                            tree prefix_attributes,
9937                            bool function_definition_allowed_p,
9938                            bool member_p,
9939                            int declares_class_or_enum,
9940                            bool* function_definition_p)
9941 {
9942   cp_token *token;
9943   tree declarator;
9944   tree attributes;
9945   tree asm_specification;
9946   tree initializer;
9947   tree decl = NULL_TREE;
9948   tree scope;
9949   bool is_initialized;
9950   bool is_parenthesized_init;
9951   bool is_non_constant_init;
9952   int ctor_dtor_or_conv_p;
9953   bool friend_p;
9954   bool pop_p = false;
9955
9956   /* Assume that this is not the declarator for a function
9957      definition.  */
9958   if (function_definition_p)
9959     *function_definition_p = false;
9960
9961   /* Defer access checks while parsing the declarator; we cannot know
9962      what names are accessible until we know what is being 
9963      declared.  */
9964   resume_deferring_access_checks ();
9965
9966   /* Parse the declarator.  */
9967   declarator 
9968     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9969                             &ctor_dtor_or_conv_p,
9970                             /*parenthesized_p=*/NULL,
9971                             /*member_p=*/false);
9972   /* Gather up the deferred checks.  */
9973   stop_deferring_access_checks ();
9974
9975   /* If the DECLARATOR was erroneous, there's no need to go
9976      further.  */
9977   if (declarator == error_mark_node)
9978     return error_mark_node;
9979
9980   if (declares_class_or_enum & 2)
9981     cp_parser_check_for_definition_in_return_type
9982       (declarator, TREE_VALUE (decl_specifiers));
9983
9984   /* Figure out what scope the entity declared by the DECLARATOR is
9985      located in.  `grokdeclarator' sometimes changes the scope, so
9986      we compute it now.  */
9987   scope = get_scope_of_declarator (declarator);
9988
9989   /* If we're allowing GNU extensions, look for an asm-specification
9990      and attributes.  */
9991   if (cp_parser_allow_gnu_extensions_p (parser))
9992     {
9993       /* Look for an asm-specification.  */
9994       asm_specification = cp_parser_asm_specification_opt (parser);
9995       /* And attributes.  */
9996       attributes = cp_parser_attributes_opt (parser);
9997     }
9998   else
9999     {
10000       asm_specification = NULL_TREE;
10001       attributes = NULL_TREE;
10002     }
10003
10004   /* Peek at the next token.  */
10005   token = cp_lexer_peek_token (parser->lexer);
10006   /* Check to see if the token indicates the start of a
10007      function-definition.  */
10008   if (cp_parser_token_starts_function_definition_p (token))
10009     {
10010       if (!function_definition_allowed_p)
10011         {
10012           /* If a function-definition should not appear here, issue an
10013              error message.  */
10014           cp_parser_error (parser,
10015                            "a function-definition is not allowed here");
10016           return error_mark_node;
10017         }
10018       else
10019         {
10020           /* Neither attributes nor an asm-specification are allowed
10021              on a function-definition.  */
10022           if (asm_specification)
10023             error ("an asm-specification is not allowed on a function-definition");
10024           if (attributes)
10025             error ("attributes are not allowed on a function-definition");
10026           /* This is a function-definition.  */
10027           *function_definition_p = true;
10028
10029           /* Parse the function definition.  */
10030           if (member_p)
10031             decl = cp_parser_save_member_function_body (parser,
10032                                                         decl_specifiers,
10033                                                         declarator,
10034                                                         prefix_attributes);
10035           else
10036             decl 
10037               = (cp_parser_function_definition_from_specifiers_and_declarator
10038                  (parser, decl_specifiers, prefix_attributes, declarator));
10039
10040           return decl;
10041         }
10042     }
10043
10044   /* [dcl.dcl]
10045
10046      Only in function declarations for constructors, destructors, and
10047      type conversions can the decl-specifier-seq be omitted.  
10048
10049      We explicitly postpone this check past the point where we handle
10050      function-definitions because we tolerate function-definitions
10051      that are missing their return types in some modes.  */
10052   if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
10053     {
10054       cp_parser_error (parser, 
10055                        "expected constructor, destructor, or type conversion");
10056       return error_mark_node;
10057     }
10058
10059   /* An `=' or an `(' indicates an initializer.  */
10060   is_initialized = (token->type == CPP_EQ 
10061                      || token->type == CPP_OPEN_PAREN);
10062   /* If the init-declarator isn't initialized and isn't followed by a
10063      `,' or `;', it's not a valid init-declarator.  */
10064   if (!is_initialized 
10065       && token->type != CPP_COMMA
10066       && token->type != CPP_SEMICOLON)
10067     {
10068       cp_parser_error (parser, "expected initializer");
10069       return error_mark_node;
10070     }
10071
10072   /* Because start_decl has side-effects, we should only call it if we
10073      know we're going ahead.  By this point, we know that we cannot
10074      possibly be looking at any other construct.  */
10075   cp_parser_commit_to_tentative_parse (parser);
10076
10077   /* If the decl specifiers were bad, issue an error now that we're
10078      sure this was intended to be a declarator.  Then continue
10079      declaring the variable(s), as int, to try to cut down on further
10080      errors.  */
10081   if (decl_specifiers != NULL
10082       && TREE_VALUE (decl_specifiers) == error_mark_node)
10083     {
10084       cp_parser_error (parser, "invalid type in declaration");
10085       TREE_VALUE (decl_specifiers) = integer_type_node;
10086     }
10087
10088   /* Check to see whether or not this declaration is a friend.  */
10089   friend_p = cp_parser_friend_p (decl_specifiers);
10090
10091   /* Check that the number of template-parameter-lists is OK.  */
10092   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10093     return error_mark_node;
10094
10095   /* Enter the newly declared entry in the symbol table.  If we're
10096      processing a declaration in a class-specifier, we wait until
10097      after processing the initializer.  */
10098   if (!member_p)
10099     {
10100       if (parser->in_unbraced_linkage_specification_p)
10101         {
10102           decl_specifiers = tree_cons (error_mark_node,
10103                                        get_identifier ("extern"),
10104                                        decl_specifiers);
10105           have_extern_spec = false;
10106         }
10107       decl = start_decl (declarator, decl_specifiers,
10108                          is_initialized, attributes, prefix_attributes);
10109     }
10110
10111   /* Enter the SCOPE.  That way unqualified names appearing in the
10112      initializer will be looked up in SCOPE.  */
10113   if (scope)
10114     pop_p = push_scope (scope);
10115
10116   /* Perform deferred access control checks, now that we know in which
10117      SCOPE the declared entity resides.  */
10118   if (!member_p && decl) 
10119     {
10120       tree saved_current_function_decl = NULL_TREE;
10121
10122       /* If the entity being declared is a function, pretend that we
10123          are in its scope.  If it is a `friend', it may have access to
10124          things that would not otherwise be accessible.  */
10125       if (TREE_CODE (decl) == FUNCTION_DECL)
10126         {
10127           saved_current_function_decl = current_function_decl;
10128           current_function_decl = decl;
10129         }
10130         
10131       /* Perform the access control checks for the declarator and the
10132          the decl-specifiers.  */
10133       perform_deferred_access_checks ();
10134
10135       /* Restore the saved value.  */
10136       if (TREE_CODE (decl) == FUNCTION_DECL)
10137         current_function_decl = saved_current_function_decl;
10138     }
10139
10140   /* Parse the initializer.  */
10141   if (is_initialized)
10142     initializer = cp_parser_initializer (parser, 
10143                                          &is_parenthesized_init,
10144                                          &is_non_constant_init);
10145   else
10146     {
10147       initializer = NULL_TREE;
10148       is_parenthesized_init = false;
10149       is_non_constant_init = true;
10150     }
10151
10152   /* The old parser allows attributes to appear after a parenthesized
10153      initializer.  Mark Mitchell proposed removing this functionality
10154      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10155      attributes -- but ignores them.  */
10156   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10157     if (cp_parser_attributes_opt (parser))
10158       warning ("attributes after parenthesized initializer ignored");
10159
10160   /* Leave the SCOPE, now that we have processed the initializer.  It
10161      is important to do this before calling cp_finish_decl because it
10162      makes decisions about whether to create DECL_STMTs or not based
10163      on the current scope.  */
10164   if (pop_p)
10165     pop_scope (scope);
10166
10167   /* For an in-class declaration, use `grokfield' to create the
10168      declaration.  */
10169   if (member_p)
10170     {
10171       decl = grokfield (declarator, decl_specifiers,
10172                         initializer, /*asmspec=*/NULL_TREE,
10173                         /*attributes=*/NULL_TREE);
10174       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10175         cp_parser_save_default_args (parser, decl);
10176     }
10177   
10178   /* Finish processing the declaration.  But, skip friend
10179      declarations.  */
10180   if (!friend_p && decl)
10181     cp_finish_decl (decl, 
10182                     initializer, 
10183                     asm_specification,
10184                     /* If the initializer is in parentheses, then this is
10185                        a direct-initialization, which means that an
10186                        `explicit' constructor is OK.  Otherwise, an
10187                        `explicit' constructor cannot be used.  */
10188                     ((is_parenthesized_init || !is_initialized)
10189                      ? 0 : LOOKUP_ONLYCONVERTING));
10190
10191   /* Remember whether or not variables were initialized by
10192      constant-expressions.  */
10193   if (decl && TREE_CODE (decl) == VAR_DECL 
10194       && is_initialized && !is_non_constant_init)
10195     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10196
10197   return decl;
10198 }
10199
10200 /* Parse a declarator.
10201    
10202    declarator:
10203      direct-declarator
10204      ptr-operator declarator  
10205
10206    abstract-declarator:
10207      ptr-operator abstract-declarator [opt]
10208      direct-abstract-declarator
10209
10210    GNU Extensions:
10211
10212    declarator:
10213      attributes [opt] direct-declarator
10214      attributes [opt] ptr-operator declarator  
10215
10216    abstract-declarator:
10217      attributes [opt] ptr-operator abstract-declarator [opt]
10218      attributes [opt] direct-abstract-declarator
10219      
10220    Returns a representation of the declarator.  If the declarator has
10221    the form `* declarator', then an INDIRECT_REF is returned, whose
10222    only operand is the sub-declarator.  Analogously, `& declarator' is
10223    represented as an ADDR_EXPR.  For `X::* declarator', a SCOPE_REF is
10224    used.  The first operand is the TYPE for `X'.  The second operand
10225    is an INDIRECT_REF whose operand is the sub-declarator.
10226
10227    Otherwise, the representation is as for a direct-declarator.
10228
10229    (It would be better to define a structure type to represent
10230    declarators, rather than abusing `tree' nodes to represent
10231    declarators.  That would be much clearer and save some memory.
10232    There is no reason for declarators to be garbage-collected, for
10233    example; they are created during parser and no longer needed after
10234    `grokdeclarator' has been called.)
10235
10236    For a ptr-operator that has the optional cv-qualifier-seq,
10237    cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10238    node.
10239
10240    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10241    detect constructor, destructor or conversion operators. It is set
10242    to -1 if the declarator is a name, and +1 if it is a
10243    function. Otherwise it is set to zero. Usually you just want to
10244    test for >0, but internally the negative value is used.
10245    
10246    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10247    a decl-specifier-seq unless it declares a constructor, destructor,
10248    or conversion.  It might seem that we could check this condition in
10249    semantic analysis, rather than parsing, but that makes it difficult
10250    to handle something like `f()'.  We want to notice that there are
10251    no decl-specifiers, and therefore realize that this is an
10252    expression, not a declaration.)  
10253  
10254    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10255    the declarator is a direct-declarator of the form "(...)".
10256
10257    MEMBER_P is true iff this declarator is a member-declarator.  */
10258
10259 static tree
10260 cp_parser_declarator (cp_parser* parser, 
10261                       cp_parser_declarator_kind dcl_kind, 
10262                       int* ctor_dtor_or_conv_p,
10263                       bool* parenthesized_p,
10264                       bool member_p)
10265 {
10266   cp_token *token;
10267   tree declarator;
10268   enum tree_code code;
10269   tree cv_qualifier_seq;
10270   tree class_type;
10271   tree attributes = NULL_TREE;
10272
10273   /* Assume this is not a constructor, destructor, or type-conversion
10274      operator.  */
10275   if (ctor_dtor_or_conv_p)
10276     *ctor_dtor_or_conv_p = 0;
10277
10278   if (cp_parser_allow_gnu_extensions_p (parser))
10279     attributes = cp_parser_attributes_opt (parser);
10280   
10281   /* Peek at the next token.  */
10282   token = cp_lexer_peek_token (parser->lexer);
10283   
10284   /* Check for the ptr-operator production.  */
10285   cp_parser_parse_tentatively (parser);
10286   /* Parse the ptr-operator.  */
10287   code = cp_parser_ptr_operator (parser, 
10288                                  &class_type, 
10289                                  &cv_qualifier_seq);
10290   /* If that worked, then we have a ptr-operator.  */
10291   if (cp_parser_parse_definitely (parser))
10292     {
10293       /* If a ptr-operator was found, then this declarator was not
10294          parenthesized.  */
10295       if (parenthesized_p)
10296         *parenthesized_p = true;
10297       /* The dependent declarator is optional if we are parsing an
10298          abstract-declarator.  */
10299       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10300         cp_parser_parse_tentatively (parser);
10301
10302       /* Parse the dependent declarator.  */
10303       declarator = cp_parser_declarator (parser, dcl_kind,
10304                                          /*ctor_dtor_or_conv_p=*/NULL,
10305                                          /*parenthesized_p=*/NULL,
10306                                          /*member_p=*/false);
10307
10308       /* If we are parsing an abstract-declarator, we must handle the
10309          case where the dependent declarator is absent.  */
10310       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10311           && !cp_parser_parse_definitely (parser))
10312         declarator = NULL_TREE;
10313         
10314       /* Build the representation of the ptr-operator.  */
10315       if (code == INDIRECT_REF)
10316         declarator = make_pointer_declarator (cv_qualifier_seq, 
10317                                               declarator);
10318       else
10319         declarator = make_reference_declarator (cv_qualifier_seq,
10320                                                 declarator);
10321       /* Handle the pointer-to-member case.  */
10322       if (class_type)
10323         declarator = build_nt (SCOPE_REF, class_type, declarator);
10324     }
10325   /* Everything else is a direct-declarator.  */
10326   else
10327     {
10328       if (parenthesized_p)
10329         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10330                                                    CPP_OPEN_PAREN);
10331       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10332                                                 ctor_dtor_or_conv_p,
10333                                                 member_p);
10334     }
10335
10336   if (attributes && declarator != error_mark_node)
10337     declarator = tree_cons (attributes, declarator, NULL_TREE);
10338   
10339   return declarator;
10340 }
10341
10342 /* Parse a direct-declarator or direct-abstract-declarator.
10343
10344    direct-declarator:
10345      declarator-id
10346      direct-declarator ( parameter-declaration-clause )
10347        cv-qualifier-seq [opt] 
10348        exception-specification [opt]
10349      direct-declarator [ constant-expression [opt] ]
10350      ( declarator )  
10351
10352    direct-abstract-declarator:
10353      direct-abstract-declarator [opt]
10354        ( parameter-declaration-clause ) 
10355        cv-qualifier-seq [opt]
10356        exception-specification [opt]
10357      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10358      ( abstract-declarator )
10359
10360    Returns a representation of the declarator.  DCL_KIND is
10361    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10362    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10363    we are parsing a direct-declarator.  It is
10364    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10365    of ambiguity we prefer an abstract declarator, as per
10366    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
10367    cp_parser_declarator.
10368
10369    For the declarator-id production, the representation is as for an
10370    id-expression, except that a qualified name is represented as a
10371    SCOPE_REF.  A function-declarator is represented as a CALL_EXPR;
10372    see the documentation of the FUNCTION_DECLARATOR_* macros for
10373    information about how to find the various declarator components.
10374    An array-declarator is represented as an ARRAY_REF.  The
10375    direct-declarator is the first operand; the constant-expression
10376    indicating the size of the array is the second operand.  */
10377
10378 static tree
10379 cp_parser_direct_declarator (cp_parser* parser,
10380                              cp_parser_declarator_kind dcl_kind,
10381                              int* ctor_dtor_or_conv_p,
10382                              bool member_p)
10383 {
10384   cp_token *token;
10385   tree declarator = NULL_TREE;
10386   tree scope = NULL_TREE;
10387   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10388   bool saved_in_declarator_p = parser->in_declarator_p;
10389   bool first = true;
10390   bool pop_p = false;
10391
10392   while (true)
10393     {
10394       /* Peek at the next token.  */
10395       token = cp_lexer_peek_token (parser->lexer);
10396       if (token->type == CPP_OPEN_PAREN)
10397         {
10398           /* This is either a parameter-declaration-clause, or a
10399              parenthesized declarator. When we know we are parsing a
10400              named declarator, it must be a parenthesized declarator
10401              if FIRST is true. For instance, `(int)' is a
10402              parameter-declaration-clause, with an omitted
10403              direct-abstract-declarator. But `((*))', is a
10404              parenthesized abstract declarator. Finally, when T is a
10405              template parameter `(T)' is a
10406              parameter-declaration-clause, and not a parenthesized
10407              named declarator.
10408              
10409              We first try and parse a parameter-declaration-clause,
10410              and then try a nested declarator (if FIRST is true).
10411
10412              It is not an error for it not to be a
10413              parameter-declaration-clause, even when FIRST is
10414              false. Consider,
10415
10416                int i (int);
10417                int i (3);
10418
10419              The first is the declaration of a function while the
10420              second is a the definition of a variable, including its
10421              initializer.
10422
10423              Having seen only the parenthesis, we cannot know which of
10424              these two alternatives should be selected.  Even more
10425              complex are examples like:
10426
10427                int i (int (a));
10428                int i (int (3));
10429
10430              The former is a function-declaration; the latter is a
10431              variable initialization.  
10432
10433              Thus again, we try a parameter-declaration-clause, and if
10434              that fails, we back out and return.  */
10435
10436           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10437             {
10438               tree params;
10439               unsigned saved_num_template_parameter_lists;
10440               
10441               /* In a member-declarator, the only valid interpretation
10442                  of a parenthesis is the start of a
10443                  parameter-declaration-clause.  (It is invalid to
10444                  initialize a static data member with a parenthesized
10445                  initializer; only the "=" form of initialization is
10446                  permitted.)  */
10447               if (!member_p)
10448                 cp_parser_parse_tentatively (parser);
10449
10450               /* Consume the `('.  */
10451               cp_lexer_consume_token (parser->lexer);
10452               if (first)
10453                 {
10454                   /* If this is going to be an abstract declarator, we're
10455                      in a declarator and we can't have default args.  */
10456                   parser->default_arg_ok_p = false;
10457                   parser->in_declarator_p = true;
10458                 }
10459           
10460               /* Inside the function parameter list, surrounding
10461                  template-parameter-lists do not apply.  */
10462               saved_num_template_parameter_lists
10463                 = parser->num_template_parameter_lists;
10464               parser->num_template_parameter_lists = 0;
10465
10466               /* Parse the parameter-declaration-clause.  */
10467               params = cp_parser_parameter_declaration_clause (parser);
10468
10469               parser->num_template_parameter_lists
10470                 = saved_num_template_parameter_lists;
10471
10472               /* If all went well, parse the cv-qualifier-seq and the
10473                  exception-specification.  */
10474               if (member_p || cp_parser_parse_definitely (parser))
10475                 {
10476                   tree cv_qualifiers;
10477                   tree exception_specification;
10478
10479                   if (ctor_dtor_or_conv_p)
10480                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10481                   first = false;
10482                   /* Consume the `)'.  */
10483                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10484
10485                   /* Parse the cv-qualifier-seq.  */
10486                   cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10487                   /* And the exception-specification.  */
10488                   exception_specification 
10489                     = cp_parser_exception_specification_opt (parser);
10490
10491                   /* Create the function-declarator.  */
10492                   declarator = make_call_declarator (declarator,
10493                                                      params,
10494                                                      cv_qualifiers,
10495                                                      exception_specification);
10496                   /* Any subsequent parameter lists are to do with
10497                      return type, so are not those of the declared
10498                      function.  */
10499                   parser->default_arg_ok_p = false;
10500                   
10501                   /* Repeat the main loop.  */
10502                   continue;
10503                 }
10504             }
10505           
10506           /* If this is the first, we can try a parenthesized
10507              declarator.  */
10508           if (first)
10509             {
10510               bool saved_in_type_id_in_expr_p;
10511
10512               parser->default_arg_ok_p = saved_default_arg_ok_p;
10513               parser->in_declarator_p = saved_in_declarator_p;
10514               
10515               /* Consume the `('.  */
10516               cp_lexer_consume_token (parser->lexer);
10517               /* Parse the nested declarator.  */
10518               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10519               parser->in_type_id_in_expr_p = true;
10520               declarator 
10521                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10522                                         /*parenthesized_p=*/NULL,
10523                                         member_p);
10524               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10525               first = false;
10526               /* Expect a `)'.  */
10527               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10528                 declarator = error_mark_node;
10529               if (declarator == error_mark_node)
10530                 break;
10531               
10532               goto handle_declarator;
10533             }
10534           /* Otherwise, we must be done.  */
10535           else
10536             break;
10537         }
10538       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10539                && token->type == CPP_OPEN_SQUARE)
10540         {
10541           /* Parse an array-declarator.  */
10542           tree bounds;
10543
10544           if (ctor_dtor_or_conv_p)
10545             *ctor_dtor_or_conv_p = 0;
10546           
10547           first = false;
10548           parser->default_arg_ok_p = false;
10549           parser->in_declarator_p = true;
10550           /* Consume the `['.  */
10551           cp_lexer_consume_token (parser->lexer);
10552           /* Peek at the next token.  */
10553           token = cp_lexer_peek_token (parser->lexer);
10554           /* If the next token is `]', then there is no
10555              constant-expression.  */
10556           if (token->type != CPP_CLOSE_SQUARE)
10557             {
10558               bool non_constant_p;
10559
10560               bounds 
10561                 = cp_parser_constant_expression (parser,
10562                                                  /*allow_non_constant=*/true,
10563                                                  &non_constant_p);
10564               if (!non_constant_p)
10565                 bounds = fold_non_dependent_expr (bounds);
10566             }
10567           else
10568             bounds = NULL_TREE;
10569           /* Look for the closing `]'.  */
10570           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10571             {
10572               declarator = error_mark_node;
10573               break;
10574             }
10575
10576           declarator = build_nt (ARRAY_REF, declarator, bounds);
10577         }
10578       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10579         {
10580           /* Parse a declarator-id */
10581           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10582             cp_parser_parse_tentatively (parser);
10583           declarator = cp_parser_declarator_id (parser);
10584           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10585             {
10586               if (!cp_parser_parse_definitely (parser))
10587                 declarator = error_mark_node;
10588               else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10589                 {
10590                   cp_parser_error (parser, "expected unqualified-id");
10591                   declarator = error_mark_node;
10592                 }
10593             }
10594           
10595           if (declarator == error_mark_node)
10596             break;
10597           
10598           if (TREE_CODE (declarator) == SCOPE_REF
10599               && !current_scope ())
10600             {
10601               tree scope = TREE_OPERAND (declarator, 0);
10602
10603               /* In the declaration of a member of a template class
10604                  outside of the class itself, the SCOPE will sometimes
10605                  be a TYPENAME_TYPE.  For example, given:
10606                   
10607                  template <typename T>
10608                  int S<T>::R::i = 3;
10609                   
10610                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
10611                  this context, we must resolve S<T>::R to an ordinary
10612                  type, rather than a typename type.
10613                   
10614                  The reason we normally avoid resolving TYPENAME_TYPEs
10615                  is that a specialization of `S' might render
10616                  `S<T>::R' not a type.  However, if `S' is
10617                  specialized, then this `i' will not be used, so there
10618                  is no harm in resolving the types here.  */
10619               if (TREE_CODE (scope) == TYPENAME_TYPE)
10620                 {
10621                   tree type;
10622
10623                   /* Resolve the TYPENAME_TYPE.  */
10624                   type = resolve_typename_type (scope,
10625                                                 /*only_current_p=*/false);
10626                   /* If that failed, the declarator is invalid.  */
10627                   if (type == error_mark_node)
10628                     error ("`%T::%D' is not a type",
10629                            TYPE_CONTEXT (scope),
10630                            TYPE_IDENTIFIER (scope));
10631                   /* Build a new DECLARATOR.  */
10632                   declarator = build_nt (SCOPE_REF, 
10633                                          type,
10634                                          TREE_OPERAND (declarator, 1));
10635                 }
10636             }
10637       
10638           /* Check to see whether the declarator-id names a constructor, 
10639              destructor, or conversion.  */
10640           if (declarator && ctor_dtor_or_conv_p 
10641               && ((TREE_CODE (declarator) == SCOPE_REF 
10642                    && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10643                   || (TREE_CODE (declarator) != SCOPE_REF
10644                       && at_class_scope_p ())))
10645             {
10646               tree unqualified_name;
10647               tree class_type;
10648
10649               /* Get the unqualified part of the name.  */
10650               if (TREE_CODE (declarator) == SCOPE_REF)
10651                 {
10652                   class_type = TREE_OPERAND (declarator, 0);
10653                   unqualified_name = TREE_OPERAND (declarator, 1);
10654                 }
10655               else
10656                 {
10657                   class_type = current_class_type;
10658                   unqualified_name = declarator;
10659                 }
10660
10661               /* See if it names ctor, dtor or conv.  */
10662               if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10663                   || IDENTIFIER_TYPENAME_P (unqualified_name)
10664                   || constructor_name_p (unqualified_name, class_type)
10665                   || (TREE_CODE (unqualified_name) == TYPE_DECL
10666                       && same_type_p (TREE_TYPE (unqualified_name),
10667                                       class_type)))
10668                 *ctor_dtor_or_conv_p = -1;
10669             }
10670
10671         handle_declarator:;
10672           scope = get_scope_of_declarator (declarator);
10673           if (scope)
10674             /* Any names that appear after the declarator-id for a
10675                member are looked up in the containing scope.  */
10676             pop_p = push_scope (scope);
10677           parser->in_declarator_p = true;
10678           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10679               || (declarator
10680                   && (TREE_CODE (declarator) == SCOPE_REF
10681                       || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10682             /* Default args are only allowed on function
10683                declarations.  */
10684             parser->default_arg_ok_p = saved_default_arg_ok_p;
10685           else
10686             parser->default_arg_ok_p = false;
10687
10688           first = false;
10689         }
10690       /* We're done.  */
10691       else
10692         break;
10693     }
10694
10695   /* For an abstract declarator, we might wind up with nothing at this
10696      point.  That's an error; the declarator is not optional.  */
10697   if (!declarator)
10698     cp_parser_error (parser, "expected declarator");
10699
10700   /* If we entered a scope, we must exit it now.  */
10701   if (pop_p)
10702     pop_scope (scope);
10703
10704   parser->default_arg_ok_p = saved_default_arg_ok_p;
10705   parser->in_declarator_p = saved_in_declarator_p;
10706   
10707   return declarator;
10708 }
10709
10710 /* Parse a ptr-operator.  
10711
10712    ptr-operator:
10713      * cv-qualifier-seq [opt]
10714      &
10715      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10716
10717    GNU Extension:
10718
10719    ptr-operator:
10720      & cv-qualifier-seq [opt]
10721
10722    Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10723    used.  Returns ADDR_EXPR if a reference was used.  In the
10724    case of a pointer-to-member, *TYPE is filled in with the 
10725    TYPE containing the member.  *CV_QUALIFIER_SEQ is filled in
10726    with the cv-qualifier-seq, or NULL_TREE, if there are no
10727    cv-qualifiers.  Returns ERROR_MARK if an error occurred.  */
10728    
10729 static enum tree_code
10730 cp_parser_ptr_operator (cp_parser* parser, 
10731                         tree* type, 
10732                         tree* cv_qualifier_seq)
10733 {
10734   enum tree_code code = ERROR_MARK;
10735   cp_token *token;
10736
10737   /* Assume that it's not a pointer-to-member.  */
10738   *type = NULL_TREE;
10739   /* And that there are no cv-qualifiers.  */
10740   *cv_qualifier_seq = NULL_TREE;
10741
10742   /* Peek at the next token.  */
10743   token = cp_lexer_peek_token (parser->lexer);
10744   /* If it's a `*' or `&' we have a pointer or reference.  */
10745   if (token->type == CPP_MULT || token->type == CPP_AND)
10746     {
10747       /* Remember which ptr-operator we were processing.  */
10748       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10749
10750       /* Consume the `*' or `&'.  */
10751       cp_lexer_consume_token (parser->lexer);
10752
10753       /* A `*' can be followed by a cv-qualifier-seq, and so can a
10754          `&', if we are allowing GNU extensions.  (The only qualifier
10755          that can legally appear after `&' is `restrict', but that is
10756          enforced during semantic analysis.  */
10757       if (code == INDIRECT_REF 
10758           || cp_parser_allow_gnu_extensions_p (parser))
10759         *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10760     }
10761   else
10762     {
10763       /* Try the pointer-to-member case.  */
10764       cp_parser_parse_tentatively (parser);
10765       /* Look for the optional `::' operator.  */
10766       cp_parser_global_scope_opt (parser,
10767                                   /*current_scope_valid_p=*/false);
10768       /* Look for the nested-name specifier.  */
10769       cp_parser_nested_name_specifier (parser,
10770                                        /*typename_keyword_p=*/false,
10771                                        /*check_dependency_p=*/true,
10772                                        /*type_p=*/false,
10773                                        /*is_declaration=*/false);
10774       /* If we found it, and the next token is a `*', then we are
10775          indeed looking at a pointer-to-member operator.  */
10776       if (!cp_parser_error_occurred (parser)
10777           && cp_parser_require (parser, CPP_MULT, "`*'"))
10778         {
10779           /* The type of which the member is a member is given by the
10780              current SCOPE.  */
10781           *type = parser->scope;
10782           /* The next name will not be qualified.  */
10783           parser->scope = NULL_TREE;
10784           parser->qualifying_scope = NULL_TREE;
10785           parser->object_scope = NULL_TREE;
10786           /* Indicate that the `*' operator was used.  */
10787           code = INDIRECT_REF;
10788           /* Look for the optional cv-qualifier-seq.  */
10789           *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10790         }
10791       /* If that didn't work we don't have a ptr-operator.  */
10792       if (!cp_parser_parse_definitely (parser))
10793         cp_parser_error (parser, "expected ptr-operator");
10794     }
10795
10796   return code;
10797 }
10798
10799 /* Parse an (optional) cv-qualifier-seq.
10800
10801    cv-qualifier-seq:
10802      cv-qualifier cv-qualifier-seq [opt]  
10803
10804    Returns a TREE_LIST.  The TREE_VALUE of each node is the
10805    representation of a cv-qualifier.  */
10806
10807 static tree
10808 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10809 {
10810   tree cv_qualifiers = NULL_TREE;
10811   
10812   while (true)
10813     {
10814       tree cv_qualifier;
10815
10816       /* Look for the next cv-qualifier.  */
10817       cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10818       /* If we didn't find one, we're done.  */
10819       if (!cv_qualifier)
10820         break;
10821
10822       /* Add this cv-qualifier to the list.  */
10823       cv_qualifiers 
10824         = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10825     }
10826
10827   /* We built up the list in reverse order.  */
10828   return nreverse (cv_qualifiers);
10829 }
10830
10831 /* Parse an (optional) cv-qualifier.
10832
10833    cv-qualifier:
10834      const
10835      volatile  
10836
10837    GNU Extension:
10838
10839    cv-qualifier:
10840      __restrict__ */
10841
10842 static tree
10843 cp_parser_cv_qualifier_opt (cp_parser* parser)
10844 {
10845   cp_token *token;
10846   tree cv_qualifier = NULL_TREE;
10847
10848   /* Peek at the next token.  */
10849   token = cp_lexer_peek_token (parser->lexer);
10850   /* See if it's a cv-qualifier.  */
10851   switch (token->keyword)
10852     {
10853     case RID_CONST:
10854     case RID_VOLATILE:
10855     case RID_RESTRICT:
10856       /* Save the value of the token.  */
10857       cv_qualifier = token->value;
10858       /* Consume the token.  */
10859       cp_lexer_consume_token (parser->lexer);
10860       break;
10861
10862     default:
10863       break;
10864     }
10865
10866   return cv_qualifier;
10867 }
10868
10869 /* Parse a declarator-id.
10870
10871    declarator-id:
10872      id-expression
10873      :: [opt] nested-name-specifier [opt] type-name  
10874
10875    In the `id-expression' case, the value returned is as for
10876    cp_parser_id_expression if the id-expression was an unqualified-id.
10877    If the id-expression was a qualified-id, then a SCOPE_REF is
10878    returned.  The first operand is the scope (either a NAMESPACE_DECL
10879    or TREE_TYPE), but the second is still just a representation of an
10880    unqualified-id.  */
10881
10882 static tree
10883 cp_parser_declarator_id (cp_parser* parser)
10884 {
10885   tree id_expression;
10886
10887   /* The expression must be an id-expression.  Assume that qualified
10888      names are the names of types so that:
10889
10890        template <class T>
10891        int S<T>::R::i = 3;
10892
10893      will work; we must treat `S<T>::R' as the name of a type.
10894      Similarly, assume that qualified names are templates, where
10895      required, so that:
10896
10897        template <class T>
10898        int S<T>::R<T>::i = 3;
10899
10900      will work, too.  */
10901   id_expression = cp_parser_id_expression (parser,
10902                                            /*template_keyword_p=*/false,
10903                                            /*check_dependency_p=*/false,
10904                                            /*template_p=*/NULL,
10905                                            /*declarator_p=*/true);
10906   /* If the name was qualified, create a SCOPE_REF to represent 
10907      that.  */
10908   if (parser->scope && id_expression != error_mark_node)
10909     {
10910       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10911       parser->scope = NULL_TREE;
10912     }
10913
10914   return id_expression;
10915 }
10916
10917 /* Parse a type-id.
10918
10919    type-id:
10920      type-specifier-seq abstract-declarator [opt]
10921
10922    Returns the TYPE specified.  */
10923
10924 static tree
10925 cp_parser_type_id (cp_parser* parser)
10926 {
10927   tree type_specifier_seq;
10928   tree abstract_declarator;
10929
10930   /* Parse the type-specifier-seq.  */
10931   type_specifier_seq 
10932     = cp_parser_type_specifier_seq (parser);
10933   if (type_specifier_seq == error_mark_node)
10934     return error_mark_node;
10935
10936   /* There might or might not be an abstract declarator.  */
10937   cp_parser_parse_tentatively (parser);
10938   /* Look for the declarator.  */
10939   abstract_declarator 
10940     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10941                             /*parenthesized_p=*/NULL,
10942                             /*member_p=*/false);
10943   /* Check to see if there really was a declarator.  */
10944   if (!cp_parser_parse_definitely (parser))
10945     abstract_declarator = NULL_TREE;
10946
10947   return groktypename (build_tree_list (type_specifier_seq,
10948                                         abstract_declarator));
10949 }
10950
10951 /* Parse a type-specifier-seq.
10952
10953    type-specifier-seq:
10954      type-specifier type-specifier-seq [opt]
10955
10956    GNU extension:
10957
10958    type-specifier-seq:
10959      attributes type-specifier-seq [opt]
10960
10961    Returns a TREE_LIST.  Either the TREE_VALUE of each node is a
10962    type-specifier, or the TREE_PURPOSE is a list of attributes.  */
10963
10964 static tree
10965 cp_parser_type_specifier_seq (cp_parser* parser)
10966 {
10967   bool seen_type_specifier = false;
10968   tree type_specifier_seq = NULL_TREE;
10969
10970   /* Parse the type-specifiers and attributes.  */
10971   while (true)
10972     {
10973       tree type_specifier;
10974
10975       /* Check for attributes first.  */
10976       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10977         {
10978           type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10979                                           NULL_TREE,
10980                                           type_specifier_seq);
10981           continue;
10982         }
10983
10984       /* After the first type-specifier, others are optional.  */
10985       if (seen_type_specifier)
10986         cp_parser_parse_tentatively (parser);
10987       /* Look for the type-specifier.  */
10988       type_specifier = cp_parser_type_specifier (parser, 
10989                                                  CP_PARSER_FLAGS_NONE,
10990                                                  /*is_friend=*/false,
10991                                                  /*is_declaration=*/false,
10992                                                  NULL,
10993                                                  NULL);
10994       /* If the first type-specifier could not be found, this is not a
10995          type-specifier-seq at all.  */
10996       if (!seen_type_specifier && type_specifier == error_mark_node)
10997         return error_mark_node;
10998       /* If subsequent type-specifiers could not be found, the
10999          type-specifier-seq is complete.  */
11000       else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
11001         break;
11002
11003       /* Add the new type-specifier to the list.  */
11004       type_specifier_seq 
11005         = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
11006       seen_type_specifier = true;
11007     }
11008
11009   /* We built up the list in reverse order.  */
11010   return nreverse (type_specifier_seq);
11011 }
11012
11013 /* Parse a parameter-declaration-clause.
11014
11015    parameter-declaration-clause:
11016      parameter-declaration-list [opt] ... [opt]
11017      parameter-declaration-list , ...
11018
11019    Returns a representation for the parameter declarations.  Each node
11020    is a TREE_LIST.  (See cp_parser_parameter_declaration for the exact
11021    representation.)  If the parameter-declaration-clause ends with an
11022    ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
11023    list.  A return value of NULL_TREE indicates a
11024    parameter-declaration-clause consisting only of an ellipsis.  */
11025
11026 static tree
11027 cp_parser_parameter_declaration_clause (cp_parser* parser)
11028 {
11029   tree parameters;
11030   cp_token *token;
11031   bool ellipsis_p;
11032
11033   /* Peek at the next token.  */
11034   token = cp_lexer_peek_token (parser->lexer);
11035   /* Check for trivial parameter-declaration-clauses.  */
11036   if (token->type == CPP_ELLIPSIS)
11037     {
11038       /* Consume the `...' token.  */
11039       cp_lexer_consume_token (parser->lexer);
11040       return NULL_TREE;
11041     }
11042   else if (token->type == CPP_CLOSE_PAREN)
11043     /* There are no parameters.  */
11044     {
11045 #ifndef NO_IMPLICIT_EXTERN_C
11046       if (in_system_header && current_class_type == NULL
11047           && current_lang_name == lang_name_c)
11048         return NULL_TREE;
11049       else
11050 #endif
11051         return void_list_node;
11052     }
11053   /* Check for `(void)', too, which is a special case.  */
11054   else if (token->keyword == RID_VOID
11055            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
11056                == CPP_CLOSE_PAREN))
11057     {
11058       /* Consume the `void' token.  */
11059       cp_lexer_consume_token (parser->lexer);
11060       /* There are no parameters.  */
11061       return void_list_node;
11062     }
11063   
11064   /* Parse the parameter-declaration-list.  */
11065   parameters = cp_parser_parameter_declaration_list (parser);
11066   /* If a parse error occurred while parsing the
11067      parameter-declaration-list, then the entire
11068      parameter-declaration-clause is erroneous.  */
11069   if (parameters == error_mark_node)
11070     return error_mark_node;
11071
11072   /* Peek at the next token.  */
11073   token = cp_lexer_peek_token (parser->lexer);
11074   /* If it's a `,', the clause should terminate with an ellipsis.  */
11075   if (token->type == CPP_COMMA)
11076     {
11077       /* Consume the `,'.  */
11078       cp_lexer_consume_token (parser->lexer);
11079       /* Expect an ellipsis.  */
11080       ellipsis_p 
11081         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11082     }
11083   /* It might also be `...' if the optional trailing `,' was 
11084      omitted.  */
11085   else if (token->type == CPP_ELLIPSIS)
11086     {
11087       /* Consume the `...' token.  */
11088       cp_lexer_consume_token (parser->lexer);
11089       /* And remember that we saw it.  */
11090       ellipsis_p = true;
11091     }
11092   else
11093     ellipsis_p = false;
11094
11095   /* Finish the parameter list.  */
11096   return finish_parmlist (parameters, ellipsis_p);
11097 }
11098
11099 /* Parse a parameter-declaration-list.
11100
11101    parameter-declaration-list:
11102      parameter-declaration
11103      parameter-declaration-list , parameter-declaration
11104
11105    Returns a representation of the parameter-declaration-list, as for
11106    cp_parser_parameter_declaration_clause.  However, the
11107    `void_list_node' is never appended to the list.  */
11108
11109 static tree
11110 cp_parser_parameter_declaration_list (cp_parser* parser)
11111 {
11112   tree parameters = NULL_TREE;
11113
11114   /* Look for more parameters.  */
11115   while (true)
11116     {
11117       tree parameter;
11118       bool parenthesized_p;
11119       /* Parse the parameter.  */
11120       parameter 
11121         = cp_parser_parameter_declaration (parser, 
11122                                            /*template_parm_p=*/false,
11123                                            &parenthesized_p);
11124
11125       /* If a parse error occurred parsing the parameter declaration,
11126          then the entire parameter-declaration-list is erroneous.  */
11127       if (parameter == error_mark_node)
11128         {
11129           parameters = error_mark_node;
11130           break;
11131         }
11132       /* Add the new parameter to the list.  */
11133       TREE_CHAIN (parameter) = parameters;
11134       parameters = parameter;
11135
11136       /* Peek at the next token.  */
11137       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11138           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11139         /* The parameter-declaration-list is complete.  */
11140         break;
11141       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11142         {
11143           cp_token *token;
11144
11145           /* Peek at the next token.  */
11146           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11147           /* If it's an ellipsis, then the list is complete.  */
11148           if (token->type == CPP_ELLIPSIS)
11149             break;
11150           /* Otherwise, there must be more parameters.  Consume the
11151              `,'.  */
11152           cp_lexer_consume_token (parser->lexer);
11153           /* When parsing something like:
11154
11155                 int i(float f, double d)
11156                 
11157              we can tell after seeing the declaration for "f" that we
11158              are not looking at an initialization of a variable "i",
11159              but rather at the declaration of a function "i".  
11160
11161              Due to the fact that the parsing of template arguments
11162              (as specified to a template-id) requires backtracking we
11163              cannot use this technique when inside a template argument
11164              list.  */
11165           if (!parser->in_template_argument_list_p
11166               && !parser->in_type_id_in_expr_p
11167               && cp_parser_parsing_tentatively (parser)
11168               && !cp_parser_committed_to_tentative_parse (parser)
11169               /* However, a parameter-declaration of the form
11170                  "foat(f)" (which is a valid declaration of a
11171                  parameter "f") can also be interpreted as an
11172                  expression (the conversion of "f" to "float").  */
11173               && !parenthesized_p)
11174             cp_parser_commit_to_tentative_parse (parser);
11175         }
11176       else
11177         {
11178           cp_parser_error (parser, "expected `,' or `...'");
11179           if (!cp_parser_parsing_tentatively (parser)
11180               || cp_parser_committed_to_tentative_parse (parser))
11181             cp_parser_skip_to_closing_parenthesis (parser, 
11182                                                    /*recovering=*/true,
11183                                                    /*or_comma=*/false,
11184                                                    /*consume_paren=*/false);
11185           break;
11186         }
11187     }
11188
11189   /* We built up the list in reverse order; straighten it out now.  */
11190   return nreverse (parameters);
11191 }
11192
11193 /* Parse a parameter declaration.
11194
11195    parameter-declaration:
11196      decl-specifier-seq declarator
11197      decl-specifier-seq declarator = assignment-expression
11198      decl-specifier-seq abstract-declarator [opt]
11199      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11200
11201    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11202    declares a template parameter.  (In that case, a non-nested `>'
11203    token encountered during the parsing of the assignment-expression
11204    is not interpreted as a greater-than operator.)
11205
11206    Returns a TREE_LIST representing the parameter-declaration.  The
11207    TREE_PURPOSE is the default argument expression, or NULL_TREE if
11208    there is no default argument.  The TREE_VALUE is a representation
11209    of the decl-specifier-seq and declarator.  In particular, the
11210    TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11211    decl-specifier-seq and whose TREE_VALUE represents the declarator.
11212    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11213    the declarator is of the form "(p)".  */
11214
11215 static tree
11216 cp_parser_parameter_declaration (cp_parser *parser, 
11217                                  bool template_parm_p,
11218                                  bool *parenthesized_p)
11219 {
11220   int declares_class_or_enum;
11221   bool greater_than_is_operator_p;
11222   tree decl_specifiers;
11223   tree attributes;
11224   tree declarator;
11225   tree default_argument;
11226   tree parameter;
11227   cp_token *token;
11228   const char *saved_message;
11229
11230   /* In a template parameter, `>' is not an operator.
11231
11232      [temp.param]
11233
11234      When parsing a default template-argument for a non-type
11235      template-parameter, the first non-nested `>' is taken as the end
11236      of the template parameter-list rather than a greater-than
11237      operator.  */
11238   greater_than_is_operator_p = !template_parm_p;
11239
11240   /* Type definitions may not appear in parameter types.  */
11241   saved_message = parser->type_definition_forbidden_message;
11242   parser->type_definition_forbidden_message 
11243     = "types may not be defined in parameter types";
11244
11245   /* Parse the declaration-specifiers.  */
11246   decl_specifiers 
11247     = cp_parser_decl_specifier_seq (parser,
11248                                     CP_PARSER_FLAGS_NONE,
11249                                     &attributes,
11250                                     &declares_class_or_enum);
11251   /* If an error occurred, there's no reason to attempt to parse the
11252      rest of the declaration.  */
11253   if (cp_parser_error_occurred (parser))
11254     {
11255       parser->type_definition_forbidden_message = saved_message;
11256       return error_mark_node;
11257     }
11258
11259   /* Peek at the next token.  */
11260   token = cp_lexer_peek_token (parser->lexer);
11261   /* If the next token is a `)', `,', `=', `>', or `...', then there
11262      is no declarator.  */
11263   if (token->type == CPP_CLOSE_PAREN 
11264       || token->type == CPP_COMMA
11265       || token->type == CPP_EQ
11266       || token->type == CPP_ELLIPSIS
11267       || token->type == CPP_GREATER)
11268     {
11269       declarator = NULL_TREE;
11270       if (parenthesized_p)
11271         *parenthesized_p = false;
11272     }
11273   /* Otherwise, there should be a declarator.  */
11274   else
11275     {
11276       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11277       parser->default_arg_ok_p = false;
11278   
11279       /* After seeing a decl-specifier-seq, if the next token is not a
11280          "(", there is no possibility that the code is a valid
11281          expression.  Therefore, if parsing tentatively, we commit at
11282          this point.  */
11283       if (!parser->in_template_argument_list_p
11284           /* In an expression context, having seen:
11285
11286                (int((char ...
11287
11288              we cannot be sure whether we are looking at a
11289              function-type (taking a "char" as a parameter) or a cast
11290              of some object of type "char" to "int".  */
11291           && !parser->in_type_id_in_expr_p
11292           && cp_parser_parsing_tentatively (parser)
11293           && !cp_parser_committed_to_tentative_parse (parser)
11294           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11295         cp_parser_commit_to_tentative_parse (parser);
11296       /* Parse the declarator.  */
11297       declarator = cp_parser_declarator (parser,
11298                                          CP_PARSER_DECLARATOR_EITHER,
11299                                          /*ctor_dtor_or_conv_p=*/NULL,
11300                                          parenthesized_p,
11301                                          /*member_p=*/false);
11302       parser->default_arg_ok_p = saved_default_arg_ok_p;
11303       /* After the declarator, allow more attributes.  */
11304       attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11305     }
11306
11307   /* The restriction on defining new types applies only to the type
11308      of the parameter, not to the default argument.  */
11309   parser->type_definition_forbidden_message = saved_message;
11310
11311   /* If the next token is `=', then process a default argument.  */
11312   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11313     {
11314       bool saved_greater_than_is_operator_p;
11315       /* Consume the `='.  */
11316       cp_lexer_consume_token (parser->lexer);
11317
11318       /* If we are defining a class, then the tokens that make up the
11319          default argument must be saved and processed later.  */
11320       if (!template_parm_p && at_class_scope_p () 
11321           && TYPE_BEING_DEFINED (current_class_type))
11322         {
11323           unsigned depth = 0;
11324
11325           /* Create a DEFAULT_ARG to represented the unparsed default
11326              argument.  */
11327           default_argument = make_node (DEFAULT_ARG);
11328           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11329
11330           /* Add tokens until we have processed the entire default
11331              argument.  */
11332           while (true)
11333             {
11334               bool done = false;
11335               cp_token *token;
11336
11337               /* Peek at the next token.  */
11338               token = cp_lexer_peek_token (parser->lexer);
11339               /* What we do depends on what token we have.  */
11340               switch (token->type)
11341                 {
11342                   /* In valid code, a default argument must be
11343                      immediately followed by a `,' `)', or `...'.  */
11344                 case CPP_COMMA:
11345                 case CPP_CLOSE_PAREN:
11346                 case CPP_ELLIPSIS:
11347                   /* If we run into a non-nested `;', `}', or `]',
11348                      then the code is invalid -- but the default
11349                      argument is certainly over.  */
11350                 case CPP_SEMICOLON:
11351                 case CPP_CLOSE_BRACE:
11352                 case CPP_CLOSE_SQUARE:
11353                   if (depth == 0)
11354                     done = true;
11355                   /* Update DEPTH, if necessary.  */
11356                   else if (token->type == CPP_CLOSE_PAREN
11357                            || token->type == CPP_CLOSE_BRACE
11358                            || token->type == CPP_CLOSE_SQUARE)
11359                     --depth;
11360                   break;
11361
11362                 case CPP_OPEN_PAREN:
11363                 case CPP_OPEN_SQUARE:
11364                 case CPP_OPEN_BRACE:
11365                   ++depth;
11366                   break;
11367
11368                 case CPP_GREATER:
11369                   /* If we see a non-nested `>', and `>' is not an
11370                      operator, then it marks the end of the default
11371                      argument.  */
11372                   if (!depth && !greater_than_is_operator_p)
11373                     done = true;
11374                   break;
11375
11376                   /* If we run out of tokens, issue an error message.  */
11377                 case CPP_EOF:
11378                   error ("file ends in default argument");
11379                   done = true;
11380                   break;
11381
11382                 case CPP_NAME:
11383                 case CPP_SCOPE:
11384                   /* In these cases, we should look for template-ids.
11385                      For example, if the default argument is 
11386                      `X<int, double>()', we need to do name lookup to
11387                      figure out whether or not `X' is a template; if
11388                      so, the `,' does not end the default argument.
11389
11390                      That is not yet done.  */
11391                   break;
11392
11393                 default:
11394                   break;
11395                 }
11396
11397               /* If we've reached the end, stop.  */
11398               if (done)
11399                 break;
11400               
11401               /* Add the token to the token block.  */
11402               token = cp_lexer_consume_token (parser->lexer);
11403               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11404                                          token);
11405             }
11406         }
11407       /* Outside of a class definition, we can just parse the
11408          assignment-expression.  */
11409       else
11410         {
11411           bool saved_local_variables_forbidden_p;
11412
11413           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11414              set correctly.  */
11415           saved_greater_than_is_operator_p 
11416             = parser->greater_than_is_operator_p;
11417           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11418           /* Local variable names (and the `this' keyword) may not
11419              appear in a default argument.  */
11420           saved_local_variables_forbidden_p 
11421             = parser->local_variables_forbidden_p;
11422           parser->local_variables_forbidden_p = true;
11423           /* Parse the assignment-expression.  */
11424           default_argument = cp_parser_assignment_expression (parser);
11425           /* Restore saved state.  */
11426           parser->greater_than_is_operator_p 
11427             = saved_greater_than_is_operator_p;
11428           parser->local_variables_forbidden_p 
11429             = saved_local_variables_forbidden_p; 
11430         }
11431       if (!parser->default_arg_ok_p)
11432         {
11433           if (!flag_pedantic_errors)
11434             warning ("deprecated use of default argument for parameter of non-function");
11435           else
11436             {
11437               error ("default arguments are only permitted for function parameters");
11438               default_argument = NULL_TREE;
11439             }
11440         }
11441     }
11442   else
11443     default_argument = NULL_TREE;
11444   
11445   /* Create the representation of the parameter.  */
11446   if (attributes)
11447     decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11448   parameter = build_tree_list (default_argument, 
11449                                build_tree_list (decl_specifiers,
11450                                                 declarator));
11451
11452   return parameter;
11453 }
11454
11455 /* Parse a function-body.
11456
11457    function-body:
11458      compound_statement  */
11459
11460 static void
11461 cp_parser_function_body (cp_parser *parser)
11462 {
11463   cp_parser_compound_statement (parser, false);
11464 }
11465
11466 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11467    true if a ctor-initializer was present.  */
11468
11469 static bool
11470 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11471 {
11472   tree body;
11473   bool ctor_initializer_p;
11474
11475   /* Begin the function body.  */
11476   body = begin_function_body ();
11477   /* Parse the optional ctor-initializer.  */
11478   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11479   /* Parse the function-body.  */
11480   cp_parser_function_body (parser);
11481   /* Finish the function body.  */
11482   finish_function_body (body);
11483
11484   return ctor_initializer_p;
11485 }
11486
11487 /* Parse an initializer.
11488
11489    initializer:
11490      = initializer-clause
11491      ( expression-list )  
11492
11493    Returns a expression representing the initializer.  If no
11494    initializer is present, NULL_TREE is returned.  
11495
11496    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11497    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11498    set to FALSE if there is no initializer present.  If there is an
11499    initializer, and it is not a constant-expression, *NON_CONSTANT_P
11500    is set to true; otherwise it is set to false.  */
11501
11502 static tree
11503 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11504                        bool* non_constant_p)
11505 {
11506   cp_token *token;
11507   tree init;
11508
11509   /* Peek at the next token.  */
11510   token = cp_lexer_peek_token (parser->lexer);
11511
11512   /* Let our caller know whether or not this initializer was
11513      parenthesized.  */
11514   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11515   /* Assume that the initializer is constant.  */
11516   *non_constant_p = false;
11517
11518   if (token->type == CPP_EQ)
11519     {
11520       /* Consume the `='.  */
11521       cp_lexer_consume_token (parser->lexer);
11522       /* Parse the initializer-clause.  */
11523       init = cp_parser_initializer_clause (parser, non_constant_p);
11524     }
11525   else if (token->type == CPP_OPEN_PAREN)
11526     init = cp_parser_parenthesized_expression_list (parser, false,
11527                                                     non_constant_p);
11528   else
11529     {
11530       /* Anything else is an error.  */
11531       cp_parser_error (parser, "expected initializer");
11532       init = error_mark_node;
11533     }
11534
11535   return init;
11536 }
11537
11538 /* Parse an initializer-clause.  
11539
11540    initializer-clause:
11541      assignment-expression
11542      { initializer-list , [opt] }
11543      { }
11544
11545    Returns an expression representing the initializer.  
11546
11547    If the `assignment-expression' production is used the value
11548    returned is simply a representation for the expression.  
11549
11550    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11551    the elements of the initializer-list (or NULL_TREE, if the last
11552    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11553    NULL_TREE.  There is no way to detect whether or not the optional
11554    trailing `,' was provided.  NON_CONSTANT_P is as for
11555    cp_parser_initializer.  */
11556
11557 static tree
11558 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11559 {
11560   tree initializer = NULL_TREE;
11561
11562   /* Assume the expression is constant.  */
11563   *non_constant_p = false;
11564
11565   /* If it is not a `{', then we are looking at an
11566      assignment-expression.  */
11567   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11568     {
11569       /* Speed up common initializers (simply a literal).  */
11570       cp_token* token = cp_lexer_peek_token (parser->lexer);
11571       cp_token* token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11572
11573       if (token2->type == CPP_COMMA)
11574         switch (token->type)
11575           {
11576           case CPP_CHAR:
11577           case CPP_WCHAR:
11578           case CPP_NUMBER:
11579             token = cp_lexer_consume_token (parser->lexer);
11580             initializer = token->value;
11581             break;
11582
11583           case CPP_STRING:
11584           case CPP_WSTRING:
11585             token = cp_lexer_consume_token (parser->lexer);
11586             if (TREE_CHAIN (token->value))
11587               initializer = TREE_CHAIN (token->value);
11588             else
11589               initializer = token->value;
11590             break;
11591
11592           default:
11593             break;
11594           }
11595
11596       /* Otherwise, fall back to the generic assignment expression.  */
11597       if (!initializer)
11598         {
11599           initializer
11600             = cp_parser_constant_expression (parser,
11601                                             /*allow_non_constant_p=*/true,
11602                                             non_constant_p);
11603           if (!*non_constant_p)
11604             initializer = fold_non_dependent_expr (initializer);
11605         }
11606     }
11607   else
11608     {
11609       /* Consume the `{' token.  */
11610       cp_lexer_consume_token (parser->lexer);
11611       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11612       initializer = make_node (CONSTRUCTOR);
11613       /* Mark it with TREE_HAS_CONSTRUCTOR.  This should not be
11614          necessary, but check_initializer depends upon it, for 
11615          now.  */
11616       TREE_HAS_CONSTRUCTOR (initializer) = 1;
11617       /* If it's not a `}', then there is a non-trivial initializer.  */
11618       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11619         {
11620           /* Parse the initializer list.  */
11621           CONSTRUCTOR_ELTS (initializer)
11622             = cp_parser_initializer_list (parser, non_constant_p);
11623           /* A trailing `,' token is allowed.  */
11624           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11625             cp_lexer_consume_token (parser->lexer);
11626         }
11627       /* Now, there should be a trailing `}'.  */
11628       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11629     }
11630
11631   return initializer;
11632 }
11633
11634 /* Parse an initializer-list.
11635
11636    initializer-list:
11637      initializer-clause
11638      initializer-list , initializer-clause
11639
11640    GNU Extension:
11641    
11642    initializer-list:
11643      identifier : initializer-clause
11644      initializer-list, identifier : initializer-clause
11645
11646    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
11647    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
11648    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
11649    as for cp_parser_initializer.  */
11650
11651 static tree
11652 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11653 {
11654   tree initializers = NULL_TREE;
11655
11656   /* Assume all of the expressions are constant.  */
11657   *non_constant_p = false;
11658
11659   /* Parse the rest of the list.  */
11660   while (true)
11661     {
11662       cp_token *token;
11663       tree identifier;
11664       tree initializer;
11665       bool clause_non_constant_p;
11666
11667       /* If the next token is an identifier and the following one is a
11668          colon, we are looking at the GNU designated-initializer
11669          syntax.  */
11670       if (cp_parser_allow_gnu_extensions_p (parser)
11671           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11672           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11673         {
11674           /* Consume the identifier.  */
11675           identifier = cp_lexer_consume_token (parser->lexer)->value;
11676           /* Consume the `:'.  */
11677           cp_lexer_consume_token (parser->lexer);
11678         }
11679       else
11680         identifier = NULL_TREE;
11681
11682       /* Parse the initializer.  */
11683       initializer = cp_parser_initializer_clause (parser, 
11684                                                   &clause_non_constant_p);
11685       /* If any clause is non-constant, so is the entire initializer.  */
11686       if (clause_non_constant_p)
11687         *non_constant_p = true;
11688       /* Add it to the list.  */
11689       initializers = tree_cons (identifier, initializer, initializers);
11690
11691       /* If the next token is not a comma, we have reached the end of
11692          the list.  */
11693       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11694         break;
11695
11696       /* Peek at the next token.  */
11697       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11698       /* If the next token is a `}', then we're still done.  An
11699          initializer-clause can have a trailing `,' after the
11700          initializer-list and before the closing `}'.  */
11701       if (token->type == CPP_CLOSE_BRACE)
11702         break;
11703
11704       /* Consume the `,' token.  */
11705       cp_lexer_consume_token (parser->lexer);
11706     }
11707
11708   /* The initializers were built up in reverse order, so we need to
11709      reverse them now.  */
11710   return nreverse (initializers);
11711 }
11712
11713 /* Classes [gram.class] */
11714
11715 /* Parse a class-name.
11716
11717    class-name:
11718      identifier
11719      template-id
11720
11721    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11722    to indicate that names looked up in dependent types should be
11723    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
11724    keyword has been used to indicate that the name that appears next
11725    is a template.  TYPE_P is true iff the next name should be treated
11726    as class-name, even if it is declared to be some other kind of name
11727    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11728    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
11729    being defined in a class-head.
11730
11731    Returns the TYPE_DECL representing the class.  */
11732
11733 static tree
11734 cp_parser_class_name (cp_parser *parser, 
11735                       bool typename_keyword_p, 
11736                       bool template_keyword_p, 
11737                       bool type_p,
11738                       bool check_dependency_p,
11739                       bool class_head_p,
11740                       bool is_declaration)
11741 {
11742   tree decl;
11743   tree scope;
11744   bool typename_p;
11745   cp_token *token;
11746
11747   /* All class-names start with an identifier.  */
11748   token = cp_lexer_peek_token (parser->lexer);
11749   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11750     {
11751       cp_parser_error (parser, "expected class-name");
11752       return error_mark_node;
11753     }
11754     
11755   /* PARSER->SCOPE can be cleared when parsing the template-arguments
11756      to a template-id, so we save it here.  */
11757   scope = parser->scope;
11758   if (scope == error_mark_node)
11759     return error_mark_node;
11760   
11761   /* Any name names a type if we're following the `typename' keyword
11762      in a qualified name where the enclosing scope is type-dependent.  */
11763   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11764                 && dependent_type_p (scope));
11765   /* Handle the common case (an identifier, but not a template-id)
11766      efficiently.  */
11767   if (token->type == CPP_NAME 
11768       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11769     {
11770       tree identifier;
11771
11772       /* Look for the identifier.  */
11773       identifier = cp_parser_identifier (parser);
11774       /* If the next token isn't an identifier, we are certainly not
11775          looking at a class-name.  */
11776       if (identifier == error_mark_node)
11777         decl = error_mark_node;
11778       /* If we know this is a type-name, there's no need to look it
11779          up.  */
11780       else if (typename_p)
11781         decl = identifier;
11782       else
11783         {
11784           /* If the next token is a `::', then the name must be a type
11785              name.
11786
11787              [basic.lookup.qual]
11788
11789              During the lookup for a name preceding the :: scope
11790              resolution operator, object, function, and enumerator
11791              names are ignored.  */
11792           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11793             type_p = true;
11794           /* Look up the name.  */
11795           decl = cp_parser_lookup_name (parser, identifier, 
11796                                         type_p,
11797                                         /*is_template=*/false,
11798                                         /*is_namespace=*/false,
11799                                         check_dependency_p);
11800         }
11801     }
11802   else
11803     {
11804       /* Try a template-id.  */
11805       decl = cp_parser_template_id (parser, template_keyword_p,
11806                                     check_dependency_p,
11807                                     is_declaration);
11808       if (decl == error_mark_node)
11809         return error_mark_node;
11810     }
11811
11812   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11813
11814   /* If this is a typename, create a TYPENAME_TYPE.  */
11815   if (typename_p && decl != error_mark_node)
11816     {
11817       decl = make_typename_type (scope, decl, /*complain=*/1);
11818       if (decl != error_mark_node)
11819         decl = TYPE_NAME (decl);
11820     }
11821
11822   /* Check to see that it is really the name of a class.  */
11823   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 
11824       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11825       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11826     /* Situations like this:
11827
11828          template <typename T> struct A {
11829            typename T::template X<int>::I i; 
11830          };
11831
11832        are problematic.  Is `T::template X<int>' a class-name?  The
11833        standard does not seem to be definitive, but there is no other
11834        valid interpretation of the following `::'.  Therefore, those
11835        names are considered class-names.  */
11836     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11837   else if (decl == error_mark_node
11838            || TREE_CODE (decl) != TYPE_DECL
11839            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11840     {
11841       cp_parser_error (parser, "expected class-name");
11842       return error_mark_node;
11843     }
11844
11845   return decl;
11846 }
11847
11848 /* Parse a class-specifier.
11849
11850    class-specifier:
11851      class-head { member-specification [opt] }
11852
11853    Returns the TREE_TYPE representing the class.  */
11854
11855 static tree
11856 cp_parser_class_specifier (cp_parser* parser)
11857 {
11858   cp_token *token;
11859   tree type;
11860   tree attributes;
11861   int has_trailing_semicolon;
11862   bool nested_name_specifier_p;
11863   unsigned saved_num_template_parameter_lists;
11864   bool pop_p = false;
11865   tree scope = NULL_TREE;
11866
11867   push_deferring_access_checks (dk_no_deferred);
11868
11869   /* Parse the class-head.  */
11870   type = cp_parser_class_head (parser,
11871                                &nested_name_specifier_p,
11872                                &attributes);
11873   /* If the class-head was a semantic disaster, skip the entire body
11874      of the class.  */
11875   if (!type)
11876     {
11877       cp_parser_skip_to_end_of_block_or_statement (parser);
11878       pop_deferring_access_checks ();
11879       return error_mark_node;
11880     }
11881
11882   /* Look for the `{'.  */
11883   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11884     {
11885       pop_deferring_access_checks ();
11886       return error_mark_node;
11887     }
11888
11889   /* Issue an error message if type-definitions are forbidden here.  */
11890   cp_parser_check_type_definition (parser);
11891   /* Remember that we are defining one more class.  */
11892   ++parser->num_classes_being_defined;
11893   /* Inside the class, surrounding template-parameter-lists do not
11894      apply.  */
11895   saved_num_template_parameter_lists 
11896     = parser->num_template_parameter_lists; 
11897   parser->num_template_parameter_lists = 0;
11898
11899   /* Start the class.  */
11900   if (nested_name_specifier_p)
11901     {
11902       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
11903       pop_p = push_scope (scope);
11904     }
11905   type = begin_class_definition (type);
11906   if (type == error_mark_node)
11907     /* If the type is erroneous, skip the entire body of the class.  */
11908     cp_parser_skip_to_closing_brace (parser);
11909   else
11910     /* Parse the member-specification.  */
11911     cp_parser_member_specification_opt (parser);
11912   /* Look for the trailing `}'.  */
11913   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11914   /* We get better error messages by noticing a common problem: a
11915      missing trailing `;'.  */
11916   token = cp_lexer_peek_token (parser->lexer);
11917   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11918   /* Look for trailing attributes to apply to this class.  */
11919   if (cp_parser_allow_gnu_extensions_p (parser))
11920     {
11921       tree sub_attr = cp_parser_attributes_opt (parser);
11922       attributes = chainon (attributes, sub_attr);
11923     }
11924   if (type != error_mark_node)
11925     type = finish_struct (type, attributes);
11926   if (pop_p)
11927     pop_scope (scope);
11928   /* If this class is not itself within the scope of another class,
11929      then we need to parse the bodies of all of the queued function
11930      definitions.  Note that the queued functions defined in a class
11931      are not always processed immediately following the
11932      class-specifier for that class.  Consider:
11933
11934        struct A {
11935          struct B { void f() { sizeof (A); } };
11936        };
11937
11938      If `f' were processed before the processing of `A' were
11939      completed, there would be no way to compute the size of `A'.
11940      Note that the nesting we are interested in here is lexical --
11941      not the semantic nesting given by TYPE_CONTEXT.  In particular,
11942      for:
11943
11944        struct A { struct B; };
11945        struct A::B { void f() { } };
11946
11947      there is no need to delay the parsing of `A::B::f'.  */
11948   if (--parser->num_classes_being_defined == 0) 
11949     {
11950       tree queue_entry;
11951       tree fn;
11952
11953       /* In a first pass, parse default arguments to the functions.
11954          Then, in a second pass, parse the bodies of the functions.
11955          This two-phased approach handles cases like:
11956          
11957             struct S { 
11958               void f() { g(); } 
11959               void g(int i = 3);
11960             };
11961
11962          */
11963       for (TREE_PURPOSE (parser->unparsed_functions_queues)
11964              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11965            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11966            TREE_PURPOSE (parser->unparsed_functions_queues)
11967              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11968         {
11969           fn = TREE_VALUE (queue_entry);
11970           /* Make sure that any template parameters are in scope.  */
11971           maybe_begin_member_template_processing (fn);
11972           /* If there are default arguments that have not yet been processed,
11973              take care of them now.  */
11974           cp_parser_late_parsing_default_args (parser, fn);
11975           /* Remove any template parameters from the symbol table.  */
11976           maybe_end_member_template_processing ();
11977         }
11978       /* Now parse the body of the functions.  */
11979       for (TREE_VALUE (parser->unparsed_functions_queues)
11980              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11981            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11982            TREE_VALUE (parser->unparsed_functions_queues)
11983              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11984         {
11985           /* Figure out which function we need to process.  */
11986           fn = TREE_VALUE (queue_entry);
11987
11988           /* A hack to prevent garbage collection.  */
11989           function_depth++;
11990
11991           /* Parse the function.  */
11992           cp_parser_late_parsing_for_member (parser, fn);
11993           function_depth--;
11994         }
11995
11996     }
11997
11998   /* Put back any saved access checks.  */
11999   pop_deferring_access_checks ();
12000
12001   /* Restore the count of active template-parameter-lists.  */
12002   parser->num_template_parameter_lists
12003     = saved_num_template_parameter_lists;
12004
12005   return type;
12006 }
12007
12008 /* Parse a class-head.
12009
12010    class-head:
12011      class-key identifier [opt] base-clause [opt]
12012      class-key nested-name-specifier identifier base-clause [opt]
12013      class-key nested-name-specifier [opt] template-id 
12014        base-clause [opt]  
12015
12016    GNU Extensions:
12017      class-key attributes identifier [opt] base-clause [opt]
12018      class-key attributes nested-name-specifier identifier base-clause [opt]
12019      class-key attributes nested-name-specifier [opt] template-id 
12020        base-clause [opt]  
12021
12022    Returns the TYPE of the indicated class.  Sets
12023    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12024    involving a nested-name-specifier was used, and FALSE otherwise.
12025
12026    Returns NULL_TREE if the class-head is syntactically valid, but
12027    semantically invalid in a way that means we should skip the entire
12028    body of the class.  */
12029
12030 static tree
12031 cp_parser_class_head (cp_parser* parser, 
12032                       bool* nested_name_specifier_p,
12033                       tree *attributes_p)
12034 {
12035   cp_token *token;
12036   tree nested_name_specifier;
12037   enum tag_types class_key;
12038   tree id = NULL_TREE;
12039   tree type = NULL_TREE;
12040   tree attributes;
12041   bool template_id_p = false;
12042   bool qualified_p = false;
12043   bool invalid_nested_name_p = false;
12044   bool invalid_explicit_specialization_p = false;
12045   bool pop_p = false;
12046   unsigned num_templates;
12047
12048   /* Assume no nested-name-specifier will be present.  */
12049   *nested_name_specifier_p = false;
12050   /* Assume no template parameter lists will be used in defining the
12051      type.  */
12052   num_templates = 0;
12053
12054   /* Look for the class-key.  */
12055   class_key = cp_parser_class_key (parser);
12056   if (class_key == none_type)
12057     return error_mark_node;
12058
12059   /* Parse the attributes.  */
12060   attributes = cp_parser_attributes_opt (parser);
12061
12062   /* If the next token is `::', that is invalid -- but sometimes
12063      people do try to write:
12064
12065        struct ::S {};  
12066
12067      Handle this gracefully by accepting the extra qualifier, and then
12068      issuing an error about it later if this really is a
12069      class-head.  If it turns out just to be an elaborated type
12070      specifier, remain silent.  */
12071   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12072     qualified_p = true;
12073
12074   push_deferring_access_checks (dk_no_check);
12075
12076   /* Determine the name of the class.  Begin by looking for an
12077      optional nested-name-specifier.  */
12078   nested_name_specifier 
12079     = cp_parser_nested_name_specifier_opt (parser,
12080                                            /*typename_keyword_p=*/false,
12081                                            /*check_dependency_p=*/false,
12082                                            /*type_p=*/false,
12083                                            /*is_declaration=*/false);
12084   /* If there was a nested-name-specifier, then there *must* be an
12085      identifier.  */
12086   if (nested_name_specifier)
12087     {
12088       /* Although the grammar says `identifier', it really means
12089          `class-name' or `template-name'.  You are only allowed to
12090          define a class that has already been declared with this
12091          syntax.  
12092
12093          The proposed resolution for Core Issue 180 says that whever
12094          you see `class T::X' you should treat `X' as a type-name.
12095          
12096          It is OK to define an inaccessible class; for example:
12097          
12098            class A { class B; };
12099            class A::B {};
12100          
12101          We do not know if we will see a class-name, or a
12102          template-name.  We look for a class-name first, in case the
12103          class-name is a template-id; if we looked for the
12104          template-name first we would stop after the template-name.  */
12105       cp_parser_parse_tentatively (parser);
12106       type = cp_parser_class_name (parser,
12107                                    /*typename_keyword_p=*/false,
12108                                    /*template_keyword_p=*/false,
12109                                    /*type_p=*/true,
12110                                    /*check_dependency_p=*/false,
12111                                    /*class_head_p=*/true,
12112                                    /*is_declaration=*/false);
12113       /* If that didn't work, ignore the nested-name-specifier.  */
12114       if (!cp_parser_parse_definitely (parser))
12115         {
12116           invalid_nested_name_p = true;
12117           id = cp_parser_identifier (parser);
12118           if (id == error_mark_node)
12119             id = NULL_TREE;
12120         }
12121       /* If we could not find a corresponding TYPE, treat this
12122          declaration like an unqualified declaration.  */
12123       if (type == error_mark_node)
12124         nested_name_specifier = NULL_TREE;
12125       /* Otherwise, count the number of templates used in TYPE and its
12126          containing scopes.  */
12127       else 
12128         {
12129           tree scope;
12130
12131           for (scope = TREE_TYPE (type); 
12132                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12133                scope = (TYPE_P (scope) 
12134                         ? TYPE_CONTEXT (scope)
12135                         : DECL_CONTEXT (scope))) 
12136             if (TYPE_P (scope) 
12137                 && CLASS_TYPE_P (scope)
12138                 && CLASSTYPE_TEMPLATE_INFO (scope)
12139                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12140                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12141               ++num_templates;
12142         }
12143     }
12144   /* Otherwise, the identifier is optional.  */
12145   else
12146     {
12147       /* We don't know whether what comes next is a template-id,
12148          an identifier, or nothing at all.  */
12149       cp_parser_parse_tentatively (parser);
12150       /* Check for a template-id.  */
12151       id = cp_parser_template_id (parser, 
12152                                   /*template_keyword_p=*/false,
12153                                   /*check_dependency_p=*/true,
12154                                   /*is_declaration=*/true);
12155       /* If that didn't work, it could still be an identifier.  */
12156       if (!cp_parser_parse_definitely (parser))
12157         {
12158           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12159             id = cp_parser_identifier (parser);
12160           else
12161             id = NULL_TREE;
12162         }
12163       else
12164         {
12165           template_id_p = true;
12166           ++num_templates;
12167         }
12168     }
12169
12170   pop_deferring_access_checks ();
12171
12172   if (id)
12173     cp_parser_check_for_invalid_template_id (parser, id);
12174
12175   /* If it's not a `:' or a `{' then we can't really be looking at a
12176      class-head, since a class-head only appears as part of a
12177      class-specifier.  We have to detect this situation before calling
12178      xref_tag, since that has irreversible side-effects.  */
12179   if (!cp_parser_next_token_starts_class_definition_p (parser))
12180     {
12181       cp_parser_error (parser, "expected `{' or `:'");
12182       return error_mark_node;
12183     }
12184
12185   /* At this point, we're going ahead with the class-specifier, even
12186      if some other problem occurs.  */
12187   cp_parser_commit_to_tentative_parse (parser);
12188   /* Issue the error about the overly-qualified name now.  */
12189   if (qualified_p)
12190     cp_parser_error (parser,
12191                      "global qualification of class name is invalid");
12192   else if (invalid_nested_name_p)
12193     cp_parser_error (parser,
12194                      "qualified name does not name a class");
12195   else if (nested_name_specifier)
12196     {
12197       tree scope;
12198
12199       /* Reject typedef-names in class heads.  */
12200       if (!DECL_IMPLICIT_TYPEDEF_P (type))
12201         {
12202           error ("invalid class name in declaration of `%D'", type);
12203           type = NULL_TREE;
12204           goto done;
12205         }
12206
12207       /* Figure out in what scope the declaration is being placed.  */
12208       scope = current_scope ();
12209       if (!scope)
12210         scope = current_namespace;
12211       /* If that scope does not contain the scope in which the
12212          class was originally declared, the program is invalid.  */
12213       if (scope && !is_ancestor (scope, nested_name_specifier))
12214         {
12215           error ("declaration of `%D' in `%D' which does not "
12216                  "enclose `%D'", type, scope, nested_name_specifier);
12217           type = NULL_TREE;
12218           goto done;
12219         }
12220       /* [dcl.meaning]
12221
12222          A declarator-id shall not be qualified exception of the
12223          definition of a ... nested class outside of its class
12224          ... [or] a the definition or explicit instantiation of a
12225          class member of a namespace outside of its namespace.  */
12226       if (scope == nested_name_specifier)
12227         {
12228           pedwarn ("extra qualification ignored");
12229           nested_name_specifier = NULL_TREE;
12230           num_templates = 0;
12231         }
12232     }
12233   /* An explicit-specialization must be preceded by "template <>".  If
12234      it is not, try to recover gracefully.  */
12235   if (at_namespace_scope_p () 
12236       && parser->num_template_parameter_lists == 0
12237       && template_id_p)
12238     {
12239       error ("an explicit specialization must be preceded by 'template <>'");
12240       invalid_explicit_specialization_p = true;
12241       /* Take the same action that would have been taken by
12242          cp_parser_explicit_specialization.  */
12243       ++parser->num_template_parameter_lists;
12244       begin_specialization ();
12245     }
12246   /* There must be no "return" statements between this point and the
12247      end of this function; set "type "to the correct return value and
12248      use "goto done;" to return.  */
12249   /* Make sure that the right number of template parameters were
12250      present.  */
12251   if (!cp_parser_check_template_parameters (parser, num_templates))
12252     {
12253       /* If something went wrong, there is no point in even trying to
12254          process the class-definition.  */
12255       type = NULL_TREE;
12256       goto done;
12257     }
12258
12259   /* Look up the type.  */
12260   if (template_id_p)
12261     {
12262       type = TREE_TYPE (id);
12263       maybe_process_partial_specialization (type);
12264     }
12265   else if (!nested_name_specifier)
12266     {
12267       /* If the class was unnamed, create a dummy name.  */
12268       if (!id)
12269         id = make_anon_name ();
12270       type = xref_tag (class_key, id, /*globalize=*/false,
12271                        parser->num_template_parameter_lists);
12272     }
12273   else
12274     {
12275       tree class_type;
12276       bool pop_p = false;
12277
12278       /* Given:
12279
12280             template <typename T> struct S { struct T };
12281             template <typename T> struct S<T>::T { };
12282
12283          we will get a TYPENAME_TYPE when processing the definition of
12284          `S::T'.  We need to resolve it to the actual type before we
12285          try to define it.  */
12286       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12287         {
12288           class_type = resolve_typename_type (TREE_TYPE (type),
12289                                               /*only_current_p=*/false);
12290           if (class_type != error_mark_node)
12291             type = TYPE_NAME (class_type);
12292           else
12293             {
12294               cp_parser_error (parser, "could not resolve typename type");
12295               type = error_mark_node;
12296             }
12297         }
12298
12299       maybe_process_partial_specialization (TREE_TYPE (type));
12300       class_type = current_class_type;
12301       /* Enter the scope indicated by the nested-name-specifier.  */
12302       if (nested_name_specifier)
12303         pop_p = push_scope (nested_name_specifier);
12304       /* Get the canonical version of this type.  */
12305       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12306       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12307           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12308         type = push_template_decl (type);
12309       type = TREE_TYPE (type);
12310       if (nested_name_specifier)
12311         {
12312           *nested_name_specifier_p = true;
12313           if (pop_p)
12314             pop_scope (nested_name_specifier);
12315         }
12316     }
12317   /* Indicate whether this class was declared as a `class' or as a
12318      `struct'.  */
12319   if (TREE_CODE (type) == RECORD_TYPE)
12320     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12321   cp_parser_check_class_key (class_key, type);
12322
12323   /* Enter the scope containing the class; the names of base classes
12324      should be looked up in that context.  For example, given:
12325
12326        struct A { struct B {}; struct C; };
12327        struct A::C : B {};
12328
12329      is valid.  */
12330   if (nested_name_specifier)
12331     pop_p = push_scope (nested_name_specifier);
12332   /* Now, look for the base-clause.  */
12333   token = cp_lexer_peek_token (parser->lexer);
12334   if (token->type == CPP_COLON)
12335     {
12336       tree bases;
12337
12338       /* Get the list of base-classes.  */
12339       bases = cp_parser_base_clause (parser);
12340       /* Process them.  */
12341       xref_basetypes (type, bases);
12342     }
12343   /* Leave the scope given by the nested-name-specifier.  We will
12344      enter the class scope itself while processing the members.  */
12345   if (pop_p)
12346     pop_scope (nested_name_specifier);
12347
12348  done:
12349   if (invalid_explicit_specialization_p)
12350     {
12351       end_specialization ();
12352       --parser->num_template_parameter_lists;
12353     }
12354   *attributes_p = attributes;
12355   return type;
12356 }
12357
12358 /* Parse a class-key.
12359
12360    class-key:
12361      class
12362      struct
12363      union
12364
12365    Returns the kind of class-key specified, or none_type to indicate
12366    error.  */
12367
12368 static enum tag_types
12369 cp_parser_class_key (cp_parser* parser)
12370 {
12371   cp_token *token;
12372   enum tag_types tag_type;
12373
12374   /* Look for the class-key.  */
12375   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12376   if (!token)
12377     return none_type;
12378
12379   /* Check to see if the TOKEN is a class-key.  */
12380   tag_type = cp_parser_token_is_class_key (token);
12381   if (!tag_type)
12382     cp_parser_error (parser, "expected class-key");
12383   return tag_type;
12384 }
12385
12386 /* Parse an (optional) member-specification.
12387
12388    member-specification:
12389      member-declaration member-specification [opt]
12390      access-specifier : member-specification [opt]  */
12391
12392 static void
12393 cp_parser_member_specification_opt (cp_parser* parser)
12394 {
12395   while (true)
12396     {
12397       cp_token *token;
12398       enum rid keyword;
12399
12400       /* Peek at the next token.  */
12401       token = cp_lexer_peek_token (parser->lexer);
12402       /* If it's a `}', or EOF then we've seen all the members.  */
12403       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12404         break;
12405
12406       /* See if this token is a keyword.  */
12407       keyword = token->keyword;
12408       switch (keyword)
12409         {
12410         case RID_PUBLIC:
12411         case RID_PROTECTED:
12412         case RID_PRIVATE:
12413           /* Consume the access-specifier.  */
12414           cp_lexer_consume_token (parser->lexer);
12415           /* Remember which access-specifier is active.  */
12416           current_access_specifier = token->value;
12417           /* Look for the `:'.  */
12418           cp_parser_require (parser, CPP_COLON, "`:'");
12419           break;
12420
12421         default:
12422           /* Otherwise, the next construction must be a
12423              member-declaration.  */
12424           cp_parser_member_declaration (parser);
12425         }
12426     }
12427 }
12428
12429 /* Parse a member-declaration.  
12430
12431    member-declaration:
12432      decl-specifier-seq [opt] member-declarator-list [opt] ;
12433      function-definition ; [opt]
12434      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12435      using-declaration
12436      template-declaration 
12437
12438    member-declarator-list:
12439      member-declarator
12440      member-declarator-list , member-declarator
12441
12442    member-declarator:
12443      declarator pure-specifier [opt] 
12444      declarator constant-initializer [opt]
12445      identifier [opt] : constant-expression 
12446
12447    GNU Extensions:
12448
12449    member-declaration:
12450      __extension__ member-declaration
12451
12452    member-declarator:
12453      declarator attributes [opt] pure-specifier [opt]
12454      declarator attributes [opt] constant-initializer [opt]
12455      identifier [opt] attributes [opt] : constant-expression  */
12456
12457 static void
12458 cp_parser_member_declaration (cp_parser* parser)
12459 {
12460   tree decl_specifiers;
12461   tree prefix_attributes;
12462   tree decl;
12463   int declares_class_or_enum;
12464   bool friend_p;
12465   cp_token *token;
12466   int saved_pedantic;
12467
12468   /* Check for the `__extension__' keyword.  */
12469   if (cp_parser_extension_opt (parser, &saved_pedantic))
12470     {
12471       /* Recurse.  */
12472       cp_parser_member_declaration (parser);
12473       /* Restore the old value of the PEDANTIC flag.  */
12474       pedantic = saved_pedantic;
12475
12476       return;
12477     }
12478
12479   /* Check for a template-declaration.  */
12480   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12481     {
12482       /* An explicit specialization here is an error condition, and we
12483          expect the specialization handler to detect and report this.  */
12484       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12485           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12486         cp_parser_explicit_specialization (parser);
12487       else
12488         cp_parser_template_declaration (parser, /*member_p=*/true);
12489
12490       return;
12491     }
12492
12493   /* Check for a using-declaration.  */
12494   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12495     {
12496       /* Parse the using-declaration.  */
12497       cp_parser_using_declaration (parser);
12498
12499       return;
12500     }
12501   
12502   /* Parse the decl-specifier-seq.  */
12503   decl_specifiers 
12504     = cp_parser_decl_specifier_seq (parser,
12505                                     CP_PARSER_FLAGS_OPTIONAL,
12506                                     &prefix_attributes,
12507                                     &declares_class_or_enum);
12508   /* Check for an invalid type-name.  */
12509   if (cp_parser_diagnose_invalid_type_name (parser))
12510     return;
12511   /* If there is no declarator, then the decl-specifier-seq should
12512      specify a type.  */
12513   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12514     {
12515       /* If there was no decl-specifier-seq, and the next token is a
12516          `;', then we have something like:
12517
12518            struct S { ; };
12519
12520          [class.mem]
12521
12522          Each member-declaration shall declare at least one member
12523          name of the class.  */
12524       if (!decl_specifiers)
12525         {
12526           if (pedantic)
12527             pedwarn ("extra semicolon");
12528         }
12529       else 
12530         {
12531           tree type;
12532           
12533           /* See if this declaration is a friend.  */
12534           friend_p = cp_parser_friend_p (decl_specifiers);
12535           /* If there were decl-specifiers, check to see if there was
12536              a class-declaration.  */
12537           type = check_tag_decl (decl_specifiers);
12538           /* Nested classes have already been added to the class, but
12539              a `friend' needs to be explicitly registered.  */
12540           if (friend_p)
12541             {
12542               /* If the `friend' keyword was present, the friend must
12543                  be introduced with a class-key.  */
12544                if (!declares_class_or_enum)
12545                  error ("a class-key must be used when declaring a friend");
12546                /* In this case:
12547
12548                     template <typename T> struct A { 
12549                       friend struct A<T>::B; 
12550                     };
12551  
12552                   A<T>::B will be represented by a TYPENAME_TYPE, and
12553                   therefore not recognized by check_tag_decl.  */
12554                if (!type)
12555                  {
12556                    tree specifier;
12557
12558                    for (specifier = decl_specifiers; 
12559                         specifier;
12560                         specifier = TREE_CHAIN (specifier))
12561                      {
12562                        tree s = TREE_VALUE (specifier);
12563
12564                        if (TREE_CODE (s) == IDENTIFIER_NODE)
12565                          get_global_value_if_present (s, &type);
12566                        if (TREE_CODE (s) == TYPE_DECL)
12567                          s = TREE_TYPE (s);
12568                        if (TYPE_P (s))
12569                          {
12570                            type = s;
12571                            break;
12572                          }
12573                      }
12574                  }
12575                if (!type || !TYPE_P (type))
12576                  error ("friend declaration does not name a class or "
12577                         "function");
12578                else
12579                  make_friend_class (current_class_type, type,
12580                                     /*complain=*/true);
12581             }
12582           /* If there is no TYPE, an error message will already have
12583              been issued.  */
12584           else if (!type)
12585             ;
12586           /* An anonymous aggregate has to be handled specially; such
12587              a declaration really declares a data member (with a
12588              particular type), as opposed to a nested class.  */
12589           else if (ANON_AGGR_TYPE_P (type))
12590             {
12591               /* Remove constructors and such from TYPE, now that we
12592                  know it is an anonymous aggregate.  */
12593               fixup_anonymous_aggr (type);
12594               /* And make the corresponding data member.  */
12595               decl = build_decl (FIELD_DECL, NULL_TREE, type);
12596               /* Add it to the class.  */
12597               finish_member_declaration (decl);
12598             }
12599           else
12600             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12601         }
12602     }
12603   else
12604     {
12605       /* See if these declarations will be friends.  */
12606       friend_p = cp_parser_friend_p (decl_specifiers);
12607
12608       /* Keep going until we hit the `;' at the end of the 
12609          declaration.  */
12610       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12611         {
12612           tree attributes = NULL_TREE;
12613           tree first_attribute;
12614
12615           /* Peek at the next token.  */
12616           token = cp_lexer_peek_token (parser->lexer);
12617
12618           /* Check for a bitfield declaration.  */
12619           if (token->type == CPP_COLON
12620               || (token->type == CPP_NAME
12621                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type 
12622                   == CPP_COLON))
12623             {
12624               tree identifier;
12625               tree width;
12626
12627               /* Get the name of the bitfield.  Note that we cannot just
12628                  check TOKEN here because it may have been invalidated by
12629                  the call to cp_lexer_peek_nth_token above.  */
12630               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12631                 identifier = cp_parser_identifier (parser);
12632               else
12633                 identifier = NULL_TREE;
12634
12635               /* Consume the `:' token.  */
12636               cp_lexer_consume_token (parser->lexer);
12637               /* Get the width of the bitfield.  */
12638               width 
12639                 = cp_parser_constant_expression (parser,
12640                                                  /*allow_non_constant=*/false,
12641                                                  NULL);
12642
12643               /* Look for attributes that apply to the bitfield.  */
12644               attributes = cp_parser_attributes_opt (parser);
12645               /* Remember which attributes are prefix attributes and
12646                  which are not.  */
12647               first_attribute = attributes;
12648               /* Combine the attributes.  */
12649               attributes = chainon (prefix_attributes, attributes);
12650
12651               /* Create the bitfield declaration.  */
12652               decl = grokbitfield (identifier, 
12653                                    decl_specifiers,
12654                                    width);
12655               /* Apply the attributes.  */
12656               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12657             }
12658           else
12659             {
12660               tree declarator;
12661               tree initializer;
12662               tree asm_specification;
12663               int ctor_dtor_or_conv_p;
12664
12665               /* Parse the declarator.  */
12666               declarator 
12667                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12668                                         &ctor_dtor_or_conv_p,
12669                                         /*parenthesized_p=*/NULL,
12670                                         /*member_p=*/true);
12671
12672               /* If something went wrong parsing the declarator, make sure
12673                  that we at least consume some tokens.  */
12674               if (declarator == error_mark_node)
12675                 {
12676                   /* Skip to the end of the statement.  */
12677                   cp_parser_skip_to_end_of_statement (parser);
12678                   /* If the next token is not a semicolon, that is
12679                      probably because we just skipped over the body of
12680                      a function.  So, we consume a semicolon if
12681                      present, but do not issue an error message if it
12682                      is not present.  */
12683                   if (cp_lexer_next_token_is (parser->lexer,
12684                                               CPP_SEMICOLON))
12685                     cp_lexer_consume_token (parser->lexer);
12686                   return;
12687                 }
12688
12689               if (declares_class_or_enum & 2)
12690                 cp_parser_check_for_definition_in_return_type
12691                   (declarator, TREE_VALUE (decl_specifiers));
12692
12693               /* Look for an asm-specification.  */
12694               asm_specification = cp_parser_asm_specification_opt (parser);
12695               /* Look for attributes that apply to the declaration.  */
12696               attributes = cp_parser_attributes_opt (parser);
12697               /* Remember which attributes are prefix attributes and
12698                  which are not.  */
12699               first_attribute = attributes;
12700               /* Combine the attributes.  */
12701               attributes = chainon (prefix_attributes, attributes);
12702
12703               /* If it's an `=', then we have a constant-initializer or a
12704                  pure-specifier.  It is not correct to parse the
12705                  initializer before registering the member declaration
12706                  since the member declaration should be in scope while
12707                  its initializer is processed.  However, the rest of the
12708                  front end does not yet provide an interface that allows
12709                  us to handle this correctly.  */
12710               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12711                 {
12712                   /* In [class.mem]:
12713
12714                      A pure-specifier shall be used only in the declaration of
12715                      a virtual function.  
12716
12717                      A member-declarator can contain a constant-initializer
12718                      only if it declares a static member of integral or
12719                      enumeration type.  
12720
12721                      Therefore, if the DECLARATOR is for a function, we look
12722                      for a pure-specifier; otherwise, we look for a
12723                      constant-initializer.  When we call `grokfield', it will
12724                      perform more stringent semantics checks.  */
12725                   if (TREE_CODE (declarator) == CALL_EXPR)
12726                     initializer = cp_parser_pure_specifier (parser);
12727                   else
12728                     /* Parse the initializer.  */
12729                     initializer = cp_parser_constant_initializer (parser);
12730                 }
12731               /* Otherwise, there is no initializer.  */
12732               else
12733                 initializer = NULL_TREE;
12734
12735               /* See if we are probably looking at a function
12736                  definition.  We are certainly not looking at at a
12737                  member-declarator.  Calling `grokfield' has
12738                  side-effects, so we must not do it unless we are sure
12739                  that we are looking at a member-declarator.  */
12740               if (cp_parser_token_starts_function_definition_p 
12741                   (cp_lexer_peek_token (parser->lexer)))
12742                 {
12743                   /* The grammar does not allow a pure-specifier to be
12744                      used when a member function is defined.  (It is
12745                      possible that this fact is an oversight in the
12746                      standard, since a pure function may be defined
12747                      outside of the class-specifier.  */
12748                   if (initializer)
12749                     error ("pure-specifier on function-definition");
12750                   decl = cp_parser_save_member_function_body (parser,
12751                                                               decl_specifiers,
12752                                                               declarator,
12753                                                               attributes);
12754                   /* If the member was not a friend, declare it here.  */
12755                   if (!friend_p)
12756                     finish_member_declaration (decl);
12757                   /* Peek at the next token.  */
12758                   token = cp_lexer_peek_token (parser->lexer);
12759                   /* If the next token is a semicolon, consume it.  */
12760                   if (token->type == CPP_SEMICOLON)
12761                     cp_lexer_consume_token (parser->lexer);
12762                   return;
12763                 }
12764               else
12765                 {
12766                   /* Create the declaration.  */
12767                   decl = grokfield (declarator, decl_specifiers, 
12768                                     initializer, asm_specification,
12769                                     attributes);
12770                   /* Any initialization must have been from a
12771                      constant-expression.  */
12772                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12773                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12774                 }
12775             }
12776
12777           /* Reset PREFIX_ATTRIBUTES.  */
12778           while (attributes && TREE_CHAIN (attributes) != first_attribute)
12779             attributes = TREE_CHAIN (attributes);
12780           if (attributes)
12781             TREE_CHAIN (attributes) = NULL_TREE;
12782
12783           /* If there is any qualification still in effect, clear it
12784              now; we will be starting fresh with the next declarator.  */
12785           parser->scope = NULL_TREE;
12786           parser->qualifying_scope = NULL_TREE;
12787           parser->object_scope = NULL_TREE;
12788           /* If it's a `,', then there are more declarators.  */
12789           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12790             cp_lexer_consume_token (parser->lexer);
12791           /* If the next token isn't a `;', then we have a parse error.  */
12792           else if (cp_lexer_next_token_is_not (parser->lexer,
12793                                                CPP_SEMICOLON))
12794             {
12795               cp_parser_error (parser, "expected `;'");
12796               /* Skip tokens until we find a `;'.  */
12797               cp_parser_skip_to_end_of_statement (parser);
12798
12799               break;
12800             }
12801
12802           if (decl)
12803             {
12804               /* Add DECL to the list of members.  */
12805               if (!friend_p)
12806                 finish_member_declaration (decl);
12807
12808               if (TREE_CODE (decl) == FUNCTION_DECL)
12809                 cp_parser_save_default_args (parser, decl);
12810             }
12811         }
12812     }
12813
12814   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12815 }
12816
12817 /* Parse a pure-specifier.
12818
12819    pure-specifier:
12820      = 0
12821
12822    Returns INTEGER_ZERO_NODE if a pure specifier is found.
12823    Otherwise, ERROR_MARK_NODE is returned.  */
12824
12825 static tree
12826 cp_parser_pure_specifier (cp_parser* parser)
12827 {
12828   cp_token *token;
12829
12830   /* Look for the `=' token.  */
12831   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12832     return error_mark_node;
12833   /* Look for the `0' token.  */
12834   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12835   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
12836      to get information from the lexer about how the number was
12837      spelled in order to fix this problem.  */
12838   if (!token || !integer_zerop (token->value))
12839     return error_mark_node;
12840
12841   return integer_zero_node;
12842 }
12843
12844 /* Parse a constant-initializer.
12845
12846    constant-initializer:
12847      = constant-expression
12848
12849    Returns a representation of the constant-expression.  */
12850
12851 static tree
12852 cp_parser_constant_initializer (cp_parser* parser)
12853 {
12854   /* Look for the `=' token.  */
12855   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12856     return error_mark_node;
12857
12858   /* It is invalid to write:
12859
12860        struct S { static const int i = { 7 }; };
12861
12862      */
12863   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12864     {
12865       cp_parser_error (parser,
12866                        "a brace-enclosed initializer is not allowed here");
12867       /* Consume the opening brace.  */
12868       cp_lexer_consume_token (parser->lexer);
12869       /* Skip the initializer.  */
12870       cp_parser_skip_to_closing_brace (parser);
12871       /* Look for the trailing `}'.  */
12872       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12873       
12874       return error_mark_node;
12875     }
12876
12877   return cp_parser_constant_expression (parser, 
12878                                         /*allow_non_constant=*/false,
12879                                         NULL);
12880 }
12881
12882 /* Derived classes [gram.class.derived] */
12883
12884 /* Parse a base-clause.
12885
12886    base-clause:
12887      : base-specifier-list  
12888
12889    base-specifier-list:
12890      base-specifier
12891      base-specifier-list , base-specifier
12892
12893    Returns a TREE_LIST representing the base-classes, in the order in
12894    which they were declared.  The representation of each node is as
12895    described by cp_parser_base_specifier.  
12896
12897    In the case that no bases are specified, this function will return
12898    NULL_TREE, not ERROR_MARK_NODE.  */
12899
12900 static tree
12901 cp_parser_base_clause (cp_parser* parser)
12902 {
12903   tree bases = NULL_TREE;
12904
12905   /* Look for the `:' that begins the list.  */
12906   cp_parser_require (parser, CPP_COLON, "`:'");
12907
12908   /* Scan the base-specifier-list.  */
12909   while (true)
12910     {
12911       cp_token *token;
12912       tree base;
12913
12914       /* Look for the base-specifier.  */
12915       base = cp_parser_base_specifier (parser);
12916       /* Add BASE to the front of the list.  */
12917       if (base != error_mark_node)
12918         {
12919           TREE_CHAIN (base) = bases;
12920           bases = base;
12921         }
12922       /* Peek at the next token.  */
12923       token = cp_lexer_peek_token (parser->lexer);
12924       /* If it's not a comma, then the list is complete.  */
12925       if (token->type != CPP_COMMA)
12926         break;
12927       /* Consume the `,'.  */
12928       cp_lexer_consume_token (parser->lexer);
12929     }
12930
12931   /* PARSER->SCOPE may still be non-NULL at this point, if the last
12932      base class had a qualified name.  However, the next name that
12933      appears is certainly not qualified.  */
12934   parser->scope = NULL_TREE;
12935   parser->qualifying_scope = NULL_TREE;
12936   parser->object_scope = NULL_TREE;
12937
12938   return nreverse (bases);
12939 }
12940
12941 /* Parse a base-specifier.
12942
12943    base-specifier:
12944      :: [opt] nested-name-specifier [opt] class-name
12945      virtual access-specifier [opt] :: [opt] nested-name-specifier
12946        [opt] class-name
12947      access-specifier virtual [opt] :: [opt] nested-name-specifier
12948        [opt] class-name
12949
12950    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
12951    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12952    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
12953    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
12954        
12955 static tree
12956 cp_parser_base_specifier (cp_parser* parser)
12957 {
12958   cp_token *token;
12959   bool done = false;
12960   bool virtual_p = false;
12961   bool duplicate_virtual_error_issued_p = false;
12962   bool duplicate_access_error_issued_p = false;
12963   bool class_scope_p, template_p;
12964   tree access = access_default_node;
12965   tree type;
12966
12967   /* Process the optional `virtual' and `access-specifier'.  */
12968   while (!done)
12969     {
12970       /* Peek at the next token.  */
12971       token = cp_lexer_peek_token (parser->lexer);
12972       /* Process `virtual'.  */
12973       switch (token->keyword)
12974         {
12975         case RID_VIRTUAL:
12976           /* If `virtual' appears more than once, issue an error.  */
12977           if (virtual_p && !duplicate_virtual_error_issued_p)
12978             {
12979               cp_parser_error (parser,
12980                                "`virtual' specified more than once in base-specified");
12981               duplicate_virtual_error_issued_p = true;
12982             }
12983
12984           virtual_p = true;
12985
12986           /* Consume the `virtual' token.  */
12987           cp_lexer_consume_token (parser->lexer);
12988
12989           break;
12990
12991         case RID_PUBLIC:
12992         case RID_PROTECTED:
12993         case RID_PRIVATE:
12994           /* If more than one access specifier appears, issue an
12995              error.  */
12996           if (access != access_default_node
12997               && !duplicate_access_error_issued_p)
12998             {
12999               cp_parser_error (parser,
13000                                "more than one access specifier in base-specified");
13001               duplicate_access_error_issued_p = true;
13002             }
13003
13004           access = ridpointers[(int) token->keyword];
13005
13006           /* Consume the access-specifier.  */
13007           cp_lexer_consume_token (parser->lexer);
13008
13009           break;
13010
13011         default:
13012           done = true;
13013           break;
13014         }
13015     }
13016   /* It is not uncommon to see programs mechanically, errouneously, use
13017      the 'typename' keyword to denote (dependent) qualified types
13018      as base classes.  */
13019   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13020     {
13021       if (!processing_template_decl)
13022         error ("keyword `typename' not allowed outside of templates");
13023       else
13024         error ("keyword `typename' not allowed in this context "
13025                "(the base class is implicitly a type)");
13026       cp_lexer_consume_token (parser->lexer);
13027     }
13028
13029   /* Look for the optional `::' operator.  */
13030   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13031   /* Look for the nested-name-specifier.  The simplest way to
13032      implement:
13033
13034        [temp.res]
13035
13036        The keyword `typename' is not permitted in a base-specifier or
13037        mem-initializer; in these contexts a qualified name that
13038        depends on a template-parameter is implicitly assumed to be a
13039        type name.
13040
13041      is to pretend that we have seen the `typename' keyword at this
13042      point.  */ 
13043   cp_parser_nested_name_specifier_opt (parser,
13044                                        /*typename_keyword_p=*/true,
13045                                        /*check_dependency_p=*/true,
13046                                        /*type_p=*/true,
13047                                        /*is_declaration=*/true);
13048   /* If the base class is given by a qualified name, assume that names
13049      we see are type names or templates, as appropriate.  */
13050   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13051   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13052   
13053   /* Finally, look for the class-name.  */
13054   type = cp_parser_class_name (parser, 
13055                                class_scope_p,
13056                                template_p,
13057                                /*type_p=*/true,
13058                                /*check_dependency_p=*/true,
13059                                /*class_head_p=*/false,
13060                                /*is_declaration=*/true);
13061
13062   if (type == error_mark_node)
13063     return error_mark_node;
13064
13065   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13066 }
13067
13068 /* Exception handling [gram.exception] */
13069
13070 /* Parse an (optional) exception-specification.
13071
13072    exception-specification:
13073      throw ( type-id-list [opt] )
13074
13075    Returns a TREE_LIST representing the exception-specification.  The
13076    TREE_VALUE of each node is a type.  */
13077
13078 static tree
13079 cp_parser_exception_specification_opt (cp_parser* parser)
13080 {
13081   cp_token *token;
13082   tree type_id_list;
13083
13084   /* Peek at the next token.  */
13085   token = cp_lexer_peek_token (parser->lexer);
13086   /* If it's not `throw', then there's no exception-specification.  */
13087   if (!cp_parser_is_keyword (token, RID_THROW))
13088     return NULL_TREE;
13089
13090   /* Consume the `throw'.  */
13091   cp_lexer_consume_token (parser->lexer);
13092
13093   /* Look for the `('.  */
13094   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13095
13096   /* Peek at the next token.  */
13097   token = cp_lexer_peek_token (parser->lexer);
13098   /* If it's not a `)', then there is a type-id-list.  */
13099   if (token->type != CPP_CLOSE_PAREN)
13100     {
13101       const char *saved_message;
13102
13103       /* Types may not be defined in an exception-specification.  */
13104       saved_message = parser->type_definition_forbidden_message;
13105       parser->type_definition_forbidden_message
13106         = "types may not be defined in an exception-specification";
13107       /* Parse the type-id-list.  */
13108       type_id_list = cp_parser_type_id_list (parser);
13109       /* Restore the saved message.  */
13110       parser->type_definition_forbidden_message = saved_message;
13111     }
13112   else
13113     type_id_list = empty_except_spec;
13114
13115   /* Look for the `)'.  */
13116   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13117
13118   return type_id_list;
13119 }
13120
13121 /* Parse an (optional) type-id-list.
13122
13123    type-id-list:
13124      type-id
13125      type-id-list , type-id
13126
13127    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13128    in the order that the types were presented.  */
13129
13130 static tree
13131 cp_parser_type_id_list (cp_parser* parser)
13132 {
13133   tree types = NULL_TREE;
13134
13135   while (true)
13136     {
13137       cp_token *token;
13138       tree type;
13139
13140       /* Get the next type-id.  */
13141       type = cp_parser_type_id (parser);
13142       /* Add it to the list.  */
13143       types = add_exception_specifier (types, type, /*complain=*/1);
13144       /* Peek at the next token.  */
13145       token = cp_lexer_peek_token (parser->lexer);
13146       /* If it is not a `,', we are done.  */
13147       if (token->type != CPP_COMMA)
13148         break;
13149       /* Consume the `,'.  */
13150       cp_lexer_consume_token (parser->lexer);
13151     }
13152
13153   return nreverse (types);
13154 }
13155
13156 /* Parse a try-block.
13157
13158    try-block:
13159      try compound-statement handler-seq  */
13160
13161 static tree
13162 cp_parser_try_block (cp_parser* parser)
13163 {
13164   tree try_block;
13165
13166   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13167   try_block = begin_try_block ();
13168   cp_parser_compound_statement (parser, false);
13169   finish_try_block (try_block);
13170   cp_parser_handler_seq (parser);
13171   finish_handler_sequence (try_block);
13172
13173   return try_block;
13174 }
13175
13176 /* Parse a function-try-block.
13177
13178    function-try-block:
13179      try ctor-initializer [opt] function-body handler-seq  */
13180
13181 static bool
13182 cp_parser_function_try_block (cp_parser* parser)
13183 {
13184   tree try_block;
13185   bool ctor_initializer_p;
13186
13187   /* Look for the `try' keyword.  */
13188   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13189     return false;
13190   /* Let the rest of the front-end know where we are.  */
13191   try_block = begin_function_try_block ();
13192   /* Parse the function-body.  */
13193   ctor_initializer_p 
13194     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13195   /* We're done with the `try' part.  */
13196   finish_function_try_block (try_block);
13197   /* Parse the handlers.  */
13198   cp_parser_handler_seq (parser);
13199   /* We're done with the handlers.  */
13200   finish_function_handler_sequence (try_block);
13201
13202   return ctor_initializer_p;
13203 }
13204
13205 /* Parse a handler-seq.
13206
13207    handler-seq:
13208      handler handler-seq [opt]  */
13209
13210 static void
13211 cp_parser_handler_seq (cp_parser* parser)
13212 {
13213   while (true)
13214     {
13215       cp_token *token;
13216
13217       /* Parse the handler.  */
13218       cp_parser_handler (parser);
13219       /* Peek at the next token.  */
13220       token = cp_lexer_peek_token (parser->lexer);
13221       /* If it's not `catch' then there are no more handlers.  */
13222       if (!cp_parser_is_keyword (token, RID_CATCH))
13223         break;
13224     }
13225 }
13226
13227 /* Parse a handler.
13228
13229    handler:
13230      catch ( exception-declaration ) compound-statement  */
13231
13232 static void
13233 cp_parser_handler (cp_parser* parser)
13234 {
13235   tree handler;
13236   tree declaration;
13237
13238   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13239   handler = begin_handler ();
13240   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13241   declaration = cp_parser_exception_declaration (parser);
13242   finish_handler_parms (declaration, handler);
13243   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13244   cp_parser_compound_statement (parser, false);
13245   finish_handler (handler);
13246 }
13247
13248 /* Parse an exception-declaration.
13249
13250    exception-declaration:
13251      type-specifier-seq declarator
13252      type-specifier-seq abstract-declarator
13253      type-specifier-seq
13254      ...  
13255
13256    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13257    ellipsis variant is used.  */
13258
13259 static tree
13260 cp_parser_exception_declaration (cp_parser* parser)
13261 {
13262   tree type_specifiers;
13263   tree declarator;
13264   const char *saved_message;
13265
13266   /* If it's an ellipsis, it's easy to handle.  */
13267   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13268     {
13269       /* Consume the `...' token.  */
13270       cp_lexer_consume_token (parser->lexer);
13271       return NULL_TREE;
13272     }
13273
13274   /* Types may not be defined in exception-declarations.  */
13275   saved_message = parser->type_definition_forbidden_message;
13276   parser->type_definition_forbidden_message
13277     = "types may not be defined in exception-declarations";
13278
13279   /* Parse the type-specifier-seq.  */
13280   type_specifiers = cp_parser_type_specifier_seq (parser);
13281   /* If it's a `)', then there is no declarator.  */
13282   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13283     declarator = NULL_TREE;
13284   else
13285     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13286                                        /*ctor_dtor_or_conv_p=*/NULL,
13287                                        /*parenthesized_p=*/NULL,
13288                                        /*member_p=*/false);
13289
13290   /* Restore the saved message.  */
13291   parser->type_definition_forbidden_message = saved_message;
13292
13293   return start_handler_parms (type_specifiers, declarator);
13294 }
13295
13296 /* Parse a throw-expression. 
13297
13298    throw-expression:
13299      throw assignment-expression [opt]
13300
13301    Returns a THROW_EXPR representing the throw-expression.  */
13302
13303 static tree
13304 cp_parser_throw_expression (cp_parser* parser)
13305 {
13306   tree expression;
13307   cp_token* token;
13308
13309   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13310   token = cp_lexer_peek_token (parser->lexer);
13311   /* Figure out whether or not there is an assignment-expression
13312      following the "throw" keyword.  */
13313   if (token->type == CPP_COMMA
13314       || token->type == CPP_SEMICOLON
13315       || token->type == CPP_CLOSE_PAREN
13316       || token->type == CPP_CLOSE_SQUARE
13317       || token->type == CPP_CLOSE_BRACE
13318       || token->type == CPP_COLON)
13319     expression = NULL_TREE;
13320   else
13321     expression = cp_parser_assignment_expression (parser);
13322
13323   return build_throw (expression);
13324 }
13325
13326 /* GNU Extensions */
13327
13328 /* Parse an (optional) asm-specification.
13329
13330    asm-specification:
13331      asm ( string-literal )
13332
13333    If the asm-specification is present, returns a STRING_CST
13334    corresponding to the string-literal.  Otherwise, returns
13335    NULL_TREE.  */
13336
13337 static tree
13338 cp_parser_asm_specification_opt (cp_parser* parser)
13339 {
13340   cp_token *token;
13341   tree asm_specification;
13342
13343   /* Peek at the next token.  */
13344   token = cp_lexer_peek_token (parser->lexer);
13345   /* If the next token isn't the `asm' keyword, then there's no 
13346      asm-specification.  */
13347   if (!cp_parser_is_keyword (token, RID_ASM))
13348     return NULL_TREE;
13349
13350   /* Consume the `asm' token.  */
13351   cp_lexer_consume_token (parser->lexer);
13352   /* Look for the `('.  */
13353   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13354
13355   /* Look for the string-literal.  */
13356   token = cp_parser_require (parser, CPP_STRING, "string-literal");
13357   if (token)
13358     asm_specification = token->value;
13359   else
13360     asm_specification = NULL_TREE;
13361
13362   /* Look for the `)'.  */
13363   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13364
13365   return asm_specification;
13366 }
13367
13368 /* Parse an asm-operand-list.  
13369
13370    asm-operand-list:
13371      asm-operand
13372      asm-operand-list , asm-operand
13373      
13374    asm-operand:
13375      string-literal ( expression )  
13376      [ string-literal ] string-literal ( expression )
13377
13378    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13379    each node is the expression.  The TREE_PURPOSE is itself a
13380    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13381    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13382    is a STRING_CST for the string literal before the parenthesis.  */
13383
13384 static tree
13385 cp_parser_asm_operand_list (cp_parser* parser)
13386 {
13387   tree asm_operands = NULL_TREE;
13388
13389   while (true)
13390     {
13391       tree string_literal;
13392       tree expression;
13393       tree name;
13394       cp_token *token;
13395       
13396       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 
13397         {
13398           /* Consume the `[' token.  */
13399           cp_lexer_consume_token (parser->lexer);
13400           /* Read the operand name.  */
13401           name = cp_parser_identifier (parser);
13402           if (name != error_mark_node) 
13403             name = build_string (IDENTIFIER_LENGTH (name),
13404                                  IDENTIFIER_POINTER (name));
13405           /* Look for the closing `]'.  */
13406           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13407         }
13408       else
13409         name = NULL_TREE;
13410       /* Look for the string-literal.  */
13411       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13412       string_literal = token ? token->value : error_mark_node;
13413       /* Look for the `('.  */
13414       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13415       /* Parse the expression.  */
13416       expression = cp_parser_expression (parser);
13417       /* Look for the `)'.  */
13418       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13419       /* Add this operand to the list.  */
13420       asm_operands = tree_cons (build_tree_list (name, string_literal),
13421                                 expression, 
13422                                 asm_operands);
13423       /* If the next token is not a `,', there are no more 
13424          operands.  */
13425       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13426         break;
13427       /* Consume the `,'.  */
13428       cp_lexer_consume_token (parser->lexer);
13429     }
13430
13431   return nreverse (asm_operands);
13432 }
13433
13434 /* Parse an asm-clobber-list.  
13435
13436    asm-clobber-list:
13437      string-literal
13438      asm-clobber-list , string-literal  
13439
13440    Returns a TREE_LIST, indicating the clobbers in the order that they
13441    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13442
13443 static tree
13444 cp_parser_asm_clobber_list (cp_parser* parser)
13445 {
13446   tree clobbers = NULL_TREE;
13447
13448   while (true)
13449     {
13450       cp_token *token;
13451       tree string_literal;
13452
13453       /* Look for the string literal.  */
13454       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13455       string_literal = token ? token->value : error_mark_node;
13456       /* Add it to the list.  */
13457       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13458       /* If the next token is not a `,', then the list is 
13459          complete.  */
13460       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13461         break;
13462       /* Consume the `,' token.  */
13463       cp_lexer_consume_token (parser->lexer);
13464     }
13465
13466   return clobbers;
13467 }
13468
13469 /* Parse an (optional) series of attributes.
13470
13471    attributes:
13472      attributes attribute
13473
13474    attribute:
13475      __attribute__ (( attribute-list [opt] ))  
13476
13477    The return value is as for cp_parser_attribute_list.  */
13478      
13479 static tree
13480 cp_parser_attributes_opt (cp_parser* parser)
13481 {
13482   tree attributes = NULL_TREE;
13483
13484   while (true)
13485     {
13486       cp_token *token;
13487       tree attribute_list;
13488
13489       /* Peek at the next token.  */
13490       token = cp_lexer_peek_token (parser->lexer);
13491       /* If it's not `__attribute__', then we're done.  */
13492       if (token->keyword != RID_ATTRIBUTE)
13493         break;
13494
13495       /* Consume the `__attribute__' keyword.  */
13496       cp_lexer_consume_token (parser->lexer);
13497       /* Look for the two `(' tokens.  */
13498       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13499       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13500
13501       /* Peek at the next token.  */
13502       token = cp_lexer_peek_token (parser->lexer);
13503       if (token->type != CPP_CLOSE_PAREN)
13504         /* Parse the attribute-list.  */
13505         attribute_list = cp_parser_attribute_list (parser);
13506       else
13507         /* If the next token is a `)', then there is no attribute
13508            list.  */
13509         attribute_list = NULL;
13510
13511       /* Look for the two `)' tokens.  */
13512       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13513       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13514
13515       /* Add these new attributes to the list.  */
13516       attributes = chainon (attributes, attribute_list);
13517     }
13518
13519   return attributes;
13520 }
13521
13522 /* Parse an attribute-list.  
13523
13524    attribute-list:  
13525      attribute 
13526      attribute-list , attribute
13527
13528    attribute:
13529      identifier     
13530      identifier ( identifier )
13531      identifier ( identifier , expression-list )
13532      identifier ( expression-list ) 
13533
13534    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13535    TREE_PURPOSE of each node is the identifier indicating which
13536    attribute is in use.  The TREE_VALUE represents the arguments, if
13537    any.  */
13538
13539 static tree
13540 cp_parser_attribute_list (cp_parser* parser)
13541 {
13542   tree attribute_list = NULL_TREE;
13543
13544   while (true)
13545     {
13546       cp_token *token;
13547       tree identifier;
13548       tree attribute;
13549
13550       /* Look for the identifier.  We also allow keywords here; for
13551          example `__attribute__ ((const))' is legal.  */
13552       token = cp_lexer_peek_token (parser->lexer);
13553       if (token->type == CPP_NAME 
13554           || token->type == CPP_KEYWORD)
13555         {
13556           /* Consume the token.  */
13557           token = cp_lexer_consume_token (parser->lexer);
13558
13559           /* Save away the identifier that indicates which attribute 
13560              this is.  */
13561           identifier = token->value;
13562           attribute = build_tree_list (identifier, NULL_TREE);
13563
13564           /* Peek at the next token.  */
13565           token = cp_lexer_peek_token (parser->lexer);
13566           /* If it's an `(', then parse the attribute arguments.  */
13567           if (token->type == CPP_OPEN_PAREN)
13568             {
13569               tree arguments;
13570
13571               arguments = (cp_parser_parenthesized_expression_list 
13572                            (parser, true, /*non_constant_p=*/NULL));
13573               /* Save the identifier and arguments away.  */
13574               TREE_VALUE (attribute) = arguments;
13575             }
13576
13577           /* Add this attribute to the list.  */
13578           TREE_CHAIN (attribute) = attribute_list;
13579           attribute_list = attribute;
13580
13581           /* Now, look for more attributes.  */
13582           token = cp_lexer_peek_token (parser->lexer);
13583         }
13584       /* Now, look for more attributes.  If the next token isn't a
13585          `,', we're done.  */
13586       if (token->type != CPP_COMMA)
13587         break;
13588
13589       /* Consume the comma and keep going.  */
13590       cp_lexer_consume_token (parser->lexer);
13591     }
13592
13593   /* We built up the list in reverse order.  */
13594   return nreverse (attribute_list);
13595 }
13596
13597 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13598    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13599    current value of the PEDANTIC flag, regardless of whether or not
13600    the `__extension__' keyword is present.  The caller is responsible
13601    for restoring the value of the PEDANTIC flag.  */
13602
13603 static bool
13604 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13605 {
13606   /* Save the old value of the PEDANTIC flag.  */
13607   *saved_pedantic = pedantic;
13608
13609   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13610     {
13611       /* Consume the `__extension__' token.  */
13612       cp_lexer_consume_token (parser->lexer);
13613       /* We're not being pedantic while the `__extension__' keyword is
13614          in effect.  */
13615       pedantic = 0;
13616
13617       return true;
13618     }
13619
13620   return false;
13621 }
13622
13623 /* Parse a label declaration.
13624
13625    label-declaration:
13626      __label__ label-declarator-seq ;
13627
13628    label-declarator-seq:
13629      identifier , label-declarator-seq
13630      identifier  */
13631
13632 static void
13633 cp_parser_label_declaration (cp_parser* parser)
13634 {
13635   /* Look for the `__label__' keyword.  */
13636   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13637
13638   while (true)
13639     {
13640       tree identifier;
13641
13642       /* Look for an identifier.  */
13643       identifier = cp_parser_identifier (parser);
13644       /* If we failed, stop.  */
13645       if (identifier == error_mark_node)
13646         break;
13647       /* Declare it as a label.  */
13648       finish_label_decl (identifier);
13649       /* If the next token is a `;', stop.  */
13650       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13651         break;
13652       /* Look for the `,' separating the label declarations.  */
13653       cp_parser_require (parser, CPP_COMMA, "`,'");
13654     }
13655
13656   /* Look for the final `;'.  */
13657   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13658 }
13659
13660 /* Support Functions */
13661
13662 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13663    NAME should have one of the representations used for an
13664    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13665    is returned.  If PARSER->SCOPE is a dependent type, then a
13666    SCOPE_REF is returned.
13667
13668    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13669    returned; the name was already resolved when the TEMPLATE_ID_EXPR
13670    was formed.  Abstractly, such entities should not be passed to this
13671    function, because they do not need to be looked up, but it is
13672    simpler to check for this special case here, rather than at the
13673    call-sites.
13674
13675    In cases not explicitly covered above, this function returns a
13676    DECL, OVERLOAD, or baselink representing the result of the lookup.
13677    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13678    is returned.
13679
13680    If IS_TYPE is TRUE, bindings that do not refer to types are
13681    ignored.
13682
13683    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13684    ignored.
13685
13686    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13687    are ignored.
13688
13689    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13690    types.  */
13691
13692 static tree
13693 cp_parser_lookup_name (cp_parser *parser, tree name, 
13694                        bool is_type, bool is_template, bool is_namespace,
13695                        bool check_dependency)
13696 {
13697   int flags = 0;
13698   tree decl;
13699   tree object_type = parser->context->object_type;
13700
13701   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13702      no longer valid.  Note that if we are parsing tentatively, and
13703      the parse fails, OBJECT_TYPE will be automatically restored.  */
13704   parser->context->object_type = NULL_TREE;
13705
13706   if (name == error_mark_node)
13707     return error_mark_node;
13708
13709   if (!cp_parser_parsing_tentatively (parser)
13710       || cp_parser_committed_to_tentative_parse (parser))
13711     flags |= LOOKUP_COMPLAIN;
13712
13713   /* A template-id has already been resolved; there is no lookup to
13714      do.  */
13715   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13716     return name;
13717   if (BASELINK_P (name))
13718     {
13719       my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13720                            == TEMPLATE_ID_EXPR),
13721                           20020909);
13722       return name;
13723     }
13724
13725   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
13726      it should already have been checked to make sure that the name
13727      used matches the type being destroyed.  */
13728   if (TREE_CODE (name) == BIT_NOT_EXPR)
13729     {
13730       tree type;
13731
13732       /* Figure out to which type this destructor applies.  */
13733       if (parser->scope)
13734         type = parser->scope;
13735       else if (object_type)
13736         type = object_type;
13737       else
13738         type = current_class_type;
13739       /* If that's not a class type, there is no destructor.  */
13740       if (!type || !CLASS_TYPE_P (type))
13741         return error_mark_node;
13742       if (!CLASSTYPE_DESTRUCTORS (type))
13743           return error_mark_node;
13744       /* If it was a class type, return the destructor.  */
13745       return CLASSTYPE_DESTRUCTORS (type);
13746     }
13747
13748   /* By this point, the NAME should be an ordinary identifier.  If
13749      the id-expression was a qualified name, the qualifying scope is
13750      stored in PARSER->SCOPE at this point.  */
13751   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13752                       20000619);
13753   
13754   /* Perform the lookup.  */
13755   if (parser->scope)
13756     { 
13757       bool dependent_p;
13758
13759       if (parser->scope == error_mark_node)
13760         return error_mark_node;
13761
13762       /* If the SCOPE is dependent, the lookup must be deferred until
13763          the template is instantiated -- unless we are explicitly
13764          looking up names in uninstantiated templates.  Even then, we
13765          cannot look up the name if the scope is not a class type; it
13766          might, for example, be a template type parameter.  */
13767       dependent_p = (TYPE_P (parser->scope)
13768                      && !(parser->in_declarator_p
13769                           && currently_open_class (parser->scope))
13770                      && dependent_type_p (parser->scope));
13771       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13772            && dependent_p)
13773         {
13774           if (is_type)
13775             /* The resolution to Core Issue 180 says that `struct A::B'
13776                should be considered a type-name, even if `A' is
13777                dependent.  */
13778             decl = TYPE_NAME (make_typename_type (parser->scope,
13779                                                   name,
13780                                                   /*complain=*/1));
13781           else if (is_template)
13782             decl = make_unbound_class_template (parser->scope,
13783                                                 name,
13784                                                 /*complain=*/1);
13785           else
13786             decl = build_nt (SCOPE_REF, parser->scope, name);
13787         }
13788       else
13789         {
13790           bool pop_p = false;
13791
13792           /* If PARSER->SCOPE is a dependent type, then it must be a
13793              class type, and we must not be checking dependencies;
13794              otherwise, we would have processed this lookup above.  So
13795              that PARSER->SCOPE is not considered a dependent base by
13796              lookup_member, we must enter the scope here.  */
13797           if (dependent_p)
13798             pop_p = push_scope (parser->scope);
13799           /* If the PARSER->SCOPE is a a template specialization, it
13800              may be instantiated during name lookup.  In that case,
13801              errors may be issued.  Even if we rollback the current
13802              tentative parse, those errors are valid.  */
13803           decl = lookup_qualified_name (parser->scope, name, is_type,
13804                                         /*complain=*/true);
13805           if (pop_p)
13806             pop_scope (parser->scope);
13807         }
13808       parser->qualifying_scope = parser->scope;
13809       parser->object_scope = NULL_TREE;
13810     }
13811   else if (object_type)
13812     {
13813       tree object_decl = NULL_TREE;
13814       /* Look up the name in the scope of the OBJECT_TYPE, unless the
13815          OBJECT_TYPE is not a class.  */
13816       if (CLASS_TYPE_P (object_type))
13817         /* If the OBJECT_TYPE is a template specialization, it may
13818            be instantiated during name lookup.  In that case, errors
13819            may be issued.  Even if we rollback the current tentative
13820            parse, those errors are valid.  */
13821         object_decl = lookup_member (object_type,
13822                                      name,
13823                                      /*protect=*/0, is_type);
13824       /* Look it up in the enclosing context, too.  */
13825       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13826                                is_namespace, flags);
13827       parser->object_scope = object_type;
13828       parser->qualifying_scope = NULL_TREE;
13829       if (object_decl)
13830         decl = object_decl;
13831     }
13832   else
13833     {
13834       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13835                                is_namespace, flags);
13836       parser->qualifying_scope = NULL_TREE;
13837       parser->object_scope = NULL_TREE;
13838     }
13839
13840   /* If the lookup failed, let our caller know.  */
13841   if (!decl 
13842       || decl == error_mark_node
13843       || (TREE_CODE (decl) == FUNCTION_DECL 
13844           && DECL_ANTICIPATED (decl)))
13845     return error_mark_node;
13846
13847   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
13848   if (TREE_CODE (decl) == TREE_LIST)
13849     {
13850       /* The error message we have to print is too complicated for
13851          cp_parser_error, so we incorporate its actions directly.  */
13852       if (!cp_parser_simulate_error (parser))
13853         {
13854           error ("reference to `%D' is ambiguous", name);
13855           print_candidates (decl);
13856         }
13857       return error_mark_node;
13858     }
13859
13860   my_friendly_assert (DECL_P (decl) 
13861                       || TREE_CODE (decl) == OVERLOAD
13862                       || TREE_CODE (decl) == SCOPE_REF
13863                       || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13864                       || BASELINK_P (decl),
13865                       20000619);
13866
13867   /* If we have resolved the name of a member declaration, check to
13868      see if the declaration is accessible.  When the name resolves to
13869      set of overloaded functions, accessibility is checked when
13870      overload resolution is done.  
13871
13872      During an explicit instantiation, access is not checked at all,
13873      as per [temp.explicit].  */
13874   if (DECL_P (decl))
13875     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13876
13877   return decl;
13878 }
13879
13880 /* Like cp_parser_lookup_name, but for use in the typical case where
13881    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13882    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
13883
13884 static tree
13885 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13886 {
13887   return cp_parser_lookup_name (parser, name, 
13888                                 /*is_type=*/false,
13889                                 /*is_template=*/false,
13890                                 /*is_namespace=*/false,
13891                                 /*check_dependency=*/true);
13892 }
13893
13894 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13895    the current context, return the TYPE_DECL.  If TAG_NAME_P is
13896    true, the DECL indicates the class being defined in a class-head,
13897    or declared in an elaborated-type-specifier.
13898
13899    Otherwise, return DECL.  */
13900
13901 static tree
13902 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13903 {
13904   /* If the TEMPLATE_DECL is being declared as part of a class-head,
13905      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13906
13907        struct A { 
13908          template <typename T> struct B;
13909        };
13910
13911        template <typename T> struct A::B {}; 
13912    
13913      Similarly, in a elaborated-type-specifier:
13914
13915        namespace N { struct X{}; }
13916
13917        struct A {
13918          template <typename T> friend struct N::X;
13919        };
13920
13921      However, if the DECL refers to a class type, and we are in
13922      the scope of the class, then the name lookup automatically
13923      finds the TYPE_DECL created by build_self_reference rather
13924      than a TEMPLATE_DECL.  For example, in:
13925
13926        template <class T> struct S {
13927          S s;
13928        };
13929
13930      there is no need to handle such case.  */
13931
13932   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13933     return DECL_TEMPLATE_RESULT (decl);
13934
13935   return decl;
13936 }
13937
13938 /* If too many, or too few, template-parameter lists apply to the
13939    declarator, issue an error message.  Returns TRUE if all went well,
13940    and FALSE otherwise.  */
13941
13942 static bool
13943 cp_parser_check_declarator_template_parameters (cp_parser* parser, 
13944                                                 tree declarator)
13945 {
13946   unsigned num_templates;
13947
13948   /* We haven't seen any classes that involve template parameters yet.  */
13949   num_templates = 0;
13950
13951   switch (TREE_CODE (declarator))
13952     {
13953     case CALL_EXPR:
13954     case ARRAY_REF:
13955     case INDIRECT_REF:
13956     case ADDR_EXPR:
13957       {
13958         tree main_declarator = TREE_OPERAND (declarator, 0);
13959         return
13960           cp_parser_check_declarator_template_parameters (parser, 
13961                                                           main_declarator);
13962       }
13963
13964     case SCOPE_REF:
13965       {
13966         tree scope;
13967         tree member;
13968
13969         scope = TREE_OPERAND (declarator, 0);
13970         member = TREE_OPERAND (declarator, 1);
13971
13972         /* If this is a pointer-to-member, then we are not interested
13973            in the SCOPE, because it does not qualify the thing that is
13974            being declared.  */
13975         if (TREE_CODE (member) == INDIRECT_REF)
13976           return (cp_parser_check_declarator_template_parameters
13977                   (parser, member));
13978
13979         while (scope && CLASS_TYPE_P (scope))
13980           {
13981             /* You're supposed to have one `template <...>'
13982                for every template class, but you don't need one
13983                for a full specialization.  For example:
13984                
13985                template <class T> struct S{};
13986                template <> struct S<int> { void f(); };
13987                void S<int>::f () {}
13988                
13989                is correct; there shouldn't be a `template <>' for
13990                the definition of `S<int>::f'.  */
13991             if (CLASSTYPE_TEMPLATE_INFO (scope)
13992                 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13993                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13994                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13995               ++num_templates;
13996
13997             scope = TYPE_CONTEXT (scope);
13998           }
13999       }
14000
14001       /* Fall through.  */
14002
14003     default:
14004       /* If the DECLARATOR has the form `X<y>' then it uses one
14005          additional level of template parameters.  */
14006       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
14007         ++num_templates;
14008
14009       return cp_parser_check_template_parameters (parser, 
14010                                                   num_templates);
14011     }
14012 }
14013
14014 /* NUM_TEMPLATES were used in the current declaration.  If that is
14015    invalid, return FALSE and issue an error messages.  Otherwise,
14016    return TRUE.  */
14017
14018 static bool
14019 cp_parser_check_template_parameters (cp_parser* parser,
14020                                      unsigned num_templates)
14021 {
14022   /* If there are more template classes than parameter lists, we have
14023      something like:
14024      
14025        template <class T> void S<T>::R<T>::f ();  */
14026   if (parser->num_template_parameter_lists < num_templates)
14027     {
14028       error ("too few template-parameter-lists");
14029       return false;
14030     }
14031   /* If there are the same number of template classes and parameter
14032      lists, that's OK.  */
14033   if (parser->num_template_parameter_lists == num_templates)
14034     return true;
14035   /* If there are more, but only one more, then we are referring to a
14036      member template.  That's OK too.  */
14037   if (parser->num_template_parameter_lists == num_templates + 1)
14038       return true;
14039   /* Otherwise, there are too many template parameter lists.  We have
14040      something like:
14041
14042      template <class T> template <class U> void S::f();  */
14043   error ("too many template-parameter-lists");
14044   return false;
14045 }
14046
14047 /* Parse a binary-expression of the general form:
14048
14049    binary-expression:
14050      <expr>
14051      binary-expression <token> <expr>
14052
14053    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
14054    to parser the <expr>s.  If the first production is used, then the
14055    value returned by FN is returned directly.  Otherwise, a node with
14056    the indicated EXPR_TYPE is returned, with operands corresponding to
14057    the two sub-expressions.  */
14058
14059 static tree
14060 cp_parser_binary_expression (cp_parser* parser, 
14061                              const cp_parser_token_tree_map token_tree_map, 
14062                              cp_parser_expression_fn fn)
14063 {
14064   tree lhs;
14065
14066   /* Parse the first expression.  */
14067   lhs = (*fn) (parser);
14068   /* Now, look for more expressions.  */
14069   while (true)
14070     {
14071       cp_token *token;
14072       const cp_parser_token_tree_map_node *map_node;
14073       tree rhs;
14074
14075       /* Peek at the next token.  */
14076       token = cp_lexer_peek_token (parser->lexer);
14077       /* If the token is `>', and that's not an operator at the
14078          moment, then we're done.  */
14079       if (token->type == CPP_GREATER
14080           && !parser->greater_than_is_operator_p)
14081         break;
14082       /* If we find one of the tokens we want, build the corresponding
14083          tree representation.  */
14084       for (map_node = token_tree_map; 
14085            map_node->token_type != CPP_EOF;
14086            ++map_node)
14087         if (map_node->token_type == token->type)
14088           {
14089             /* Assume that an overloaded operator will not be used.  */
14090             bool overloaded_p = false;
14091
14092             /* Consume the operator token.  */
14093             cp_lexer_consume_token (parser->lexer);
14094             /* Parse the right-hand side of the expression.  */
14095             rhs = (*fn) (parser);
14096             /* Build the binary tree node.  */
14097             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs, 
14098                                      &overloaded_p);
14099             /* If the binary operator required the use of an
14100                overloaded operator, then this expression cannot be an
14101                integral constant-expression.  An overloaded operator
14102                can be used even if both operands are otherwise
14103                permissible in an integral constant-expression if at
14104                least one of the operands is of enumeration type.  */
14105             if (overloaded_p
14106                 && (cp_parser_non_integral_constant_expression 
14107                     (parser, "calls to overloaded operators")))
14108               lhs = error_mark_node;
14109             break;
14110           }
14111
14112       /* If the token wasn't one of the ones we want, we're done.  */
14113       if (map_node->token_type == CPP_EOF)
14114         break;
14115     }
14116
14117   return lhs;
14118 }
14119
14120 /* Parse an optional `::' token indicating that the following name is
14121    from the global namespace.  If so, PARSER->SCOPE is set to the
14122    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14123    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14124    Returns the new value of PARSER->SCOPE, if the `::' token is
14125    present, and NULL_TREE otherwise.  */
14126
14127 static tree
14128 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14129 {
14130   cp_token *token;
14131
14132   /* Peek at the next token.  */
14133   token = cp_lexer_peek_token (parser->lexer);
14134   /* If we're looking at a `::' token then we're starting from the
14135      global namespace, not our current location.  */
14136   if (token->type == CPP_SCOPE)
14137     {
14138       /* Consume the `::' token.  */
14139       cp_lexer_consume_token (parser->lexer);
14140       /* Set the SCOPE so that we know where to start the lookup.  */
14141       parser->scope = global_namespace;
14142       parser->qualifying_scope = global_namespace;
14143       parser->object_scope = NULL_TREE;
14144
14145       return parser->scope;
14146     }
14147   else if (!current_scope_valid_p)
14148     {
14149       parser->scope = NULL_TREE;
14150       parser->qualifying_scope = NULL_TREE;
14151       parser->object_scope = NULL_TREE;
14152     }
14153
14154   return NULL_TREE;
14155 }
14156
14157 /* Returns TRUE if the upcoming token sequence is the start of a
14158    constructor declarator.  If FRIEND_P is true, the declarator is
14159    preceded by the `friend' specifier.  */
14160
14161 static bool
14162 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14163 {
14164   bool constructor_p;
14165   tree type_decl = NULL_TREE;
14166   bool nested_name_p;
14167   cp_token *next_token;
14168
14169   /* The common case is that this is not a constructor declarator, so
14170      try to avoid doing lots of work if at all possible.  It's not
14171      valid declare a constructor at function scope.  */
14172   if (at_function_scope_p ())
14173     return false;
14174   /* And only certain tokens can begin a constructor declarator.  */
14175   next_token = cp_lexer_peek_token (parser->lexer);
14176   if (next_token->type != CPP_NAME
14177       && next_token->type != CPP_SCOPE
14178       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14179       && next_token->type != CPP_TEMPLATE_ID)
14180     return false;
14181
14182   /* Parse tentatively; we are going to roll back all of the tokens
14183      consumed here.  */
14184   cp_parser_parse_tentatively (parser);
14185   /* Assume that we are looking at a constructor declarator.  */
14186   constructor_p = true;
14187
14188   /* Look for the optional `::' operator.  */
14189   cp_parser_global_scope_opt (parser,
14190                               /*current_scope_valid_p=*/false);
14191   /* Look for the nested-name-specifier.  */
14192   nested_name_p 
14193     = (cp_parser_nested_name_specifier_opt (parser,
14194                                             /*typename_keyword_p=*/false,
14195                                             /*check_dependency_p=*/false,
14196                                             /*type_p=*/false,
14197                                             /*is_declaration=*/false)
14198        != NULL_TREE);
14199   /* Outside of a class-specifier, there must be a
14200      nested-name-specifier.  */
14201   if (!nested_name_p && 
14202       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14203        || friend_p))
14204     constructor_p = false;
14205   /* If we still think that this might be a constructor-declarator,
14206      look for a class-name.  */
14207   if (constructor_p)
14208     {
14209       /* If we have:
14210
14211            template <typename T> struct S { S(); };
14212            template <typename T> S<T>::S ();
14213
14214          we must recognize that the nested `S' names a class.
14215          Similarly, for:
14216
14217            template <typename T> S<T>::S<T> ();
14218
14219          we must recognize that the nested `S' names a template.  */
14220       type_decl = cp_parser_class_name (parser,
14221                                         /*typename_keyword_p=*/false,
14222                                         /*template_keyword_p=*/false,
14223                                         /*type_p=*/false,
14224                                         /*check_dependency_p=*/false,
14225                                         /*class_head_p=*/false,
14226                                         /*is_declaration=*/false);
14227       /* If there was no class-name, then this is not a constructor.  */
14228       constructor_p = !cp_parser_error_occurred (parser);
14229     }
14230
14231   /* If we're still considering a constructor, we have to see a `(',
14232      to begin the parameter-declaration-clause, followed by either a
14233      `)', an `...', or a decl-specifier.  We need to check for a
14234      type-specifier to avoid being fooled into thinking that:
14235
14236        S::S (f) (int);
14237
14238      is a constructor.  (It is actually a function named `f' that
14239      takes one parameter (of type `int') and returns a value of type
14240      `S::S'.  */
14241   if (constructor_p 
14242       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14243     {
14244       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14245           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14246           /* A parameter declaration begins with a decl-specifier,
14247              which is either the "attribute" keyword, a storage class
14248              specifier, or (usually) a type-specifier.  */
14249           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14250           && !cp_parser_storage_class_specifier_opt (parser))
14251         {
14252           tree type;
14253           bool pop_p = false;
14254           unsigned saved_num_template_parameter_lists;
14255
14256           /* Names appearing in the type-specifier should be looked up
14257              in the scope of the class.  */
14258           if (current_class_type)
14259             type = NULL_TREE;
14260           else
14261             {
14262               type = TREE_TYPE (type_decl);
14263               if (TREE_CODE (type) == TYPENAME_TYPE)
14264                 {
14265                   type = resolve_typename_type (type, 
14266                                                 /*only_current_p=*/false);
14267                   if (type == error_mark_node)
14268                     {
14269                       cp_parser_abort_tentative_parse (parser);
14270                       return false;
14271                     }
14272                 }
14273               pop_p = push_scope (type);
14274             }
14275
14276           /* Inside the constructor parameter list, surrounding
14277              template-parameter-lists do not apply.  */
14278           saved_num_template_parameter_lists
14279             = parser->num_template_parameter_lists;
14280           parser->num_template_parameter_lists = 0;
14281
14282           /* Look for the type-specifier.  */
14283           cp_parser_type_specifier (parser,
14284                                     CP_PARSER_FLAGS_NONE,
14285                                     /*is_friend=*/false,
14286                                     /*is_declarator=*/true,
14287                                     /*declares_class_or_enum=*/NULL,
14288                                     /*is_cv_qualifier=*/NULL);
14289
14290           parser->num_template_parameter_lists
14291             = saved_num_template_parameter_lists;
14292
14293           /* Leave the scope of the class.  */
14294           if (pop_p)
14295             pop_scope (type);
14296
14297           constructor_p = !cp_parser_error_occurred (parser);
14298         }
14299     }
14300   else
14301     constructor_p = false;
14302   /* We did not really want to consume any tokens.  */
14303   cp_parser_abort_tentative_parse (parser);
14304
14305   return constructor_p;
14306 }
14307
14308 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14309    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14310    they must be performed once we are in the scope of the function.
14311
14312    Returns the function defined.  */
14313
14314 static tree
14315 cp_parser_function_definition_from_specifiers_and_declarator
14316   (cp_parser* parser,
14317    tree decl_specifiers,
14318    tree attributes,
14319    tree declarator)
14320 {
14321   tree fn;
14322   bool success_p;
14323
14324   /* Begin the function-definition.  */
14325   success_p = begin_function_definition (decl_specifiers, 
14326                                          attributes, 
14327                                          declarator);
14328
14329   /* If there were names looked up in the decl-specifier-seq that we
14330      did not check, check them now.  We must wait until we are in the
14331      scope of the function to perform the checks, since the function
14332      might be a friend.  */
14333   perform_deferred_access_checks ();
14334
14335   if (!success_p)
14336     {
14337       /* If begin_function_definition didn't like the definition, skip
14338          the entire function.  */
14339       error ("invalid function declaration");
14340       cp_parser_skip_to_end_of_block_or_statement (parser);
14341       fn = error_mark_node;
14342     }
14343   else
14344     fn = cp_parser_function_definition_after_declarator (parser,
14345                                                          /*inline_p=*/false);
14346
14347   return fn;
14348 }
14349
14350 /* Parse the part of a function-definition that follows the
14351    declarator.  INLINE_P is TRUE iff this function is an inline
14352    function defined with a class-specifier.
14353
14354    Returns the function defined.  */
14355
14356 static tree 
14357 cp_parser_function_definition_after_declarator (cp_parser* parser, 
14358                                                 bool inline_p)
14359 {
14360   tree fn;
14361   bool ctor_initializer_p = false;
14362   bool saved_in_unbraced_linkage_specification_p;
14363   unsigned saved_num_template_parameter_lists;
14364
14365   /* If the next token is `return', then the code may be trying to
14366      make use of the "named return value" extension that G++ used to
14367      support.  */
14368   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14369     {
14370       /* Consume the `return' keyword.  */
14371       cp_lexer_consume_token (parser->lexer);
14372       /* Look for the identifier that indicates what value is to be
14373          returned.  */
14374       cp_parser_identifier (parser);
14375       /* Issue an error message.  */
14376       error ("named return values are no longer supported");
14377       /* Skip tokens until we reach the start of the function body.  */
14378       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14379              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14380         cp_lexer_consume_token (parser->lexer);
14381     }
14382   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14383      anything declared inside `f'.  */
14384   saved_in_unbraced_linkage_specification_p 
14385     = parser->in_unbraced_linkage_specification_p;
14386   parser->in_unbraced_linkage_specification_p = false;
14387   /* Inside the function, surrounding template-parameter-lists do not
14388      apply.  */
14389   saved_num_template_parameter_lists 
14390     = parser->num_template_parameter_lists; 
14391   parser->num_template_parameter_lists = 0;
14392   /* If the next token is `try', then we are looking at a
14393      function-try-block.  */
14394   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14395     ctor_initializer_p = cp_parser_function_try_block (parser);
14396   /* A function-try-block includes the function-body, so we only do
14397      this next part if we're not processing a function-try-block.  */
14398   else
14399     ctor_initializer_p 
14400       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14401
14402   /* Finish the function.  */
14403   fn = finish_function ((ctor_initializer_p ? 1 : 0) | 
14404                         (inline_p ? 2 : 0));
14405   /* Generate code for it, if necessary.  */
14406   expand_or_defer_fn (fn);
14407   /* Restore the saved values.  */
14408   parser->in_unbraced_linkage_specification_p 
14409     = saved_in_unbraced_linkage_specification_p;
14410   parser->num_template_parameter_lists 
14411     = saved_num_template_parameter_lists;
14412
14413   return fn;
14414 }
14415
14416 /* Parse a template-declaration, assuming that the `export' (and
14417    `extern') keywords, if present, has already been scanned.  MEMBER_P
14418    is as for cp_parser_template_declaration.  */
14419
14420 static void
14421 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14422 {
14423   tree decl = NULL_TREE;
14424   tree parameter_list;
14425   bool friend_p = false;
14426
14427   /* Look for the `template' keyword.  */
14428   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14429     return;
14430       
14431   /* And the `<'.  */
14432   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14433     return;
14434       
14435   /* If the next token is `>', then we have an invalid
14436      specialization.  Rather than complain about an invalid template
14437      parameter, issue an error message here.  */
14438   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14439     {
14440       cp_parser_error (parser, "invalid explicit specialization");
14441       begin_specialization ();
14442       parameter_list = NULL_TREE;
14443     }
14444   else
14445     {
14446       /* Parse the template parameters.  */
14447       begin_template_parm_list ();
14448       parameter_list = cp_parser_template_parameter_list (parser);
14449       parameter_list = end_template_parm_list (parameter_list);
14450     }
14451
14452   /* Look for the `>'.  */
14453   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14454   /* We just processed one more parameter list.  */
14455   ++parser->num_template_parameter_lists;
14456   /* If the next token is `template', there are more template
14457      parameters.  */
14458   if (cp_lexer_next_token_is_keyword (parser->lexer, 
14459                                       RID_TEMPLATE))
14460     cp_parser_template_declaration_after_export (parser, member_p);
14461   else
14462     {
14463       decl = cp_parser_single_declaration (parser,
14464                                            member_p,
14465                                            &friend_p);
14466
14467       /* If this is a member template declaration, let the front
14468          end know.  */
14469       if (member_p && !friend_p && decl)
14470         {
14471           if (TREE_CODE (decl) == TYPE_DECL)
14472             cp_parser_check_access_in_redeclaration (decl);
14473
14474           decl = finish_member_template_decl (decl);
14475         }
14476       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14477         make_friend_class (current_class_type, TREE_TYPE (decl),
14478                            /*complain=*/true);
14479     }
14480   /* We are done with the current parameter list.  */
14481   --parser->num_template_parameter_lists;
14482
14483   /* Finish up.  */
14484   finish_template_decl (parameter_list);
14485
14486   /* Register member declarations.  */
14487   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14488     finish_member_declaration (decl);
14489
14490   /* If DECL is a function template, we must return to parse it later.
14491      (Even though there is no definition, there might be default
14492      arguments that need handling.)  */
14493   if (member_p && decl 
14494       && (TREE_CODE (decl) == FUNCTION_DECL
14495           || DECL_FUNCTION_TEMPLATE_P (decl)))
14496     TREE_VALUE (parser->unparsed_functions_queues)
14497       = tree_cons (NULL_TREE, decl, 
14498                    TREE_VALUE (parser->unparsed_functions_queues));
14499 }
14500
14501 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14502    `function-definition' sequence.  MEMBER_P is true, this declaration
14503    appears in a class scope.
14504
14505    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14506    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14507
14508 static tree
14509 cp_parser_single_declaration (cp_parser* parser, 
14510                               bool member_p,
14511                               bool* friend_p)
14512 {
14513   int declares_class_or_enum;
14514   tree decl = NULL_TREE;
14515   tree decl_specifiers;
14516   tree attributes;
14517   bool function_definition_p = false;
14518
14519   /* This function is only used when processing a template
14520      declaration.  */
14521   if (innermost_scope_kind () != sk_template_parms
14522       && innermost_scope_kind () != sk_template_spec)
14523     abort ();
14524
14525   /* Defer access checks until we know what is being declared.  */
14526   push_deferring_access_checks (dk_deferred);
14527
14528   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14529      alternative.  */
14530   decl_specifiers 
14531     = cp_parser_decl_specifier_seq (parser,
14532                                     CP_PARSER_FLAGS_OPTIONAL,
14533                                     &attributes,
14534                                     &declares_class_or_enum);
14535   if (friend_p)
14536     *friend_p = cp_parser_friend_p (decl_specifiers);
14537
14538   /* There are no template typedefs.  */
14539   if (cp_parser_typedef_p (decl_specifiers))
14540     {
14541       error ("template declaration of `typedef'");
14542       decl = error_mark_node;
14543     }
14544
14545   /* Gather up the access checks that occurred the
14546      decl-specifier-seq.  */
14547   stop_deferring_access_checks ();
14548
14549   /* Check for the declaration of a template class.  */
14550   if (declares_class_or_enum)
14551     {
14552       if (cp_parser_declares_only_class_p (parser))
14553         {
14554           decl = shadow_tag (decl_specifiers);
14555           if (decl)
14556             decl = TYPE_NAME (decl);
14557           else
14558             decl = error_mark_node;
14559         }
14560     }
14561   /* If it's not a template class, try for a template function.  If
14562      the next token is a `;', then this declaration does not declare
14563      anything.  But, if there were errors in the decl-specifiers, then
14564      the error might well have come from an attempted class-specifier.
14565      In that case, there's no need to warn about a missing declarator.  */
14566   if (!decl
14567       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14568           || !value_member (error_mark_node, decl_specifiers)))
14569     decl = cp_parser_init_declarator (parser, 
14570                                       decl_specifiers,
14571                                       attributes,
14572                                       /*function_definition_allowed_p=*/true,
14573                                       member_p,
14574                                       declares_class_or_enum,
14575                                       &function_definition_p);
14576
14577   pop_deferring_access_checks ();
14578
14579   /* Clear any current qualification; whatever comes next is the start
14580      of something new.  */
14581   parser->scope = NULL_TREE;
14582   parser->qualifying_scope = NULL_TREE;
14583   parser->object_scope = NULL_TREE;
14584   /* Look for a trailing `;' after the declaration.  */
14585   if (!function_definition_p
14586       && (decl == error_mark_node
14587           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
14588     cp_parser_skip_to_end_of_block_or_statement (parser);
14589
14590   return decl;
14591 }
14592
14593 /* Parse a cast-expression that is not the operand of a unary "&".  */
14594
14595 static tree
14596 cp_parser_simple_cast_expression (cp_parser *parser)
14597 {
14598   return cp_parser_cast_expression (parser, /*address_p=*/false);
14599 }
14600
14601 /* Parse a functional cast to TYPE.  Returns an expression
14602    representing the cast.  */
14603
14604 static tree
14605 cp_parser_functional_cast (cp_parser* parser, tree type)
14606 {
14607   tree expression_list;
14608   tree cast;
14609
14610   expression_list
14611     = cp_parser_parenthesized_expression_list (parser, false,
14612                                                /*non_constant_p=*/NULL);
14613
14614   cast = build_functional_cast (type, expression_list);
14615   /* [expr.const]/1: In an integral constant expression "only type
14616      conversions to integral or enumeration type can be used".  */
14617   if (TREE_CODE (type) == TYPE_DECL)
14618     type = TREE_TYPE (type);
14619   if (cast != error_mark_node && !dependent_type_p (type)
14620       && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
14621     {
14622       if (cp_parser_non_integral_constant_expression 
14623           (parser, "a call to a constructor"))
14624         return error_mark_node;
14625     }
14626   return cast;
14627 }
14628
14629 /* Save the tokens that make up the body of a member function defined
14630    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
14631    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
14632    specifiers applied to the declaration.  Returns the FUNCTION_DECL
14633    for the member function.  */
14634
14635 static tree
14636 cp_parser_save_member_function_body (cp_parser* parser,
14637                                      tree decl_specifiers,
14638                                      tree declarator,
14639                                      tree attributes)
14640 {
14641   cp_token_cache *cache;
14642   tree fn;
14643
14644   /* Create the function-declaration.  */
14645   fn = start_method (decl_specifiers, declarator, attributes);
14646   /* If something went badly wrong, bail out now.  */
14647   if (fn == error_mark_node)
14648     {
14649       /* If there's a function-body, skip it.  */
14650       if (cp_parser_token_starts_function_definition_p 
14651           (cp_lexer_peek_token (parser->lexer)))
14652         cp_parser_skip_to_end_of_block_or_statement (parser);
14653       return error_mark_node;
14654     }
14655
14656   /* Remember it, if there default args to post process.  */
14657   cp_parser_save_default_args (parser, fn);
14658
14659   /* Create a token cache.  */
14660   cache = cp_token_cache_new ();
14661   /* Save away the tokens that make up the body of the 
14662      function.  */
14663   cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14664   /* Handle function try blocks.  */
14665   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14666     cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14667
14668   /* Save away the inline definition; we will process it when the
14669      class is complete.  */
14670   DECL_PENDING_INLINE_INFO (fn) = cache;
14671   DECL_PENDING_INLINE_P (fn) = 1;
14672
14673   /* We need to know that this was defined in the class, so that
14674      friend templates are handled correctly.  */
14675   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14676
14677   /* We're done with the inline definition.  */
14678   finish_method (fn);
14679
14680   /* Add FN to the queue of functions to be parsed later.  */
14681   TREE_VALUE (parser->unparsed_functions_queues)
14682     = tree_cons (NULL_TREE, fn, 
14683                  TREE_VALUE (parser->unparsed_functions_queues));
14684
14685   return fn;
14686 }
14687
14688 /* Parse a template-argument-list, as well as the trailing ">" (but
14689    not the opening ">").  See cp_parser_template_argument_list for the
14690    return value.  */
14691
14692 static tree
14693 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14694 {
14695   tree arguments;
14696   tree saved_scope;
14697   tree saved_qualifying_scope;
14698   tree saved_object_scope;
14699   bool saved_greater_than_is_operator_p;
14700
14701   /* [temp.names]
14702
14703      When parsing a template-id, the first non-nested `>' is taken as
14704      the end of the template-argument-list rather than a greater-than
14705      operator.  */
14706   saved_greater_than_is_operator_p 
14707     = parser->greater_than_is_operator_p;
14708   parser->greater_than_is_operator_p = false;
14709   /* Parsing the argument list may modify SCOPE, so we save it
14710      here.  */
14711   saved_scope = parser->scope;
14712   saved_qualifying_scope = parser->qualifying_scope;
14713   saved_object_scope = parser->object_scope;
14714   /* Parse the template-argument-list itself.  */
14715   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14716     arguments = NULL_TREE;
14717   else
14718     arguments = cp_parser_template_argument_list (parser);
14719   /* Look for the `>' that ends the template-argument-list. If we find
14720      a '>>' instead, it's probably just a typo.  */
14721   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14722     {
14723       if (!saved_greater_than_is_operator_p)
14724         {
14725           /* If we're in a nested template argument list, the '>>' has to be
14726             a typo for '> >'. We emit the error message, but we continue
14727             parsing and we push a '>' as next token, so that the argument
14728             list will be parsed correctly..  */
14729           cp_token* token;
14730           error ("`>>' should be `> >' within a nested template argument list");
14731           token = cp_lexer_peek_token (parser->lexer);
14732           token->type = CPP_GREATER;
14733         }
14734       else
14735         {
14736           /* If this is not a nested template argument list, the '>>' is
14737             a typo for '>'. Emit an error message and continue.  */
14738           error ("spurious `>>', use `>' to terminate a template argument list");
14739           cp_lexer_consume_token (parser->lexer);
14740         }
14741     }
14742   else
14743     cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14744   /* The `>' token might be a greater-than operator again now.  */
14745   parser->greater_than_is_operator_p 
14746     = saved_greater_than_is_operator_p;
14747   /* Restore the SAVED_SCOPE.  */
14748   parser->scope = saved_scope;
14749   parser->qualifying_scope = saved_qualifying_scope;
14750   parser->object_scope = saved_object_scope;
14751
14752   return arguments;
14753 }
14754
14755 /* MEMBER_FUNCTION is a member function, or a friend.  If default
14756    arguments, or the body of the function have not yet been parsed,
14757    parse them now.  */
14758
14759 static void
14760 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14761 {
14762   cp_lexer *saved_lexer;
14763
14764   /* If this member is a template, get the underlying
14765      FUNCTION_DECL.  */
14766   if (DECL_FUNCTION_TEMPLATE_P (member_function))
14767     member_function = DECL_TEMPLATE_RESULT (member_function);
14768
14769   /* There should not be any class definitions in progress at this
14770      point; the bodies of members are only parsed outside of all class
14771      definitions.  */
14772   my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14773   /* While we're parsing the member functions we might encounter more
14774      classes.  We want to handle them right away, but we don't want
14775      them getting mixed up with functions that are currently in the
14776      queue.  */
14777   parser->unparsed_functions_queues
14778     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14779
14780   /* Make sure that any template parameters are in scope.  */
14781   maybe_begin_member_template_processing (member_function);
14782
14783   /* If the body of the function has not yet been parsed, parse it
14784      now.  */
14785   if (DECL_PENDING_INLINE_P (member_function))
14786     {
14787       tree function_scope;
14788       cp_token_cache *tokens;
14789
14790       /* The function is no longer pending; we are processing it.  */
14791       tokens = DECL_PENDING_INLINE_INFO (member_function);
14792       DECL_PENDING_INLINE_INFO (member_function) = NULL;
14793       DECL_PENDING_INLINE_P (member_function) = 0;
14794       
14795       /* If this is a local class, enter the scope of the containing
14796          function.  */
14797       function_scope = current_function_decl;
14798       if (function_scope)
14799         push_function_context_to (function_scope);
14800       
14801       /* Save away the current lexer.  */
14802       saved_lexer = parser->lexer;
14803       /* Make a new lexer to feed us the tokens saved for this function.  */
14804       parser->lexer = cp_lexer_new_from_tokens (tokens);
14805       parser->lexer->next = saved_lexer;
14806       
14807       /* Set the current source position to be the location of the first
14808          token in the saved inline body.  */
14809       cp_lexer_peek_token (parser->lexer);
14810       
14811       /* Let the front end know that we going to be defining this
14812          function.  */
14813       start_function (NULL_TREE, member_function, NULL_TREE,
14814                       SF_PRE_PARSED | SF_INCLASS_INLINE);
14815       
14816       /* Now, parse the body of the function.  */
14817       cp_parser_function_definition_after_declarator (parser,
14818                                                       /*inline_p=*/true);
14819       
14820       /* Leave the scope of the containing function.  */
14821       if (function_scope)
14822         pop_function_context_from (function_scope);
14823       /* Restore the lexer.  */
14824       parser->lexer = saved_lexer;
14825     }
14826
14827   /* Remove any template parameters from the symbol table.  */
14828   maybe_end_member_template_processing ();
14829
14830   /* Restore the queue.  */
14831   parser->unparsed_functions_queues 
14832     = TREE_CHAIN (parser->unparsed_functions_queues);
14833 }
14834
14835 /* If DECL contains any default args, remember it on the unparsed
14836    functions queue.  */
14837
14838 static void
14839 cp_parser_save_default_args (cp_parser* parser, tree decl)
14840 {
14841   tree probe;
14842
14843   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14844        probe;
14845        probe = TREE_CHAIN (probe))
14846     if (TREE_PURPOSE (probe))
14847       {
14848         TREE_PURPOSE (parser->unparsed_functions_queues)
14849           = tree_cons (NULL_TREE, decl, 
14850                        TREE_PURPOSE (parser->unparsed_functions_queues));
14851         break;
14852       }
14853   return;
14854 }
14855
14856 /* FN is a FUNCTION_DECL which may contains a parameter with an
14857    unparsed DEFAULT_ARG.  Parse the default args now.  */
14858
14859 static void
14860 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14861 {
14862   cp_lexer *saved_lexer;
14863   cp_token_cache *tokens;
14864   bool saved_local_variables_forbidden_p;
14865   tree parameters;
14866
14867   /* While we're parsing the default args, we might (due to the
14868      statement expression extension) encounter more classes.  We want
14869      to handle them right away, but we don't want them getting mixed
14870      up with default args that are currently in the queue.  */
14871   parser->unparsed_functions_queues
14872     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14873
14874   for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14875        parameters;
14876        parameters = TREE_CHAIN (parameters))
14877     {
14878       tree default_arg = TREE_PURPOSE (parameters);
14879       tree parsed_arg;
14880
14881       if (!default_arg)
14882         continue;
14883
14884       if (TREE_CODE (default_arg) != DEFAULT_ARG)
14885         /* This can happen for a friend declaration for a function
14886            already declared with default arguments.  */
14887         continue;
14888   
14889       /* Save away the current lexer.  */
14890       saved_lexer = parser->lexer;
14891       /* Create a new one, using the tokens we have saved.  */
14892       tokens =  DEFARG_TOKENS (default_arg);
14893       parser->lexer = cp_lexer_new_from_tokens (tokens);
14894
14895       /* Set the current source position to be the location of the
14896          first token in the default argument.  */
14897       cp_lexer_peek_token (parser->lexer);
14898
14899       /* Local variable names (and the `this' keyword) may not appear
14900          in a default argument.  */
14901       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14902       parser->local_variables_forbidden_p = true;
14903       
14904       /* Parse the assignment-expression.  */
14905       if (DECL_FRIEND_CONTEXT (fn))
14906         push_nested_class (DECL_FRIEND_CONTEXT (fn));
14907       else if (DECL_CLASS_SCOPE_P (fn))
14908         push_nested_class (DECL_CONTEXT (fn));
14909       parsed_arg = cp_parser_assignment_expression (parser);
14910       if (DECL_FRIEND_CONTEXT (fn) || DECL_CLASS_SCOPE_P (fn))
14911         pop_nested_class ();
14912       
14913       TREE_PURPOSE (parameters) = parsed_arg;
14914       
14915       /* Update any instantiations we've already created.  */
14916       for (default_arg = TREE_CHAIN (default_arg);
14917            default_arg;
14918            default_arg = TREE_CHAIN (default_arg))
14919         TREE_PURPOSE (TREE_PURPOSE (default_arg)) = parsed_arg;
14920      
14921       /* If the token stream has not been completely used up, then
14922          there was extra junk after the end of the default
14923          argument.  */
14924       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14925         cp_parser_error (parser, "expected `,'");
14926
14927        /* Restore saved state.  */
14928       parser->lexer = saved_lexer;
14929       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14930     }
14931
14932   /* Make sure no default arg is missing.  */
14933   check_default_args (fn);
14934
14935   /* Restore the queue.  */
14936   parser->unparsed_functions_queues 
14937     = TREE_CHAIN (parser->unparsed_functions_queues);
14938 }
14939
14940 /* Parse the operand of `sizeof' (or a similar operator).  Returns
14941    either a TYPE or an expression, depending on the form of the
14942    input.  The KEYWORD indicates which kind of expression we have
14943    encountered.  */
14944
14945 static tree
14946 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14947 {
14948   static const char *format;
14949   tree expr = NULL_TREE;
14950   const char *saved_message;
14951   bool saved_integral_constant_expression_p;
14952
14953   /* Initialize FORMAT the first time we get here.  */
14954   if (!format)
14955     format = "types may not be defined in `%s' expressions";
14956
14957   /* Types cannot be defined in a `sizeof' expression.  Save away the
14958      old message.  */
14959   saved_message = parser->type_definition_forbidden_message;
14960   /* And create the new one.  */
14961   parser->type_definition_forbidden_message 
14962     = xmalloc (strlen (format) 
14963                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14964                + 1 /* `\0' */);
14965   sprintf ((char *) parser->type_definition_forbidden_message,
14966            format, IDENTIFIER_POINTER (ridpointers[keyword]));
14967
14968   /* The restrictions on constant-expressions do not apply inside
14969      sizeof expressions.  */
14970   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14971   parser->integral_constant_expression_p = false;
14972
14973   /* Do not actually evaluate the expression.  */
14974   ++skip_evaluation;
14975   /* If it's a `(', then we might be looking at the type-id
14976      construction.  */
14977   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14978     {
14979       tree type;
14980       bool saved_in_type_id_in_expr_p;
14981
14982       /* We can't be sure yet whether we're looking at a type-id or an
14983          expression.  */
14984       cp_parser_parse_tentatively (parser);
14985       /* Consume the `('.  */
14986       cp_lexer_consume_token (parser->lexer);
14987       /* Parse the type-id.  */
14988       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14989       parser->in_type_id_in_expr_p = true;
14990       type = cp_parser_type_id (parser);
14991       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14992       /* Now, look for the trailing `)'.  */
14993       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14994       /* If all went well, then we're done.  */
14995       if (cp_parser_parse_definitely (parser))
14996         {
14997           /* Build a list of decl-specifiers; right now, we have only
14998              a single type-specifier.  */
14999           type = build_tree_list (NULL_TREE,
15000                                   type);
15001
15002           /* Call grokdeclarator to figure out what type this is.  */
15003           expr = grokdeclarator (NULL_TREE,
15004                                  type,
15005                                  TYPENAME,
15006                                  /*initialized=*/0,
15007                                  /*attrlist=*/NULL);
15008         }
15009     }
15010
15011   /* If the type-id production did not work out, then we must be
15012      looking at the unary-expression production.  */
15013   if (!expr)
15014     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15015   /* Go back to evaluating expressions.  */
15016   --skip_evaluation;
15017
15018   /* Free the message we created.  */
15019   free ((char *) parser->type_definition_forbidden_message);
15020   /* And restore the old one.  */
15021   parser->type_definition_forbidden_message = saved_message;
15022   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15023
15024   return expr;
15025 }
15026
15027 /* If the current declaration has no declarator, return true.  */
15028
15029 static bool
15030 cp_parser_declares_only_class_p (cp_parser *parser)
15031 {
15032   /* If the next token is a `;' or a `,' then there is no 
15033      declarator.  */
15034   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15035           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15036 }
15037
15038 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15039    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15040
15041 static bool
15042 cp_parser_friend_p (tree decl_specifiers)
15043 {
15044   while (decl_specifiers)
15045     {
15046       /* See if this decl-specifier is `friend'.  */
15047       if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
15048           && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
15049         return true;
15050
15051       /* Go on to the next decl-specifier.  */
15052       decl_specifiers = TREE_CHAIN (decl_specifiers);
15053     }
15054
15055   return false;
15056 }
15057
15058 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15059    Returns TRUE iff `typedef' appears among the DECL_SPECIFIERS.  */
15060
15061 static bool
15062 cp_parser_typedef_p (tree decl_specifiers)
15063 {
15064   while (decl_specifiers)
15065     {
15066       /* See if this decl-specifier is `typedef'.  */
15067       if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
15068           && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_TYPEDEF)
15069         return true;
15070
15071       /* Go on to the next decl-specifier.  */
15072       decl_specifiers = TREE_CHAIN (decl_specifiers);
15073     }
15074
15075   return false;
15076 }
15077
15078
15079 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15080    issue an error message indicating that TOKEN_DESC was expected.
15081    
15082    Returns the token consumed, if the token had the appropriate type.
15083    Otherwise, returns NULL.  */
15084
15085 static cp_token *
15086 cp_parser_require (cp_parser* parser,
15087                    enum cpp_ttype type,
15088                    const char* token_desc)
15089 {
15090   if (cp_lexer_next_token_is (parser->lexer, type))
15091     return cp_lexer_consume_token (parser->lexer);
15092   else
15093     {
15094       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15095       if (!cp_parser_simulate_error (parser))
15096         {
15097           char *message = concat ("expected ", token_desc, NULL);
15098           cp_parser_error (parser, message);
15099           free (message);
15100         }
15101       return NULL;
15102     }
15103 }
15104
15105 /* Like cp_parser_require, except that tokens will be skipped until
15106    the desired token is found.  An error message is still produced if
15107    the next token is not as expected.  */
15108
15109 static void
15110 cp_parser_skip_until_found (cp_parser* parser, 
15111                             enum cpp_ttype type, 
15112                             const char* token_desc)
15113 {
15114   cp_token *token;
15115   unsigned nesting_depth = 0;
15116
15117   if (cp_parser_require (parser, type, token_desc))
15118     return;
15119
15120   /* Skip tokens until the desired token is found.  */
15121   while (true)
15122     {
15123       /* Peek at the next token.  */
15124       token = cp_lexer_peek_token (parser->lexer);
15125       /* If we've reached the token we want, consume it and 
15126          stop.  */
15127       if (token->type == type && !nesting_depth)
15128         {
15129           cp_lexer_consume_token (parser->lexer);
15130           return;
15131         }
15132       /* If we've run out of tokens, stop.  */
15133       if (token->type == CPP_EOF)
15134         return;
15135       if (token->type == CPP_OPEN_BRACE 
15136           || token->type == CPP_OPEN_PAREN
15137           || token->type == CPP_OPEN_SQUARE)
15138         ++nesting_depth;
15139       else if (token->type == CPP_CLOSE_BRACE 
15140                || token->type == CPP_CLOSE_PAREN
15141                || token->type == CPP_CLOSE_SQUARE)
15142         {
15143           if (nesting_depth-- == 0)
15144             return;
15145         }
15146       /* Consume this token.  */
15147       cp_lexer_consume_token (parser->lexer);
15148     }
15149 }
15150
15151 /* If the next token is the indicated keyword, consume it.  Otherwise,
15152    issue an error message indicating that TOKEN_DESC was expected.
15153    
15154    Returns the token consumed, if the token had the appropriate type.
15155    Otherwise, returns NULL.  */
15156
15157 static cp_token *
15158 cp_parser_require_keyword (cp_parser* parser,
15159                            enum rid keyword,
15160                            const char* token_desc)
15161 {
15162   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15163
15164   if (token && token->keyword != keyword)
15165     {
15166       dyn_string_t error_msg;
15167
15168       /* Format the error message.  */
15169       error_msg = dyn_string_new (0);
15170       dyn_string_append_cstr (error_msg, "expected ");
15171       dyn_string_append_cstr (error_msg, token_desc);
15172       cp_parser_error (parser, error_msg->s);
15173       dyn_string_delete (error_msg);
15174       return NULL;
15175     }
15176
15177   return token;
15178 }
15179
15180 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15181    function-definition.  */
15182
15183 static bool 
15184 cp_parser_token_starts_function_definition_p (cp_token* token)
15185 {
15186   return (/* An ordinary function-body begins with an `{'.  */
15187           token->type == CPP_OPEN_BRACE
15188           /* A ctor-initializer begins with a `:'.  */
15189           || token->type == CPP_COLON
15190           /* A function-try-block begins with `try'.  */
15191           || token->keyword == RID_TRY
15192           /* The named return value extension begins with `return'.  */
15193           || token->keyword == RID_RETURN);
15194 }
15195
15196 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15197    definition.  */
15198
15199 static bool
15200 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15201 {
15202   cp_token *token;
15203
15204   token = cp_lexer_peek_token (parser->lexer);
15205   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15206 }
15207
15208 /* Returns TRUE iff the next token is the "," or ">" ending a
15209    template-argument.   */
15210
15211 static bool
15212 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15213 {
15214   cp_token *token;
15215
15216   token = cp_lexer_peek_token (parser->lexer);
15217   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15218 }
15219
15220 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15221    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15222
15223 static bool
15224 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser, 
15225                                                      size_t n)
15226 {
15227   cp_token *token;
15228
15229   token = cp_lexer_peek_nth_token (parser->lexer, n);
15230   if (token->type == CPP_LESS)
15231     return true;
15232   /* Check for the sequence `<::' in the original code. It would be lexed as
15233      `[:', where `[' is a digraph, and there is no whitespace before
15234      `:'.  */
15235   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15236     {
15237       cp_token *token2;
15238       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15239       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15240         return true;
15241     }
15242   return false;
15243 }
15244  
15245 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15246    or none_type otherwise.  */
15247
15248 static enum tag_types
15249 cp_parser_token_is_class_key (cp_token* token)
15250 {
15251   switch (token->keyword)
15252     {
15253     case RID_CLASS:
15254       return class_type;
15255     case RID_STRUCT:
15256       return record_type;
15257     case RID_UNION:
15258       return union_type;
15259       
15260     default:
15261       return none_type;
15262     }
15263 }
15264
15265 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15266
15267 static void
15268 cp_parser_check_class_key (enum tag_types class_key, tree type)
15269 {
15270   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15271     pedwarn ("`%s' tag used in naming `%#T'",
15272             class_key == union_type ? "union"
15273              : class_key == record_type ? "struct" : "class", 
15274              type);
15275 }
15276                            
15277 /* Issue an error message if DECL is redeclared with different
15278    access than its original declaration [class.access.spec/3].
15279    This applies to nested classes and nested class templates.
15280    [class.mem/1].  */
15281
15282 static void cp_parser_check_access_in_redeclaration (tree decl)
15283 {
15284   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15285     return;
15286
15287   if ((TREE_PRIVATE (decl)
15288        != (current_access_specifier == access_private_node))
15289       || (TREE_PROTECTED (decl)
15290           != (current_access_specifier == access_protected_node)))
15291     error ("%D redeclared with different access", decl);
15292 }
15293
15294 /* Look for the `template' keyword, as a syntactic disambiguator.
15295    Return TRUE iff it is present, in which case it will be 
15296    consumed.  */
15297
15298 static bool
15299 cp_parser_optional_template_keyword (cp_parser *parser)
15300 {
15301   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15302     {
15303       /* The `template' keyword can only be used within templates;
15304          outside templates the parser can always figure out what is a
15305          template and what is not.  */
15306       if (!processing_template_decl)
15307         {
15308           error ("`template' (as a disambiguator) is only allowed "
15309                  "within templates");
15310           /* If this part of the token stream is rescanned, the same
15311              error message would be generated.  So, we purge the token
15312              from the stream.  */
15313           cp_lexer_purge_token (parser->lexer);
15314           return false;
15315         }
15316       else
15317         {
15318           /* Consume the `template' keyword.  */
15319           cp_lexer_consume_token (parser->lexer);
15320           return true;
15321         }
15322     }
15323
15324   return false;
15325 }
15326
15327 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15328    set PARSER->SCOPE, and perform other related actions.  */
15329
15330 static void
15331 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15332 {
15333   tree value;
15334   tree check;
15335
15336   /* Get the stored value.  */
15337   value = cp_lexer_consume_token (parser->lexer)->value;
15338   /* Perform any access checks that were deferred.  */
15339   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15340     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15341   /* Set the scope from the stored value.  */
15342   parser->scope = TREE_VALUE (value);
15343   parser->qualifying_scope = TREE_TYPE (value);
15344   parser->object_scope = NULL_TREE;
15345 }
15346
15347 /* Add tokens to CACHE until an non-nested END token appears.  */
15348
15349 static void
15350 cp_parser_cache_group (cp_parser *parser, 
15351                        cp_token_cache *cache,
15352                        enum cpp_ttype end,
15353                        unsigned depth)
15354 {
15355   while (true)
15356     {
15357       cp_token *token;
15358
15359       /* Abort a parenthesized expression if we encounter a brace.  */
15360       if ((end == CPP_CLOSE_PAREN || depth == 0)
15361           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15362         return;
15363       /* If we've reached the end of the file, stop.  */
15364       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15365         return;
15366       /* Consume the next token.  */
15367       token = cp_lexer_consume_token (parser->lexer);
15368       /* Add this token to the tokens we are saving.  */
15369       cp_token_cache_push_token (cache, token);
15370       /* See if it starts a new group.  */
15371       if (token->type == CPP_OPEN_BRACE)
15372         {
15373           cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15374           if (depth == 0)
15375             return;
15376         }
15377       else if (token->type == CPP_OPEN_PAREN)
15378         cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15379       else if (token->type == end)
15380         return;
15381     }
15382 }
15383
15384 /* Begin parsing tentatively.  We always save tokens while parsing
15385    tentatively so that if the tentative parsing fails we can restore the
15386    tokens.  */
15387
15388 static void
15389 cp_parser_parse_tentatively (cp_parser* parser)
15390 {
15391   /* Enter a new parsing context.  */
15392   parser->context = cp_parser_context_new (parser->context);
15393   /* Begin saving tokens.  */
15394   cp_lexer_save_tokens (parser->lexer);
15395   /* In order to avoid repetitive access control error messages,
15396      access checks are queued up until we are no longer parsing
15397      tentatively.  */
15398   push_deferring_access_checks (dk_deferred);
15399 }
15400
15401 /* Commit to the currently active tentative parse.  */
15402
15403 static void
15404 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15405 {
15406   cp_parser_context *context;
15407   cp_lexer *lexer;
15408
15409   /* Mark all of the levels as committed.  */
15410   lexer = parser->lexer;
15411   for (context = parser->context; context->next; context = context->next)
15412     {
15413       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15414         break;
15415       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15416       while (!cp_lexer_saving_tokens (lexer))
15417         lexer = lexer->next;
15418       cp_lexer_commit_tokens (lexer);
15419     }
15420 }
15421
15422 /* Abort the currently active tentative parse.  All consumed tokens
15423    will be rolled back, and no diagnostics will be issued.  */
15424
15425 static void
15426 cp_parser_abort_tentative_parse (cp_parser* parser)
15427 {
15428   cp_parser_simulate_error (parser);
15429   /* Now, pretend that we want to see if the construct was
15430      successfully parsed.  */
15431   cp_parser_parse_definitely (parser);
15432 }
15433
15434 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15435    token stream.  Otherwise, commit to the tokens we have consumed.
15436    Returns true if no error occurred; false otherwise.  */
15437
15438 static bool
15439 cp_parser_parse_definitely (cp_parser* parser)
15440 {
15441   bool error_occurred;
15442   cp_parser_context *context;
15443
15444   /* Remember whether or not an error occurred, since we are about to
15445      destroy that information.  */
15446   error_occurred = cp_parser_error_occurred (parser);
15447   /* Remove the topmost context from the stack.  */
15448   context = parser->context;
15449   parser->context = context->next;
15450   /* If no parse errors occurred, commit to the tentative parse.  */
15451   if (!error_occurred)
15452     {
15453       /* Commit to the tokens read tentatively, unless that was
15454          already done.  */
15455       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15456         cp_lexer_commit_tokens (parser->lexer);
15457
15458       pop_to_parent_deferring_access_checks ();
15459     }
15460   /* Otherwise, if errors occurred, roll back our state so that things
15461      are just as they were before we began the tentative parse.  */
15462   else
15463     {
15464       cp_lexer_rollback_tokens (parser->lexer);
15465       pop_deferring_access_checks ();
15466     }
15467   /* Add the context to the front of the free list.  */
15468   context->next = cp_parser_context_free_list;
15469   cp_parser_context_free_list = context;
15470
15471   return !error_occurred;
15472 }
15473
15474 /* Returns true if we are parsing tentatively -- but have decided that
15475    we will stick with this tentative parse, even if errors occur.  */
15476
15477 static bool
15478 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15479 {
15480   return (cp_parser_parsing_tentatively (parser)
15481           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15482 }
15483
15484 /* Returns nonzero iff an error has occurred during the most recent
15485    tentative parse.  */
15486    
15487 static bool
15488 cp_parser_error_occurred (cp_parser* parser)
15489 {
15490   return (cp_parser_parsing_tentatively (parser)
15491           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15492 }
15493
15494 /* Returns nonzero if GNU extensions are allowed.  */
15495
15496 static bool
15497 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15498 {
15499   return parser->allow_gnu_extensions_p;
15500 }
15501
15502 \f
15503
15504 /* The parser.  */
15505
15506 static GTY (()) cp_parser *the_parser;
15507
15508 /* External interface.  */
15509
15510 /* Parse one entire translation unit.  */
15511
15512 void
15513 c_parse_file (void)
15514 {
15515   bool error_occurred;
15516
15517   the_parser = cp_parser_new ();
15518   push_deferring_access_checks (flag_access_control
15519                                 ? dk_no_deferred : dk_no_check);
15520   error_occurred = cp_parser_translation_unit (the_parser);
15521   the_parser = NULL;
15522 }
15523
15524 /* This variable must be provided by every front end.  */
15525
15526 int yydebug;
15527
15528 #include "gt-cp-parser.h"