Update GCC 3.4 to current 3.4.5 pre-release.
[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, int);
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 a declaration is parsed.  If
1855    DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1856    indicates that a type was defined in the decl-specifiers for DECL,
1857    then an error is issued.  */
1858
1859 static void
1860 cp_parser_check_for_definition_in_return_type (tree declarator, 
1861                                                int declares_class_or_enum)
1862 {
1863   /* [dcl.fct] forbids type definitions in return types.
1864      Unfortunately, it's not easy to know whether or not we are
1865      processing a return type until after the fact.  */
1866   while (declarator
1867          && (TREE_CODE (declarator) == INDIRECT_REF
1868              || TREE_CODE (declarator) == ADDR_EXPR))
1869     declarator = TREE_OPERAND (declarator, 0);
1870   if (declarator
1871       && TREE_CODE (declarator) == CALL_EXPR 
1872       && declares_class_or_enum & 2)
1873     error ("new types may not be defined in a return type");
1874 }
1875
1876 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1877    "<" in any valid C++ program.  If the next token is indeed "<",
1878    issue a message warning the user about what appears to be an
1879    invalid attempt to form a template-id.  */
1880
1881 static void
1882 cp_parser_check_for_invalid_template_id (cp_parser* parser, 
1883                                          tree type)
1884 {
1885   ptrdiff_t start;
1886   cp_token *token;
1887
1888   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1889     {
1890       if (TYPE_P (type))
1891         error ("`%T' is not a template", type);
1892       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1893         error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1894       else
1895         error ("invalid template-id");
1896       /* Remember the location of the invalid "<".  */
1897       if (cp_parser_parsing_tentatively (parser)
1898           && !cp_parser_committed_to_tentative_parse (parser))
1899         {
1900           token = cp_lexer_peek_token (parser->lexer);
1901           token = cp_lexer_prev_token (parser->lexer, token);
1902           start = cp_lexer_token_difference (parser->lexer,
1903                                              parser->lexer->first_token,
1904                                              token);
1905         }
1906       else
1907         start = -1;
1908       /* Consume the "<".  */
1909       cp_lexer_consume_token (parser->lexer);
1910       /* Parse the template arguments.  */
1911       cp_parser_enclosed_template_argument_list (parser);
1912       /* Permanently remove the invalid template arguments so that
1913          this error message is not issued again.  */
1914       if (start >= 0)
1915         {
1916           token = cp_lexer_advance_token (parser->lexer,
1917                                           parser->lexer->first_token,
1918                                           start);
1919           cp_lexer_purge_tokens_after (parser->lexer, token);
1920         }
1921     }
1922 }
1923
1924 /* If parsing an integral constant-expression, issue an error message
1925    about the fact that THING appeared and return true.  Otherwise,
1926    return false, marking the current expression as non-constant.  */
1927
1928 static bool
1929 cp_parser_non_integral_constant_expression (cp_parser  *parser,
1930                                             const char *thing)
1931 {
1932   if (parser->integral_constant_expression_p)
1933     {
1934       if (!parser->allow_non_integral_constant_expression_p)
1935         {
1936           error ("%s cannot appear in a constant-expression", thing);
1937           return true;
1938         }
1939       parser->non_integral_constant_expression_p = true;
1940     }
1941   return false;
1942 }
1943
1944 /* Check for a common situation where a type-name should be present,
1945    but is not, and issue a sensible error message.  Returns true if an
1946    invalid type-name was detected.  */
1947
1948 static bool
1949 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1950 {
1951   /* If the next two tokens are both identifiers, the code is
1952      erroneous. The usual cause of this situation is code like:
1953
1954        T t;
1955
1956      where "T" should name a type -- but does not.  */
1957   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1958       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1959     {
1960       tree name;
1961
1962       /* If parsing tentatively, we should commit; we really are
1963          looking at a declaration.  */
1964       /* Consume the first identifier.  */
1965       name = cp_lexer_consume_token (parser->lexer)->value;
1966       /* Issue an error message.  */
1967       error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1968       /* If we're in a template class, it's possible that the user was
1969          referring to a type from a base class.  For example:
1970
1971            template <typename T> struct A { typedef T X; };
1972            template <typename T> struct B : public A<T> { X x; };
1973
1974          The user should have said "typename A<T>::X".  */
1975       if (processing_template_decl && current_class_type)
1976         {
1977           tree b;
1978
1979           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1980                b;
1981                b = TREE_CHAIN (b))
1982             {
1983               tree base_type = BINFO_TYPE (b);
1984               if (CLASS_TYPE_P (base_type) 
1985                   && dependent_type_p (base_type))
1986                 {
1987                   tree field;
1988                   /* Go from a particular instantiation of the
1989                      template (which will have an empty TYPE_FIELDs),
1990                      to the main version.  */
1991                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1992                   for (field = TYPE_FIELDS (base_type);
1993                        field;
1994                        field = TREE_CHAIN (field))
1995                     if (TREE_CODE (field) == TYPE_DECL
1996                         && DECL_NAME (field) == name)
1997                       {
1998                         error ("(perhaps `typename %T::%s' was intended)",
1999                                BINFO_TYPE (b), IDENTIFIER_POINTER (name));
2000                         break;
2001                       }
2002                   if (field)
2003                     break;
2004                 }
2005             }
2006         }
2007       /* Skip to the end of the declaration; there's no point in
2008          trying to process it.  */
2009       cp_parser_skip_to_end_of_statement (parser);
2010       
2011       return true;
2012     }
2013
2014   return false;
2015 }
2016
2017 /* Consume tokens up to, and including, the next non-nested closing `)'. 
2018    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2019    are doing error recovery. Returns -1 if OR_COMMA is true and we
2020    found an unnested comma.  */
2021
2022 static int
2023 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2024                                        bool recovering, 
2025                                        bool or_comma,
2026                                        bool consume_paren)
2027 {
2028   unsigned paren_depth = 0;
2029   unsigned brace_depth = 0;
2030
2031   if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2032       && !cp_parser_committed_to_tentative_parse (parser))
2033     return 0;
2034   
2035   while (true)
2036     {
2037       cp_token *token;
2038       
2039       /* If we've run out of tokens, then there is no closing `)'.  */
2040       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2041         return 0;
2042
2043       token = cp_lexer_peek_token (parser->lexer);
2044       
2045       /* This matches the processing in skip_to_end_of_statement.  */
2046       if (token->type == CPP_SEMICOLON && !brace_depth)
2047         return 0;
2048       if (token->type == CPP_OPEN_BRACE)
2049         ++brace_depth;
2050       if (token->type == CPP_CLOSE_BRACE)
2051         {
2052           if (!brace_depth--)
2053             return 0;
2054         }
2055       if (recovering && or_comma && token->type == CPP_COMMA
2056           && !brace_depth && !paren_depth)
2057         return -1;
2058       
2059       if (!brace_depth)
2060         {
2061           /* If it is an `(', we have entered another level of nesting.  */
2062           if (token->type == CPP_OPEN_PAREN)
2063             ++paren_depth;
2064           /* If it is a `)', then we might be done.  */
2065           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2066             {
2067               if (consume_paren)
2068                 cp_lexer_consume_token (parser->lexer);
2069               return 1;
2070             }
2071         }
2072       
2073       /* Consume the token.  */
2074       cp_lexer_consume_token (parser->lexer);
2075     }
2076 }
2077
2078 /* Consume tokens until we reach the end of the current statement.
2079    Normally, that will be just before consuming a `;'.  However, if a
2080    non-nested `}' comes first, then we stop before consuming that.  */
2081
2082 static void
2083 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2084 {
2085   unsigned nesting_depth = 0;
2086
2087   while (true)
2088     {
2089       cp_token *token;
2090
2091       /* Peek at the next token.  */
2092       token = cp_lexer_peek_token (parser->lexer);
2093       /* If we've run out of tokens, stop.  */
2094       if (token->type == CPP_EOF)
2095         break;
2096       /* If the next token is a `;', we have reached the end of the
2097          statement.  */
2098       if (token->type == CPP_SEMICOLON && !nesting_depth)
2099         break;
2100       /* If the next token is a non-nested `}', then we have reached
2101          the end of the current block.  */
2102       if (token->type == CPP_CLOSE_BRACE)
2103         {
2104           /* If this is a non-nested `}', stop before consuming it.
2105              That way, when confronted with something like:
2106
2107                { 3 + } 
2108
2109              we stop before consuming the closing `}', even though we
2110              have not yet reached a `;'.  */
2111           if (nesting_depth == 0)
2112             break;
2113           /* If it is the closing `}' for a block that we have
2114              scanned, stop -- but only after consuming the token.
2115              That way given:
2116
2117                 void f g () { ... }
2118                 typedef int I;
2119
2120              we will stop after the body of the erroneously declared
2121              function, but before consuming the following `typedef'
2122              declaration.  */
2123           if (--nesting_depth == 0)
2124             {
2125               cp_lexer_consume_token (parser->lexer);
2126               break;
2127             }
2128         }
2129       /* If it the next token is a `{', then we are entering a new
2130          block.  Consume the entire block.  */
2131       else if (token->type == CPP_OPEN_BRACE)
2132         ++nesting_depth;
2133       /* Consume the token.  */
2134       cp_lexer_consume_token (parser->lexer);
2135     }
2136 }
2137
2138 /* This function is called at the end of a statement or declaration.
2139    If the next token is a semicolon, it is consumed; otherwise, error
2140    recovery is attempted.  */
2141
2142 static void
2143 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2144 {
2145   /* Look for the trailing `;'.  */
2146   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2147     {
2148       /* If there is additional (erroneous) input, skip to the end of
2149          the statement.  */
2150       cp_parser_skip_to_end_of_statement (parser);
2151       /* If the next token is now a `;', consume it.  */
2152       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2153         cp_lexer_consume_token (parser->lexer);
2154     }
2155 }
2156
2157 /* Skip tokens until we have consumed an entire block, or until we
2158    have consumed a non-nested `;'.  */
2159
2160 static void
2161 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2162 {
2163   unsigned nesting_depth = 0;
2164
2165   while (true)
2166     {
2167       cp_token *token;
2168
2169       /* Peek at the next token.  */
2170       token = cp_lexer_peek_token (parser->lexer);
2171       /* If we've run out of tokens, stop.  */
2172       if (token->type == CPP_EOF)
2173         break;
2174       /* If the next token is a `;', we have reached the end of the
2175          statement.  */
2176       if (token->type == CPP_SEMICOLON && !nesting_depth)
2177         {
2178           /* Consume the `;'.  */
2179           cp_lexer_consume_token (parser->lexer);
2180           break;
2181         }
2182       /* Consume the token.  */
2183       token = cp_lexer_consume_token (parser->lexer);
2184       /* If the next token is a non-nested `}', then we have reached
2185          the end of the current block.  */
2186       if (token->type == CPP_CLOSE_BRACE 
2187           && (nesting_depth == 0 || --nesting_depth == 0))
2188         break;
2189       /* If it the next token is a `{', then we are entering a new
2190          block.  Consume the entire block.  */
2191       if (token->type == CPP_OPEN_BRACE)
2192         ++nesting_depth;
2193     }
2194 }
2195
2196 /* Skip tokens until a non-nested closing curly brace is the next
2197    token.  */
2198
2199 static void
2200 cp_parser_skip_to_closing_brace (cp_parser *parser)
2201 {
2202   unsigned nesting_depth = 0;
2203
2204   while (true)
2205     {
2206       cp_token *token;
2207
2208       /* Peek at the next token.  */
2209       token = cp_lexer_peek_token (parser->lexer);
2210       /* If we've run out of tokens, stop.  */
2211       if (token->type == CPP_EOF)
2212         break;
2213       /* If the next token is a non-nested `}', then we have reached
2214          the end of the current block.  */
2215       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2216         break;
2217       /* If it the next token is a `{', then we are entering a new
2218          block.  Consume the entire block.  */
2219       else if (token->type == CPP_OPEN_BRACE)
2220         ++nesting_depth;
2221       /* Consume the token.  */
2222       cp_lexer_consume_token (parser->lexer);
2223     }
2224 }
2225
2226 /* Create a new C++ parser.  */
2227
2228 static cp_parser *
2229 cp_parser_new (void)
2230 {
2231   cp_parser *parser;
2232   cp_lexer *lexer;
2233
2234   /* cp_lexer_new_main is called before calling ggc_alloc because
2235      cp_lexer_new_main might load a PCH file.  */
2236   lexer = cp_lexer_new_main ();
2237
2238   parser = ggc_alloc_cleared (sizeof (cp_parser));
2239   parser->lexer = lexer;
2240   parser->context = cp_parser_context_new (NULL);
2241
2242   /* For now, we always accept GNU extensions.  */
2243   parser->allow_gnu_extensions_p = 1;
2244
2245   /* The `>' token is a greater-than operator, not the end of a
2246      template-id.  */
2247   parser->greater_than_is_operator_p = true;
2248
2249   parser->default_arg_ok_p = true;
2250   
2251   /* We are not parsing a constant-expression.  */
2252   parser->integral_constant_expression_p = false;
2253   parser->allow_non_integral_constant_expression_p = false;
2254   parser->non_integral_constant_expression_p = false;
2255
2256   /* We are not parsing offsetof.  */
2257   parser->in_offsetof_p = false;
2258
2259   /* Local variable names are not forbidden.  */
2260   parser->local_variables_forbidden_p = false;
2261
2262   /* We are not processing an `extern "C"' declaration.  */
2263   parser->in_unbraced_linkage_specification_p = false;
2264
2265   /* We are not processing a declarator.  */
2266   parser->in_declarator_p = false;
2267
2268   /* We are not processing a template-argument-list.  */
2269   parser->in_template_argument_list_p = false;
2270
2271   /* We are not in an iteration statement.  */
2272   parser->in_iteration_statement_p = false;
2273
2274   /* We are not in a switch statement.  */
2275   parser->in_switch_statement_p = false;
2276
2277   /* We are not parsing a type-id inside an expression.  */
2278   parser->in_type_id_in_expr_p = false;
2279
2280   /* The unparsed function queue is empty.  */
2281   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2282
2283   /* There are no classes being defined.  */
2284   parser->num_classes_being_defined = 0;
2285
2286   /* No template parameters apply.  */
2287   parser->num_template_parameter_lists = 0;
2288
2289   return parser;
2290 }
2291
2292 /* Lexical conventions [gram.lex]  */
2293
2294 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2295    identifier.  */
2296
2297 static tree 
2298 cp_parser_identifier (cp_parser* parser)
2299 {
2300   cp_token *token;
2301
2302   /* Look for the identifier.  */
2303   token = cp_parser_require (parser, CPP_NAME, "identifier");
2304   /* Return the value.  */
2305   return token ? token->value : error_mark_node;
2306 }
2307
2308 /* Basic concepts [gram.basic]  */
2309
2310 /* Parse a translation-unit.
2311
2312    translation-unit:
2313      declaration-seq [opt]  
2314
2315    Returns TRUE if all went well.  */
2316
2317 static bool
2318 cp_parser_translation_unit (cp_parser* parser)
2319 {
2320   while (true)
2321     {
2322       cp_parser_declaration_seq_opt (parser);
2323
2324       /* If there are no tokens left then all went well.  */
2325       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2326         break;
2327       
2328       /* Otherwise, issue an error message.  */
2329       cp_parser_error (parser, "expected declaration");
2330       return false;
2331     }
2332
2333   /* Consume the EOF token.  */
2334   cp_parser_require (parser, CPP_EOF, "end-of-file");
2335   
2336   /* Finish up.  */
2337   finish_translation_unit ();
2338
2339   /* All went well.  */
2340   return true;
2341 }
2342
2343 /* Expressions [gram.expr] */
2344
2345 /* Parse a primary-expression.
2346
2347    primary-expression:
2348      literal
2349      this
2350      ( expression )
2351      id-expression
2352
2353    GNU Extensions:
2354
2355    primary-expression:
2356      ( compound-statement )
2357      __builtin_va_arg ( assignment-expression , type-id )
2358
2359    literal:
2360      __null
2361
2362    Returns a representation of the expression.  
2363
2364    *IDK indicates what kind of id-expression (if any) was present.  
2365
2366    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2367    used as the operand of a pointer-to-member.  In that case,
2368    *QUALIFYING_CLASS gives the class that is used as the qualifying
2369    class in the pointer-to-member.  */
2370
2371 static tree
2372 cp_parser_primary_expression (cp_parser *parser, 
2373                               cp_id_kind *idk,
2374                               tree *qualifying_class)
2375 {
2376   cp_token *token;
2377
2378   /* Assume the primary expression is not an id-expression.  */
2379   *idk = CP_ID_KIND_NONE;
2380   /* And that it cannot be used as pointer-to-member.  */
2381   *qualifying_class = NULL_TREE;
2382
2383   /* Peek at the next token.  */
2384   token = cp_lexer_peek_token (parser->lexer);
2385   switch (token->type)
2386     {
2387       /* literal:
2388            integer-literal
2389            character-literal
2390            floating-literal
2391            string-literal
2392            boolean-literal  */
2393     case CPP_CHAR:
2394     case CPP_WCHAR:
2395     case CPP_STRING:
2396     case CPP_WSTRING:
2397     case CPP_NUMBER:
2398       token = cp_lexer_consume_token (parser->lexer);
2399       return token->value;
2400
2401     case CPP_OPEN_PAREN:
2402       {
2403         tree expr;
2404         bool saved_greater_than_is_operator_p;
2405
2406         /* Consume the `('.  */
2407         cp_lexer_consume_token (parser->lexer);
2408         /* Within a parenthesized expression, a `>' token is always
2409            the greater-than operator.  */
2410         saved_greater_than_is_operator_p 
2411           = parser->greater_than_is_operator_p;
2412         parser->greater_than_is_operator_p = true;
2413         /* If we see `( { ' then we are looking at the beginning of
2414            a GNU statement-expression.  */
2415         if (cp_parser_allow_gnu_extensions_p (parser)
2416             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2417           {
2418             /* Statement-expressions are not allowed by the standard.  */
2419             if (pedantic)
2420               pedwarn ("ISO C++ forbids braced-groups within expressions");  
2421             
2422             /* And they're not allowed outside of a function-body; you
2423                cannot, for example, write:
2424                
2425                  int i = ({ int j = 3; j + 1; });
2426                
2427                at class or namespace scope.  */
2428             if (!at_function_scope_p ())
2429               error ("statement-expressions are allowed only inside functions");
2430             /* Start the statement-expression.  */
2431             expr = begin_stmt_expr ();
2432             /* Parse the compound-statement.  */
2433             cp_parser_compound_statement (parser, true);
2434             /* Finish up.  */
2435             expr = finish_stmt_expr (expr, false);
2436           }
2437         else
2438           {
2439             /* Parse the parenthesized expression.  */
2440             expr = cp_parser_expression (parser);
2441             /* Let the front end know that this expression was
2442                enclosed in parentheses. This matters in case, for
2443                example, the expression is of the form `A::B', since
2444                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2445                not.  */
2446             finish_parenthesized_expr (expr);
2447           }
2448         /* The `>' token might be the end of a template-id or
2449            template-parameter-list now.  */
2450         parser->greater_than_is_operator_p 
2451           = saved_greater_than_is_operator_p;
2452         /* Consume the `)'.  */
2453         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2454           cp_parser_skip_to_end_of_statement (parser);
2455
2456         return expr;
2457       }
2458
2459     case CPP_KEYWORD:
2460       switch (token->keyword)
2461         {
2462           /* These two are the boolean literals.  */
2463         case RID_TRUE:
2464           cp_lexer_consume_token (parser->lexer);
2465           return boolean_true_node;
2466         case RID_FALSE:
2467           cp_lexer_consume_token (parser->lexer);
2468           return boolean_false_node;
2469           
2470           /* The `__null' literal.  */
2471         case RID_NULL:
2472           cp_lexer_consume_token (parser->lexer);
2473           return null_node;
2474
2475           /* Recognize the `this' keyword.  */
2476         case RID_THIS:
2477           cp_lexer_consume_token (parser->lexer);
2478           if (parser->local_variables_forbidden_p)
2479             {
2480               error ("`this' may not be used in this context");
2481               return error_mark_node;
2482             }
2483           /* Pointers cannot appear in constant-expressions.  */
2484           if (cp_parser_non_integral_constant_expression (parser,
2485                                                           "`this'"))
2486             return error_mark_node;
2487           return finish_this_expr ();
2488
2489           /* The `operator' keyword can be the beginning of an
2490              id-expression.  */
2491         case RID_OPERATOR:
2492           goto id_expression;
2493
2494         case RID_FUNCTION_NAME:
2495         case RID_PRETTY_FUNCTION_NAME:
2496         case RID_C99_FUNCTION_NAME:
2497           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2498              __func__ are the names of variables -- but they are
2499              treated specially.  Therefore, they are handled here,
2500              rather than relying on the generic id-expression logic
2501              below.  Grammatically, these names are id-expressions.  
2502
2503              Consume the token.  */
2504           token = cp_lexer_consume_token (parser->lexer);
2505           /* Look up the name.  */
2506           return finish_fname (token->value);
2507
2508         case RID_VA_ARG:
2509           {
2510             tree expression;
2511             tree type;
2512
2513             /* The `__builtin_va_arg' construct is used to handle
2514                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2515             cp_lexer_consume_token (parser->lexer);
2516             /* Look for the opening `('.  */
2517             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2518             /* Now, parse the assignment-expression.  */
2519             expression = cp_parser_assignment_expression (parser);
2520             /* Look for the `,'.  */
2521             cp_parser_require (parser, CPP_COMMA, "`,'");
2522             /* Parse the type-id.  */
2523             type = cp_parser_type_id (parser);
2524             /* Look for the closing `)'.  */
2525             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2526             /* Using `va_arg' in a constant-expression is not
2527                allowed.  */
2528             if (cp_parser_non_integral_constant_expression (parser,
2529                                                             "`va_arg'"))
2530               return error_mark_node;
2531             return build_x_va_arg (expression, type);
2532           }
2533
2534         case RID_OFFSETOF:
2535           {
2536             tree expression;
2537             bool saved_in_offsetof_p;
2538
2539             /* Consume the "__offsetof__" token.  */
2540             cp_lexer_consume_token (parser->lexer);
2541             /* Consume the opening `('.  */
2542             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2543             /* Parse the parenthesized (almost) constant-expression.  */
2544             saved_in_offsetof_p = parser->in_offsetof_p;
2545             parser->in_offsetof_p = true;
2546             expression 
2547               = cp_parser_constant_expression (parser,
2548                                                /*allow_non_constant_p=*/false,
2549                                                /*non_constant_p=*/NULL);
2550             parser->in_offsetof_p = saved_in_offsetof_p;
2551             /* Consume the closing ')'.  */
2552             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2553
2554             return expression;
2555           }
2556
2557         default:
2558           cp_parser_error (parser, "expected primary-expression");
2559           return error_mark_node;
2560         }
2561
2562       /* An id-expression can start with either an identifier, a
2563          `::' as the beginning of a qualified-id, or the "operator"
2564          keyword.  */
2565     case CPP_NAME:
2566     case CPP_SCOPE:
2567     case CPP_TEMPLATE_ID:
2568     case CPP_NESTED_NAME_SPECIFIER:
2569       {
2570         tree id_expression;
2571         tree decl;
2572         const char *error_msg;
2573
2574       id_expression:
2575         /* Parse the id-expression.  */
2576         id_expression 
2577           = cp_parser_id_expression (parser, 
2578                                      /*template_keyword_p=*/false,
2579                                      /*check_dependency_p=*/true,
2580                                      /*template_p=*/NULL,
2581                                      /*declarator_p=*/false);
2582         if (id_expression == error_mark_node)
2583           return error_mark_node;
2584         /* If we have a template-id, then no further lookup is
2585            required.  If the template-id was for a template-class, we
2586            will sometimes have a TYPE_DECL at this point.  */
2587         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2588             || TREE_CODE (id_expression) == TYPE_DECL)
2589           decl = id_expression;
2590         /* Look up the name.  */
2591         else 
2592           {
2593             decl = cp_parser_lookup_name_simple (parser, id_expression);
2594             /* If name lookup gives us a SCOPE_REF, then the
2595                qualifying scope was dependent.  Just propagate the
2596                name.  */
2597             if (TREE_CODE (decl) == SCOPE_REF)
2598               {
2599                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2600                   *qualifying_class = TREE_OPERAND (decl, 0);
2601                 return decl;
2602               }
2603             /* Check to see if DECL is a local variable in a context
2604                where that is forbidden.  */
2605             if (parser->local_variables_forbidden_p
2606                 && local_variable_p (decl))
2607               {
2608                 /* It might be that we only found DECL because we are
2609                    trying to be generous with pre-ISO scoping rules.
2610                    For example, consider:
2611
2612                      int i;
2613                      void g() {
2614                        for (int i = 0; i < 10; ++i) {}
2615                        extern void f(int j = i);
2616                      }
2617
2618                    Here, name look up will originally find the out 
2619                    of scope `i'.  We need to issue a warning message,
2620                    but then use the global `i'.  */
2621                 decl = check_for_out_of_scope_variable (decl);
2622                 if (local_variable_p (decl))
2623                   {
2624                     error ("local variable `%D' may not appear in this context",
2625                            decl);
2626                     return error_mark_node;
2627                   }
2628               }
2629           }
2630         
2631         decl = finish_id_expression (id_expression, decl, parser->scope, 
2632                                      idk, qualifying_class,
2633                                      parser->integral_constant_expression_p,
2634                                      parser->allow_non_integral_constant_expression_p,
2635                                      &parser->non_integral_constant_expression_p,
2636                                      &error_msg);
2637         if (error_msg)
2638           cp_parser_error (parser, error_msg);
2639         return decl;
2640       }
2641
2642       /* Anything else is an error.  */
2643     default:
2644       cp_parser_error (parser, "expected primary-expression");
2645       return error_mark_node;
2646     }
2647 }
2648
2649 /* Parse an id-expression.
2650
2651    id-expression:
2652      unqualified-id
2653      qualified-id
2654
2655    qualified-id:
2656      :: [opt] nested-name-specifier template [opt] unqualified-id
2657      :: identifier
2658      :: operator-function-id
2659      :: template-id
2660
2661    Return a representation of the unqualified portion of the
2662    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2663    a `::' or nested-name-specifier.
2664
2665    Often, if the id-expression was a qualified-id, the caller will
2666    want to make a SCOPE_REF to represent the qualified-id.  This
2667    function does not do this in order to avoid wastefully creating
2668    SCOPE_REFs when they are not required.
2669
2670    If TEMPLATE_KEYWORD_P is true, then we have just seen the
2671    `template' keyword.
2672
2673    If CHECK_DEPENDENCY_P is false, then names are looked up inside
2674    uninstantiated templates.  
2675
2676    If *TEMPLATE_P is non-NULL, it is set to true iff the
2677    `template' keyword is used to explicitly indicate that the entity
2678    named is a template.  
2679
2680    If DECLARATOR_P is true, the id-expression is appearing as part of
2681    a declarator, rather than as part of an expression.  */
2682
2683 static tree
2684 cp_parser_id_expression (cp_parser *parser,
2685                          bool template_keyword_p,
2686                          bool check_dependency_p,
2687                          bool *template_p,
2688                          bool declarator_p)
2689 {
2690   bool global_scope_p;
2691   bool nested_name_specifier_p;
2692
2693   /* Assume the `template' keyword was not used.  */
2694   if (template_p)
2695     *template_p = false;
2696
2697   /* Look for the optional `::' operator.  */
2698   global_scope_p 
2699     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) 
2700        != NULL_TREE);
2701   /* Look for the optional nested-name-specifier.  */
2702   nested_name_specifier_p 
2703     = (cp_parser_nested_name_specifier_opt (parser,
2704                                             /*typename_keyword_p=*/false,
2705                                             check_dependency_p,
2706                                             /*type_p=*/false,
2707                                             declarator_p)
2708        != NULL_TREE);
2709   /* If there is a nested-name-specifier, then we are looking at
2710      the first qualified-id production.  */
2711   if (nested_name_specifier_p)
2712     {
2713       tree saved_scope;
2714       tree saved_object_scope;
2715       tree saved_qualifying_scope;
2716       tree unqualified_id;
2717       bool is_template;
2718
2719       /* See if the next token is the `template' keyword.  */
2720       if (!template_p)
2721         template_p = &is_template;
2722       *template_p = cp_parser_optional_template_keyword (parser);
2723       /* Name lookup we do during the processing of the
2724          unqualified-id might obliterate SCOPE.  */
2725       saved_scope = parser->scope;
2726       saved_object_scope = parser->object_scope;
2727       saved_qualifying_scope = parser->qualifying_scope;
2728       /* Process the final unqualified-id.  */
2729       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2730                                                  check_dependency_p,
2731                                                  declarator_p);
2732       /* Restore the SAVED_SCOPE for our caller.  */
2733       parser->scope = saved_scope;
2734       parser->object_scope = saved_object_scope;
2735       parser->qualifying_scope = saved_qualifying_scope;
2736
2737       return unqualified_id;
2738     }
2739   /* Otherwise, if we are in global scope, then we are looking at one
2740      of the other qualified-id productions.  */
2741   else if (global_scope_p)
2742     {
2743       cp_token *token;
2744       tree id;
2745
2746       /* Peek at the next token.  */
2747       token = cp_lexer_peek_token (parser->lexer);
2748
2749       /* If it's an identifier, and the next token is not a "<", then
2750          we can avoid the template-id case.  This is an optimization
2751          for this common case.  */
2752       if (token->type == CPP_NAME 
2753           && !cp_parser_nth_token_starts_template_argument_list_p 
2754                (parser, 2))
2755         return cp_parser_identifier (parser);
2756
2757       cp_parser_parse_tentatively (parser);
2758       /* Try a template-id.  */
2759       id = cp_parser_template_id (parser, 
2760                                   /*template_keyword_p=*/false,
2761                                   /*check_dependency_p=*/true,
2762                                   declarator_p);
2763       /* If that worked, we're done.  */
2764       if (cp_parser_parse_definitely (parser))
2765         return id;
2766
2767       /* Peek at the next token.  (Changes in the token buffer may
2768          have invalidated the pointer obtained above.)  */
2769       token = cp_lexer_peek_token (parser->lexer);
2770
2771       switch (token->type)
2772         {
2773         case CPP_NAME:
2774           return cp_parser_identifier (parser);
2775
2776         case CPP_KEYWORD:
2777           if (token->keyword == RID_OPERATOR)
2778             return cp_parser_operator_function_id (parser);
2779           /* Fall through.  */
2780           
2781         default:
2782           cp_parser_error (parser, "expected id-expression");
2783           return error_mark_node;
2784         }
2785     }
2786   else
2787     return cp_parser_unqualified_id (parser, template_keyword_p,
2788                                      /*check_dependency_p=*/true,
2789                                      declarator_p);
2790 }
2791
2792 /* Parse an unqualified-id.
2793
2794    unqualified-id:
2795      identifier
2796      operator-function-id
2797      conversion-function-id
2798      ~ class-name
2799      template-id
2800
2801    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2802    keyword, in a construct like `A::template ...'.
2803
2804    Returns a representation of unqualified-id.  For the `identifier'
2805    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
2806    production a BIT_NOT_EXPR is returned; the operand of the
2807    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
2808    other productions, see the documentation accompanying the
2809    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
2810    names are looked up in uninstantiated templates.  If DECLARATOR_P
2811    is true, the unqualified-id is appearing as part of a declarator,
2812    rather than as part of an expression.  */
2813
2814 static tree
2815 cp_parser_unqualified_id (cp_parser* parser, 
2816                           bool template_keyword_p,
2817                           bool check_dependency_p,
2818                           bool declarator_p)
2819 {
2820   cp_token *token;
2821
2822   /* Peek at the next token.  */
2823   token = cp_lexer_peek_token (parser->lexer);
2824   
2825   switch (token->type)
2826     {
2827     case CPP_NAME:
2828       {
2829         tree id;
2830
2831         /* We don't know yet whether or not this will be a
2832            template-id.  */
2833         cp_parser_parse_tentatively (parser);
2834         /* Try a template-id.  */
2835         id = cp_parser_template_id (parser, template_keyword_p,
2836                                     check_dependency_p,
2837                                     declarator_p);
2838         /* If it worked, we're done.  */
2839         if (cp_parser_parse_definitely (parser))
2840           return id;
2841         /* Otherwise, it's an ordinary identifier.  */
2842         return cp_parser_identifier (parser);
2843       }
2844
2845     case CPP_TEMPLATE_ID:
2846       return cp_parser_template_id (parser, template_keyword_p,
2847                                     check_dependency_p,
2848                                     declarator_p);
2849
2850     case CPP_COMPL:
2851       {
2852         tree type_decl;
2853         tree qualifying_scope;
2854         tree object_scope;
2855         tree scope;
2856         bool done;
2857
2858         /* Consume the `~' token.  */
2859         cp_lexer_consume_token (parser->lexer);
2860         /* Parse the class-name.  The standard, as written, seems to
2861            say that:
2862
2863              template <typename T> struct S { ~S (); };
2864              template <typename T> S<T>::~S() {}
2865
2866            is invalid, since `~' must be followed by a class-name, but
2867            `S<T>' is dependent, and so not known to be a class.
2868            That's not right; we need to look in uninstantiated
2869            templates.  A further complication arises from:
2870
2871              template <typename T> void f(T t) {
2872                t.T::~T();
2873              } 
2874
2875            Here, it is not possible to look up `T' in the scope of `T'
2876            itself.  We must look in both the current scope, and the
2877            scope of the containing complete expression.  
2878
2879            Yet another issue is:
2880
2881              struct S {
2882                int S;
2883                ~S();
2884              };
2885
2886              S::~S() {}
2887
2888            The standard does not seem to say that the `S' in `~S'
2889            should refer to the type `S' and not the data member
2890            `S::S'.  */
2891
2892         /* DR 244 says that we look up the name after the "~" in the
2893            same scope as we looked up the qualifying name.  That idea
2894            isn't fully worked out; it's more complicated than that.  */
2895         scope = parser->scope;
2896         object_scope = parser->object_scope;
2897         qualifying_scope = parser->qualifying_scope;
2898
2899         /* If the name is of the form "X::~X" it's OK.  */
2900         if (scope && TYPE_P (scope)
2901             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2902             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
2903                 == CPP_OPEN_PAREN)
2904             && (cp_lexer_peek_token (parser->lexer)->value 
2905                 == TYPE_IDENTIFIER (scope)))
2906           {
2907             cp_lexer_consume_token (parser->lexer);
2908             return build_nt (BIT_NOT_EXPR, scope);
2909           }
2910
2911         /* If there was an explicit qualification (S::~T), first look
2912            in the scope given by the qualification (i.e., S).  */
2913         done = false;
2914         type_decl = NULL_TREE;
2915         if (scope)
2916           {
2917             cp_parser_parse_tentatively (parser);
2918             type_decl = cp_parser_class_name (parser, 
2919                                               /*typename_keyword_p=*/false,
2920                                               /*template_keyword_p=*/false,
2921                                               /*type_p=*/false,
2922                                               /*check_dependency=*/false,
2923                                               /*class_head_p=*/false,
2924                                               declarator_p);
2925             if (cp_parser_parse_definitely (parser))
2926               done = true;
2927           }
2928         /* In "N::S::~S", look in "N" as well.  */
2929         if (!done && scope && qualifying_scope)
2930           {
2931             cp_parser_parse_tentatively (parser);
2932             parser->scope = qualifying_scope;
2933             parser->object_scope = NULL_TREE;
2934             parser->qualifying_scope = NULL_TREE;
2935             type_decl 
2936               = cp_parser_class_name (parser, 
2937                                       /*typename_keyword_p=*/false,
2938                                       /*template_keyword_p=*/false,
2939                                       /*type_p=*/false,
2940                                       /*check_dependency=*/false,
2941                                       /*class_head_p=*/false,
2942                                       declarator_p);
2943             if (cp_parser_parse_definitely (parser))
2944               done = true;
2945           }
2946         /* In "p->S::~T", look in the scope given by "*p" as well.  */
2947         else if (!done && object_scope)
2948           {
2949             cp_parser_parse_tentatively (parser);
2950             parser->scope = object_scope;
2951             parser->object_scope = NULL_TREE;
2952             parser->qualifying_scope = NULL_TREE;
2953             type_decl 
2954               = cp_parser_class_name (parser, 
2955                                       /*typename_keyword_p=*/false,
2956                                       /*template_keyword_p=*/false,
2957                                       /*type_p=*/false,
2958                                       /*check_dependency=*/false,
2959                                       /*class_head_p=*/false,
2960                                       declarator_p);
2961             if (cp_parser_parse_definitely (parser))
2962               done = true;
2963           }
2964         /* Look in the surrounding context.  */
2965         if (!done)
2966           {
2967             parser->scope = NULL_TREE;
2968             parser->object_scope = NULL_TREE;
2969             parser->qualifying_scope = NULL_TREE;
2970             type_decl 
2971               = cp_parser_class_name (parser, 
2972                                       /*typename_keyword_p=*/false,
2973                                       /*template_keyword_p=*/false,
2974                                       /*type_p=*/false,
2975                                       /*check_dependency=*/false,
2976                                       /*class_head_p=*/false,
2977                                       declarator_p);
2978           }
2979         /* If an error occurred, assume that the name of the
2980            destructor is the same as the name of the qualifying
2981            class.  That allows us to keep parsing after running
2982            into ill-formed destructor names.  */
2983         if (type_decl == error_mark_node && scope && TYPE_P (scope))
2984           return build_nt (BIT_NOT_EXPR, scope);
2985         else if (type_decl == error_mark_node)
2986           return error_mark_node;
2987
2988         /* [class.dtor]
2989
2990            A typedef-name that names a class shall not be used as the
2991            identifier in the declarator for a destructor declaration.  */
2992         if (declarator_p 
2993             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2994             && !DECL_SELF_REFERENCE_P (type_decl))
2995           error ("typedef-name `%D' used as destructor declarator",
2996                  type_decl);
2997
2998         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2999       }
3000
3001     case CPP_KEYWORD:
3002       if (token->keyword == RID_OPERATOR)
3003         {
3004           tree id;
3005
3006           /* This could be a template-id, so we try that first.  */
3007           cp_parser_parse_tentatively (parser);
3008           /* Try a template-id.  */
3009           id = cp_parser_template_id (parser, template_keyword_p,
3010                                       /*check_dependency_p=*/true,
3011                                       declarator_p);
3012           /* If that worked, we're done.  */
3013           if (cp_parser_parse_definitely (parser))
3014             return id;
3015           /* We still don't know whether we're looking at an
3016              operator-function-id or a conversion-function-id.  */
3017           cp_parser_parse_tentatively (parser);
3018           /* Try an operator-function-id.  */
3019           id = cp_parser_operator_function_id (parser);
3020           /* If that didn't work, try a conversion-function-id.  */
3021           if (!cp_parser_parse_definitely (parser))
3022             id = cp_parser_conversion_function_id (parser);
3023
3024           return id;
3025         }
3026       /* Fall through.  */
3027
3028     default:
3029       cp_parser_error (parser, "expected unqualified-id");
3030       return error_mark_node;
3031     }
3032 }
3033
3034 /* Parse an (optional) nested-name-specifier.
3035
3036    nested-name-specifier:
3037      class-or-namespace-name :: nested-name-specifier [opt]
3038      class-or-namespace-name :: template nested-name-specifier [opt]
3039
3040    PARSER->SCOPE should be set appropriately before this function is
3041    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3042    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3043    in name lookups.
3044
3045    Sets PARSER->SCOPE to the class (TYPE) or namespace
3046    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3047    it unchanged if there is no nested-name-specifier.  Returns the new
3048    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.  
3049
3050    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3051    part of a declaration and/or decl-specifier.  */
3052
3053 static tree
3054 cp_parser_nested_name_specifier_opt (cp_parser *parser, 
3055                                      bool typename_keyword_p, 
3056                                      bool check_dependency_p,
3057                                      bool type_p,
3058                                      bool is_declaration)
3059 {
3060   bool success = false;
3061   tree access_check = NULL_TREE;
3062   ptrdiff_t start;
3063   cp_token* token;
3064
3065   /* If the next token corresponds to a nested name specifier, there
3066      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3067      false, it may have been true before, in which case something 
3068      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3069      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3070      CHECK_DEPENDENCY_P is false, we have to fall through into the
3071      main loop.  */
3072   if (check_dependency_p
3073       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3074     {
3075       cp_parser_pre_parsed_nested_name_specifier (parser);
3076       return parser->scope;
3077     }
3078
3079   /* Remember where the nested-name-specifier starts.  */
3080   if (cp_parser_parsing_tentatively (parser)
3081       && !cp_parser_committed_to_tentative_parse (parser))
3082     {
3083       token = cp_lexer_peek_token (parser->lexer);
3084       start = cp_lexer_token_difference (parser->lexer,
3085                                          parser->lexer->first_token,
3086                                          token);
3087     }
3088   else
3089     start = -1;
3090
3091   push_deferring_access_checks (dk_deferred);
3092
3093   while (true)
3094     {
3095       tree new_scope;
3096       tree old_scope;
3097       tree saved_qualifying_scope;
3098       bool template_keyword_p;
3099
3100       /* Spot cases that cannot be the beginning of a
3101          nested-name-specifier.  */
3102       token = cp_lexer_peek_token (parser->lexer);
3103
3104       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3105          the already parsed nested-name-specifier.  */
3106       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3107         {
3108           /* Grab the nested-name-specifier and continue the loop.  */
3109           cp_parser_pre_parsed_nested_name_specifier (parser);
3110           success = true;
3111           continue;
3112         }
3113
3114       /* Spot cases that cannot be the beginning of a
3115          nested-name-specifier.  On the second and subsequent times
3116          through the loop, we look for the `template' keyword.  */
3117       if (success && token->keyword == RID_TEMPLATE)
3118         ;
3119       /* A template-id can start a nested-name-specifier.  */
3120       else if (token->type == CPP_TEMPLATE_ID)
3121         ;
3122       else
3123         {
3124           /* If the next token is not an identifier, then it is
3125              definitely not a class-or-namespace-name.  */
3126           if (token->type != CPP_NAME)
3127             break;
3128           /* If the following token is neither a `<' (to begin a
3129              template-id), nor a `::', then we are not looking at a
3130              nested-name-specifier.  */
3131           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3132           if (token->type != CPP_SCOPE
3133               && !cp_parser_nth_token_starts_template_argument_list_p
3134                   (parser, 2))
3135             break;
3136         }
3137
3138       /* The nested-name-specifier is optional, so we parse
3139          tentatively.  */
3140       cp_parser_parse_tentatively (parser);
3141
3142       /* Look for the optional `template' keyword, if this isn't the
3143          first time through the loop.  */
3144       if (success)
3145         template_keyword_p = cp_parser_optional_template_keyword (parser);
3146       else
3147         template_keyword_p = false;
3148
3149       /* Save the old scope since the name lookup we are about to do
3150          might destroy it.  */
3151       old_scope = parser->scope;
3152       saved_qualifying_scope = parser->qualifying_scope;
3153       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3154          look up names in "X<T>::I" in order to determine that "Y" is
3155          a template.  So, if we have a typename at this point, we make
3156          an effort to look through it.  */
3157       if (is_declaration 
3158           && !typename_keyword_p
3159           && parser->scope 
3160           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3161         parser->scope = resolve_typename_type (parser->scope, 
3162                                                /*only_current_p=*/false);
3163       /* Parse the qualifying entity.  */
3164       new_scope 
3165         = cp_parser_class_or_namespace_name (parser,
3166                                              typename_keyword_p,
3167                                              template_keyword_p,
3168                                              check_dependency_p,
3169                                              type_p,
3170                                              is_declaration);
3171       /* Look for the `::' token.  */
3172       cp_parser_require (parser, CPP_SCOPE, "`::'");
3173
3174       /* If we found what we wanted, we keep going; otherwise, we're
3175          done.  */
3176       if (!cp_parser_parse_definitely (parser))
3177         {
3178           bool error_p = false;
3179
3180           /* Restore the OLD_SCOPE since it was valid before the
3181              failed attempt at finding the last
3182              class-or-namespace-name.  */
3183           parser->scope = old_scope;
3184           parser->qualifying_scope = saved_qualifying_scope;
3185           /* If the next token is an identifier, and the one after
3186              that is a `::', then any valid interpretation would have
3187              found a class-or-namespace-name.  */
3188           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3189                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
3190                      == CPP_SCOPE)
3191                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
3192                      != CPP_COMPL))
3193             {
3194               token = cp_lexer_consume_token (parser->lexer);
3195               if (!error_p) 
3196                 {
3197                   tree decl;
3198
3199                   decl = cp_parser_lookup_name_simple (parser, token->value);
3200                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3201                     error ("`%D' used without template parameters",
3202                            decl);
3203                   else
3204                     cp_parser_name_lookup_error 
3205                       (parser, token->value, decl, 
3206                        "is not a class or namespace");
3207                   parser->scope = NULL_TREE;
3208                   error_p = true;
3209                   /* Treat this as a successful nested-name-specifier
3210                      due to:
3211
3212                      [basic.lookup.qual]
3213
3214                      If the name found is not a class-name (clause
3215                      _class_) or namespace-name (_namespace.def_), the
3216                      program is ill-formed.  */
3217                   success = true;
3218                 }
3219               cp_lexer_consume_token (parser->lexer);
3220             }
3221           break;
3222         }
3223
3224       /* We've found one valid nested-name-specifier.  */
3225       success = true;
3226       /* Make sure we look in the right scope the next time through
3227          the loop.  */
3228       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL 
3229                        ? TREE_TYPE (new_scope)
3230                        : new_scope);
3231       /* If it is a class scope, try to complete it; we are about to
3232          be looking up names inside the class.  */
3233       if (TYPE_P (parser->scope)
3234           /* Since checking types for dependency can be expensive,
3235              avoid doing it if the type is already complete.  */
3236           && !COMPLETE_TYPE_P (parser->scope)
3237           /* Do not try to complete dependent types.  */
3238           && !dependent_type_p (parser->scope))
3239         complete_type (parser->scope);
3240     }
3241
3242   /* Retrieve any deferred checks.  Do not pop this access checks yet
3243      so the memory will not be reclaimed during token replacing below.  */
3244   access_check = get_deferred_access_checks ();
3245
3246   /* If parsing tentatively, replace the sequence of tokens that makes
3247      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3248      token.  That way, should we re-parse the token stream, we will
3249      not have to repeat the effort required to do the parse, nor will
3250      we issue duplicate error messages.  */
3251   if (success && start >= 0)
3252     {
3253       /* Find the token that corresponds to the start of the
3254          template-id.  */
3255       token = cp_lexer_advance_token (parser->lexer, 
3256                                       parser->lexer->first_token,
3257                                       start);
3258
3259       /* Reset the contents of the START token.  */
3260       token->type = CPP_NESTED_NAME_SPECIFIER;
3261       token->value = build_tree_list (access_check, parser->scope);
3262       TREE_TYPE (token->value) = parser->qualifying_scope;
3263       token->keyword = RID_MAX;
3264       /* Purge all subsequent tokens.  */
3265       cp_lexer_purge_tokens_after (parser->lexer, token);
3266     }
3267
3268   pop_deferring_access_checks ();
3269   return success ? parser->scope : NULL_TREE;
3270 }
3271
3272 /* Parse a nested-name-specifier.  See
3273    cp_parser_nested_name_specifier_opt for details.  This function
3274    behaves identically, except that it will an issue an error if no
3275    nested-name-specifier is present, and it will return
3276    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3277    is present.  */
3278
3279 static tree
3280 cp_parser_nested_name_specifier (cp_parser *parser, 
3281                                  bool typename_keyword_p, 
3282                                  bool check_dependency_p,
3283                                  bool type_p,
3284                                  bool is_declaration)
3285 {
3286   tree scope;
3287
3288   /* Look for the nested-name-specifier.  */
3289   scope = cp_parser_nested_name_specifier_opt (parser,
3290                                                typename_keyword_p,
3291                                                check_dependency_p,
3292                                                type_p,
3293                                                is_declaration);
3294   /* If it was not present, issue an error message.  */
3295   if (!scope)
3296     {
3297       cp_parser_error (parser, "expected nested-name-specifier");
3298       parser->scope = NULL_TREE;
3299       return error_mark_node;
3300     }
3301
3302   return scope;
3303 }
3304
3305 /* Parse a class-or-namespace-name.
3306
3307    class-or-namespace-name:
3308      class-name
3309      namespace-name
3310
3311    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3312    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3313    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3314    TYPE_P is TRUE iff the next name should be taken as a class-name,
3315    even the same name is declared to be another entity in the same
3316    scope.
3317
3318    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3319    specified by the class-or-namespace-name.  If neither is found the
3320    ERROR_MARK_NODE is returned.  */
3321
3322 static tree
3323 cp_parser_class_or_namespace_name (cp_parser *parser, 
3324                                    bool typename_keyword_p,
3325                                    bool template_keyword_p,
3326                                    bool check_dependency_p,
3327                                    bool type_p,
3328                                    bool is_declaration)
3329 {
3330   tree saved_scope;
3331   tree saved_qualifying_scope;
3332   tree saved_object_scope;
3333   tree scope;
3334   bool only_class_p;
3335
3336   /* Before we try to parse the class-name, we must save away the
3337      current PARSER->SCOPE since cp_parser_class_name will destroy
3338      it.  */
3339   saved_scope = parser->scope;
3340   saved_qualifying_scope = parser->qualifying_scope;
3341   saved_object_scope = parser->object_scope;
3342   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3343      there is no need to look for a namespace-name.  */
3344   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3345   if (!only_class_p)
3346     cp_parser_parse_tentatively (parser);
3347   scope = cp_parser_class_name (parser, 
3348                                 typename_keyword_p,
3349                                 template_keyword_p,
3350                                 type_p,
3351                                 check_dependency_p,
3352                                 /*class_head_p=*/false,
3353                                 is_declaration);
3354   /* If that didn't work, try for a namespace-name.  */
3355   if (!only_class_p && !cp_parser_parse_definitely (parser))
3356     {
3357       /* Restore the saved scope.  */
3358       parser->scope = saved_scope;
3359       parser->qualifying_scope = saved_qualifying_scope;
3360       parser->object_scope = saved_object_scope;
3361       /* If we are not looking at an identifier followed by the scope
3362          resolution operator, then this is not part of a
3363          nested-name-specifier.  (Note that this function is only used
3364          to parse the components of a nested-name-specifier.)  */
3365       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3366           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3367         return error_mark_node;
3368       scope = cp_parser_namespace_name (parser);
3369     }
3370
3371   return scope;
3372 }
3373
3374 /* Parse a postfix-expression.
3375
3376    postfix-expression:
3377      primary-expression
3378      postfix-expression [ expression ]
3379      postfix-expression ( expression-list [opt] )
3380      simple-type-specifier ( expression-list [opt] )
3381      typename :: [opt] nested-name-specifier identifier 
3382        ( expression-list [opt] )
3383      typename :: [opt] nested-name-specifier template [opt] template-id
3384        ( expression-list [opt] )
3385      postfix-expression . template [opt] id-expression
3386      postfix-expression -> template [opt] id-expression
3387      postfix-expression . pseudo-destructor-name
3388      postfix-expression -> pseudo-destructor-name
3389      postfix-expression ++
3390      postfix-expression --
3391      dynamic_cast < type-id > ( expression )
3392      static_cast < type-id > ( expression )
3393      reinterpret_cast < type-id > ( expression )
3394      const_cast < type-id > ( expression )
3395      typeid ( expression )
3396      typeid ( type-id )
3397
3398    GNU Extension:
3399      
3400    postfix-expression:
3401      ( type-id ) { initializer-list , [opt] }
3402
3403    This extension is a GNU version of the C99 compound-literal
3404    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3405    but they are essentially the same concept.)
3406
3407    If ADDRESS_P is true, the postfix expression is the operand of the
3408    `&' operator.
3409
3410    Returns a representation of the expression.  */
3411
3412 static tree
3413 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3414 {
3415   cp_token *token;
3416   enum rid keyword;
3417   cp_id_kind idk = CP_ID_KIND_NONE;
3418   tree postfix_expression = NULL_TREE;
3419   /* Non-NULL only if the current postfix-expression can be used to
3420      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3421      class used to qualify the member.  */
3422   tree qualifying_class = NULL_TREE;
3423
3424   /* Peek at the next token.  */
3425   token = cp_lexer_peek_token (parser->lexer);
3426   /* Some of the productions are determined by keywords.  */
3427   keyword = token->keyword;
3428   switch (keyword)
3429     {
3430     case RID_DYNCAST:
3431     case RID_STATCAST:
3432     case RID_REINTCAST:
3433     case RID_CONSTCAST:
3434       {
3435         tree type;
3436         tree expression;
3437         const char *saved_message;
3438
3439         /* All of these can be handled in the same way from the point
3440            of view of parsing.  Begin by consuming the token
3441            identifying the cast.  */
3442         cp_lexer_consume_token (parser->lexer);
3443         
3444         /* New types cannot be defined in the cast.  */
3445         saved_message = parser->type_definition_forbidden_message;
3446         parser->type_definition_forbidden_message
3447           = "types may not be defined in casts";
3448
3449         /* Look for the opening `<'.  */
3450         cp_parser_require (parser, CPP_LESS, "`<'");
3451         /* Parse the type to which we are casting.  */
3452         type = cp_parser_type_id (parser);
3453         /* Look for the closing `>'.  */
3454         cp_parser_require (parser, CPP_GREATER, "`>'");
3455         /* Restore the old message.  */
3456         parser->type_definition_forbidden_message = saved_message;
3457
3458         /* And the expression which is being cast.  */
3459         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3460         expression = cp_parser_expression (parser);
3461         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3462
3463         /* Only type conversions to integral or enumeration types
3464            can be used in constant-expressions.  */
3465         if (parser->integral_constant_expression_p
3466             && !dependent_type_p (type)
3467             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3468             /* A cast to pointer or reference type is allowed in the
3469                implementation of "offsetof".  */
3470             && !(parser->in_offsetof_p && POINTER_TYPE_P (type))
3471             && (cp_parser_non_integral_constant_expression 
3472                 (parser,
3473                  "a cast to a type other than an integral or "
3474                  "enumeration type")))
3475           return error_mark_node;
3476
3477         switch (keyword)
3478           {
3479           case RID_DYNCAST:
3480             postfix_expression
3481               = build_dynamic_cast (type, expression);
3482             break;
3483           case RID_STATCAST:
3484             postfix_expression
3485               = build_static_cast (type, expression);
3486             break;
3487           case RID_REINTCAST:
3488             postfix_expression
3489               = build_reinterpret_cast (type, expression);
3490             break;
3491           case RID_CONSTCAST:
3492             postfix_expression
3493               = build_const_cast (type, expression);
3494             break;
3495           default:
3496             abort ();
3497           }
3498       }
3499       break;
3500
3501     case RID_TYPEID:
3502       {
3503         tree type;
3504         const char *saved_message;
3505         bool saved_in_type_id_in_expr_p;
3506
3507         /* Consume the `typeid' token.  */
3508         cp_lexer_consume_token (parser->lexer);
3509         /* Look for the `(' token.  */
3510         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3511         /* Types cannot be defined in a `typeid' expression.  */
3512         saved_message = parser->type_definition_forbidden_message;
3513         parser->type_definition_forbidden_message
3514           = "types may not be defined in a `typeid\' expression";
3515         /* We can't be sure yet whether we're looking at a type-id or an
3516            expression.  */
3517         cp_parser_parse_tentatively (parser);
3518         /* Try a type-id first.  */
3519         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3520         parser->in_type_id_in_expr_p = true;
3521         type = cp_parser_type_id (parser);
3522         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3523         /* Look for the `)' token.  Otherwise, we can't be sure that
3524            we're not looking at an expression: consider `typeid (int
3525            (3))', for example.  */
3526         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3527         /* If all went well, simply lookup the type-id.  */
3528         if (cp_parser_parse_definitely (parser))
3529           postfix_expression = get_typeid (type);
3530         /* Otherwise, fall back to the expression variant.  */
3531         else
3532           {
3533             tree expression;
3534
3535             /* Look for an expression.  */
3536             expression = cp_parser_expression (parser);
3537             /* Compute its typeid.  */
3538             postfix_expression = build_typeid (expression);
3539             /* Look for the `)' token.  */
3540             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3541           }
3542         /* `typeid' may not appear in an integral constant expression.  */
3543         if (cp_parser_non_integral_constant_expression(parser, 
3544                                                        "`typeid' operator"))
3545           return error_mark_node;
3546         /* Restore the saved message.  */
3547         parser->type_definition_forbidden_message = saved_message;
3548       }
3549       break;
3550       
3551     case RID_TYPENAME:
3552       {
3553         bool template_p = false;
3554         tree id;
3555         tree type;
3556         tree scope;
3557
3558         /* Consume the `typename' token.  */
3559         cp_lexer_consume_token (parser->lexer);
3560         /* Look for the optional `::' operator.  */
3561         cp_parser_global_scope_opt (parser, 
3562                                     /*current_scope_valid_p=*/false);
3563         /* Look for the nested-name-specifier.  In case of error here,
3564            consume the trailing id to avoid subsequent error messages
3565            for usual cases.  */
3566         scope = cp_parser_nested_name_specifier (parser,
3567                                                  /*typename_keyword_p=*/true,
3568                                                  /*check_dependency_p=*/true,
3569                                                  /*type_p=*/true,
3570                                                  /*is_declaration=*/true);
3571
3572         /* Look for the optional `template' keyword.  */
3573         template_p = cp_parser_optional_template_keyword (parser);
3574         /* We don't know whether we're looking at a template-id or an
3575            identifier.  */
3576         cp_parser_parse_tentatively (parser);
3577         /* Try a template-id.  */
3578         id = cp_parser_template_id (parser, template_p,
3579                                     /*check_dependency_p=*/true,
3580                                     /*is_declaration=*/true);
3581         /* If that didn't work, try an identifier.  */
3582         if (!cp_parser_parse_definitely (parser))
3583           id = cp_parser_identifier (parser);
3584
3585         /* Don't process id if nested name specifier is invalid.  */
3586         if (scope == error_mark_node)
3587           return error_mark_node;
3588         /* If we look up a template-id in a non-dependent qualifying
3589            scope, there's no need to create a dependent type.  */
3590         else if (TREE_CODE (id) == TYPE_DECL
3591             && !dependent_type_p (parser->scope))
3592           type = TREE_TYPE (id);
3593         /* Create a TYPENAME_TYPE to represent the type to which the
3594            functional cast is being performed.  */
3595         else
3596           type = make_typename_type (parser->scope, id, 
3597                                      /*complain=*/1);
3598
3599         postfix_expression = cp_parser_functional_cast (parser, type);
3600       }
3601       break;
3602
3603     default:
3604       {
3605         tree type;
3606
3607         /* If the next thing is a simple-type-specifier, we may be
3608            looking at a functional cast.  We could also be looking at
3609            an id-expression.  So, we try the functional cast, and if
3610            that doesn't work we fall back to the primary-expression.  */
3611         cp_parser_parse_tentatively (parser);
3612         /* Look for the simple-type-specifier.  */
3613         type = cp_parser_simple_type_specifier (parser, 
3614                                                 CP_PARSER_FLAGS_NONE,
3615                                                 /*identifier_p=*/false);
3616         /* Parse the cast itself.  */
3617         if (!cp_parser_error_occurred (parser))
3618           postfix_expression 
3619             = cp_parser_functional_cast (parser, type);
3620         /* If that worked, we're done.  */
3621         if (cp_parser_parse_definitely (parser))
3622           break;
3623
3624         /* If the functional-cast didn't work out, try a
3625            compound-literal.  */
3626         if (cp_parser_allow_gnu_extensions_p (parser)
3627             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3628           {
3629             tree initializer_list = NULL_TREE;
3630             bool saved_in_type_id_in_expr_p;
3631
3632             cp_parser_parse_tentatively (parser);
3633             /* Consume the `('.  */
3634             cp_lexer_consume_token (parser->lexer);
3635             /* Parse the type.  */
3636             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3637             parser->in_type_id_in_expr_p = true;
3638             type = cp_parser_type_id (parser);
3639             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3640             /* Look for the `)'.  */
3641             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3642             /* Look for the `{'.  */
3643             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3644             /* If things aren't going well, there's no need to
3645                keep going.  */
3646             if (!cp_parser_error_occurred (parser))
3647               {
3648                 bool non_constant_p;
3649                 /* Parse the initializer-list.  */
3650                 initializer_list 
3651                   = cp_parser_initializer_list (parser, &non_constant_p);
3652                 /* Allow a trailing `,'.  */
3653                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3654                   cp_lexer_consume_token (parser->lexer);
3655                 /* Look for the final `}'.  */
3656                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3657               }
3658             /* If that worked, we're definitely looking at a
3659                compound-literal expression.  */
3660             if (cp_parser_parse_definitely (parser))
3661               {
3662                 /* Warn the user that a compound literal is not
3663                    allowed in standard C++.  */
3664                 if (pedantic)
3665                   pedwarn ("ISO C++ forbids compound-literals");
3666                 /* Form the representation of the compound-literal.  */
3667                 postfix_expression 
3668                   = finish_compound_literal (type, initializer_list);
3669                 break;
3670               }
3671           }
3672
3673         /* It must be a primary-expression.  */
3674         postfix_expression = cp_parser_primary_expression (parser, 
3675                                                            &idk,
3676                                                            &qualifying_class);
3677       }
3678       break;
3679     }
3680
3681   /* If we were avoiding committing to the processing of a
3682      qualified-id until we knew whether or not we had a
3683      pointer-to-member, we now know.  */
3684   if (qualifying_class)
3685     {
3686       bool done;
3687
3688       /* Peek at the next token.  */
3689       token = cp_lexer_peek_token (parser->lexer);
3690       done = (token->type != CPP_OPEN_SQUARE
3691               && token->type != CPP_OPEN_PAREN
3692               && token->type != CPP_DOT
3693               && token->type != CPP_DEREF
3694               && token->type != CPP_PLUS_PLUS
3695               && token->type != CPP_MINUS_MINUS);
3696
3697       postfix_expression = finish_qualified_id_expr (qualifying_class,
3698                                                      postfix_expression,
3699                                                      done,
3700                                                      address_p);
3701       if (done)
3702         return postfix_expression;
3703     }
3704
3705   /* Keep looping until the postfix-expression is complete.  */
3706   while (true)
3707     {
3708       if (idk == CP_ID_KIND_UNQUALIFIED
3709           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3710           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3711         /* It is not a Koenig lookup function call.  */
3712         postfix_expression 
3713           = unqualified_name_lookup_error (postfix_expression);
3714       
3715       /* Peek at the next token.  */
3716       token = cp_lexer_peek_token (parser->lexer);
3717
3718       switch (token->type)
3719         {
3720         case CPP_OPEN_SQUARE:
3721           /* postfix-expression [ expression ] */
3722           {
3723             tree index;
3724
3725             /* Consume the `[' token.  */
3726             cp_lexer_consume_token (parser->lexer);
3727             /* Parse the index expression.  */
3728             index = cp_parser_expression (parser);
3729             /* Look for the closing `]'.  */
3730             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3731
3732             /* Build the ARRAY_REF.  */
3733             postfix_expression 
3734               = grok_array_decl (postfix_expression, index);
3735             idk = CP_ID_KIND_NONE;
3736             /* Array references are not permitted in
3737                constant-expressions (but they are allowed
3738                in offsetof).  */
3739             if (!parser->in_offsetof_p
3740                 && cp_parser_non_integral_constant_expression
3741                     (parser, "an array reference"))
3742               postfix_expression = error_mark_node;
3743           }
3744           break;
3745
3746         case CPP_OPEN_PAREN:
3747           /* postfix-expression ( expression-list [opt] ) */
3748           {
3749             bool koenig_p;
3750             tree args = (cp_parser_parenthesized_expression_list 
3751                          (parser, false, /*non_constant_p=*/NULL));
3752
3753             if (args == error_mark_node)
3754               {
3755                 postfix_expression = error_mark_node;
3756                 break;
3757               }
3758             
3759             /* Function calls are not permitted in
3760                constant-expressions.  */
3761             if (cp_parser_non_integral_constant_expression (parser,
3762                                                             "a function call"))
3763               {
3764                 postfix_expression = error_mark_node;
3765                 break;
3766               }
3767
3768             koenig_p = false;
3769             if (idk == CP_ID_KIND_UNQUALIFIED)
3770               {
3771                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3772                   {
3773                     if (args)
3774                       {
3775                         koenig_p = true;
3776                         postfix_expression
3777                           = perform_koenig_lookup (postfix_expression, args);
3778                       }
3779                     else
3780                       postfix_expression
3781                         = unqualified_fn_lookup_error (postfix_expression);
3782                   }
3783                 /* We do not perform argument-dependent lookup if
3784                    normal lookup finds a non-function, in accordance
3785                    with the expected resolution of DR 218.  */
3786                 else if (args && is_overloaded_fn (postfix_expression))
3787                   {
3788                     tree fn = get_first_fn (postfix_expression);
3789                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3790                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
3791                     /* Only do argument dependent lookup if regular
3792                        lookup does not find a set of member functions.
3793                        [basic.lookup.koenig]/2a  */
3794                     if (!DECL_FUNCTION_MEMBER_P (fn))
3795                       {
3796                         koenig_p = true;
3797                         postfix_expression
3798                           = perform_koenig_lookup (postfix_expression, args);
3799                       }
3800                   }
3801               }
3802           
3803             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3804               {
3805                 tree instance = TREE_OPERAND (postfix_expression, 0);
3806                 tree fn = TREE_OPERAND (postfix_expression, 1);
3807
3808                 if (processing_template_decl
3809                     && (type_dependent_expression_p (instance)
3810                         || (!BASELINK_P (fn)
3811                             && TREE_CODE (fn) != FIELD_DECL)
3812                         || type_dependent_expression_p (fn)
3813                         || any_type_dependent_arguments_p (args)))
3814                   {
3815                     postfix_expression
3816                       = build_min_nt (CALL_EXPR, postfix_expression, args);
3817                     break;
3818                   }
3819
3820                 if (BASELINK_P (fn))
3821                   postfix_expression
3822                     = (build_new_method_call 
3823                        (instance, fn, args, NULL_TREE, 
3824                         (idk == CP_ID_KIND_QUALIFIED 
3825                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3826                 else
3827                   postfix_expression
3828                     = finish_call_expr (postfix_expression, args,
3829                                         /*disallow_virtual=*/false,
3830                                         /*koenig_p=*/false);
3831               }
3832             else if (TREE_CODE (postfix_expression) == OFFSET_REF
3833                      || TREE_CODE (postfix_expression) == MEMBER_REF
3834                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3835               postfix_expression = (build_offset_ref_call_from_tree
3836                                     (postfix_expression, args));
3837             else if (idk == CP_ID_KIND_QUALIFIED)
3838               /* A call to a static class member, or a namespace-scope
3839                  function.  */
3840               postfix_expression
3841                 = finish_call_expr (postfix_expression, args,
3842                                     /*disallow_virtual=*/true,
3843                                     koenig_p);
3844             else
3845               /* All other function calls.  */
3846               postfix_expression 
3847                 = finish_call_expr (postfix_expression, args, 
3848                                     /*disallow_virtual=*/false,
3849                                     koenig_p);
3850
3851             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
3852             idk = CP_ID_KIND_NONE;
3853           }
3854           break;
3855           
3856         case CPP_DOT:
3857         case CPP_DEREF:
3858           /* postfix-expression . template [opt] id-expression  
3859              postfix-expression . pseudo-destructor-name 
3860              postfix-expression -> template [opt] id-expression
3861              postfix-expression -> pseudo-destructor-name */
3862           {
3863             tree name;
3864             bool dependent_p;
3865             bool template_p;
3866             bool pseudo_destructor_p;
3867             tree scope = NULL_TREE;
3868             enum cpp_ttype token_type = token->type;
3869
3870             /* If this is a `->' operator, dereference the pointer.  */
3871             if (token->type == CPP_DEREF)
3872               postfix_expression = build_x_arrow (postfix_expression);
3873             /* Check to see whether or not the expression is
3874                type-dependent.  */
3875             dependent_p = type_dependent_expression_p (postfix_expression);
3876             /* The identifier following the `->' or `.' is not
3877                qualified.  */
3878             parser->scope = NULL_TREE;
3879             parser->qualifying_scope = NULL_TREE;
3880             parser->object_scope = NULL_TREE;
3881             idk = CP_ID_KIND_NONE;
3882             /* Enter the scope corresponding to the type of the object
3883                given by the POSTFIX_EXPRESSION.  */
3884             if (!dependent_p 
3885                 && TREE_TYPE (postfix_expression) != NULL_TREE)
3886               {
3887                 scope = TREE_TYPE (postfix_expression);
3888                 /* According to the standard, no expression should
3889                    ever have reference type.  Unfortunately, we do not
3890                    currently match the standard in this respect in
3891                    that our internal representation of an expression
3892                    may have reference type even when the standard says
3893                    it does not.  Therefore, we have to manually obtain
3894                    the underlying type here.  */
3895                 scope = non_reference (scope);
3896                 /* The type of the POSTFIX_EXPRESSION must be
3897                    complete.  */
3898                 scope = complete_type_or_else (scope, NULL_TREE);
3899                 /* Let the name lookup machinery know that we are
3900                    processing a class member access expression.  */
3901                 parser->context->object_type = scope;
3902                 /* If something went wrong, we want to be able to
3903                    discern that case, as opposed to the case where
3904                    there was no SCOPE due to the type of expression
3905                    being dependent.  */
3906                 if (!scope)
3907                   scope = error_mark_node;
3908                 /* If the SCOPE was erroneous, make the various
3909                    semantic analysis functions exit quickly -- and
3910                    without issuing additional error messages.  */
3911                 if (scope == error_mark_node)
3912                   postfix_expression = error_mark_node;
3913               }
3914
3915             /* Consume the `.' or `->' operator.  */
3916             cp_lexer_consume_token (parser->lexer);
3917             
3918             /* Assume this expression is not a pseudo-destructor access.  */
3919             pseudo_destructor_p = false;
3920
3921             /* If the SCOPE is a scalar type, then, if this is a valid program,
3922                we must be looking at a pseudo-destructor-name.  */
3923             if (scope && SCALAR_TYPE_P (scope))
3924               {
3925                 tree s = NULL_TREE;
3926                 tree type;
3927
3928                 cp_parser_parse_tentatively (parser);
3929                 /* Parse the pseudo-destructor-name.  */
3930                 cp_parser_pseudo_destructor_name (parser, &s, &type);
3931                 if (cp_parser_parse_definitely (parser))
3932                   {
3933                     pseudo_destructor_p = true;
3934                     postfix_expression
3935                       = finish_pseudo_destructor_expr (postfix_expression,
3936                                                        s, TREE_TYPE (type));
3937                   }
3938               }
3939
3940             if (!pseudo_destructor_p)
3941               {
3942                 /* If the SCOPE is not a scalar type, we are looking
3943                    at an ordinary class member access expression,
3944                    rather than a pseudo-destructor-name.  */
3945                 template_p = cp_parser_optional_template_keyword (parser);
3946                 /* Parse the id-expression.  */
3947                 name = cp_parser_id_expression (parser,
3948                                                 template_p,
3949                                                 /*check_dependency_p=*/true,
3950                                                 /*template_p=*/NULL,
3951                                                 /*declarator_p=*/false);
3952                 /* In general, build a SCOPE_REF if the member name is
3953                    qualified.  However, if the name was not dependent
3954                    and has already been resolved; there is no need to
3955                    build the SCOPE_REF.  For example;
3956
3957                      struct X { void f(); };
3958                      template <typename T> void f(T* t) { t->X::f(); }
3959  
3960                    Even though "t" is dependent, "X::f" is not and has
3961                    been resolved to a BASELINK; there is no need to
3962                    include scope information.  */
3963
3964                 /* But we do need to remember that there was an explicit
3965                    scope for virtual function calls.  */
3966                 if (parser->scope)
3967                   idk = CP_ID_KIND_QUALIFIED;
3968
3969                 if (name != error_mark_node 
3970                     && !BASELINK_P (name)
3971                     && parser->scope)
3972                   {
3973                     name = build_nt (SCOPE_REF, parser->scope, name);
3974                     parser->scope = NULL_TREE;
3975                     parser->qualifying_scope = NULL_TREE;
3976                     parser->object_scope = NULL_TREE;
3977                   }
3978                 if (scope && name && BASELINK_P (name))
3979                   adjust_result_of_qualified_name_lookup 
3980                     (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
3981                 postfix_expression 
3982                   = finish_class_member_access_expr (postfix_expression, name);
3983               }
3984
3985             /* We no longer need to look up names in the scope of the
3986                object on the left-hand side of the `.' or `->'
3987                operator.  */
3988             parser->context->object_type = NULL_TREE;
3989             /* These operators may not appear in constant-expressions.  */
3990             if (/* The "->" operator is allowed in the implementation
3991                    of "offsetof".  The "." operator may appear in the
3992                    name of the member.  */
3993                 !parser->in_offsetof_p
3994                 && (cp_parser_non_integral_constant_expression 
3995                     (parser,
3996                      token_type == CPP_DEREF ? "'->'" : "`.'")))
3997               postfix_expression = error_mark_node;
3998           }
3999           break;
4000
4001         case CPP_PLUS_PLUS:
4002           /* postfix-expression ++  */
4003           /* Consume the `++' token.  */
4004           cp_lexer_consume_token (parser->lexer);
4005           /* Generate a representation for the complete expression.  */
4006           postfix_expression 
4007             = finish_increment_expr (postfix_expression, 
4008                                      POSTINCREMENT_EXPR);
4009           /* Increments may not appear in constant-expressions.  */
4010           if (cp_parser_non_integral_constant_expression (parser,
4011                                                           "an increment"))
4012             postfix_expression = error_mark_node;
4013           idk = CP_ID_KIND_NONE;
4014           break;
4015
4016         case CPP_MINUS_MINUS:
4017           /* postfix-expression -- */
4018           /* Consume the `--' token.  */
4019           cp_lexer_consume_token (parser->lexer);
4020           /* Generate a representation for the complete expression.  */
4021           postfix_expression 
4022             = finish_increment_expr (postfix_expression, 
4023                                      POSTDECREMENT_EXPR);
4024           /* Decrements may not appear in constant-expressions.  */
4025           if (cp_parser_non_integral_constant_expression (parser,
4026                                                           "a decrement"))
4027             postfix_expression = error_mark_node;
4028           idk = CP_ID_KIND_NONE;
4029           break;
4030
4031         default:
4032           return postfix_expression;
4033         }
4034     }
4035
4036   /* We should never get here.  */
4037   abort ();
4038   return error_mark_node;
4039 }
4040
4041 /* Parse a parenthesized expression-list.
4042
4043    expression-list:
4044      assignment-expression
4045      expression-list, assignment-expression
4046
4047    attribute-list:
4048      expression-list
4049      identifier
4050      identifier, expression-list
4051
4052    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4053    representation of an assignment-expression.  Note that a TREE_LIST
4054    is returned even if there is only a single expression in the list.
4055    error_mark_node is returned if the ( and or ) are
4056    missing. NULL_TREE is returned on no expressions. The parentheses
4057    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4058    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4059    indicates whether or not all of the expressions in the list were
4060    constant.  */
4061
4062 static tree
4063 cp_parser_parenthesized_expression_list (cp_parser* parser, 
4064                                          bool is_attribute_list,
4065                                          bool *non_constant_p)
4066 {
4067   tree expression_list = NULL_TREE;
4068   tree identifier = NULL_TREE;
4069
4070   /* Assume all the expressions will be constant.  */
4071   if (non_constant_p)
4072     *non_constant_p = false;
4073
4074   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4075     return error_mark_node;
4076   
4077   /* Consume expressions until there are no more.  */
4078   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4079     while (true)
4080       {
4081         tree expr;
4082         
4083         /* At the beginning of attribute lists, check to see if the
4084            next token is an identifier.  */
4085         if (is_attribute_list
4086             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4087           {
4088             cp_token *token;
4089             
4090             /* Consume the identifier.  */
4091             token = cp_lexer_consume_token (parser->lexer);
4092             /* Save the identifier.  */
4093             identifier = token->value;
4094           }
4095         else
4096           {
4097             /* Parse the next assignment-expression.  */
4098             if (non_constant_p)
4099               {
4100                 bool expr_non_constant_p;
4101                 expr = (cp_parser_constant_expression 
4102                         (parser, /*allow_non_constant_p=*/true,
4103                          &expr_non_constant_p));
4104                 if (expr_non_constant_p)
4105                   *non_constant_p = true;
4106               }
4107             else
4108               expr = cp_parser_assignment_expression (parser);
4109
4110              /* Add it to the list.  We add error_mark_node
4111                 expressions to the list, so that we can still tell if
4112                 the correct form for a parenthesized expression-list
4113                 is found. That gives better errors.  */
4114             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4115
4116             if (expr == error_mark_node)
4117               goto skip_comma;
4118           }
4119
4120         /* After the first item, attribute lists look the same as
4121            expression lists.  */
4122         is_attribute_list = false;
4123         
4124       get_comma:;
4125         /* If the next token isn't a `,', then we are done.  */
4126         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4127           break;
4128
4129         /* Otherwise, consume the `,' and keep going.  */
4130         cp_lexer_consume_token (parser->lexer);
4131       }
4132   
4133   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4134     {
4135       int ending;
4136       
4137     skip_comma:;
4138       /* We try and resync to an unnested comma, as that will give the
4139          user better diagnostics.  */
4140       ending = cp_parser_skip_to_closing_parenthesis (parser, 
4141                                                       /*recovering=*/true, 
4142                                                       /*or_comma=*/true,
4143                                                       /*consume_paren=*/true);
4144       if (ending < 0)
4145         goto get_comma;
4146       if (!ending)
4147         return error_mark_node;
4148     }
4149
4150   /* We built up the list in reverse order so we must reverse it now.  */
4151   expression_list = nreverse (expression_list);
4152   if (identifier)
4153     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4154   
4155   return expression_list;
4156 }
4157
4158 /* Parse a pseudo-destructor-name.
4159
4160    pseudo-destructor-name:
4161      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4162      :: [opt] nested-name-specifier template template-id :: ~ type-name
4163      :: [opt] nested-name-specifier [opt] ~ type-name
4164
4165    If either of the first two productions is used, sets *SCOPE to the
4166    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4167    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4168    or ERROR_MARK_NODE if the parse fails.  */
4169
4170 static void
4171 cp_parser_pseudo_destructor_name (cp_parser* parser, 
4172                                   tree* scope, 
4173                                   tree* type)
4174 {
4175   bool nested_name_specifier_p;
4176
4177   /* Look for the optional `::' operator.  */
4178   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4179   /* Look for the optional nested-name-specifier.  */
4180   nested_name_specifier_p 
4181     = (cp_parser_nested_name_specifier_opt (parser,
4182                                             /*typename_keyword_p=*/false,
4183                                             /*check_dependency_p=*/true,
4184                                             /*type_p=*/false,
4185                                             /*is_declaration=*/true) 
4186        != NULL_TREE);
4187   /* Now, if we saw a nested-name-specifier, we might be doing the
4188      second production.  */
4189   if (nested_name_specifier_p 
4190       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4191     {
4192       /* Consume the `template' keyword.  */
4193       cp_lexer_consume_token (parser->lexer);
4194       /* Parse the template-id.  */
4195       cp_parser_template_id (parser, 
4196                              /*template_keyword_p=*/true,
4197                              /*check_dependency_p=*/false,
4198                              /*is_declaration=*/true);
4199       /* Look for the `::' token.  */
4200       cp_parser_require (parser, CPP_SCOPE, "`::'");
4201     }
4202   /* If the next token is not a `~', then there might be some
4203      additional qualification.  */
4204   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4205     {
4206       /* Look for the type-name.  */
4207       *scope = TREE_TYPE (cp_parser_type_name (parser));
4208
4209       /* If we didn't get an aggregate type, or we don't have ::~,
4210          then something has gone wrong.  Since the only caller of this
4211          function is looking for something after `.' or `->' after a
4212          scalar type, most likely the program is trying to get a
4213          member of a non-aggregate type.  */
4214       if (*scope == error_mark_node
4215           || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4216           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4217         {
4218           cp_parser_error (parser, "request for member of non-aggregate type");
4219           *type = error_mark_node;
4220           return;
4221         }
4222
4223       /* Look for the `::' token.  */
4224       cp_parser_require (parser, CPP_SCOPE, "`::'");
4225     }
4226   else
4227     *scope = NULL_TREE;
4228
4229   /* Look for the `~'.  */
4230   cp_parser_require (parser, CPP_COMPL, "`~'");
4231   /* Look for the type-name again.  We are not responsible for
4232      checking that it matches the first type-name.  */
4233   *type = cp_parser_type_name (parser);
4234 }
4235
4236 /* Parse a unary-expression.
4237
4238    unary-expression:
4239      postfix-expression
4240      ++ cast-expression
4241      -- cast-expression
4242      unary-operator cast-expression
4243      sizeof unary-expression
4244      sizeof ( type-id )
4245      new-expression
4246      delete-expression
4247
4248    GNU Extensions:
4249
4250    unary-expression:
4251      __extension__ cast-expression
4252      __alignof__ unary-expression
4253      __alignof__ ( type-id )
4254      __real__ cast-expression
4255      __imag__ cast-expression
4256      && identifier
4257
4258    ADDRESS_P is true iff the unary-expression is appearing as the
4259    operand of the `&' operator.
4260
4261    Returns a representation of the expression.  */
4262
4263 static tree
4264 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4265 {
4266   cp_token *token;
4267   enum tree_code unary_operator;
4268
4269   /* Peek at the next token.  */
4270   token = cp_lexer_peek_token (parser->lexer);
4271   /* Some keywords give away the kind of expression.  */
4272   if (token->type == CPP_KEYWORD)
4273     {
4274       enum rid keyword = token->keyword;
4275
4276       switch (keyword)
4277         {
4278         case RID_ALIGNOF:
4279         case RID_SIZEOF:
4280           {
4281             tree operand;
4282             enum tree_code op;
4283             
4284             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4285             /* Consume the token.  */
4286             cp_lexer_consume_token (parser->lexer);
4287             /* Parse the operand.  */
4288             operand = cp_parser_sizeof_operand (parser, keyword);
4289
4290             if (TYPE_P (operand))
4291               return cxx_sizeof_or_alignof_type (operand, op, true);
4292             else
4293               return cxx_sizeof_or_alignof_expr (operand, op);
4294           }
4295
4296         case RID_NEW:
4297           return cp_parser_new_expression (parser);
4298
4299         case RID_DELETE:
4300           return cp_parser_delete_expression (parser);
4301           
4302         case RID_EXTENSION:
4303           {
4304             /* The saved value of the PEDANTIC flag.  */
4305             int saved_pedantic;
4306             tree expr;
4307
4308             /* Save away the PEDANTIC flag.  */
4309             cp_parser_extension_opt (parser, &saved_pedantic);
4310             /* Parse the cast-expression.  */
4311             expr = cp_parser_simple_cast_expression (parser);
4312             /* Restore the PEDANTIC flag.  */
4313             pedantic = saved_pedantic;
4314
4315             return expr;
4316           }
4317
4318         case RID_REALPART:
4319         case RID_IMAGPART:
4320           {
4321             tree expression;
4322
4323             /* Consume the `__real__' or `__imag__' token.  */
4324             cp_lexer_consume_token (parser->lexer);
4325             /* Parse the cast-expression.  */
4326             expression = cp_parser_simple_cast_expression (parser);
4327             /* Create the complete representation.  */
4328             return build_x_unary_op ((keyword == RID_REALPART
4329                                       ? REALPART_EXPR : IMAGPART_EXPR),
4330                                      expression);
4331           }
4332           break;
4333
4334         default:
4335           break;
4336         }
4337     }
4338
4339   /* Look for the `:: new' and `:: delete', which also signal the
4340      beginning of a new-expression, or delete-expression,
4341      respectively.  If the next token is `::', then it might be one of
4342      these.  */
4343   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4344     {
4345       enum rid keyword;
4346
4347       /* See if the token after the `::' is one of the keywords in
4348          which we're interested.  */
4349       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4350       /* If it's `new', we have a new-expression.  */
4351       if (keyword == RID_NEW)
4352         return cp_parser_new_expression (parser);
4353       /* Similarly, for `delete'.  */
4354       else if (keyword == RID_DELETE)
4355         return cp_parser_delete_expression (parser);
4356     }
4357
4358   /* Look for a unary operator.  */
4359   unary_operator = cp_parser_unary_operator (token);
4360   /* The `++' and `--' operators can be handled similarly, even though
4361      they are not technically unary-operators in the grammar.  */
4362   if (unary_operator == ERROR_MARK)
4363     {
4364       if (token->type == CPP_PLUS_PLUS)
4365         unary_operator = PREINCREMENT_EXPR;
4366       else if (token->type == CPP_MINUS_MINUS)
4367         unary_operator = PREDECREMENT_EXPR;
4368       /* Handle the GNU address-of-label extension.  */
4369       else if (cp_parser_allow_gnu_extensions_p (parser)
4370                && token->type == CPP_AND_AND)
4371         {
4372           tree identifier;
4373
4374           /* Consume the '&&' token.  */
4375           cp_lexer_consume_token (parser->lexer);
4376           /* Look for the identifier.  */
4377           identifier = cp_parser_identifier (parser);
4378           /* Create an expression representing the address.  */
4379           return finish_label_address_expr (identifier);
4380         }
4381     }
4382   if (unary_operator != ERROR_MARK)
4383     {
4384       tree cast_expression;
4385       tree expression = error_mark_node;
4386       const char *non_constant_p = NULL;
4387
4388       /* Consume the operator token.  */
4389       token = cp_lexer_consume_token (parser->lexer);
4390       /* Parse the cast-expression.  */
4391       cast_expression 
4392         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4393       /* Now, build an appropriate representation.  */
4394       switch (unary_operator)
4395         {
4396         case INDIRECT_REF:
4397           non_constant_p = "`*'";
4398           expression = build_x_indirect_ref (cast_expression, "unary *");
4399           break;
4400
4401         case ADDR_EXPR:
4402           /* The "&" operator is allowed in the implementation of
4403              "offsetof".  */
4404           if (!parser->in_offsetof_p)
4405             non_constant_p = "`&'";
4406           /* Fall through.  */
4407         case BIT_NOT_EXPR:
4408           expression = build_x_unary_op (unary_operator, cast_expression);
4409           break;
4410
4411         case PREINCREMENT_EXPR:
4412         case PREDECREMENT_EXPR:
4413           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4414                             ? "`++'" : "`--'");
4415           /* Fall through.  */
4416         case CONVERT_EXPR:
4417         case NEGATE_EXPR:
4418         case TRUTH_NOT_EXPR:
4419           expression = finish_unary_op_expr (unary_operator, cast_expression);
4420           break;
4421
4422         default:
4423           abort ();
4424         }
4425
4426       if (non_constant_p 
4427           && cp_parser_non_integral_constant_expression (parser,
4428                                                          non_constant_p))
4429         expression = error_mark_node;
4430
4431       return expression;
4432     }
4433
4434   return cp_parser_postfix_expression (parser, address_p);
4435 }
4436
4437 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4438    unary-operator, the corresponding tree code is returned.  */
4439
4440 static enum tree_code
4441 cp_parser_unary_operator (cp_token* token)
4442 {
4443   switch (token->type)
4444     {
4445     case CPP_MULT:
4446       return INDIRECT_REF;
4447
4448     case CPP_AND:
4449       return ADDR_EXPR;
4450
4451     case CPP_PLUS:
4452       return CONVERT_EXPR;
4453
4454     case CPP_MINUS:
4455       return NEGATE_EXPR;
4456
4457     case CPP_NOT:
4458       return TRUTH_NOT_EXPR;
4459       
4460     case CPP_COMPL:
4461       return BIT_NOT_EXPR;
4462
4463     default:
4464       return ERROR_MARK;
4465     }
4466 }
4467
4468 /* Parse a new-expression.
4469
4470    new-expression:
4471      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4472      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4473
4474    Returns a representation of the expression.  */
4475
4476 static tree
4477 cp_parser_new_expression (cp_parser* parser)
4478 {
4479   bool global_scope_p;
4480   tree placement;
4481   tree type;
4482   tree initializer;
4483
4484   /* Look for the optional `::' operator.  */
4485   global_scope_p 
4486     = (cp_parser_global_scope_opt (parser,
4487                                    /*current_scope_valid_p=*/false)
4488        != NULL_TREE);
4489   /* Look for the `new' operator.  */
4490   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4491   /* There's no easy way to tell a new-placement from the
4492      `( type-id )' construct.  */
4493   cp_parser_parse_tentatively (parser);
4494   /* Look for a new-placement.  */
4495   placement = cp_parser_new_placement (parser);
4496   /* If that didn't work out, there's no new-placement.  */
4497   if (!cp_parser_parse_definitely (parser))
4498     placement = NULL_TREE;
4499
4500   /* If the next token is a `(', then we have a parenthesized
4501      type-id.  */
4502   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4503     {
4504       /* Consume the `('.  */
4505       cp_lexer_consume_token (parser->lexer);
4506       /* Parse the type-id.  */
4507       type = cp_parser_type_id (parser);
4508       /* Look for the closing `)'.  */
4509       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4510       /* There should not be a direct-new-declarator in this production, 
4511          but GCC used to allowed this, so we check and emit a sensible error
4512          message for this case.  */
4513       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4514         {
4515           error ("array bound forbidden after parenthesized type-id");
4516           inform ("try removing the parentheses around the type-id");
4517           cp_parser_direct_new_declarator (parser);
4518         }
4519     }
4520   /* Otherwise, there must be a new-type-id.  */
4521   else
4522     type = cp_parser_new_type_id (parser);
4523
4524   /* If the next token is a `(', then we have a new-initializer.  */
4525   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4526     initializer = cp_parser_new_initializer (parser);
4527   else
4528     initializer = NULL_TREE;
4529
4530   /* A new-expression may not appear in an integral constant
4531      expression.  */
4532   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4533     return error_mark_node;
4534
4535   /* Create a representation of the new-expression.  */
4536   return build_new (placement, type, initializer, global_scope_p);
4537 }
4538
4539 /* Parse a new-placement.
4540
4541    new-placement:
4542      ( expression-list )
4543
4544    Returns the same representation as for an expression-list.  */
4545
4546 static tree
4547 cp_parser_new_placement (cp_parser* parser)
4548 {
4549   tree expression_list;
4550
4551   /* Parse the expression-list.  */
4552   expression_list = (cp_parser_parenthesized_expression_list 
4553                      (parser, false, /*non_constant_p=*/NULL));
4554
4555   return expression_list;
4556 }
4557
4558 /* Parse a new-type-id.
4559
4560    new-type-id:
4561      type-specifier-seq new-declarator [opt]
4562
4563    Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4564    and whose TREE_VALUE is the new-declarator.  */
4565
4566 static tree
4567 cp_parser_new_type_id (cp_parser* parser)
4568 {
4569   tree type_specifier_seq;
4570   tree declarator;
4571   const char *saved_message;
4572
4573   /* The type-specifier sequence must not contain type definitions.
4574      (It cannot contain declarations of new types either, but if they
4575      are not definitions we will catch that because they are not
4576      complete.)  */
4577   saved_message = parser->type_definition_forbidden_message;
4578   parser->type_definition_forbidden_message
4579     = "types may not be defined in a new-type-id";
4580   /* Parse the type-specifier-seq.  */
4581   type_specifier_seq = cp_parser_type_specifier_seq (parser);
4582   /* Restore the old message.  */
4583   parser->type_definition_forbidden_message = saved_message;
4584   /* Parse the new-declarator.  */
4585   declarator = cp_parser_new_declarator_opt (parser);
4586
4587   return build_tree_list (type_specifier_seq, declarator);
4588 }
4589
4590 /* Parse an (optional) new-declarator.
4591
4592    new-declarator:
4593      ptr-operator new-declarator [opt]
4594      direct-new-declarator
4595
4596    Returns a representation of the declarator.  See
4597    cp_parser_declarator for the representations used.  */
4598
4599 static tree
4600 cp_parser_new_declarator_opt (cp_parser* parser)
4601 {
4602   enum tree_code code;
4603   tree type;
4604   tree cv_qualifier_seq;
4605
4606   /* We don't know if there's a ptr-operator next, or not.  */
4607   cp_parser_parse_tentatively (parser);
4608   /* Look for a ptr-operator.  */
4609   code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4610   /* If that worked, look for more new-declarators.  */
4611   if (cp_parser_parse_definitely (parser))
4612     {
4613       tree declarator;
4614
4615       /* Parse another optional declarator.  */
4616       declarator = cp_parser_new_declarator_opt (parser);
4617
4618       /* Create the representation of the declarator.  */
4619       if (code == INDIRECT_REF)
4620         declarator = make_pointer_declarator (cv_qualifier_seq,
4621                                               declarator);
4622       else
4623         declarator = make_reference_declarator (cv_qualifier_seq,
4624                                                 declarator);
4625
4626      /* Handle the pointer-to-member case.  */
4627      if (type)
4628        declarator = build_nt (SCOPE_REF, type, declarator);
4629
4630       return declarator;
4631     }
4632
4633   /* If the next token is a `[', there is a direct-new-declarator.  */
4634   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4635     return cp_parser_direct_new_declarator (parser);
4636
4637   return NULL_TREE;
4638 }
4639
4640 /* Parse a direct-new-declarator.
4641
4642    direct-new-declarator:
4643      [ expression ]
4644      direct-new-declarator [constant-expression]  
4645
4646    Returns an ARRAY_REF, following the same conventions as are
4647    documented for cp_parser_direct_declarator.  */
4648
4649 static tree
4650 cp_parser_direct_new_declarator (cp_parser* parser)
4651 {
4652   tree declarator = NULL_TREE;
4653
4654   while (true)
4655     {
4656       tree expression;
4657
4658       /* Look for the opening `['.  */
4659       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4660       /* The first expression is not required to be constant.  */
4661       if (!declarator)
4662         {
4663           expression = cp_parser_expression (parser);
4664           /* The standard requires that the expression have integral
4665              type.  DR 74 adds enumeration types.  We believe that the
4666              real intent is that these expressions be handled like the
4667              expression in a `switch' condition, which also allows
4668              classes with a single conversion to integral or
4669              enumeration type.  */
4670           if (!processing_template_decl)
4671             {
4672               expression 
4673                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4674                                               expression,
4675                                               /*complain=*/true);
4676               if (!expression)
4677                 {
4678                   error ("expression in new-declarator must have integral or enumeration type");
4679                   expression = error_mark_node;
4680                 }
4681             }
4682         }
4683       /* But all the other expressions must be.  */
4684       else
4685         expression 
4686           = cp_parser_constant_expression (parser, 
4687                                            /*allow_non_constant=*/false,
4688                                            NULL);
4689       /* Look for the closing `]'.  */
4690       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4691
4692       /* Add this bound to the declarator.  */
4693       declarator = build_nt (ARRAY_REF, declarator, expression);
4694
4695       /* If the next token is not a `[', then there are no more
4696          bounds.  */
4697       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4698         break;
4699     }
4700
4701   return declarator;
4702 }
4703
4704 /* Parse a new-initializer.
4705
4706    new-initializer:
4707      ( expression-list [opt] )
4708
4709    Returns a representation of the expression-list.  If there is no
4710    expression-list, VOID_ZERO_NODE is returned.  */
4711
4712 static tree
4713 cp_parser_new_initializer (cp_parser* parser)
4714 {
4715   tree expression_list;
4716
4717   expression_list = (cp_parser_parenthesized_expression_list 
4718                      (parser, false, /*non_constant_p=*/NULL));
4719   if (!expression_list)
4720     expression_list = void_zero_node;
4721
4722   return expression_list;
4723 }
4724
4725 /* Parse a delete-expression.
4726
4727    delete-expression:
4728      :: [opt] delete cast-expression
4729      :: [opt] delete [ ] cast-expression
4730
4731    Returns a representation of the expression.  */
4732
4733 static tree
4734 cp_parser_delete_expression (cp_parser* parser)
4735 {
4736   bool global_scope_p;
4737   bool array_p;
4738   tree expression;
4739
4740   /* Look for the optional `::' operator.  */
4741   global_scope_p
4742     = (cp_parser_global_scope_opt (parser,
4743                                    /*current_scope_valid_p=*/false)
4744        != NULL_TREE);
4745   /* Look for the `delete' keyword.  */
4746   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4747   /* See if the array syntax is in use.  */
4748   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4749     {
4750       /* Consume the `[' token.  */
4751       cp_lexer_consume_token (parser->lexer);
4752       /* Look for the `]' token.  */
4753       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4754       /* Remember that this is the `[]' construct.  */
4755       array_p = true;
4756     }
4757   else
4758     array_p = false;
4759
4760   /* Parse the cast-expression.  */
4761   expression = cp_parser_simple_cast_expression (parser);
4762
4763   /* A delete-expression may not appear in an integral constant
4764      expression.  */
4765   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4766     return error_mark_node;
4767
4768   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4769 }
4770
4771 /* Parse a cast-expression.
4772
4773    cast-expression:
4774      unary-expression
4775      ( type-id ) cast-expression
4776
4777    Returns a representation of the expression.  */
4778
4779 static tree
4780 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4781 {
4782   /* If it's a `(', then we might be looking at a cast.  */
4783   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4784     {
4785       tree type = NULL_TREE;
4786       tree expr = NULL_TREE;
4787       bool compound_literal_p;
4788       const char *saved_message;
4789
4790       /* There's no way to know yet whether or not this is a cast.
4791          For example, `(int (3))' is a unary-expression, while `(int)
4792          3' is a cast.  So, we resort to parsing tentatively.  */
4793       cp_parser_parse_tentatively (parser);
4794       /* Types may not be defined in a cast.  */
4795       saved_message = parser->type_definition_forbidden_message;
4796       parser->type_definition_forbidden_message
4797         = "types may not be defined in casts";
4798       /* Consume the `('.  */
4799       cp_lexer_consume_token (parser->lexer);
4800       /* A very tricky bit is that `(struct S) { 3 }' is a
4801          compound-literal (which we permit in C++ as an extension).
4802          But, that construct is not a cast-expression -- it is a
4803          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
4804          is legal; if the compound-literal were a cast-expression,
4805          you'd need an extra set of parentheses.)  But, if we parse
4806          the type-id, and it happens to be a class-specifier, then we
4807          will commit to the parse at that point, because we cannot
4808          undo the action that is done when creating a new class.  So,
4809          then we cannot back up and do a postfix-expression.  
4810
4811          Therefore, we scan ahead to the closing `)', and check to see
4812          if the token after the `)' is a `{'.  If so, we are not
4813          looking at a cast-expression.  
4814
4815          Save tokens so that we can put them back.  */
4816       cp_lexer_save_tokens (parser->lexer);
4817       /* Skip tokens until the next token is a closing parenthesis.
4818          If we find the closing `)', and the next token is a `{', then
4819          we are looking at a compound-literal.  */
4820       compound_literal_p 
4821         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4822                                                   /*consume_paren=*/true)
4823            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4824       /* Roll back the tokens we skipped.  */
4825       cp_lexer_rollback_tokens (parser->lexer);
4826       /* If we were looking at a compound-literal, simulate an error
4827          so that the call to cp_parser_parse_definitely below will
4828          fail.  */
4829       if (compound_literal_p)
4830         cp_parser_simulate_error (parser);
4831       else
4832         {
4833           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4834           parser->in_type_id_in_expr_p = true;
4835           /* Look for the type-id.  */
4836           type = cp_parser_type_id (parser);
4837           /* Look for the closing `)'.  */
4838           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4839           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4840         }
4841
4842       /* Restore the saved message.  */
4843       parser->type_definition_forbidden_message = saved_message;
4844
4845       /* If ok so far, parse the dependent expression. We cannot be
4846          sure it is a cast. Consider `(T ())'.  It is a parenthesized
4847          ctor of T, but looks like a cast to function returning T
4848          without a dependent expression.  */
4849       if (!cp_parser_error_occurred (parser))
4850         expr = cp_parser_simple_cast_expression (parser);
4851
4852       if (cp_parser_parse_definitely (parser))
4853         {
4854           /* Warn about old-style casts, if so requested.  */
4855           if (warn_old_style_cast 
4856               && !in_system_header 
4857               && !VOID_TYPE_P (type) 
4858               && current_lang_name != lang_name_c)
4859             warning ("use of old-style cast");
4860
4861           /* Only type conversions to integral or enumeration types
4862              can be used in constant-expressions.  */
4863           if (parser->integral_constant_expression_p
4864               && !dependent_type_p (type)
4865               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4866               && (cp_parser_non_integral_constant_expression 
4867                   (parser,
4868                    "a casts to a type other than an integral or "
4869                    "enumeration type")))
4870             return error_mark_node;
4871
4872           /* Perform the cast.  */
4873           expr = build_c_cast (type, expr);
4874           return expr;
4875         }
4876     }
4877
4878   /* If we get here, then it's not a cast, so it must be a
4879      unary-expression.  */
4880   return cp_parser_unary_expression (parser, address_p);
4881 }
4882
4883 /* Parse a pm-expression.
4884
4885    pm-expression:
4886      cast-expression
4887      pm-expression .* cast-expression
4888      pm-expression ->* cast-expression
4889
4890      Returns a representation of the expression.  */
4891
4892 static tree
4893 cp_parser_pm_expression (cp_parser* parser)
4894 {
4895   static const cp_parser_token_tree_map map = {
4896     { CPP_DEREF_STAR, MEMBER_REF },
4897     { CPP_DOT_STAR, DOTSTAR_EXPR },
4898     { CPP_EOF, ERROR_MARK }
4899   };
4900
4901   return cp_parser_binary_expression (parser, map, 
4902                                       cp_parser_simple_cast_expression);
4903 }
4904
4905 /* Parse a multiplicative-expression.
4906
4907    mulitplicative-expression:
4908      pm-expression
4909      multiplicative-expression * pm-expression
4910      multiplicative-expression / pm-expression
4911      multiplicative-expression % pm-expression
4912
4913    Returns a representation of the expression.  */
4914
4915 static tree
4916 cp_parser_multiplicative_expression (cp_parser* parser)
4917 {
4918   static const cp_parser_token_tree_map map = {
4919     { CPP_MULT, MULT_EXPR },
4920     { CPP_DIV, TRUNC_DIV_EXPR },
4921     { CPP_MOD, TRUNC_MOD_EXPR },
4922     { CPP_EOF, ERROR_MARK }
4923   };
4924
4925   return cp_parser_binary_expression (parser,
4926                                       map,
4927                                       cp_parser_pm_expression);
4928 }
4929
4930 /* Parse an additive-expression.
4931
4932    additive-expression:
4933      multiplicative-expression
4934      additive-expression + multiplicative-expression
4935      additive-expression - multiplicative-expression
4936
4937    Returns a representation of the expression.  */
4938
4939 static tree
4940 cp_parser_additive_expression (cp_parser* parser)
4941 {
4942   static const cp_parser_token_tree_map map = {
4943     { CPP_PLUS, PLUS_EXPR },
4944     { CPP_MINUS, MINUS_EXPR },
4945     { CPP_EOF, ERROR_MARK }
4946   };
4947
4948   return cp_parser_binary_expression (parser,
4949                                       map,
4950                                       cp_parser_multiplicative_expression);
4951 }
4952
4953 /* Parse a shift-expression.
4954
4955    shift-expression:
4956      additive-expression
4957      shift-expression << additive-expression
4958      shift-expression >> additive-expression
4959
4960    Returns a representation of the expression.  */
4961
4962 static tree
4963 cp_parser_shift_expression (cp_parser* parser)
4964 {
4965   static const cp_parser_token_tree_map map = {
4966     { CPP_LSHIFT, LSHIFT_EXPR },
4967     { CPP_RSHIFT, RSHIFT_EXPR },
4968     { CPP_EOF, ERROR_MARK }
4969   };
4970
4971   return cp_parser_binary_expression (parser,
4972                                       map,
4973                                       cp_parser_additive_expression);
4974 }
4975
4976 /* Parse a relational-expression.
4977
4978    relational-expression:
4979      shift-expression
4980      relational-expression < shift-expression
4981      relational-expression > shift-expression
4982      relational-expression <= shift-expression
4983      relational-expression >= shift-expression
4984
4985    GNU Extension:
4986
4987    relational-expression:
4988      relational-expression <? shift-expression
4989      relational-expression >? shift-expression
4990
4991    Returns a representation of the expression.  */
4992
4993 static tree
4994 cp_parser_relational_expression (cp_parser* parser)
4995 {
4996   static const cp_parser_token_tree_map map = {
4997     { CPP_LESS, LT_EXPR },
4998     { CPP_GREATER, GT_EXPR },
4999     { CPP_LESS_EQ, LE_EXPR },
5000     { CPP_GREATER_EQ, GE_EXPR },
5001     { CPP_MIN, MIN_EXPR },
5002     { CPP_MAX, MAX_EXPR },
5003     { CPP_EOF, ERROR_MARK }
5004   };
5005
5006   return cp_parser_binary_expression (parser,
5007                                       map,
5008                                       cp_parser_shift_expression);
5009 }
5010
5011 /* Parse an equality-expression.
5012
5013    equality-expression:
5014      relational-expression
5015      equality-expression == relational-expression
5016      equality-expression != relational-expression
5017
5018    Returns a representation of the expression.  */
5019
5020 static tree
5021 cp_parser_equality_expression (cp_parser* parser)
5022 {
5023   static const cp_parser_token_tree_map map = {
5024     { CPP_EQ_EQ, EQ_EXPR },
5025     { CPP_NOT_EQ, NE_EXPR },
5026     { CPP_EOF, ERROR_MARK }
5027   };
5028
5029   return cp_parser_binary_expression (parser,
5030                                       map,
5031                                       cp_parser_relational_expression);
5032 }
5033
5034 /* Parse an and-expression.
5035
5036    and-expression:
5037      equality-expression
5038      and-expression & equality-expression
5039
5040    Returns a representation of the expression.  */
5041
5042 static tree
5043 cp_parser_and_expression (cp_parser* parser)
5044 {
5045   static const cp_parser_token_tree_map map = {
5046     { CPP_AND, BIT_AND_EXPR },
5047     { CPP_EOF, ERROR_MARK }
5048   };
5049
5050   return cp_parser_binary_expression (parser,
5051                                       map,
5052                                       cp_parser_equality_expression);
5053 }
5054
5055 /* Parse an exclusive-or-expression.
5056
5057    exclusive-or-expression:
5058      and-expression
5059      exclusive-or-expression ^ and-expression
5060
5061    Returns a representation of the expression.  */
5062
5063 static tree
5064 cp_parser_exclusive_or_expression (cp_parser* parser)
5065 {
5066   static const cp_parser_token_tree_map map = {
5067     { CPP_XOR, BIT_XOR_EXPR },
5068     { CPP_EOF, ERROR_MARK }
5069   };
5070
5071   return cp_parser_binary_expression (parser,
5072                                       map,
5073                                       cp_parser_and_expression);
5074 }
5075
5076
5077 /* Parse an inclusive-or-expression.
5078
5079    inclusive-or-expression:
5080      exclusive-or-expression
5081      inclusive-or-expression | exclusive-or-expression
5082
5083    Returns a representation of the expression.  */
5084
5085 static tree
5086 cp_parser_inclusive_or_expression (cp_parser* parser)
5087 {
5088   static const cp_parser_token_tree_map map = {
5089     { CPP_OR, BIT_IOR_EXPR },
5090     { CPP_EOF, ERROR_MARK }
5091   };
5092
5093   return cp_parser_binary_expression (parser,
5094                                       map,
5095                                       cp_parser_exclusive_or_expression);
5096 }
5097
5098 /* Parse a logical-and-expression.
5099
5100    logical-and-expression:
5101      inclusive-or-expression
5102      logical-and-expression && inclusive-or-expression
5103
5104    Returns a representation of the expression.  */
5105
5106 static tree
5107 cp_parser_logical_and_expression (cp_parser* parser)
5108 {
5109   static const cp_parser_token_tree_map map = {
5110     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5111     { CPP_EOF, ERROR_MARK }
5112   };
5113
5114   return cp_parser_binary_expression (parser,
5115                                       map,
5116                                       cp_parser_inclusive_or_expression);
5117 }
5118
5119 /* Parse a logical-or-expression.
5120
5121    logical-or-expression:
5122      logical-and-expression
5123      logical-or-expression || logical-and-expression
5124
5125    Returns a representation of the expression.  */
5126
5127 static tree
5128 cp_parser_logical_or_expression (cp_parser* parser)
5129 {
5130   static const cp_parser_token_tree_map map = {
5131     { CPP_OR_OR, TRUTH_ORIF_EXPR },
5132     { CPP_EOF, ERROR_MARK }
5133   };
5134
5135   return cp_parser_binary_expression (parser,
5136                                       map,
5137                                       cp_parser_logical_and_expression);
5138 }
5139
5140 /* Parse the `? expression : assignment-expression' part of a
5141    conditional-expression.  The LOGICAL_OR_EXPR is the
5142    logical-or-expression that started the conditional-expression.
5143    Returns a representation of the entire conditional-expression.
5144
5145    This routine is used by cp_parser_assignment_expression.
5146
5147      ? expression : assignment-expression
5148    
5149    GNU Extensions:
5150    
5151      ? : assignment-expression */
5152
5153 static tree
5154 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5155 {
5156   tree expr;
5157   tree assignment_expr;
5158
5159   /* Consume the `?' token.  */
5160   cp_lexer_consume_token (parser->lexer);
5161   if (cp_parser_allow_gnu_extensions_p (parser)
5162       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5163     /* Implicit true clause.  */
5164     expr = NULL_TREE;
5165   else
5166     /* Parse the expression.  */
5167     expr = cp_parser_expression (parser);
5168   
5169   /* The next token should be a `:'.  */
5170   cp_parser_require (parser, CPP_COLON, "`:'");
5171   /* Parse the assignment-expression.  */
5172   assignment_expr = cp_parser_assignment_expression (parser);
5173
5174   /* Build the conditional-expression.  */
5175   return build_x_conditional_expr (logical_or_expr,
5176                                    expr,
5177                                    assignment_expr);
5178 }
5179
5180 /* Parse an assignment-expression.
5181
5182    assignment-expression:
5183      conditional-expression
5184      logical-or-expression assignment-operator assignment_expression
5185      throw-expression
5186
5187    Returns a representation for the expression.  */
5188
5189 static tree
5190 cp_parser_assignment_expression (cp_parser* parser)
5191 {
5192   tree expr;
5193
5194   /* If the next token is the `throw' keyword, then we're looking at
5195      a throw-expression.  */
5196   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5197     expr = cp_parser_throw_expression (parser);
5198   /* Otherwise, it must be that we are looking at a
5199      logical-or-expression.  */
5200   else
5201     {
5202       /* Parse the logical-or-expression.  */
5203       expr = cp_parser_logical_or_expression (parser);
5204       /* If the next token is a `?' then we're actually looking at a
5205          conditional-expression.  */
5206       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5207         return cp_parser_question_colon_clause (parser, expr);
5208       else 
5209         {
5210           enum tree_code assignment_operator;
5211
5212           /* If it's an assignment-operator, we're using the second
5213              production.  */
5214           assignment_operator 
5215             = cp_parser_assignment_operator_opt (parser);
5216           if (assignment_operator != ERROR_MARK)
5217             {
5218               tree rhs;
5219
5220               /* Parse the right-hand side of the assignment.  */
5221               rhs = cp_parser_assignment_expression (parser);
5222               /* An assignment may not appear in a
5223                  constant-expression.  */
5224               if (cp_parser_non_integral_constant_expression (parser,
5225                                                               "an assignment"))
5226                 return error_mark_node;
5227               /* Build the assignment expression.  */
5228               expr = build_x_modify_expr (expr, 
5229                                           assignment_operator, 
5230                                           rhs);
5231             }
5232         }
5233     }
5234
5235   return expr;
5236 }
5237
5238 /* Parse an (optional) assignment-operator.
5239
5240    assignment-operator: one of 
5241      = *= /= %= += -= >>= <<= &= ^= |=  
5242
5243    GNU Extension:
5244    
5245    assignment-operator: one of
5246      <?= >?=
5247
5248    If the next token is an assignment operator, the corresponding tree
5249    code is returned, and the token is consumed.  For example, for
5250    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5251    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5252    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5253    operator, ERROR_MARK is returned.  */
5254
5255 static enum tree_code
5256 cp_parser_assignment_operator_opt (cp_parser* parser)
5257 {
5258   enum tree_code op;
5259   cp_token *token;
5260
5261   /* Peek at the next toen.  */
5262   token = cp_lexer_peek_token (parser->lexer);
5263
5264   switch (token->type)
5265     {
5266     case CPP_EQ:
5267       op = NOP_EXPR;
5268       break;
5269
5270     case CPP_MULT_EQ:
5271       op = MULT_EXPR;
5272       break;
5273
5274     case CPP_DIV_EQ:
5275       op = TRUNC_DIV_EXPR;
5276       break;
5277
5278     case CPP_MOD_EQ:
5279       op = TRUNC_MOD_EXPR;
5280       break;
5281
5282     case CPP_PLUS_EQ:
5283       op = PLUS_EXPR;
5284       break;
5285
5286     case CPP_MINUS_EQ:
5287       op = MINUS_EXPR;
5288       break;
5289
5290     case CPP_RSHIFT_EQ:
5291       op = RSHIFT_EXPR;
5292       break;
5293
5294     case CPP_LSHIFT_EQ:
5295       op = LSHIFT_EXPR;
5296       break;
5297
5298     case CPP_AND_EQ:
5299       op = BIT_AND_EXPR;
5300       break;
5301
5302     case CPP_XOR_EQ:
5303       op = BIT_XOR_EXPR;
5304       break;
5305
5306     case CPP_OR_EQ:
5307       op = BIT_IOR_EXPR;
5308       break;
5309
5310     case CPP_MIN_EQ:
5311       op = MIN_EXPR;
5312       break;
5313
5314     case CPP_MAX_EQ:
5315       op = MAX_EXPR;
5316       break;
5317
5318     default: 
5319       /* Nothing else is an assignment operator.  */
5320       op = ERROR_MARK;
5321     }
5322
5323   /* If it was an assignment operator, consume it.  */
5324   if (op != ERROR_MARK)
5325     cp_lexer_consume_token (parser->lexer);
5326
5327   return op;
5328 }
5329
5330 /* Parse an expression.
5331
5332    expression:
5333      assignment-expression
5334      expression , assignment-expression
5335
5336    Returns a representation of the expression.  */
5337
5338 static tree
5339 cp_parser_expression (cp_parser* parser)
5340 {
5341   tree expression = NULL_TREE;
5342
5343   while (true)
5344     {
5345       tree assignment_expression;
5346
5347       /* Parse the next assignment-expression.  */
5348       assignment_expression 
5349         = cp_parser_assignment_expression (parser);
5350       /* If this is the first assignment-expression, we can just
5351          save it away.  */
5352       if (!expression)
5353         expression = assignment_expression;
5354       else
5355         expression = build_x_compound_expr (expression,
5356                                             assignment_expression);
5357       /* If the next token is not a comma, then we are done with the
5358          expression.  */
5359       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5360         break;
5361       /* Consume the `,'.  */
5362       cp_lexer_consume_token (parser->lexer);
5363       /* A comma operator cannot appear in a constant-expression.  */
5364       if (cp_parser_non_integral_constant_expression (parser,
5365                                                       "a comma operator"))
5366         expression = error_mark_node;
5367     }
5368
5369   return expression;
5370 }
5371
5372 /* Parse a constant-expression. 
5373
5374    constant-expression:
5375      conditional-expression  
5376
5377   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5378   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5379   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5380   is false, NON_CONSTANT_P should be NULL.  */
5381
5382 static tree
5383 cp_parser_constant_expression (cp_parser* parser, 
5384                                bool allow_non_constant_p,
5385                                bool *non_constant_p)
5386 {
5387   bool saved_integral_constant_expression_p;
5388   bool saved_allow_non_integral_constant_expression_p;
5389   bool saved_non_integral_constant_expression_p;
5390   tree expression;
5391
5392   /* It might seem that we could simply parse the
5393      conditional-expression, and then check to see if it were
5394      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5395      one that the compiler can figure out is constant, possibly after
5396      doing some simplifications or optimizations.  The standard has a
5397      precise definition of constant-expression, and we must honor
5398      that, even though it is somewhat more restrictive.
5399
5400      For example:
5401
5402        int i[(2, 3)];
5403
5404      is not a legal declaration, because `(2, 3)' is not a
5405      constant-expression.  The `,' operator is forbidden in a
5406      constant-expression.  However, GCC's constant-folding machinery
5407      will fold this operation to an INTEGER_CST for `3'.  */
5408
5409   /* Save the old settings.  */
5410   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5411   saved_allow_non_integral_constant_expression_p 
5412     = parser->allow_non_integral_constant_expression_p;
5413   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5414   /* We are now parsing a constant-expression.  */
5415   parser->integral_constant_expression_p = true;
5416   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5417   parser->non_integral_constant_expression_p = false;
5418   /* Although the grammar says "conditional-expression", we parse an
5419      "assignment-expression", which also permits "throw-expression"
5420      and the use of assignment operators.  In the case that
5421      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5422      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5423      actually essential that we look for an assignment-expression.
5424      For example, cp_parser_initializer_clauses uses this function to
5425      determine whether a particular assignment-expression is in fact
5426      constant.  */
5427   expression = cp_parser_assignment_expression (parser);
5428   /* Restore the old settings.  */
5429   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5430   parser->allow_non_integral_constant_expression_p 
5431     = saved_allow_non_integral_constant_expression_p;
5432   if (allow_non_constant_p)
5433     *non_constant_p = parser->non_integral_constant_expression_p;
5434   parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5435
5436   return expression;
5437 }
5438
5439 /* Statements [gram.stmt.stmt]  */
5440
5441 /* Parse a statement.  
5442
5443    statement:
5444      labeled-statement
5445      expression-statement
5446      compound-statement
5447      selection-statement
5448      iteration-statement
5449      jump-statement
5450      declaration-statement
5451      try-block  */
5452
5453 static void
5454 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5455 {
5456   tree statement;
5457   cp_token *token;
5458   int statement_line_number;
5459
5460   /* There is no statement yet.  */
5461   statement = NULL_TREE;
5462   /* Peek at the next token.  */
5463   token = cp_lexer_peek_token (parser->lexer);
5464   /* Remember the line number of the first token in the statement.  */
5465   statement_line_number = token->location.line;
5466   /* If this is a keyword, then that will often determine what kind of
5467      statement we have.  */
5468   if (token->type == CPP_KEYWORD)
5469     {
5470       enum rid keyword = token->keyword;
5471
5472       switch (keyword)
5473         {
5474         case RID_CASE:
5475         case RID_DEFAULT:
5476           statement = cp_parser_labeled_statement (parser,
5477                                                    in_statement_expr_p);
5478           break;
5479
5480         case RID_IF:
5481         case RID_SWITCH:
5482           statement = cp_parser_selection_statement (parser);
5483           break;
5484
5485         case RID_WHILE:
5486         case RID_DO:
5487         case RID_FOR:
5488           statement = cp_parser_iteration_statement (parser);
5489           break;
5490
5491         case RID_BREAK:
5492         case RID_CONTINUE:
5493         case RID_RETURN:
5494         case RID_GOTO:
5495           statement = cp_parser_jump_statement (parser);
5496           break;
5497
5498         case RID_TRY:
5499           statement = cp_parser_try_block (parser);
5500           break;
5501
5502         default:
5503           /* It might be a keyword like `int' that can start a
5504              declaration-statement.  */
5505           break;
5506         }
5507     }
5508   else if (token->type == CPP_NAME)
5509     {
5510       /* If the next token is a `:', then we are looking at a
5511          labeled-statement.  */
5512       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5513       if (token->type == CPP_COLON)
5514         statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5515     }
5516   /* Anything that starts with a `{' must be a compound-statement.  */
5517   else if (token->type == CPP_OPEN_BRACE)
5518     statement = cp_parser_compound_statement (parser, false);
5519
5520   /* Everything else must be a declaration-statement or an
5521      expression-statement.  Try for the declaration-statement 
5522      first, unless we are looking at a `;', in which case we know that
5523      we have an expression-statement.  */
5524   if (!statement)
5525     {
5526       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5527         {
5528           cp_parser_parse_tentatively (parser);
5529           /* Try to parse the declaration-statement.  */
5530           cp_parser_declaration_statement (parser);
5531           /* If that worked, we're done.  */
5532           if (cp_parser_parse_definitely (parser))
5533             return;
5534         }
5535       /* Look for an expression-statement instead.  */
5536       statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5537     }
5538
5539   /* Set the line number for the statement.  */
5540   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5541     STMT_LINENO (statement) = statement_line_number;
5542 }
5543
5544 /* Parse a labeled-statement.
5545
5546    labeled-statement:
5547      identifier : statement
5548      case constant-expression : statement
5549      default : statement
5550
5551    GNU Extension:
5552    
5553    labeled-statement:
5554      case constant-expression ... constant-expression : statement
5555
5556    Returns the new CASE_LABEL, for a `case' or `default' label.  For
5557    an ordinary label, returns a LABEL_STMT.  */
5558
5559 static tree
5560 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5561 {
5562   cp_token *token;
5563   tree statement = error_mark_node;
5564
5565   /* The next token should be an identifier.  */
5566   token = cp_lexer_peek_token (parser->lexer);
5567   if (token->type != CPP_NAME
5568       && token->type != CPP_KEYWORD)
5569     {
5570       cp_parser_error (parser, "expected labeled-statement");
5571       return error_mark_node;
5572     }
5573
5574   switch (token->keyword)
5575     {
5576     case RID_CASE:
5577       {
5578         tree expr, expr_hi;
5579         cp_token *ellipsis;
5580
5581         /* Consume the `case' token.  */
5582         cp_lexer_consume_token (parser->lexer);
5583         /* Parse the constant-expression.  */
5584         expr = cp_parser_constant_expression (parser, 
5585                                               /*allow_non_constant_p=*/false,
5586                                               NULL);
5587
5588         ellipsis = cp_lexer_peek_token (parser->lexer);
5589         if (ellipsis->type == CPP_ELLIPSIS)
5590           {
5591             /* Consume the `...' token.  */
5592             cp_lexer_consume_token (parser->lexer);
5593             expr_hi =
5594               cp_parser_constant_expression (parser,
5595                                              /*allow_non_constant_p=*/false,
5596                                              NULL);
5597             /* We don't need to emit warnings here, as the common code
5598                will do this for us.  */
5599           }
5600         else
5601           expr_hi = NULL_TREE;
5602
5603         if (!parser->in_switch_statement_p)
5604           error ("case label `%E' not within a switch statement", expr);
5605         else
5606           statement = finish_case_label (expr, expr_hi);
5607       }
5608       break;
5609
5610     case RID_DEFAULT:
5611       /* Consume the `default' token.  */
5612       cp_lexer_consume_token (parser->lexer);
5613       if (!parser->in_switch_statement_p)
5614         error ("case label not within a switch statement");
5615       else
5616         statement = finish_case_label (NULL_TREE, NULL_TREE);
5617       break;
5618
5619     default:
5620       /* Anything else must be an ordinary label.  */
5621       statement = finish_label_stmt (cp_parser_identifier (parser));
5622       break;
5623     }
5624
5625   /* Require the `:' token.  */
5626   cp_parser_require (parser, CPP_COLON, "`:'");
5627   /* Parse the labeled statement.  */
5628   cp_parser_statement (parser, in_statement_expr_p);
5629
5630   /* Return the label, in the case of a `case' or `default' label.  */
5631   return statement;
5632 }
5633
5634 /* Parse an expression-statement.
5635
5636    expression-statement:
5637      expression [opt] ;
5638
5639    Returns the new EXPR_STMT -- or NULL_TREE if the expression
5640    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5641    indicates whether this expression-statement is part of an
5642    expression statement.  */
5643
5644 static tree
5645 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5646 {
5647   tree statement = NULL_TREE;
5648
5649   /* If the next token is a ';', then there is no expression
5650      statement.  */
5651   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5652     statement = cp_parser_expression (parser);
5653   
5654   /* Consume the final `;'.  */
5655   cp_parser_consume_semicolon_at_end_of_statement (parser);
5656
5657   if (in_statement_expr_p
5658       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5659     {
5660       /* This is the final expression statement of a statement
5661          expression.  */
5662       statement = finish_stmt_expr_expr (statement);
5663     }
5664   else if (statement)
5665     statement = finish_expr_stmt (statement);
5666   else
5667     finish_stmt ();
5668   
5669   return statement;
5670 }
5671
5672 /* Parse a compound-statement.
5673
5674    compound-statement:
5675      { statement-seq [opt] }
5676      
5677    Returns a COMPOUND_STMT representing the statement.  */
5678
5679 static tree
5680 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5681 {
5682   tree compound_stmt;
5683
5684   /* Consume the `{'.  */
5685   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5686     return error_mark_node;
5687   /* Begin the compound-statement.  */
5688   compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5689   /* Parse an (optional) statement-seq.  */
5690   cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5691   /* Finish the compound-statement.  */
5692   finish_compound_stmt (compound_stmt);
5693   /* Consume the `}'.  */
5694   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5695
5696   return compound_stmt;
5697 }
5698
5699 /* Parse an (optional) statement-seq.
5700
5701    statement-seq:
5702      statement
5703      statement-seq [opt] statement  */
5704
5705 static void
5706 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5707 {
5708   /* Scan statements until there aren't any more.  */
5709   while (true)
5710     {
5711       /* If we're looking at a `}', then we've run out of statements.  */
5712       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5713           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5714         break;
5715
5716       /* Parse the statement.  */
5717       cp_parser_statement (parser, in_statement_expr_p);
5718     }
5719 }
5720
5721 /* Parse a selection-statement.
5722
5723    selection-statement:
5724      if ( condition ) statement
5725      if ( condition ) statement else statement
5726      switch ( condition ) statement  
5727
5728    Returns the new IF_STMT or SWITCH_STMT.  */
5729
5730 static tree
5731 cp_parser_selection_statement (cp_parser* parser)
5732 {
5733   cp_token *token;
5734   enum rid keyword;
5735
5736   /* Peek at the next token.  */
5737   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5738
5739   /* See what kind of keyword it is.  */
5740   keyword = token->keyword;
5741   switch (keyword)
5742     {
5743     case RID_IF:
5744     case RID_SWITCH:
5745       {
5746         tree statement;
5747         tree condition;
5748
5749         /* Look for the `('.  */
5750         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5751           {
5752             cp_parser_skip_to_end_of_statement (parser);
5753             return error_mark_node;
5754           }
5755
5756         /* Begin the selection-statement.  */
5757         if (keyword == RID_IF)
5758           statement = begin_if_stmt ();
5759         else
5760           statement = begin_switch_stmt ();
5761
5762         /* Parse the condition.  */
5763         condition = cp_parser_condition (parser);
5764         /* Look for the `)'.  */
5765         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5766           cp_parser_skip_to_closing_parenthesis (parser, true, false,
5767                                                  /*consume_paren=*/true);
5768
5769         if (keyword == RID_IF)
5770           {
5771             tree then_stmt;
5772
5773             /* Add the condition.  */
5774             finish_if_stmt_cond (condition, statement);
5775
5776             /* Parse the then-clause.  */
5777             then_stmt = cp_parser_implicitly_scoped_statement (parser);
5778             finish_then_clause (statement);
5779
5780             /* If the next token is `else', parse the else-clause.  */
5781             if (cp_lexer_next_token_is_keyword (parser->lexer,
5782                                                 RID_ELSE))
5783               {
5784                 tree else_stmt;
5785
5786                 /* Consume the `else' keyword.  */
5787                 cp_lexer_consume_token (parser->lexer);
5788                 /* Parse the else-clause.  */
5789                 else_stmt 
5790                   = cp_parser_implicitly_scoped_statement (parser);
5791                 finish_else_clause (statement);
5792               }
5793
5794             /* Now we're all done with the if-statement.  */
5795             finish_if_stmt ();
5796           }
5797         else
5798           {
5799             tree body;
5800             bool in_switch_statement_p;
5801
5802             /* Add the condition.  */
5803             finish_switch_cond (condition, statement);
5804
5805             /* Parse the body of the switch-statement.  */
5806             in_switch_statement_p = parser->in_switch_statement_p;
5807             parser->in_switch_statement_p = true;
5808             body = cp_parser_implicitly_scoped_statement (parser);
5809             parser->in_switch_statement_p = in_switch_statement_p;
5810
5811             /* Now we're all done with the switch-statement.  */
5812             finish_switch_stmt (statement);
5813           }
5814
5815         return statement;
5816       }
5817       break;
5818
5819     default:
5820       cp_parser_error (parser, "expected selection-statement");
5821       return error_mark_node;
5822     }
5823 }
5824
5825 /* Parse a condition. 
5826
5827    condition:
5828      expression
5829      type-specifier-seq declarator = assignment-expression  
5830
5831    GNU Extension:
5832    
5833    condition:
5834      type-specifier-seq declarator asm-specification [opt] 
5835        attributes [opt] = assignment-expression
5836  
5837    Returns the expression that should be tested.  */
5838
5839 static tree
5840 cp_parser_condition (cp_parser* parser)
5841 {
5842   tree type_specifiers;
5843   const char *saved_message;
5844
5845   /* Try the declaration first.  */
5846   cp_parser_parse_tentatively (parser);
5847   /* New types are not allowed in the type-specifier-seq for a
5848      condition.  */
5849   saved_message = parser->type_definition_forbidden_message;
5850   parser->type_definition_forbidden_message
5851     = "types may not be defined in conditions";
5852   /* Parse the type-specifier-seq.  */
5853   type_specifiers = cp_parser_type_specifier_seq (parser);
5854   /* Restore the saved message.  */
5855   parser->type_definition_forbidden_message = saved_message;
5856   /* If all is well, we might be looking at a declaration.  */
5857   if (!cp_parser_error_occurred (parser))
5858     {
5859       tree decl;
5860       tree asm_specification;
5861       tree attributes;
5862       tree declarator;
5863       tree initializer = NULL_TREE;
5864       
5865       /* Parse the declarator.  */
5866       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5867                                          /*ctor_dtor_or_conv_p=*/NULL,
5868                                          /*parenthesized_p=*/NULL,
5869                                          /*member_p=*/false);
5870       /* Parse the attributes.  */
5871       attributes = cp_parser_attributes_opt (parser);
5872       /* Parse the asm-specification.  */
5873       asm_specification = cp_parser_asm_specification_opt (parser);
5874       /* If the next token is not an `=', then we might still be
5875          looking at an expression.  For example:
5876          
5877            if (A(a).x)
5878           
5879          looks like a decl-specifier-seq and a declarator -- but then
5880          there is no `=', so this is an expression.  */
5881       cp_parser_require (parser, CPP_EQ, "`='");
5882       /* If we did see an `=', then we are looking at a declaration
5883          for sure.  */
5884       if (cp_parser_parse_definitely (parser))
5885         {
5886           /* Create the declaration.  */
5887           decl = start_decl (declarator, type_specifiers, 
5888                              /*initialized_p=*/true,
5889                              attributes, /*prefix_attributes=*/NULL_TREE);
5890           /* Parse the assignment-expression.  */
5891           initializer = cp_parser_assignment_expression (parser);
5892           
5893           /* Process the initializer.  */
5894           cp_finish_decl (decl, 
5895                           initializer, 
5896                           asm_specification, 
5897                           LOOKUP_ONLYCONVERTING);
5898           
5899           return convert_from_reference (decl);
5900         }
5901     }
5902   /* If we didn't even get past the declarator successfully, we are
5903      definitely not looking at a declaration.  */
5904   else
5905     cp_parser_abort_tentative_parse (parser);
5906
5907   /* Otherwise, we are looking at an expression.  */
5908   return cp_parser_expression (parser);
5909 }
5910
5911 /* Parse an iteration-statement.
5912
5913    iteration-statement:
5914      while ( condition ) statement
5915      do statement while ( expression ) ;
5916      for ( for-init-statement condition [opt] ; expression [opt] )
5917        statement
5918
5919    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
5920
5921 static tree
5922 cp_parser_iteration_statement (cp_parser* parser)
5923 {
5924   cp_token *token;
5925   enum rid keyword;
5926   tree statement;
5927   bool in_iteration_statement_p;
5928
5929
5930   /* Peek at the next token.  */
5931   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5932   if (!token)
5933     return error_mark_node;
5934
5935   /* Remember whether or not we are already within an iteration
5936      statement.  */ 
5937   in_iteration_statement_p = parser->in_iteration_statement_p;
5938
5939   /* See what kind of keyword it is.  */
5940   keyword = token->keyword;
5941   switch (keyword)
5942     {
5943     case RID_WHILE:
5944       {
5945         tree condition;
5946
5947         /* Begin the while-statement.  */
5948         statement = begin_while_stmt ();
5949         /* Look for the `('.  */
5950         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5951         /* Parse the condition.  */
5952         condition = cp_parser_condition (parser);
5953         finish_while_stmt_cond (condition, statement);
5954         /* Look for the `)'.  */
5955         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5956         /* Parse the dependent statement.  */
5957         parser->in_iteration_statement_p = true;
5958         cp_parser_already_scoped_statement (parser);
5959         parser->in_iteration_statement_p = in_iteration_statement_p;
5960         /* We're done with the while-statement.  */
5961         finish_while_stmt (statement);
5962       }
5963       break;
5964
5965     case RID_DO:
5966       {
5967         tree expression;
5968
5969         /* Begin the do-statement.  */
5970         statement = begin_do_stmt ();
5971         /* Parse the body of the do-statement.  */
5972         parser->in_iteration_statement_p = true;
5973         cp_parser_implicitly_scoped_statement (parser);
5974         parser->in_iteration_statement_p = in_iteration_statement_p;
5975         finish_do_body (statement);
5976         /* Look for the `while' keyword.  */
5977         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5978         /* Look for the `('.  */
5979         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5980         /* Parse the expression.  */
5981         expression = cp_parser_expression (parser);
5982         /* We're done with the do-statement.  */
5983         finish_do_stmt (expression, statement);
5984         /* Look for the `)'.  */
5985         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5986         /* Look for the `;'.  */
5987         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5988       }
5989       break;
5990
5991     case RID_FOR:
5992       {
5993         tree condition = NULL_TREE;
5994         tree expression = NULL_TREE;
5995
5996         /* Begin the for-statement.  */
5997         statement = begin_for_stmt ();
5998         /* Look for the `('.  */
5999         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6000         /* Parse the initialization.  */
6001         cp_parser_for_init_statement (parser);
6002         finish_for_init_stmt (statement);
6003
6004         /* If there's a condition, process it.  */
6005         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6006           condition = cp_parser_condition (parser);
6007         finish_for_cond (condition, statement);
6008         /* Look for the `;'.  */
6009         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6010
6011         /* If there's an expression, process it.  */
6012         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6013           expression = cp_parser_expression (parser);
6014         finish_for_expr (expression, statement);
6015         /* Look for the `)'.  */
6016         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6017         
6018         /* Parse the body of the for-statement.  */
6019         parser->in_iteration_statement_p = true;
6020         cp_parser_already_scoped_statement (parser);
6021         parser->in_iteration_statement_p = in_iteration_statement_p;
6022
6023         /* We're done with the for-statement.  */
6024         finish_for_stmt (statement);
6025       }
6026       break;
6027
6028     default:
6029       cp_parser_error (parser, "expected iteration-statement");
6030       statement = error_mark_node;
6031       break;
6032     }
6033
6034   return statement;
6035 }
6036
6037 /* Parse a for-init-statement.
6038
6039    for-init-statement:
6040      expression-statement
6041      simple-declaration  */
6042
6043 static void
6044 cp_parser_for_init_statement (cp_parser* parser)
6045 {
6046   /* If the next token is a `;', then we have an empty
6047      expression-statement.  Grammatically, this is also a
6048      simple-declaration, but an invalid one, because it does not
6049      declare anything.  Therefore, if we did not handle this case
6050      specially, we would issue an error message about an invalid
6051      declaration.  */
6052   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6053     {
6054       /* We're going to speculatively look for a declaration, falling back
6055          to an expression, if necessary.  */
6056       cp_parser_parse_tentatively (parser);
6057       /* Parse the declaration.  */
6058       cp_parser_simple_declaration (parser,
6059                                     /*function_definition_allowed_p=*/false);
6060       /* If the tentative parse failed, then we shall need to look for an
6061          expression-statement.  */
6062       if (cp_parser_parse_definitely (parser))
6063         return;
6064     }
6065
6066   cp_parser_expression_statement (parser, false);
6067 }
6068
6069 /* Parse a jump-statement.
6070
6071    jump-statement:
6072      break ;
6073      continue ;
6074      return expression [opt] ;
6075      goto identifier ;  
6076
6077    GNU extension:
6078
6079    jump-statement:
6080      goto * expression ;
6081
6082    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6083    GOTO_STMT.  */
6084
6085 static tree
6086 cp_parser_jump_statement (cp_parser* parser)
6087 {
6088   tree statement = error_mark_node;
6089   cp_token *token;
6090   enum rid keyword;
6091
6092   /* Peek at the next token.  */
6093   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6094   if (!token)
6095     return error_mark_node;
6096
6097   /* See what kind of keyword it is.  */
6098   keyword = token->keyword;
6099   switch (keyword)
6100     {
6101     case RID_BREAK:
6102       if (!parser->in_switch_statement_p
6103           && !parser->in_iteration_statement_p)
6104         {
6105           error ("break statement not within loop or switch");
6106           statement = error_mark_node;
6107         }
6108       else
6109         statement = finish_break_stmt ();
6110       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6111       break;
6112
6113     case RID_CONTINUE:
6114       if (!parser->in_iteration_statement_p)
6115         {
6116           error ("continue statement not within a loop");
6117           statement = error_mark_node;
6118         }
6119       else
6120         statement = finish_continue_stmt ();
6121       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6122       break;
6123
6124     case RID_RETURN:
6125       {
6126         tree expr;
6127
6128         /* If the next token is a `;', then there is no 
6129            expression.  */
6130         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6131           expr = cp_parser_expression (parser);
6132         else
6133           expr = NULL_TREE;
6134         /* Build the return-statement.  */
6135         statement = finish_return_stmt (expr);
6136         /* Look for the final `;'.  */
6137         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6138       }
6139       break;
6140
6141     case RID_GOTO:
6142       /* Create the goto-statement.  */
6143       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6144         {
6145           /* Issue a warning about this use of a GNU extension.  */
6146           if (pedantic)
6147             pedwarn ("ISO C++ forbids computed gotos");
6148           /* Consume the '*' token.  */
6149           cp_lexer_consume_token (parser->lexer);
6150           /* Parse the dependent expression.  */
6151           finish_goto_stmt (cp_parser_expression (parser));
6152         }
6153       else
6154         finish_goto_stmt (cp_parser_identifier (parser));
6155       /* Look for the final `;'.  */
6156       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6157       break;
6158
6159     default:
6160       cp_parser_error (parser, "expected jump-statement");
6161       break;
6162     }
6163
6164   return statement;
6165 }
6166
6167 /* Parse a declaration-statement.
6168
6169    declaration-statement:
6170      block-declaration  */
6171
6172 static void
6173 cp_parser_declaration_statement (cp_parser* parser)
6174 {
6175   /* Parse the block-declaration.  */
6176   cp_parser_block_declaration (parser, /*statement_p=*/true);
6177
6178   /* Finish off the statement.  */
6179   finish_stmt ();
6180 }
6181
6182 /* Some dependent statements (like `if (cond) statement'), are
6183    implicitly in their own scope.  In other words, if the statement is
6184    a single statement (as opposed to a compound-statement), it is
6185    none-the-less treated as if it were enclosed in braces.  Any
6186    declarations appearing in the dependent statement are out of scope
6187    after control passes that point.  This function parses a statement,
6188    but ensures that is in its own scope, even if it is not a
6189    compound-statement.  
6190
6191    Returns the new statement.  */
6192
6193 static tree
6194 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6195 {
6196   tree statement;
6197
6198   /* If the token is not a `{', then we must take special action.  */
6199   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6200     {
6201       /* Create a compound-statement.  */
6202       statement = begin_compound_stmt (/*has_no_scope=*/false);
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     statement = cp_parser_compound_statement (parser, false);
6211
6212   /* Return the statement.  */
6213   return statement;
6214 }
6215
6216 /* For some dependent statements (like `while (cond) statement'), we
6217    have already created a scope.  Therefore, even if the dependent
6218    statement is a compound-statement, we do not want to create another
6219    scope.  */
6220
6221 static void
6222 cp_parser_already_scoped_statement (cp_parser* parser)
6223 {
6224   /* If the token is not a `{', then we must take special action.  */
6225   if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6226     {
6227       tree statement;
6228
6229       /* Create a compound-statement.  */
6230       statement = begin_compound_stmt (/*has_no_scope=*/true);
6231       /* Parse the dependent-statement.  */
6232       cp_parser_statement (parser, false);
6233       /* Finish the dummy compound-statement.  */
6234       finish_compound_stmt (statement);
6235     }
6236   /* Otherwise, we simply parse the statement directly.  */
6237   else
6238     cp_parser_statement (parser, false);
6239 }
6240
6241 /* Declarations [gram.dcl.dcl] */
6242
6243 /* Parse an optional declaration-sequence.
6244
6245    declaration-seq:
6246      declaration
6247      declaration-seq declaration  */
6248
6249 static void
6250 cp_parser_declaration_seq_opt (cp_parser* parser)
6251 {
6252   while (true)
6253     {
6254       cp_token *token;
6255
6256       token = cp_lexer_peek_token (parser->lexer);
6257
6258       if (token->type == CPP_CLOSE_BRACE
6259           || token->type == CPP_EOF)
6260         break;
6261
6262       if (token->type == CPP_SEMICOLON) 
6263         {
6264           /* A declaration consisting of a single semicolon is
6265              invalid.  Allow it unless we're being pedantic.  */
6266           if (pedantic && !in_system_header)
6267             pedwarn ("extra `;'");
6268           cp_lexer_consume_token (parser->lexer);
6269           continue;
6270         }
6271
6272       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6273          parser to enter or exit implicit `extern "C"' blocks.  */
6274       while (pending_lang_change > 0)
6275         {
6276           push_lang_context (lang_name_c);
6277           --pending_lang_change;
6278         }
6279       while (pending_lang_change < 0)
6280         {
6281           pop_lang_context ();
6282           ++pending_lang_change;
6283         }
6284
6285       /* Parse the declaration itself.  */
6286       cp_parser_declaration (parser);
6287     }
6288 }
6289
6290 /* Parse a declaration.
6291
6292    declaration:
6293      block-declaration
6294      function-definition
6295      template-declaration
6296      explicit-instantiation
6297      explicit-specialization
6298      linkage-specification
6299      namespace-definition    
6300
6301    GNU extension:
6302
6303    declaration:
6304       __extension__ declaration */
6305
6306 static void
6307 cp_parser_declaration (cp_parser* parser)
6308 {
6309   cp_token token1;
6310   cp_token token2;
6311   int saved_pedantic;
6312
6313   /* Check for the `__extension__' keyword.  */
6314   if (cp_parser_extension_opt (parser, &saved_pedantic))
6315     {
6316       /* Parse the qualified declaration.  */
6317       cp_parser_declaration (parser);
6318       /* Restore the PEDANTIC flag.  */
6319       pedantic = saved_pedantic;
6320
6321       return;
6322     }
6323
6324   /* Try to figure out what kind of declaration is present.  */
6325   token1 = *cp_lexer_peek_token (parser->lexer);
6326   if (token1.type != CPP_EOF)
6327     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6328
6329   /* If the next token is `extern' and the following token is a string
6330      literal, then we have a linkage specification.  */
6331   if (token1.keyword == RID_EXTERN
6332       && cp_parser_is_string_literal (&token2))
6333     cp_parser_linkage_specification (parser);
6334   /* If the next token is `template', then we have either a template
6335      declaration, an explicit instantiation, or an explicit
6336      specialization.  */
6337   else if (token1.keyword == RID_TEMPLATE)
6338     {
6339       /* `template <>' indicates a template specialization.  */
6340       if (token2.type == CPP_LESS
6341           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6342         cp_parser_explicit_specialization (parser);
6343       /* `template <' indicates a template declaration.  */
6344       else if (token2.type == CPP_LESS)
6345         cp_parser_template_declaration (parser, /*member_p=*/false);
6346       /* Anything else must be an explicit instantiation.  */
6347       else
6348         cp_parser_explicit_instantiation (parser);
6349     }
6350   /* If the next token is `export', then we have a template
6351      declaration.  */
6352   else if (token1.keyword == RID_EXPORT)
6353     cp_parser_template_declaration (parser, /*member_p=*/false);
6354   /* If the next token is `extern', 'static' or 'inline' and the one
6355      after that is `template', we have a GNU extended explicit
6356      instantiation directive.  */
6357   else if (cp_parser_allow_gnu_extensions_p (parser)
6358            && (token1.keyword == RID_EXTERN
6359                || token1.keyword == RID_STATIC
6360                || token1.keyword == RID_INLINE)
6361            && token2.keyword == RID_TEMPLATE)
6362     cp_parser_explicit_instantiation (parser);
6363   /* If the next token is `namespace', check for a named or unnamed
6364      namespace definition.  */
6365   else if (token1.keyword == RID_NAMESPACE
6366            && (/* A named namespace definition.  */
6367                (token2.type == CPP_NAME
6368                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
6369                     == CPP_OPEN_BRACE))
6370                /* An unnamed namespace definition.  */
6371                || token2.type == CPP_OPEN_BRACE))
6372     cp_parser_namespace_definition (parser);
6373   /* We must have either a block declaration or a function
6374      definition.  */
6375   else
6376     /* Try to parse a block-declaration, or a function-definition.  */
6377     cp_parser_block_declaration (parser, /*statement_p=*/false);
6378 }
6379
6380 /* Parse a block-declaration.  
6381
6382    block-declaration:
6383      simple-declaration
6384      asm-definition
6385      namespace-alias-definition
6386      using-declaration
6387      using-directive  
6388
6389    GNU Extension:
6390
6391    block-declaration:
6392      __extension__ block-declaration 
6393      label-declaration
6394
6395    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6396    part of a declaration-statement.  */
6397
6398 static void
6399 cp_parser_block_declaration (cp_parser *parser, 
6400                              bool      statement_p)
6401 {
6402   cp_token *token1;
6403   int saved_pedantic;
6404
6405   /* Check for the `__extension__' keyword.  */
6406   if (cp_parser_extension_opt (parser, &saved_pedantic))
6407     {
6408       /* Parse the qualified declaration.  */
6409       cp_parser_block_declaration (parser, statement_p);
6410       /* Restore the PEDANTIC flag.  */
6411       pedantic = saved_pedantic;
6412
6413       return;
6414     }
6415
6416   /* Peek at the next token to figure out which kind of declaration is
6417      present.  */
6418   token1 = cp_lexer_peek_token (parser->lexer);
6419
6420   /* If the next keyword is `asm', we have an asm-definition.  */
6421   if (token1->keyword == RID_ASM)
6422     {
6423       if (statement_p)
6424         cp_parser_commit_to_tentative_parse (parser);
6425       cp_parser_asm_definition (parser);
6426     }
6427   /* If the next keyword is `namespace', we have a
6428      namespace-alias-definition.  */
6429   else if (token1->keyword == RID_NAMESPACE)
6430     cp_parser_namespace_alias_definition (parser);
6431   /* If the next keyword is `using', we have either a
6432      using-declaration or a using-directive.  */
6433   else if (token1->keyword == RID_USING)
6434     {
6435       cp_token *token2;
6436
6437       if (statement_p)
6438         cp_parser_commit_to_tentative_parse (parser);
6439       /* If the token after `using' is `namespace', then we have a
6440          using-directive.  */
6441       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6442       if (token2->keyword == RID_NAMESPACE)
6443         cp_parser_using_directive (parser);
6444       /* Otherwise, it's a using-declaration.  */
6445       else
6446         cp_parser_using_declaration (parser);
6447     }
6448   /* If the next keyword is `__label__' we have a label declaration.  */
6449   else if (token1->keyword == RID_LABEL)
6450     {
6451       if (statement_p)
6452         cp_parser_commit_to_tentative_parse (parser);
6453       cp_parser_label_declaration (parser);
6454     }
6455   /* Anything else must be a simple-declaration.  */
6456   else
6457     cp_parser_simple_declaration (parser, !statement_p);
6458 }
6459
6460 /* Parse a simple-declaration.
6461
6462    simple-declaration:
6463      decl-specifier-seq [opt] init-declarator-list [opt] ;  
6464
6465    init-declarator-list:
6466      init-declarator
6467      init-declarator-list , init-declarator 
6468
6469    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6470    function-definition as a simple-declaration.  */
6471
6472 static void
6473 cp_parser_simple_declaration (cp_parser* parser, 
6474                               bool function_definition_allowed_p)
6475 {
6476   tree decl_specifiers;
6477   tree attributes;
6478   int declares_class_or_enum;
6479   bool saw_declarator;
6480
6481   /* Defer access checks until we know what is being declared; the
6482      checks for names appearing in the decl-specifier-seq should be
6483      done as if we were in the scope of the thing being declared.  */
6484   push_deferring_access_checks (dk_deferred);
6485
6486   /* Parse the decl-specifier-seq.  We have to keep track of whether
6487      or not the decl-specifier-seq declares a named class or
6488      enumeration type, since that is the only case in which the
6489      init-declarator-list is allowed to be empty.  
6490
6491      [dcl.dcl]
6492
6493      In a simple-declaration, the optional init-declarator-list can be
6494      omitted only when declaring a class or enumeration, that is when
6495      the decl-specifier-seq contains either a class-specifier, an
6496      elaborated-type-specifier, or an enum-specifier.  */
6497   decl_specifiers
6498     = cp_parser_decl_specifier_seq (parser, 
6499                                     CP_PARSER_FLAGS_OPTIONAL,
6500                                     &attributes,
6501                                     &declares_class_or_enum);
6502   /* We no longer need to defer access checks.  */
6503   stop_deferring_access_checks ();
6504
6505   /* In a block scope, a valid declaration must always have a
6506      decl-specifier-seq.  By not trying to parse declarators, we can
6507      resolve the declaration/expression ambiguity more quickly.  */
6508   if (!function_definition_allowed_p && !decl_specifiers)
6509     {
6510       cp_parser_error (parser, "expected declaration");
6511       goto done;
6512     }
6513
6514   /* If the next two tokens are both identifiers, the code is
6515      erroneous. The usual cause of this situation is code like:
6516
6517        T t;
6518
6519      where "T" should name a type -- but does not.  */
6520   if (cp_parser_diagnose_invalid_type_name (parser))
6521     {
6522       /* If parsing tentatively, we should commit; we really are
6523          looking at a declaration.  */
6524       cp_parser_commit_to_tentative_parse (parser);
6525       /* Give up.  */
6526       goto done;
6527     }
6528   
6529   /* If we have seen at least one decl-specifier, and the next token
6530      is not a parenthesis, then we must be looking at a declaration.
6531      (After "int (" we might be looking at a functional cast.)  */
6532   if (decl_specifiers
6533       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6534     cp_parser_commit_to_tentative_parse (parser);
6535
6536   /* Keep going until we hit the `;' at the end of the simple
6537      declaration.  */
6538   saw_declarator = false;
6539   while (cp_lexer_next_token_is_not (parser->lexer, 
6540                                      CPP_SEMICOLON))
6541     {
6542       cp_token *token;
6543       bool function_definition_p;
6544       tree decl;
6545
6546       saw_declarator = true;
6547       /* Parse the init-declarator.  */
6548       decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6549                                         function_definition_allowed_p,
6550                                         /*member_p=*/false,
6551                                         declares_class_or_enum,
6552                                         &function_definition_p);
6553       /* If an error occurred while parsing tentatively, exit quickly.
6554          (That usually happens when in the body of a function; each
6555          statement is treated as a declaration-statement until proven
6556          otherwise.)  */
6557       if (cp_parser_error_occurred (parser))
6558         goto done;
6559       /* Handle function definitions specially.  */
6560       if (function_definition_p)
6561         {
6562           /* If the next token is a `,', then we are probably
6563              processing something like:
6564
6565                void f() {}, *p;
6566
6567              which is erroneous.  */
6568           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6569             error ("mixing declarations and function-definitions is forbidden");
6570           /* Otherwise, we're done with the list of declarators.  */
6571           else
6572             {
6573               pop_deferring_access_checks ();
6574               return;
6575             }
6576         }
6577       /* The next token should be either a `,' or a `;'.  */
6578       token = cp_lexer_peek_token (parser->lexer);
6579       /* If it's a `,', there are more declarators to come.  */
6580       if (token->type == CPP_COMMA)
6581         cp_lexer_consume_token (parser->lexer);
6582       /* If it's a `;', we are done.  */
6583       else if (token->type == CPP_SEMICOLON)
6584         break;
6585       /* Anything else is an error.  */
6586       else
6587         {
6588           /* If we have already issued an error message we don't need
6589              to issue another one.  */
6590           if (decl != error_mark_node
6591               || (cp_parser_parsing_tentatively (parser)
6592                   && !cp_parser_committed_to_tentative_parse (parser)))
6593             cp_parser_error (parser, "expected `,' or `;'");
6594           /* Skip tokens until we reach the end of the statement.  */
6595           cp_parser_skip_to_end_of_statement (parser);
6596           /* If the next token is now a `;', consume it.  */
6597           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6598             cp_lexer_consume_token (parser->lexer);
6599           goto done;
6600         }
6601       /* After the first time around, a function-definition is not
6602          allowed -- even if it was OK at first.  For example:
6603
6604            int i, f() {}
6605
6606          is not valid.  */
6607       function_definition_allowed_p = false;
6608     }
6609
6610   /* Issue an error message if no declarators are present, and the
6611      decl-specifier-seq does not itself declare a class or
6612      enumeration.  */
6613   if (!saw_declarator)
6614     {
6615       if (cp_parser_declares_only_class_p (parser))
6616         shadow_tag (decl_specifiers);
6617       /* Perform any deferred access checks.  */
6618       perform_deferred_access_checks ();
6619     }
6620
6621   /* Consume the `;'.  */
6622   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6623
6624  done:
6625   pop_deferring_access_checks ();
6626 }
6627
6628 /* Parse a decl-specifier-seq.
6629
6630    decl-specifier-seq:
6631      decl-specifier-seq [opt] decl-specifier
6632
6633    decl-specifier:
6634      storage-class-specifier
6635      type-specifier
6636      function-specifier
6637      friend
6638      typedef  
6639
6640    GNU Extension:
6641
6642    decl-specifier:
6643      attributes
6644
6645    Returns a TREE_LIST, giving the decl-specifiers in the order they
6646    appear in the source code.  The TREE_VALUE of each node is the
6647    decl-specifier.  For a keyword (such as `auto' or `friend'), the
6648    TREE_VALUE is simply the corresponding TREE_IDENTIFIER.  For the
6649    representation of a type-specifier, see cp_parser_type_specifier.  
6650
6651    If there are attributes, they will be stored in *ATTRIBUTES,
6652    represented as described above cp_parser_attributes.  
6653
6654    If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6655    appears, and the entity that will be a friend is not going to be a
6656    class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
6657    even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6658    friendship is granted might not be a class.  
6659
6660    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6661    flags:
6662
6663      1: one of the decl-specifiers is an elaborated-type-specifier
6664         (i.e., a type declaration)
6665      2: one of the decl-specifiers is an enum-specifier or a
6666         class-specifier (i.e., a type definition)
6667
6668    */
6669
6670 static tree
6671 cp_parser_decl_specifier_seq (cp_parser* parser, 
6672                               cp_parser_flags flags, 
6673                               tree* attributes,
6674                               int* declares_class_or_enum)
6675 {
6676   tree decl_specs = NULL_TREE;
6677   bool friend_p = false;
6678   bool constructor_possible_p = !parser->in_declarator_p;
6679   
6680   /* Assume no class or enumeration type is declared.  */
6681   *declares_class_or_enum = 0;
6682
6683   /* Assume there are no attributes.  */
6684   *attributes = NULL_TREE;
6685
6686   /* Keep reading specifiers until there are no more to read.  */
6687   while (true)
6688     {
6689       tree decl_spec = NULL_TREE;
6690       bool constructor_p;
6691       cp_token *token;
6692
6693       /* Peek at the next token.  */
6694       token = cp_lexer_peek_token (parser->lexer);
6695       /* Handle attributes.  */
6696       if (token->keyword == RID_ATTRIBUTE)
6697         {
6698           /* Parse the attributes.  */
6699           decl_spec = cp_parser_attributes_opt (parser);
6700           /* Add them to the list.  */
6701           *attributes = chainon (*attributes, decl_spec);
6702           continue;
6703         }
6704       /* If the next token is an appropriate keyword, we can simply
6705          add it to the list.  */
6706       switch (token->keyword)
6707         {
6708         case RID_FRIEND:
6709           /* decl-specifier:
6710                friend  */
6711           if (friend_p)
6712             error ("duplicate `friend'");
6713           else
6714             friend_p = true;
6715           /* The representation of the specifier is simply the
6716              appropriate TREE_IDENTIFIER node.  */
6717           decl_spec = token->value;
6718           /* Consume the token.  */
6719           cp_lexer_consume_token (parser->lexer);
6720           break;
6721
6722           /* function-specifier:
6723                inline
6724                virtual
6725                explicit  */
6726         case RID_INLINE:
6727         case RID_VIRTUAL:
6728         case RID_EXPLICIT:
6729           decl_spec = cp_parser_function_specifier_opt (parser);
6730           break;
6731           
6732           /* decl-specifier:
6733                typedef  */
6734         case RID_TYPEDEF:
6735           /* The representation of the specifier is simply the
6736              appropriate TREE_IDENTIFIER node.  */
6737           decl_spec = token->value;
6738           /* Consume the token.  */
6739           cp_lexer_consume_token (parser->lexer);
6740           /* A constructor declarator cannot appear in a typedef.  */
6741           constructor_possible_p = false;
6742           /* The "typedef" keyword can only occur in a declaration; we
6743              may as well commit at this point.  */
6744           cp_parser_commit_to_tentative_parse (parser);
6745           break;
6746
6747           /* storage-class-specifier:
6748                auto
6749                register
6750                static
6751                extern
6752                mutable  
6753
6754              GNU Extension:
6755                thread  */
6756         case RID_AUTO:
6757         case RID_REGISTER:
6758         case RID_STATIC:
6759         case RID_EXTERN:
6760         case RID_MUTABLE:
6761         case RID_THREAD:
6762           decl_spec = cp_parser_storage_class_specifier_opt (parser);
6763           break;
6764           
6765         default:
6766           break;
6767         }
6768
6769       /* Constructors are a special case.  The `S' in `S()' is not a
6770          decl-specifier; it is the beginning of the declarator.  */
6771       constructor_p = (!decl_spec 
6772                        && constructor_possible_p
6773                        && cp_parser_constructor_declarator_p (parser,
6774                                                               friend_p));
6775
6776       /* If we don't have a DECL_SPEC yet, then we must be looking at
6777          a type-specifier.  */
6778       if (!decl_spec && !constructor_p)
6779         {
6780           int decl_spec_declares_class_or_enum;
6781           bool is_cv_qualifier;
6782
6783           decl_spec
6784             = cp_parser_type_specifier (parser, flags,
6785                                         friend_p,
6786                                         /*is_declaration=*/true,
6787                                         &decl_spec_declares_class_or_enum,
6788                                         &is_cv_qualifier);
6789
6790           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6791
6792           /* If this type-specifier referenced a user-defined type
6793              (a typedef, class-name, etc.), then we can't allow any
6794              more such type-specifiers henceforth.
6795
6796              [dcl.spec]
6797
6798              The longest sequence of decl-specifiers that could
6799              possibly be a type name is taken as the
6800              decl-specifier-seq of a declaration.  The sequence shall
6801              be self-consistent as described below.
6802
6803              [dcl.type]
6804
6805              As a general rule, at most one type-specifier is allowed
6806              in the complete decl-specifier-seq of a declaration.  The
6807              only exceptions are the following:
6808
6809              -- const or volatile can be combined with any other
6810                 type-specifier. 
6811
6812              -- signed or unsigned can be combined with char, long,
6813                 short, or int.
6814
6815              -- ..
6816
6817              Example:
6818
6819                typedef char* Pc;
6820                void g (const int Pc);
6821
6822              Here, Pc is *not* part of the decl-specifier seq; it's
6823              the declarator.  Therefore, once we see a type-specifier
6824              (other than a cv-qualifier), we forbid any additional
6825              user-defined types.  We *do* still allow things like `int
6826              int' to be considered a decl-specifier-seq, and issue the
6827              error message later.  */
6828           if (decl_spec && !is_cv_qualifier)
6829             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6830           /* A constructor declarator cannot follow a type-specifier.  */
6831           if (decl_spec)
6832             constructor_possible_p = false;
6833         }
6834
6835       /* If we still do not have a DECL_SPEC, then there are no more
6836          decl-specifiers.  */
6837       if (!decl_spec)
6838         {
6839           /* Issue an error message, unless the entire construct was
6840              optional.  */
6841           if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6842             {
6843               cp_parser_error (parser, "expected decl specifier");
6844               return error_mark_node;
6845             }
6846
6847           break;
6848         }
6849
6850       /* Add the DECL_SPEC to the list of specifiers.  */
6851       if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6852         decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6853
6854       /* After we see one decl-specifier, further decl-specifiers are
6855          always optional.  */
6856       flags |= CP_PARSER_FLAGS_OPTIONAL;
6857     }
6858
6859   /* Don't allow a friend specifier with a class definition.  */
6860   if (friend_p && (*declares_class_or_enum & 2))
6861     error ("class definition may not be declared a friend");
6862
6863   /* We have built up the DECL_SPECS in reverse order.  Return them in
6864      the correct order.  */
6865   return nreverse (decl_specs);
6866 }
6867
6868 /* Parse an (optional) storage-class-specifier. 
6869
6870    storage-class-specifier:
6871      auto
6872      register
6873      static
6874      extern
6875      mutable  
6876
6877    GNU Extension:
6878
6879    storage-class-specifier:
6880      thread
6881
6882    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6883    
6884 static tree
6885 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6886 {
6887   switch (cp_lexer_peek_token (parser->lexer)->keyword)
6888     {
6889     case RID_AUTO:
6890     case RID_REGISTER:
6891     case RID_STATIC:
6892     case RID_EXTERN:
6893     case RID_MUTABLE:
6894     case RID_THREAD:
6895       /* Consume the token.  */
6896       return cp_lexer_consume_token (parser->lexer)->value;
6897
6898     default:
6899       return NULL_TREE;
6900     }
6901 }
6902
6903 /* Parse an (optional) function-specifier. 
6904
6905    function-specifier:
6906      inline
6907      virtual
6908      explicit
6909
6910    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6911    
6912 static tree
6913 cp_parser_function_specifier_opt (cp_parser* parser)
6914 {
6915   switch (cp_lexer_peek_token (parser->lexer)->keyword)
6916     {
6917     case RID_INLINE:
6918     case RID_VIRTUAL:
6919     case RID_EXPLICIT:
6920       /* Consume the token.  */
6921       return cp_lexer_consume_token (parser->lexer)->value;
6922
6923     default:
6924       return NULL_TREE;
6925     }
6926 }
6927
6928 /* Parse a linkage-specification.
6929
6930    linkage-specification:
6931      extern string-literal { declaration-seq [opt] }
6932      extern string-literal declaration  */
6933
6934 static void
6935 cp_parser_linkage_specification (cp_parser* parser)
6936 {
6937   cp_token *token;
6938   tree linkage;
6939
6940   /* Look for the `extern' keyword.  */
6941   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6942
6943   /* Peek at the next token.  */
6944   token = cp_lexer_peek_token (parser->lexer);
6945   /* If it's not a string-literal, then there's a problem.  */
6946   if (!cp_parser_is_string_literal (token))
6947     {
6948       cp_parser_error (parser, "expected language-name");
6949       return;
6950     }
6951   /* Consume the token.  */
6952   cp_lexer_consume_token (parser->lexer);
6953
6954   /* Transform the literal into an identifier.  If the literal is a
6955      wide-character string, or contains embedded NULs, then we can't
6956      handle it as the user wants.  */
6957   if (token->type == CPP_WSTRING
6958       || (strlen (TREE_STRING_POINTER (token->value))
6959           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6960     {
6961       cp_parser_error (parser, "invalid linkage-specification");
6962       /* Assume C++ linkage.  */
6963       linkage = get_identifier ("c++");
6964     }
6965   /* If it's a simple string constant, things are easier.  */
6966   else
6967     linkage = get_identifier (TREE_STRING_POINTER (token->value));
6968
6969   /* We're now using the new linkage.  */
6970   push_lang_context (linkage);
6971
6972   /* If the next token is a `{', then we're using the first
6973      production.  */
6974   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6975     {
6976       /* Consume the `{' token.  */
6977       cp_lexer_consume_token (parser->lexer);
6978       /* Parse the declarations.  */
6979       cp_parser_declaration_seq_opt (parser);
6980       /* Look for the closing `}'.  */
6981       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6982     }
6983   /* Otherwise, there's just one declaration.  */
6984   else
6985     {
6986       bool saved_in_unbraced_linkage_specification_p;
6987
6988       saved_in_unbraced_linkage_specification_p 
6989         = parser->in_unbraced_linkage_specification_p;
6990       parser->in_unbraced_linkage_specification_p = true;
6991       have_extern_spec = true;
6992       cp_parser_declaration (parser);
6993       have_extern_spec = false;
6994       parser->in_unbraced_linkage_specification_p 
6995         = saved_in_unbraced_linkage_specification_p;
6996     }
6997
6998   /* We're done with the linkage-specification.  */
6999   pop_lang_context ();
7000 }
7001
7002 /* Special member functions [gram.special] */
7003
7004 /* Parse a conversion-function-id.
7005
7006    conversion-function-id:
7007      operator conversion-type-id  
7008
7009    Returns an IDENTIFIER_NODE representing the operator.  */
7010
7011 static tree 
7012 cp_parser_conversion_function_id (cp_parser* parser)
7013 {
7014   tree type;
7015   tree saved_scope;
7016   tree saved_qualifying_scope;
7017   tree saved_object_scope;
7018   bool pop_p = false;
7019
7020   /* Look for the `operator' token.  */
7021   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7022     return error_mark_node;
7023   /* When we parse the conversion-type-id, the current scope will be
7024      reset.  However, we need that information in able to look up the
7025      conversion function later, so we save it here.  */
7026   saved_scope = parser->scope;
7027   saved_qualifying_scope = parser->qualifying_scope;
7028   saved_object_scope = parser->object_scope;
7029   /* We must enter the scope of the class so that the names of
7030      entities declared within the class are available in the
7031      conversion-type-id.  For example, consider:
7032
7033        struct S { 
7034          typedef int I;
7035          operator I();
7036        };
7037
7038        S::operator I() { ... }
7039
7040      In order to see that `I' is a type-name in the definition, we
7041      must be in the scope of `S'.  */
7042   if (saved_scope)
7043     pop_p = push_scope (saved_scope);
7044   /* Parse the conversion-type-id.  */
7045   type = cp_parser_conversion_type_id (parser);
7046   /* Leave the scope of the class, if any.  */
7047   if (pop_p)
7048     pop_scope (saved_scope);
7049   /* Restore the saved scope.  */
7050   parser->scope = saved_scope;
7051   parser->qualifying_scope = saved_qualifying_scope;
7052   parser->object_scope = saved_object_scope;
7053   /* If the TYPE is invalid, indicate failure.  */
7054   if (type == error_mark_node)
7055     return error_mark_node;
7056   return mangle_conv_op_name_for_type (type);
7057 }
7058
7059 /* Parse a conversion-type-id:
7060
7061    conversion-type-id:
7062      type-specifier-seq conversion-declarator [opt]
7063
7064    Returns the TYPE specified.  */
7065
7066 static tree
7067 cp_parser_conversion_type_id (cp_parser* parser)
7068 {
7069   tree attributes;
7070   tree type_specifiers;
7071   tree declarator;
7072
7073   /* Parse the attributes.  */
7074   attributes = cp_parser_attributes_opt (parser);
7075   /* Parse the type-specifiers.  */
7076   type_specifiers = cp_parser_type_specifier_seq (parser);
7077   /* If that didn't work, stop.  */
7078   if (type_specifiers == error_mark_node)
7079     return error_mark_node;
7080   /* Parse the conversion-declarator.  */
7081   declarator = cp_parser_conversion_declarator_opt (parser);
7082
7083   return grokdeclarator (declarator, type_specifiers, TYPENAME,
7084                          /*initialized=*/0, &attributes);
7085 }
7086
7087 /* Parse an (optional) conversion-declarator.
7088
7089    conversion-declarator:
7090      ptr-operator conversion-declarator [opt]  
7091
7092    Returns a representation of the declarator.  See
7093    cp_parser_declarator for details.  */
7094
7095 static tree
7096 cp_parser_conversion_declarator_opt (cp_parser* parser)
7097 {
7098   enum tree_code code;
7099   tree class_type;
7100   tree cv_qualifier_seq;
7101
7102   /* We don't know if there's a ptr-operator next, or not.  */
7103   cp_parser_parse_tentatively (parser);
7104   /* Try the ptr-operator.  */
7105   code = cp_parser_ptr_operator (parser, &class_type, 
7106                                  &cv_qualifier_seq);
7107   /* If it worked, look for more conversion-declarators.  */
7108   if (cp_parser_parse_definitely (parser))
7109     {
7110      tree declarator;
7111
7112      /* Parse another optional declarator.  */
7113      declarator = cp_parser_conversion_declarator_opt (parser);
7114
7115      /* Create the representation of the declarator.  */
7116      if (code == INDIRECT_REF)
7117        declarator = make_pointer_declarator (cv_qualifier_seq,
7118                                              declarator);
7119      else
7120        declarator =  make_reference_declarator (cv_qualifier_seq,
7121                                                 declarator);
7122
7123      /* Handle the pointer-to-member case.  */
7124      if (class_type)
7125        declarator = build_nt (SCOPE_REF, class_type, declarator);
7126
7127      return declarator;
7128    }
7129
7130   return NULL_TREE;
7131 }
7132
7133 /* Parse an (optional) ctor-initializer.
7134
7135    ctor-initializer:
7136      : mem-initializer-list  
7137
7138    Returns TRUE iff the ctor-initializer was actually present.  */
7139
7140 static bool
7141 cp_parser_ctor_initializer_opt (cp_parser* parser)
7142 {
7143   /* If the next token is not a `:', then there is no
7144      ctor-initializer.  */
7145   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7146     {
7147       /* Do default initialization of any bases and members.  */
7148       if (DECL_CONSTRUCTOR_P (current_function_decl))
7149         finish_mem_initializers (NULL_TREE);
7150
7151       return false;
7152     }
7153
7154   /* Consume the `:' token.  */
7155   cp_lexer_consume_token (parser->lexer);
7156   /* And the mem-initializer-list.  */
7157   cp_parser_mem_initializer_list (parser);
7158
7159   return true;
7160 }
7161
7162 /* Parse a mem-initializer-list.
7163
7164    mem-initializer-list:
7165      mem-initializer
7166      mem-initializer , mem-initializer-list  */
7167
7168 static void
7169 cp_parser_mem_initializer_list (cp_parser* parser)
7170 {
7171   tree mem_initializer_list = NULL_TREE;
7172
7173   /* Let the semantic analysis code know that we are starting the
7174      mem-initializer-list.  */
7175   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7176     error ("only constructors take base initializers");
7177
7178   /* Loop through the list.  */
7179   while (true)
7180     {
7181       tree mem_initializer;
7182
7183       /* Parse the mem-initializer.  */
7184       mem_initializer = cp_parser_mem_initializer (parser);
7185       /* Add it to the list, unless it was erroneous.  */
7186       if (mem_initializer)
7187         {
7188           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7189           mem_initializer_list = mem_initializer;
7190         }
7191       /* If the next token is not a `,', we're done.  */
7192       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7193         break;
7194       /* Consume the `,' token.  */
7195       cp_lexer_consume_token (parser->lexer);
7196     }
7197
7198   /* Perform semantic analysis.  */
7199   if (DECL_CONSTRUCTOR_P (current_function_decl))
7200     finish_mem_initializers (mem_initializer_list);
7201 }
7202
7203 /* Parse a mem-initializer.
7204
7205    mem-initializer:
7206      mem-initializer-id ( expression-list [opt] )  
7207
7208    GNU extension:
7209   
7210    mem-initializer:
7211      ( expression-list [opt] )
7212
7213    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7214    class) or FIELD_DECL (for a non-static data member) to initialize;
7215    the TREE_VALUE is the expression-list.  */
7216
7217 static tree
7218 cp_parser_mem_initializer (cp_parser* parser)
7219 {
7220   tree mem_initializer_id;
7221   tree expression_list;
7222   tree member;
7223   
7224   /* Find out what is being initialized.  */
7225   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7226     {
7227       pedwarn ("anachronistic old-style base class initializer");
7228       mem_initializer_id = NULL_TREE;
7229     }
7230   else
7231     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7232   member = expand_member_init (mem_initializer_id);
7233   if (member && !DECL_P (member))
7234     in_base_initializer = 1;
7235
7236   expression_list 
7237     = cp_parser_parenthesized_expression_list (parser, false,
7238                                                /*non_constant_p=*/NULL);
7239   if (!expression_list)
7240     expression_list = void_type_node;
7241
7242   in_base_initializer = 0;
7243   
7244   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7245 }
7246
7247 /* Parse a mem-initializer-id.
7248
7249    mem-initializer-id:
7250      :: [opt] nested-name-specifier [opt] class-name
7251      identifier  
7252
7253    Returns a TYPE indicating the class to be initializer for the first
7254    production.  Returns an IDENTIFIER_NODE indicating the data member
7255    to be initialized for the second production.  */
7256
7257 static tree
7258 cp_parser_mem_initializer_id (cp_parser* parser)
7259 {
7260   bool global_scope_p;
7261   bool nested_name_specifier_p;
7262   bool template_p = false;
7263   tree id;
7264
7265   /* `typename' is not allowed in this context ([temp.res]).  */
7266   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7267     {
7268       error ("keyword `typename' not allowed in this context (a qualified "
7269              "member initializer is implicitly a type)");
7270       cp_lexer_consume_token (parser->lexer);
7271     }
7272   /* Look for the optional `::' operator.  */
7273   global_scope_p 
7274     = (cp_parser_global_scope_opt (parser, 
7275                                    /*current_scope_valid_p=*/false) 
7276        != NULL_TREE);
7277   /* Look for the optional nested-name-specifier.  The simplest way to
7278      implement:
7279
7280        [temp.res]
7281
7282        The keyword `typename' is not permitted in a base-specifier or
7283        mem-initializer; in these contexts a qualified name that
7284        depends on a template-parameter is implicitly assumed to be a
7285        type name.
7286
7287      is to assume that we have seen the `typename' keyword at this
7288      point.  */
7289   nested_name_specifier_p 
7290     = (cp_parser_nested_name_specifier_opt (parser,
7291                                             /*typename_keyword_p=*/true,
7292                                             /*check_dependency_p=*/true,
7293                                             /*type_p=*/true,
7294                                             /*is_declaration=*/true)
7295        != NULL_TREE);
7296   if (nested_name_specifier_p)
7297     template_p = cp_parser_optional_template_keyword (parser);
7298   /* If there is a `::' operator or a nested-name-specifier, then we
7299      are definitely looking for a class-name.  */
7300   if (global_scope_p || nested_name_specifier_p)
7301     return cp_parser_class_name (parser,
7302                                  /*typename_keyword_p=*/true,
7303                                  /*template_keyword_p=*/template_p,
7304                                  /*type_p=*/false,
7305                                  /*check_dependency_p=*/true,
7306                                  /*class_head_p=*/false,
7307                                  /*is_declaration=*/true);
7308   /* Otherwise, we could also be looking for an ordinary identifier.  */
7309   cp_parser_parse_tentatively (parser);
7310   /* Try a class-name.  */
7311   id = cp_parser_class_name (parser, 
7312                              /*typename_keyword_p=*/true,
7313                              /*template_keyword_p=*/false,
7314                              /*type_p=*/false,
7315                              /*check_dependency_p=*/true,
7316                              /*class_head_p=*/false,
7317                              /*is_declaration=*/true);
7318   /* If we found one, we're done.  */
7319   if (cp_parser_parse_definitely (parser))
7320     return id;
7321   /* Otherwise, look for an ordinary identifier.  */
7322   return cp_parser_identifier (parser);
7323 }
7324
7325 /* Overloading [gram.over] */
7326
7327 /* Parse an operator-function-id.
7328
7329    operator-function-id:
7330      operator operator  
7331
7332    Returns an IDENTIFIER_NODE for the operator which is a
7333    human-readable spelling of the identifier, e.g., `operator +'.  */
7334
7335 static tree 
7336 cp_parser_operator_function_id (cp_parser* parser)
7337 {
7338   /* Look for the `operator' keyword.  */
7339   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7340     return error_mark_node;
7341   /* And then the name of the operator itself.  */
7342   return cp_parser_operator (parser);
7343 }
7344
7345 /* Parse an operator.
7346
7347    operator:
7348      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7349      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7350      || ++ -- , ->* -> () []
7351
7352    GNU Extensions:
7353    
7354    operator:
7355      <? >? <?= >?=
7356
7357    Returns an IDENTIFIER_NODE for the operator which is a
7358    human-readable spelling of the identifier, e.g., `operator +'.  */
7359    
7360 static tree
7361 cp_parser_operator (cp_parser* parser)
7362 {
7363   tree id = NULL_TREE;
7364   cp_token *token;
7365
7366   /* Peek at the next token.  */
7367   token = cp_lexer_peek_token (parser->lexer);
7368   /* Figure out which operator we have.  */
7369   switch (token->type)
7370     {
7371     case CPP_KEYWORD:
7372       {
7373         enum tree_code op;
7374
7375         /* The keyword should be either `new' or `delete'.  */
7376         if (token->keyword == RID_NEW)
7377           op = NEW_EXPR;
7378         else if (token->keyword == RID_DELETE)
7379           op = DELETE_EXPR;
7380         else
7381           break;
7382
7383         /* Consume the `new' or `delete' token.  */
7384         cp_lexer_consume_token (parser->lexer);
7385
7386         /* Peek at the next token.  */
7387         token = cp_lexer_peek_token (parser->lexer);
7388         /* If it's a `[' token then this is the array variant of the
7389            operator.  */
7390         if (token->type == CPP_OPEN_SQUARE)
7391           {
7392             /* Consume the `[' token.  */
7393             cp_lexer_consume_token (parser->lexer);
7394             /* Look for the `]' token.  */
7395             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7396             id = ansi_opname (op == NEW_EXPR 
7397                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7398           }
7399         /* Otherwise, we have the non-array variant.  */
7400         else
7401           id = ansi_opname (op);
7402
7403         return id;
7404       }
7405
7406     case CPP_PLUS:
7407       id = ansi_opname (PLUS_EXPR);
7408       break;
7409
7410     case CPP_MINUS:
7411       id = ansi_opname (MINUS_EXPR);
7412       break;
7413
7414     case CPP_MULT:
7415       id = ansi_opname (MULT_EXPR);
7416       break;
7417
7418     case CPP_DIV:
7419       id = ansi_opname (TRUNC_DIV_EXPR);
7420       break;
7421
7422     case CPP_MOD:
7423       id = ansi_opname (TRUNC_MOD_EXPR);
7424       break;
7425
7426     case CPP_XOR:
7427       id = ansi_opname (BIT_XOR_EXPR);
7428       break;
7429
7430     case CPP_AND:
7431       id = ansi_opname (BIT_AND_EXPR);
7432       break;
7433
7434     case CPP_OR:
7435       id = ansi_opname (BIT_IOR_EXPR);
7436       break;
7437
7438     case CPP_COMPL:
7439       id = ansi_opname (BIT_NOT_EXPR);
7440       break;
7441       
7442     case CPP_NOT:
7443       id = ansi_opname (TRUTH_NOT_EXPR);
7444       break;
7445
7446     case CPP_EQ:
7447       id = ansi_assopname (NOP_EXPR);
7448       break;
7449
7450     case CPP_LESS:
7451       id = ansi_opname (LT_EXPR);
7452       break;
7453
7454     case CPP_GREATER:
7455       id = ansi_opname (GT_EXPR);
7456       break;
7457
7458     case CPP_PLUS_EQ:
7459       id = ansi_assopname (PLUS_EXPR);
7460       break;
7461
7462     case CPP_MINUS_EQ:
7463       id = ansi_assopname (MINUS_EXPR);
7464       break;
7465
7466     case CPP_MULT_EQ:
7467       id = ansi_assopname (MULT_EXPR);
7468       break;
7469
7470     case CPP_DIV_EQ:
7471       id = ansi_assopname (TRUNC_DIV_EXPR);
7472       break;
7473
7474     case CPP_MOD_EQ:
7475       id = ansi_assopname (TRUNC_MOD_EXPR);
7476       break;
7477
7478     case CPP_XOR_EQ:
7479       id = ansi_assopname (BIT_XOR_EXPR);
7480       break;
7481
7482     case CPP_AND_EQ:
7483       id = ansi_assopname (BIT_AND_EXPR);
7484       break;
7485
7486     case CPP_OR_EQ:
7487       id = ansi_assopname (BIT_IOR_EXPR);
7488       break;
7489
7490     case CPP_LSHIFT:
7491       id = ansi_opname (LSHIFT_EXPR);
7492       break;
7493
7494     case CPP_RSHIFT:
7495       id = ansi_opname (RSHIFT_EXPR);
7496       break;
7497
7498     case CPP_LSHIFT_EQ:
7499       id = ansi_assopname (LSHIFT_EXPR);
7500       break;
7501
7502     case CPP_RSHIFT_EQ:
7503       id = ansi_assopname (RSHIFT_EXPR);
7504       break;
7505
7506     case CPP_EQ_EQ:
7507       id = ansi_opname (EQ_EXPR);
7508       break;
7509
7510     case CPP_NOT_EQ:
7511       id = ansi_opname (NE_EXPR);
7512       break;
7513
7514     case CPP_LESS_EQ:
7515       id = ansi_opname (LE_EXPR);
7516       break;
7517
7518     case CPP_GREATER_EQ:
7519       id = ansi_opname (GE_EXPR);
7520       break;
7521
7522     case CPP_AND_AND:
7523       id = ansi_opname (TRUTH_ANDIF_EXPR);
7524       break;
7525
7526     case CPP_OR_OR:
7527       id = ansi_opname (TRUTH_ORIF_EXPR);
7528       break;
7529       
7530     case CPP_PLUS_PLUS:
7531       id = ansi_opname (POSTINCREMENT_EXPR);
7532       break;
7533
7534     case CPP_MINUS_MINUS:
7535       id = ansi_opname (PREDECREMENT_EXPR);
7536       break;
7537
7538     case CPP_COMMA:
7539       id = ansi_opname (COMPOUND_EXPR);
7540       break;
7541
7542     case CPP_DEREF_STAR:
7543       id = ansi_opname (MEMBER_REF);
7544       break;
7545
7546     case CPP_DEREF:
7547       id = ansi_opname (COMPONENT_REF);
7548       break;
7549
7550     case CPP_OPEN_PAREN:
7551       /* Consume the `('.  */
7552       cp_lexer_consume_token (parser->lexer);
7553       /* Look for the matching `)'.  */
7554       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7555       return ansi_opname (CALL_EXPR);
7556
7557     case CPP_OPEN_SQUARE:
7558       /* Consume the `['.  */
7559       cp_lexer_consume_token (parser->lexer);
7560       /* Look for the matching `]'.  */
7561       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7562       return ansi_opname (ARRAY_REF);
7563
7564       /* Extensions.  */
7565     case CPP_MIN:
7566       id = ansi_opname (MIN_EXPR);
7567       break;
7568
7569     case CPP_MAX:
7570       id = ansi_opname (MAX_EXPR);
7571       break;
7572
7573     case CPP_MIN_EQ:
7574       id = ansi_assopname (MIN_EXPR);
7575       break;
7576
7577     case CPP_MAX_EQ:
7578       id = ansi_assopname (MAX_EXPR);
7579       break;
7580
7581     default:
7582       /* Anything else is an error.  */
7583       break;
7584     }
7585
7586   /* If we have selected an identifier, we need to consume the
7587      operator token.  */
7588   if (id)
7589     cp_lexer_consume_token (parser->lexer);
7590   /* Otherwise, no valid operator name was present.  */
7591   else
7592     {
7593       cp_parser_error (parser, "expected operator");
7594       id = error_mark_node;
7595     }
7596
7597   return id;
7598 }
7599
7600 /* Parse a template-declaration.
7601
7602    template-declaration:
7603      export [opt] template < template-parameter-list > declaration  
7604
7605    If MEMBER_P is TRUE, this template-declaration occurs within a
7606    class-specifier.  
7607
7608    The grammar rule given by the standard isn't correct.  What
7609    is really meant is:
7610
7611    template-declaration:
7612      export [opt] template-parameter-list-seq 
7613        decl-specifier-seq [opt] init-declarator [opt] ;
7614      export [opt] template-parameter-list-seq 
7615        function-definition
7616
7617    template-parameter-list-seq:
7618      template-parameter-list-seq [opt]
7619      template < template-parameter-list >  */
7620
7621 static void
7622 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7623 {
7624   /* Check for `export'.  */
7625   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7626     {
7627       /* Consume the `export' token.  */
7628       cp_lexer_consume_token (parser->lexer);
7629       /* Warn that we do not support `export'.  */
7630       warning ("keyword `export' not implemented, and will be ignored");
7631     }
7632
7633   cp_parser_template_declaration_after_export (parser, member_p);
7634 }
7635
7636 /* Parse a template-parameter-list.
7637
7638    template-parameter-list:
7639      template-parameter
7640      template-parameter-list , template-parameter
7641
7642    Returns a TREE_LIST.  Each node represents a template parameter.
7643    The nodes are connected via their TREE_CHAINs.  */
7644
7645 static tree
7646 cp_parser_template_parameter_list (cp_parser* parser)
7647 {
7648   tree parameter_list = NULL_TREE;
7649
7650   while (true)
7651     {
7652       tree parameter;
7653       cp_token *token;
7654
7655       /* Parse the template-parameter.  */
7656       parameter = cp_parser_template_parameter (parser);
7657       /* Add it to the list.  */
7658       parameter_list = process_template_parm (parameter_list,
7659                                               parameter);
7660
7661       /* Peek at the next token.  */
7662       token = cp_lexer_peek_token (parser->lexer);
7663       /* If it's not a `,', we're done.  */
7664       if (token->type != CPP_COMMA)
7665         break;
7666       /* Otherwise, consume the `,' token.  */
7667       cp_lexer_consume_token (parser->lexer);
7668     }
7669
7670   return parameter_list;
7671 }
7672
7673 /* Parse a template-parameter.
7674
7675    template-parameter:
7676      type-parameter
7677      parameter-declaration
7678
7679    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
7680    TREE_PURPOSE is the default value, if any.  */
7681
7682 static tree
7683 cp_parser_template_parameter (cp_parser* parser)
7684 {
7685   cp_token *token;
7686
7687   /* Peek at the next token.  */
7688   token = cp_lexer_peek_token (parser->lexer);
7689   /* If it is `class' or `template', we have a type-parameter.  */
7690   if (token->keyword == RID_TEMPLATE)
7691     return cp_parser_type_parameter (parser);
7692   /* If it is `class' or `typename' we do not know yet whether it is a
7693      type parameter or a non-type parameter.  Consider:
7694
7695        template <typename T, typename T::X X> ...
7696
7697      or:
7698      
7699        template <class C, class D*> ...
7700
7701      Here, the first parameter is a type parameter, and the second is
7702      a non-type parameter.  We can tell by looking at the token after
7703      the identifier -- if it is a `,', `=', or `>' then we have a type
7704      parameter.  */
7705   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7706     {
7707       /* Peek at the token after `class' or `typename'.  */
7708       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7709       /* If it's an identifier, skip it.  */
7710       if (token->type == CPP_NAME)
7711         token = cp_lexer_peek_nth_token (parser->lexer, 3);
7712       /* Now, see if the token looks like the end of a template
7713          parameter.  */
7714       if (token->type == CPP_COMMA 
7715           || token->type == CPP_EQ
7716           || token->type == CPP_GREATER)
7717         return cp_parser_type_parameter (parser);
7718     }
7719
7720   /* Otherwise, it is a non-type parameter.  
7721
7722      [temp.param]
7723
7724      When parsing a default template-argument for a non-type
7725      template-parameter, the first non-nested `>' is taken as the end
7726      of the template parameter-list rather than a greater-than
7727      operator.  */
7728   return 
7729     cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7730                                      /*parenthesized_p=*/NULL);
7731 }
7732
7733 /* Parse a type-parameter.
7734
7735    type-parameter:
7736      class identifier [opt]
7737      class identifier [opt] = type-id
7738      typename identifier [opt]
7739      typename identifier [opt] = type-id
7740      template < template-parameter-list > class identifier [opt]
7741      template < template-parameter-list > class identifier [opt] 
7742        = id-expression  
7743
7744    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
7745    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
7746    the declaration of the parameter.  */
7747
7748 static tree
7749 cp_parser_type_parameter (cp_parser* parser)
7750 {
7751   cp_token *token;
7752   tree parameter;
7753
7754   /* Look for a keyword to tell us what kind of parameter this is.  */
7755   token = cp_parser_require (parser, CPP_KEYWORD, 
7756                              "`class', `typename', or `template'");
7757   if (!token)
7758     return error_mark_node;
7759
7760   switch (token->keyword)
7761     {
7762     case RID_CLASS:
7763     case RID_TYPENAME:
7764       {
7765         tree identifier;
7766         tree default_argument;
7767
7768         /* If the next token is an identifier, then it names the
7769            parameter.  */
7770         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7771           identifier = cp_parser_identifier (parser);
7772         else
7773           identifier = NULL_TREE;
7774
7775         /* Create the parameter.  */
7776         parameter = finish_template_type_parm (class_type_node, identifier);
7777
7778         /* If the next token is an `=', we have a default argument.  */
7779         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7780           {
7781             /* Consume the `=' token.  */
7782             cp_lexer_consume_token (parser->lexer);
7783             /* Parse the default-argument.  */
7784             default_argument = cp_parser_type_id (parser);
7785           }
7786         else
7787           default_argument = NULL_TREE;
7788
7789         /* Create the combined representation of the parameter and the
7790            default argument.  */
7791         parameter = build_tree_list (default_argument, parameter);
7792       }
7793       break;
7794
7795     case RID_TEMPLATE:
7796       {
7797         tree parameter_list;
7798         tree identifier;
7799         tree default_argument;
7800
7801         /* Look for the `<'.  */
7802         cp_parser_require (parser, CPP_LESS, "`<'");
7803         /* Parse the template-parameter-list.  */
7804         begin_template_parm_list ();
7805         parameter_list 
7806           = cp_parser_template_parameter_list (parser);
7807         parameter_list = end_template_parm_list (parameter_list);
7808         /* Look for the `>'.  */
7809         cp_parser_require (parser, CPP_GREATER, "`>'");
7810         /* Look for the `class' keyword.  */
7811         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7812         /* If the next token is an `=', then there is a
7813            default-argument.  If the next token is a `>', we are at
7814            the end of the parameter-list.  If the next token is a `,',
7815            then we are at the end of this parameter.  */
7816         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7817             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7818             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7819           {
7820             identifier = cp_parser_identifier (parser);
7821             /* Treat invalid names as if the parameter were nameless. */
7822             if (identifier == error_mark_node)
7823               identifier = NULL_TREE;
7824           }
7825         else
7826           identifier = NULL_TREE;
7827
7828         /* Create the template parameter.  */
7829         parameter = finish_template_template_parm (class_type_node,
7830                                                    identifier);
7831                                                    
7832         /* If the next token is an `=', then there is a
7833            default-argument.  */
7834         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7835           {
7836             bool is_template;
7837
7838             /* Consume the `='.  */
7839             cp_lexer_consume_token (parser->lexer);
7840             /* Parse the id-expression.  */
7841             default_argument 
7842               = cp_parser_id_expression (parser,
7843                                          /*template_keyword_p=*/false,
7844                                          /*check_dependency_p=*/true,
7845                                          /*template_p=*/&is_template,
7846                                          /*declarator_p=*/false);
7847             if (TREE_CODE (default_argument) == TYPE_DECL)
7848               /* If the id-expression was a template-id that refers to
7849                  a template-class, we already have the declaration here,
7850                  so no further lookup is needed.  */
7851                  ;
7852             else
7853               /* Look up the name.  */
7854               default_argument 
7855                 = cp_parser_lookup_name (parser, default_argument,
7856                                         /*is_type=*/false,
7857                                         /*is_template=*/is_template,
7858                                         /*is_namespace=*/false,
7859                                         /*check_dependency=*/true);
7860             /* See if the default argument is valid.  */
7861             default_argument
7862               = check_template_template_default_arg (default_argument);
7863           }
7864         else
7865           default_argument = NULL_TREE;
7866
7867         /* Create the combined representation of the parameter and the
7868            default argument.  */
7869         parameter = build_tree_list (default_argument, parameter);
7870       }
7871       break;
7872
7873     default:
7874       abort ();
7875       break;
7876     }
7877   
7878   return parameter;
7879 }
7880
7881 /* Parse a template-id.
7882
7883    template-id:
7884      template-name < template-argument-list [opt] >
7885
7886    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7887    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
7888    returned.  Otherwise, if the template-name names a function, or set
7889    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
7890    names a class, returns a TYPE_DECL for the specialization.  
7891
7892    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7893    uninstantiated templates.  */
7894
7895 static tree
7896 cp_parser_template_id (cp_parser *parser, 
7897                        bool template_keyword_p, 
7898                        bool check_dependency_p,
7899                        bool is_declaration)
7900 {
7901   tree template;
7902   tree arguments;
7903   tree template_id;
7904   ptrdiff_t start_of_id;
7905   tree access_check = NULL_TREE;
7906   cp_token *next_token, *next_token_2;
7907   bool is_identifier;
7908
7909   /* If the next token corresponds to a template-id, there is no need
7910      to reparse it.  */
7911   next_token = cp_lexer_peek_token (parser->lexer);
7912   if (next_token->type == CPP_TEMPLATE_ID)
7913     {
7914       tree value;
7915       tree check;
7916
7917       /* Get the stored value.  */
7918       value = cp_lexer_consume_token (parser->lexer)->value;
7919       /* Perform any access checks that were deferred.  */
7920       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7921         perform_or_defer_access_check (TREE_PURPOSE (check),
7922                                        TREE_VALUE (check));
7923       /* Return the stored value.  */
7924       return TREE_VALUE (value);
7925     }
7926
7927   /* Avoid performing name lookup if there is no possibility of
7928      finding a template-id.  */
7929   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7930       || (next_token->type == CPP_NAME
7931           && !cp_parser_nth_token_starts_template_argument_list_p 
7932                (parser, 2)))
7933     {
7934       cp_parser_error (parser, "expected template-id");
7935       return error_mark_node;
7936     }
7937
7938   /* Remember where the template-id starts.  */
7939   if (cp_parser_parsing_tentatively (parser)
7940       && !cp_parser_committed_to_tentative_parse (parser))
7941     {
7942       next_token = cp_lexer_peek_token (parser->lexer);
7943       start_of_id = cp_lexer_token_difference (parser->lexer,
7944                                                parser->lexer->first_token,
7945                                                next_token);
7946     }
7947   else
7948     start_of_id = -1;
7949
7950   push_deferring_access_checks (dk_deferred);
7951
7952   /* Parse the template-name.  */
7953   is_identifier = false;
7954   template = cp_parser_template_name (parser, template_keyword_p,
7955                                       check_dependency_p,
7956                                       is_declaration,
7957                                       &is_identifier);
7958   if (template == error_mark_node || is_identifier)
7959     {
7960       pop_deferring_access_checks ();
7961       return template;
7962     }
7963
7964   /* If we find the sequence `[:' after a template-name, it's probably 
7965      a digraph-typo for `< ::'. Substitute the tokens and check if we can
7966      parse correctly the argument list.  */
7967   next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7968   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7969   if (next_token->type == CPP_OPEN_SQUARE 
7970       && next_token->flags & DIGRAPH
7971       && next_token_2->type == CPP_COLON 
7972       && !(next_token_2->flags & PREV_WHITE))
7973     {
7974       cp_parser_parse_tentatively (parser);
7975       /* Change `:' into `::'.  */
7976       next_token_2->type = CPP_SCOPE;
7977       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7978          CPP_LESS.  */
7979       cp_lexer_consume_token (parser->lexer);
7980       /* Parse the arguments.  */
7981       arguments = cp_parser_enclosed_template_argument_list (parser);
7982       if (!cp_parser_parse_definitely (parser))
7983         {
7984           /* If we couldn't parse an argument list, then we revert our changes
7985              and return simply an error. Maybe this is not a template-id
7986              after all.  */
7987           next_token_2->type = CPP_COLON;
7988           cp_parser_error (parser, "expected `<'");
7989           pop_deferring_access_checks ();
7990           return error_mark_node;
7991         }
7992       /* Otherwise, emit an error about the invalid digraph, but continue
7993          parsing because we got our argument list.  */
7994       pedwarn ("`<::' cannot begin a template-argument list");
7995       inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7996               "between `<' and `::'");
7997       if (!flag_permissive)
7998         {
7999           static bool hint;
8000           if (!hint)
8001             {
8002               inform ("(if you use `-fpermissive' G++ will accept your code)");
8003               hint = true;
8004             }
8005         }
8006     }
8007   else
8008     {
8009       /* Look for the `<' that starts the template-argument-list.  */
8010       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8011         {
8012           pop_deferring_access_checks ();
8013           return error_mark_node;
8014         }
8015       /* Parse the arguments.  */
8016       arguments = cp_parser_enclosed_template_argument_list (parser);
8017     }
8018
8019   /* Build a representation of the specialization.  */
8020   if (TREE_CODE (template) == IDENTIFIER_NODE)
8021     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8022   else if (DECL_CLASS_TEMPLATE_P (template)
8023            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8024     template_id 
8025       = finish_template_type (template, arguments, 
8026                               cp_lexer_next_token_is (parser->lexer, 
8027                                                       CPP_SCOPE));
8028   else
8029     {
8030       /* If it's not a class-template or a template-template, it should be
8031          a function-template.  */
8032       my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8033                            || TREE_CODE (template) == OVERLOAD
8034                            || BASELINK_P (template)),
8035                           20010716);
8036       
8037       template_id = lookup_template_function (template, arguments);
8038     }
8039   
8040   /* Retrieve any deferred checks.  Do not pop this access checks yet
8041      so the memory will not be reclaimed during token replacing below.  */
8042   access_check = get_deferred_access_checks ();
8043
8044   /* If parsing tentatively, replace the sequence of tokens that makes
8045      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8046      should we re-parse the token stream, we will not have to repeat
8047      the effort required to do the parse, nor will we issue duplicate
8048      error messages about problems during instantiation of the
8049      template.  */
8050   if (start_of_id >= 0)
8051     {
8052       cp_token *token;
8053
8054       /* Find the token that corresponds to the start of the
8055          template-id.  */
8056       token = cp_lexer_advance_token (parser->lexer, 
8057                                       parser->lexer->first_token,
8058                                       start_of_id);
8059
8060       /* Reset the contents of the START_OF_ID token.  */
8061       token->type = CPP_TEMPLATE_ID;
8062       token->value = build_tree_list (access_check, template_id);
8063       token->keyword = RID_MAX;
8064       /* Purge all subsequent tokens.  */
8065       cp_lexer_purge_tokens_after (parser->lexer, token);
8066
8067       /* ??? Can we actually assume that, if template_id ==
8068          error_mark_node, we will have issued a diagnostic to the
8069          user, as opposed to simply marking the tentative parse as
8070          failed?  */
8071       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8072         error ("parse error in template argument list");
8073     }
8074
8075   pop_deferring_access_checks ();
8076   return template_id;
8077 }
8078
8079 /* Parse a template-name.
8080
8081    template-name:
8082      identifier
8083  
8084    The standard should actually say:
8085
8086    template-name:
8087      identifier
8088      operator-function-id
8089
8090    A defect report has been filed about this issue.
8091
8092    A conversion-function-id cannot be a template name because they cannot
8093    be part of a template-id. In fact, looking at this code:
8094
8095    a.operator K<int>()
8096
8097    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8098    It is impossible to call a templated conversion-function-id with an 
8099    explicit argument list, since the only allowed template parameter is
8100    the type to which it is converting.
8101
8102    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8103    `template' keyword, in a construction like:
8104
8105      T::template f<3>()
8106
8107    In that case `f' is taken to be a template-name, even though there
8108    is no way of knowing for sure.
8109
8110    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8111    name refers to a set of overloaded functions, at least one of which
8112    is a template, or an IDENTIFIER_NODE with the name of the template,
8113    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8114    names are looked up inside uninstantiated templates.  */
8115
8116 static tree
8117 cp_parser_template_name (cp_parser* parser, 
8118                          bool template_keyword_p, 
8119                          bool check_dependency_p,
8120                          bool is_declaration,
8121                          bool *is_identifier)
8122 {
8123   tree identifier;
8124   tree decl;
8125   tree fns;
8126
8127   /* If the next token is `operator', then we have either an
8128      operator-function-id or a conversion-function-id.  */
8129   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8130     {
8131       /* We don't know whether we're looking at an
8132          operator-function-id or a conversion-function-id.  */
8133       cp_parser_parse_tentatively (parser);
8134       /* Try an operator-function-id.  */
8135       identifier = cp_parser_operator_function_id (parser);
8136       /* If that didn't work, try a conversion-function-id.  */
8137       if (!cp_parser_parse_definitely (parser))
8138         {
8139           cp_parser_error (parser, "expected template-name");
8140           return error_mark_node;
8141         }
8142     }
8143   /* Look for the identifier.  */
8144   else
8145     identifier = cp_parser_identifier (parser);
8146   
8147   /* If we didn't find an identifier, we don't have a template-id.  */
8148   if (identifier == error_mark_node)
8149     return error_mark_node;
8150
8151   /* If the name immediately followed the `template' keyword, then it
8152      is a template-name.  However, if the next token is not `<', then
8153      we do not treat it as a template-name, since it is not being used
8154      as part of a template-id.  This enables us to handle constructs
8155      like:
8156
8157        template <typename T> struct S { S(); };
8158        template <typename T> S<T>::S();
8159
8160      correctly.  We would treat `S' as a template -- if it were `S<T>'
8161      -- but we do not if there is no `<'.  */
8162
8163   if (processing_template_decl
8164       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8165     {
8166       /* In a declaration, in a dependent context, we pretend that the
8167          "template" keyword was present in order to improve error
8168          recovery.  For example, given:
8169          
8170            template <typename T> void f(T::X<int>);
8171          
8172          we want to treat "X<int>" as a template-id.  */
8173       if (is_declaration 
8174           && !template_keyword_p 
8175           && parser->scope && TYPE_P (parser->scope)
8176           && check_dependency_p
8177           && dependent_type_p (parser->scope)
8178           /* Do not do this for dtors (or ctors), since they never
8179              need the template keyword before their name.  */
8180           && !constructor_name_p (identifier, parser->scope))
8181         {
8182           ptrdiff_t start;
8183           cp_token* token;
8184           /* Explain what went wrong.  */
8185           error ("non-template `%D' used as template", identifier);
8186           inform ("use `%T::template %D' to indicate that it is a template",
8187                   parser->scope, identifier);
8188           /* If parsing tentatively, find the location of the "<"
8189              token.  */
8190           if (cp_parser_parsing_tentatively (parser)
8191               && !cp_parser_committed_to_tentative_parse (parser))
8192             {
8193               cp_parser_simulate_error (parser);
8194               token = cp_lexer_peek_token (parser->lexer);
8195               token = cp_lexer_prev_token (parser->lexer, token);
8196               start = cp_lexer_token_difference (parser->lexer,
8197                                                  parser->lexer->first_token,
8198                                                  token);
8199             }
8200           else
8201             start = -1;
8202           /* Parse the template arguments so that we can issue error
8203              messages about them.  */
8204           cp_lexer_consume_token (parser->lexer);
8205           cp_parser_enclosed_template_argument_list (parser);
8206           /* Skip tokens until we find a good place from which to
8207              continue parsing.  */
8208           cp_parser_skip_to_closing_parenthesis (parser,
8209                                                  /*recovering=*/true,
8210                                                  /*or_comma=*/true,
8211                                                  /*consume_paren=*/false);
8212           /* If parsing tentatively, permanently remove the
8213              template argument list.  That will prevent duplicate
8214              error messages from being issued about the missing
8215              "template" keyword.  */
8216           if (start >= 0)
8217             {
8218               token = cp_lexer_advance_token (parser->lexer,
8219                                               parser->lexer->first_token,
8220                                               start);
8221               cp_lexer_purge_tokens_after (parser->lexer, token);
8222             }
8223           if (is_identifier)
8224             *is_identifier = true;
8225           return identifier;
8226         }
8227
8228       /* If the "template" keyword is present, then there is generally
8229          no point in doing name-lookup, so we just return IDENTIFIER.
8230          But, if the qualifying scope is non-dependent then we can
8231          (and must) do name-lookup normally.  */
8232       if (template_keyword_p
8233           && (!parser->scope
8234               || (TYPE_P (parser->scope) 
8235                   && dependent_type_p (parser->scope))))
8236         return identifier;
8237     }
8238
8239   /* Look up the name.  */
8240   decl = cp_parser_lookup_name (parser, identifier,
8241                                 /*is_type=*/false,
8242                                 /*is_template=*/false,
8243                                 /*is_namespace=*/false,
8244                                 check_dependency_p);
8245   decl = maybe_get_template_decl_from_type_decl (decl);
8246
8247   /* If DECL is a template, then the name was a template-name.  */
8248   if (TREE_CODE (decl) == TEMPLATE_DECL)
8249     ;
8250   else 
8251     {
8252       tree fn = NULL_TREE;
8253
8254       /* The standard does not explicitly indicate whether a name that
8255          names a set of overloaded declarations, some of which are
8256          templates, is a template-name.  However, such a name should
8257          be a template-name; otherwise, there is no way to form a
8258          template-id for the overloaded templates.  */
8259       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8260       if (TREE_CODE (fns) == OVERLOAD)
8261         for (fn = fns; fn; fn = OVL_NEXT (fn))
8262           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8263             break;
8264
8265       if (!fn)
8266         {
8267           /* Otherwise, the name does not name a template.  */
8268           cp_parser_error (parser, "expected template-name");
8269           return error_mark_node;
8270         }
8271     }
8272
8273   /* If DECL is dependent, and refers to a function, then just return
8274      its name; we will look it up again during template instantiation.  */
8275   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8276     {
8277       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8278       if (TYPE_P (scope) && dependent_type_p (scope))
8279         return identifier;
8280     }
8281
8282   return decl;
8283 }
8284
8285 /* Parse a template-argument-list.
8286
8287    template-argument-list:
8288      template-argument
8289      template-argument-list , template-argument
8290
8291    Returns a TREE_VEC containing the arguments.  */
8292
8293 static tree
8294 cp_parser_template_argument_list (cp_parser* parser)
8295 {
8296   tree fixed_args[10];
8297   unsigned n_args = 0;
8298   unsigned alloced = 10;
8299   tree *arg_ary = fixed_args;
8300   tree vec;
8301   bool saved_in_template_argument_list_p;
8302
8303   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8304   parser->in_template_argument_list_p = true;
8305   do
8306     {
8307       tree argument;
8308
8309       if (n_args)
8310         /* Consume the comma.  */
8311         cp_lexer_consume_token (parser->lexer);
8312       
8313       /* Parse the template-argument.  */
8314       argument = cp_parser_template_argument (parser);
8315       if (n_args == alloced)
8316         {
8317           alloced *= 2;
8318           
8319           if (arg_ary == fixed_args)
8320             {
8321               arg_ary = xmalloc (sizeof (tree) * alloced);
8322               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8323             }
8324           else
8325             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8326         }
8327       arg_ary[n_args++] = argument;
8328     }
8329   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8330
8331   vec = make_tree_vec (n_args);
8332
8333   while (n_args--)
8334     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8335   
8336   if (arg_ary != fixed_args)
8337     free (arg_ary);
8338   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8339   return vec;
8340 }
8341
8342 /* Parse a template-argument.
8343
8344    template-argument:
8345      assignment-expression
8346      type-id
8347      id-expression
8348
8349    The representation is that of an assignment-expression, type-id, or
8350    id-expression -- except that the qualified id-expression is
8351    evaluated, so that the value returned is either a DECL or an
8352    OVERLOAD.  
8353
8354    Although the standard says "assignment-expression", it forbids
8355    throw-expressions or assignments in the template argument.
8356    Therefore, we use "conditional-expression" instead.  */
8357
8358 static tree
8359 cp_parser_template_argument (cp_parser* parser)
8360 {
8361   tree argument;
8362   bool template_p;
8363   bool address_p;
8364   bool maybe_type_id = false;
8365   cp_token *token;
8366   cp_id_kind idk;
8367   tree qualifying_class;
8368
8369   /* There's really no way to know what we're looking at, so we just
8370      try each alternative in order.  
8371
8372        [temp.arg]
8373
8374        In a template-argument, an ambiguity between a type-id and an
8375        expression is resolved to a type-id, regardless of the form of
8376        the corresponding template-parameter.  
8377
8378      Therefore, we try a type-id first.  */
8379   cp_parser_parse_tentatively (parser);
8380   argument = cp_parser_type_id (parser);
8381   /* If there was no error parsing the type-id but the next token is a '>>',
8382      we probably found a typo for '> >'. But there are type-id which are 
8383      also valid expressions. For instance:
8384
8385      struct X { int operator >> (int); };
8386      template <int V> struct Foo {};
8387      Foo<X () >> 5> r;
8388
8389      Here 'X()' is a valid type-id of a function type, but the user just
8390      wanted to write the expression "X() >> 5". Thus, we remember that we
8391      found a valid type-id, but we still try to parse the argument as an
8392      expression to see what happens.  */
8393   if (!cp_parser_error_occurred (parser)
8394       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8395     {
8396       maybe_type_id = true;
8397       cp_parser_abort_tentative_parse (parser);
8398     }
8399   else
8400     {
8401       /* If the next token isn't a `,' or a `>', then this argument wasn't
8402       really finished. This means that the argument is not a valid
8403       type-id.  */
8404       if (!cp_parser_next_token_ends_template_argument_p (parser))
8405         cp_parser_error (parser, "expected template-argument");
8406       /* If that worked, we're done.  */
8407       if (cp_parser_parse_definitely (parser))
8408         return argument;
8409     }
8410   /* We're still not sure what the argument will be.  */
8411   cp_parser_parse_tentatively (parser);
8412   /* Try a template.  */
8413   argument = cp_parser_id_expression (parser, 
8414                                       /*template_keyword_p=*/false,
8415                                       /*check_dependency_p=*/true,
8416                                       &template_p,
8417                                       /*declarator_p=*/false);
8418   /* If the next token isn't a `,' or a `>', then this argument wasn't
8419      really finished.  */
8420   if (!cp_parser_next_token_ends_template_argument_p (parser))
8421     cp_parser_error (parser, "expected template-argument");
8422   if (!cp_parser_error_occurred (parser))
8423     {
8424       /* Figure out what is being referred to.  If the id-expression
8425          was for a class template specialization, then we will have a
8426          TYPE_DECL at this point.  There is no need to do name lookup
8427          at this point in that case.  */
8428       if (TREE_CODE (argument) != TYPE_DECL)
8429         argument = cp_parser_lookup_name (parser, argument,
8430                                           /*is_type=*/false,
8431                                           /*is_template=*/template_p,
8432                                           /*is_namespace=*/false,
8433                                           /*check_dependency=*/true);
8434       if (TREE_CODE (argument) != TEMPLATE_DECL
8435           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8436         cp_parser_error (parser, "expected template-name");
8437     }
8438   if (cp_parser_parse_definitely (parser))
8439     return argument;
8440   /* It must be a non-type argument.  There permitted cases are given
8441      in [temp.arg.nontype]:
8442
8443      -- an integral constant-expression of integral or enumeration
8444         type; or
8445
8446      -- the name of a non-type template-parameter; or
8447
8448      -- the name of an object or function with external linkage...
8449
8450      -- the address of an object or function with external linkage...
8451
8452      -- a pointer to member...  */
8453   /* Look for a non-type template parameter.  */
8454   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8455     {
8456       cp_parser_parse_tentatively (parser);
8457       argument = cp_parser_primary_expression (parser,
8458                                                &idk,
8459                                                &qualifying_class);
8460       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8461           || !cp_parser_next_token_ends_template_argument_p (parser))
8462         cp_parser_simulate_error (parser);
8463       if (cp_parser_parse_definitely (parser))
8464         return argument;
8465     }
8466   /* If the next token is "&", the argument must be the address of an
8467      object or function with external linkage.  */
8468   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8469   if (address_p)
8470     cp_lexer_consume_token (parser->lexer);
8471   /* See if we might have an id-expression.  */
8472   token = cp_lexer_peek_token (parser->lexer);
8473   if (token->type == CPP_NAME
8474       || token->keyword == RID_OPERATOR
8475       || token->type == CPP_SCOPE
8476       || token->type == CPP_TEMPLATE_ID
8477       || token->type == CPP_NESTED_NAME_SPECIFIER)
8478     {
8479       cp_parser_parse_tentatively (parser);
8480       argument = cp_parser_primary_expression (parser,
8481                                                &idk,
8482                                                &qualifying_class);
8483       if (cp_parser_error_occurred (parser)
8484           || !cp_parser_next_token_ends_template_argument_p (parser))
8485         cp_parser_abort_tentative_parse (parser);
8486       else
8487         {
8488           if (qualifying_class)
8489             argument = finish_qualified_id_expr (qualifying_class,
8490                                                  argument,
8491                                                  /*done=*/true,
8492                                                  address_p);
8493           if (TREE_CODE (argument) == VAR_DECL)
8494             {
8495               /* A variable without external linkage might still be a
8496                  valid constant-expression, so no error is issued here
8497                  if the external-linkage check fails.  */
8498               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8499                 cp_parser_simulate_error (parser);
8500             }
8501           else if (is_overloaded_fn (argument))
8502             /* All overloaded functions are allowed; if the external
8503                linkage test does not pass, an error will be issued
8504                later.  */
8505             ;
8506           else if (address_p
8507                    && (TREE_CODE (argument) == OFFSET_REF 
8508                        || TREE_CODE (argument) == SCOPE_REF))
8509             /* A pointer-to-member.  */
8510             ;
8511           else
8512             cp_parser_simulate_error (parser);
8513
8514           if (cp_parser_parse_definitely (parser))
8515             {
8516               if (address_p)
8517                 argument = build_x_unary_op (ADDR_EXPR, argument);
8518               return argument;
8519             }
8520         }
8521     }
8522   /* If the argument started with "&", there are no other valid
8523      alternatives at this point.  */
8524   if (address_p)
8525     {
8526       cp_parser_error (parser, "invalid non-type template argument");
8527       return error_mark_node;
8528     }
8529   /* If the argument wasn't successfully parsed as a type-id followed
8530      by '>>', the argument can only be a constant expression now.  
8531      Otherwise, we try parsing the constant-expression tentatively,
8532      because the argument could really be a type-id.  */
8533   if (maybe_type_id)
8534     cp_parser_parse_tentatively (parser);
8535   argument = cp_parser_constant_expression (parser, 
8536                                             /*allow_non_constant_p=*/false,
8537                                             /*non_constant_p=*/NULL);
8538   argument = fold_non_dependent_expr (argument);
8539   if (!maybe_type_id)
8540     return argument;
8541   if (!cp_parser_next_token_ends_template_argument_p (parser))
8542     cp_parser_error (parser, "expected template-argument");
8543   if (cp_parser_parse_definitely (parser))
8544     return argument;
8545   /* We did our best to parse the argument as a non type-id, but that
8546      was the only alternative that matched (albeit with a '>' after
8547      it). We can assume it's just a typo from the user, and a 
8548      diagnostic will then be issued.  */
8549   return cp_parser_type_id (parser);
8550 }
8551
8552 /* Parse an explicit-instantiation.
8553
8554    explicit-instantiation:
8555      template declaration  
8556
8557    Although the standard says `declaration', what it really means is:
8558
8559    explicit-instantiation:
8560      template decl-specifier-seq [opt] declarator [opt] ; 
8561
8562    Things like `template int S<int>::i = 5, int S<double>::j;' are not
8563    supposed to be allowed.  A defect report has been filed about this
8564    issue.  
8565
8566    GNU Extension:
8567   
8568    explicit-instantiation:
8569      storage-class-specifier template 
8570        decl-specifier-seq [opt] declarator [opt] ;
8571      function-specifier template 
8572        decl-specifier-seq [opt] declarator [opt] ;  */
8573
8574 static void
8575 cp_parser_explicit_instantiation (cp_parser* parser)
8576 {
8577   int declares_class_or_enum;
8578   tree decl_specifiers;
8579   tree attributes;
8580   tree extension_specifier = NULL_TREE;
8581
8582   /* Look for an (optional) storage-class-specifier or
8583      function-specifier.  */
8584   if (cp_parser_allow_gnu_extensions_p (parser))
8585     {
8586       extension_specifier 
8587         = cp_parser_storage_class_specifier_opt (parser);
8588       if (!extension_specifier)
8589         extension_specifier = cp_parser_function_specifier_opt (parser);
8590     }
8591
8592   /* Look for the `template' keyword.  */
8593   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8594   /* Let the front end know that we are processing an explicit
8595      instantiation.  */
8596   begin_explicit_instantiation ();
8597   /* [temp.explicit] says that we are supposed to ignore access
8598      control while processing explicit instantiation directives.  */
8599   push_deferring_access_checks (dk_no_check);
8600   /* Parse a decl-specifier-seq.  */
8601   decl_specifiers 
8602     = cp_parser_decl_specifier_seq (parser,
8603                                     CP_PARSER_FLAGS_OPTIONAL,
8604                                     &attributes,
8605                                     &declares_class_or_enum);
8606   /* If there was exactly one decl-specifier, and it declared a class,
8607      and there's no declarator, then we have an explicit type
8608      instantiation.  */
8609   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8610     {
8611       tree type;
8612
8613       type = check_tag_decl (decl_specifiers);
8614       /* Turn access control back on for names used during
8615          template instantiation.  */
8616       pop_deferring_access_checks ();
8617       if (type)
8618         do_type_instantiation (type, extension_specifier, /*complain=*/1);
8619     }
8620   else
8621     {
8622       tree declarator;
8623       tree decl;
8624
8625       /* Parse the declarator.  */
8626       declarator 
8627         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8628                                 /*ctor_dtor_or_conv_p=*/NULL,
8629                                 /*parenthesized_p=*/NULL,
8630                                 /*member_p=*/false);
8631       cp_parser_check_for_definition_in_return_type (declarator, 
8632                                                      declares_class_or_enum);
8633       if (declarator != error_mark_node)
8634         {
8635           decl = grokdeclarator (declarator, decl_specifiers, 
8636                                  NORMAL, 0, NULL);
8637           /* Turn access control back on for names used during
8638              template instantiation.  */
8639           pop_deferring_access_checks ();
8640           /* Do the explicit instantiation.  */
8641           do_decl_instantiation (decl, extension_specifier);
8642         }
8643       else
8644         {
8645           pop_deferring_access_checks ();
8646           /* Skip the body of the explicit instantiation.  */
8647           cp_parser_skip_to_end_of_statement (parser);
8648         }
8649     }
8650   /* We're done with the instantiation.  */
8651   end_explicit_instantiation ();
8652
8653   cp_parser_consume_semicolon_at_end_of_statement (parser);
8654 }
8655
8656 /* Parse an explicit-specialization.
8657
8658    explicit-specialization:
8659      template < > declaration  
8660
8661    Although the standard says `declaration', what it really means is:
8662
8663    explicit-specialization:
8664      template <> decl-specifier [opt] init-declarator [opt] ;
8665      template <> function-definition 
8666      template <> explicit-specialization
8667      template <> template-declaration  */
8668
8669 static void
8670 cp_parser_explicit_specialization (cp_parser* parser)
8671 {
8672   /* Look for the `template' keyword.  */
8673   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8674   /* Look for the `<'.  */
8675   cp_parser_require (parser, CPP_LESS, "`<'");
8676   /* Look for the `>'.  */
8677   cp_parser_require (parser, CPP_GREATER, "`>'");
8678   /* We have processed another parameter list.  */
8679   ++parser->num_template_parameter_lists;
8680   /* Let the front end know that we are beginning a specialization.  */
8681   begin_specialization ();
8682
8683   /* If the next keyword is `template', we need to figure out whether
8684      or not we're looking a template-declaration.  */
8685   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8686     {
8687       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8688           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8689         cp_parser_template_declaration_after_export (parser,
8690                                                      /*member_p=*/false);
8691       else
8692         cp_parser_explicit_specialization (parser);
8693     }
8694   else
8695     /* Parse the dependent declaration.  */
8696     cp_parser_single_declaration (parser, 
8697                                   /*member_p=*/false,
8698                                   /*friend_p=*/NULL);
8699
8700   /* We're done with the specialization.  */
8701   end_specialization ();
8702   /* We're done with this parameter list.  */
8703   --parser->num_template_parameter_lists;
8704 }
8705
8706 /* Parse a type-specifier.
8707
8708    type-specifier:
8709      simple-type-specifier
8710      class-specifier
8711      enum-specifier
8712      elaborated-type-specifier
8713      cv-qualifier
8714
8715    GNU Extension:
8716
8717    type-specifier:
8718      __complex__
8719
8720    Returns a representation of the type-specifier.  If the
8721    type-specifier is a keyword (like `int' or `const', or
8722    `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8723    For a class-specifier, enum-specifier, or elaborated-type-specifier
8724    a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8725
8726    If IS_FRIEND is TRUE then this type-specifier is being declared a
8727    `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
8728    appearing in a decl-specifier-seq.
8729
8730    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8731    class-specifier, enum-specifier, or elaborated-type-specifier, then
8732    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
8733    if a type is declared; 2 if it is defined.  Otherwise, it is set to
8734    zero.
8735
8736    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8737    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
8738    is set to FALSE.  */
8739
8740 static tree
8741 cp_parser_type_specifier (cp_parser* parser, 
8742                           cp_parser_flags flags, 
8743                           bool is_friend,
8744                           bool is_declaration,
8745                           int* declares_class_or_enum,
8746                           bool* is_cv_qualifier)
8747 {
8748   tree type_spec = NULL_TREE;
8749   cp_token *token;
8750   enum rid keyword;
8751
8752   /* Assume this type-specifier does not declare a new type.  */
8753   if (declares_class_or_enum)
8754     *declares_class_or_enum = 0;
8755   /* And that it does not specify a cv-qualifier.  */
8756   if (is_cv_qualifier)
8757     *is_cv_qualifier = false;
8758   /* Peek at the next token.  */
8759   token = cp_lexer_peek_token (parser->lexer);
8760
8761   /* If we're looking at a keyword, we can use that to guide the
8762      production we choose.  */
8763   keyword = token->keyword;
8764   switch (keyword)
8765     {
8766     case RID_ENUM:
8767       /* 'enum' [identifier] '{' introduces an enum-specifier;
8768          'enum' <anything else> introduces an elaborated-type-specifier.  */
8769       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
8770           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
8771               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
8772                  == CPP_OPEN_BRACE))
8773         {
8774           if (parser->num_template_parameter_lists)
8775             {
8776               error ("template declaration of `enum'");
8777               cp_parser_skip_to_end_of_block_or_statement (parser);
8778               type_spec = error_mark_node;
8779             }
8780           else
8781             type_spec = cp_parser_enum_specifier (parser);
8782
8783           if (declares_class_or_enum)
8784             *declares_class_or_enum = 2;
8785           return type_spec;
8786         }
8787       else
8788         goto elaborated_type_specifier;
8789
8790       /* Any of these indicate either a class-specifier, or an
8791          elaborated-type-specifier.  */
8792     case RID_CLASS:
8793     case RID_STRUCT:
8794     case RID_UNION:
8795       /* Parse tentatively so that we can back up if we don't find a
8796          class-specifier or enum-specifier.  */
8797       cp_parser_parse_tentatively (parser);
8798       /* Look for the class-specifier.  */
8799       type_spec = cp_parser_class_specifier (parser);
8800       /* If that worked, we're done.  */
8801       if (cp_parser_parse_definitely (parser))
8802         {
8803           if (declares_class_or_enum)
8804             *declares_class_or_enum = 2;
8805           return type_spec;
8806         }
8807
8808       /* Fall through.  */
8809
8810     case RID_TYPENAME:
8811     elaborated_type_specifier:
8812       /* Look for an elaborated-type-specifier.  */
8813       type_spec = cp_parser_elaborated_type_specifier (parser,
8814                                                        is_friend,
8815                                                        is_declaration);
8816       /* We're declaring a class or enum -- unless we're using
8817          `typename'.  */
8818       if (declares_class_or_enum && keyword != RID_TYPENAME)
8819         *declares_class_or_enum = 1;
8820       return type_spec;
8821
8822     case RID_CONST:
8823     case RID_VOLATILE:
8824     case RID_RESTRICT:
8825       type_spec = cp_parser_cv_qualifier_opt (parser);
8826       /* Even though we call a routine that looks for an optional
8827          qualifier, we know that there should be one.  */
8828       my_friendly_assert (type_spec != NULL, 20000328);
8829       /* This type-specifier was a cv-qualified.  */
8830       if (is_cv_qualifier)
8831         *is_cv_qualifier = true;
8832
8833       return type_spec;
8834
8835     case RID_COMPLEX:
8836       /* The `__complex__' keyword is a GNU extension.  */
8837       return cp_lexer_consume_token (parser->lexer)->value;
8838
8839     default:
8840       break;
8841     }
8842
8843   /* If we do not already have a type-specifier, assume we are looking
8844      at a simple-type-specifier.  */
8845   type_spec = cp_parser_simple_type_specifier (parser, flags, 
8846                                                /*identifier_p=*/true);
8847
8848   /* If we didn't find a type-specifier, and a type-specifier was not
8849      optional in this context, issue an error message.  */
8850   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8851     {
8852       cp_parser_error (parser, "expected type specifier");
8853       return error_mark_node;
8854     }
8855
8856   return type_spec;
8857 }
8858
8859 /* Parse a simple-type-specifier.
8860
8861    simple-type-specifier:
8862      :: [opt] nested-name-specifier [opt] type-name
8863      :: [opt] nested-name-specifier template template-id
8864      char
8865      wchar_t
8866      bool
8867      short
8868      int
8869      long
8870      signed
8871      unsigned
8872      float
8873      double
8874      void  
8875
8876    GNU Extension:
8877
8878    simple-type-specifier:
8879      __typeof__ unary-expression
8880      __typeof__ ( type-id )
8881
8882    For the various keywords, the value returned is simply the
8883    TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8884    For the first two productions, and if IDENTIFIER_P is false, the
8885    value returned is the indicated TYPE_DECL.  */
8886
8887 static tree
8888 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8889                                  bool identifier_p)
8890 {
8891   tree type = NULL_TREE;
8892   cp_token *token;
8893
8894   /* Peek at the next token.  */
8895   token = cp_lexer_peek_token (parser->lexer);
8896
8897   /* If we're looking at a keyword, things are easy.  */
8898   switch (token->keyword)
8899     {
8900     case RID_CHAR:
8901       type = char_type_node;
8902       break;
8903     case RID_WCHAR:
8904       type = wchar_type_node;
8905       break;
8906     case RID_BOOL:
8907       type = boolean_type_node;
8908       break;
8909     case RID_SHORT:
8910       type = short_integer_type_node;
8911       break;
8912     case RID_INT:
8913       type = integer_type_node;
8914       break;
8915     case RID_LONG:
8916       type = long_integer_type_node;
8917       break;
8918     case RID_SIGNED:
8919       type = integer_type_node;
8920       break;
8921     case RID_UNSIGNED:
8922       type = unsigned_type_node;
8923       break;
8924     case RID_FLOAT:
8925       type = float_type_node;
8926       break;
8927     case RID_DOUBLE:
8928       type = double_type_node;
8929       break;
8930     case RID_VOID:
8931       type = void_type_node;
8932       break;
8933
8934     case RID_TYPEOF:
8935       {
8936         tree operand;
8937
8938         /* Consume the `typeof' token.  */
8939         cp_lexer_consume_token (parser->lexer);
8940         /* Parse the operand to `typeof'.  */
8941         operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8942         /* If it is not already a TYPE, take its type.  */
8943         if (!TYPE_P (operand))
8944           operand = finish_typeof (operand);
8945
8946         return operand;
8947       }
8948
8949     default:
8950       break;
8951     }
8952
8953   /* If the type-specifier was for a built-in type, we're done.  */
8954   if (type)
8955     {
8956       tree id;
8957
8958       /* Consume the token.  */
8959       id = cp_lexer_consume_token (parser->lexer)->value;
8960
8961       /* There is no valid C++ program where a non-template type is
8962          followed by a "<".  That usually indicates that the user thought
8963          that the type was a template.  */
8964       cp_parser_check_for_invalid_template_id (parser, type);
8965
8966       return identifier_p ? id : TYPE_NAME (type);
8967     }
8968
8969   /* The type-specifier must be a user-defined type.  */
8970   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) 
8971     {
8972       bool qualified_p;
8973       bool global_p;
8974
8975       /* Don't gobble tokens or issue error messages if this is an
8976          optional type-specifier.  */
8977       if (flags & CP_PARSER_FLAGS_OPTIONAL)
8978         cp_parser_parse_tentatively (parser);
8979
8980       /* Look for the optional `::' operator.  */
8981       global_p
8982         = (cp_parser_global_scope_opt (parser,
8983                                        /*current_scope_valid_p=*/false)
8984            != NULL_TREE);
8985       /* Look for the nested-name specifier.  */
8986       qualified_p
8987         = (cp_parser_nested_name_specifier_opt (parser,
8988                                                 /*typename_keyword_p=*/false,
8989                                                 /*check_dependency_p=*/true,
8990                                                 /*type_p=*/false,
8991                                                 /*is_declaration=*/false)
8992            != NULL_TREE);
8993       /* If we have seen a nested-name-specifier, and the next token
8994          is `template', then we are using the template-id production.  */
8995       if (parser->scope 
8996           && cp_parser_optional_template_keyword (parser))
8997         {
8998           /* Look for the template-id.  */
8999           type = cp_parser_template_id (parser, 
9000                                         /*template_keyword_p=*/true,
9001                                         /*check_dependency_p=*/true,
9002                                         /*is_declaration=*/false);
9003           /* If the template-id did not name a type, we are out of
9004              luck.  */
9005           if (TREE_CODE (type) != TYPE_DECL)
9006             {
9007               cp_parser_error (parser, "expected template-id for type");
9008               type = NULL_TREE;
9009             }
9010         }
9011       /* Otherwise, look for a type-name.  */
9012       else
9013         type = cp_parser_type_name (parser);
9014       /* Keep track of all name-lookups performed in class scopes.  */
9015       if (type  
9016           && !global_p
9017           && !qualified_p
9018           && TREE_CODE (type) == TYPE_DECL 
9019           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9020         maybe_note_name_used_in_class (DECL_NAME (type), type);
9021       /* If it didn't work out, we don't have a TYPE.  */
9022       if ((flags & CP_PARSER_FLAGS_OPTIONAL) 
9023           && !cp_parser_parse_definitely (parser))
9024         type = NULL_TREE;
9025     }
9026
9027   /* If we didn't get a type-name, issue an error message.  */
9028   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9029     {
9030       cp_parser_error (parser, "expected type-name");
9031       return error_mark_node;
9032     }
9033
9034   /* There is no valid C++ program where a non-template type is
9035      followed by a "<".  That usually indicates that the user thought
9036      that the type was a template.  */
9037   if (type && type != error_mark_node)
9038     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9039
9040   return type;
9041 }
9042
9043 /* Parse a type-name.
9044
9045    type-name:
9046      class-name
9047      enum-name
9048      typedef-name  
9049
9050    enum-name:
9051      identifier
9052
9053    typedef-name:
9054      identifier 
9055
9056    Returns a TYPE_DECL for the the type.  */
9057
9058 static tree
9059 cp_parser_type_name (cp_parser* parser)
9060 {
9061   tree type_decl;
9062   tree identifier;
9063
9064   /* We can't know yet whether it is a class-name or not.  */
9065   cp_parser_parse_tentatively (parser);
9066   /* Try a class-name.  */
9067   type_decl = cp_parser_class_name (parser, 
9068                                     /*typename_keyword_p=*/false,
9069                                     /*template_keyword_p=*/false,
9070                                     /*type_p=*/false,
9071                                     /*check_dependency_p=*/true,
9072                                     /*class_head_p=*/false,
9073                                     /*is_declaration=*/false);
9074   /* If it's not a class-name, keep looking.  */
9075   if (!cp_parser_parse_definitely (parser))
9076     {
9077       /* It must be a typedef-name or an enum-name.  */
9078       identifier = cp_parser_identifier (parser);
9079       if (identifier == error_mark_node)
9080         return error_mark_node;
9081       
9082       /* Look up the type-name.  */
9083       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9084       /* Issue an error if we did not find a type-name.  */
9085       if (TREE_CODE (type_decl) != TYPE_DECL)
9086         {
9087           if (!cp_parser_simulate_error (parser))
9088             cp_parser_name_lookup_error (parser, identifier, type_decl, 
9089                                          "is not a type");
9090           type_decl = error_mark_node;
9091         }
9092       /* Remember that the name was used in the definition of the
9093          current class so that we can check later to see if the
9094          meaning would have been different after the class was
9095          entirely defined.  */
9096       else if (type_decl != error_mark_node
9097                && !parser->scope)
9098         maybe_note_name_used_in_class (identifier, type_decl);
9099     }
9100   
9101   return type_decl;
9102 }
9103
9104
9105 /* Parse an elaborated-type-specifier.  Note that the grammar given
9106    here incorporates the resolution to DR68.
9107
9108    elaborated-type-specifier:
9109      class-key :: [opt] nested-name-specifier [opt] identifier
9110      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9111      enum :: [opt] nested-name-specifier [opt] identifier
9112      typename :: [opt] nested-name-specifier identifier
9113      typename :: [opt] nested-name-specifier template [opt] 
9114        template-id 
9115
9116    GNU extension:
9117
9118    elaborated-type-specifier:
9119      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9120      class-key attributes :: [opt] nested-name-specifier [opt] 
9121                template [opt] template-id
9122      enum attributes :: [opt] nested-name-specifier [opt] identifier
9123
9124    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9125    declared `friend'.  If IS_DECLARATION is TRUE, then this
9126    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9127    something is being declared.
9128
9129    Returns the TYPE specified.  */
9130
9131 static tree
9132 cp_parser_elaborated_type_specifier (cp_parser* parser, 
9133                                      bool is_friend, 
9134                                      bool is_declaration)
9135 {
9136   enum tag_types tag_type;
9137   tree identifier;
9138   tree type = NULL_TREE;
9139   tree attributes = NULL_TREE;
9140
9141   /* See if we're looking at the `enum' keyword.  */
9142   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9143     {
9144       /* Consume the `enum' token.  */
9145       cp_lexer_consume_token (parser->lexer);
9146       /* Remember that it's an enumeration type.  */
9147       tag_type = enum_type;
9148       /* Parse the attributes.  */
9149       attributes = cp_parser_attributes_opt (parser);
9150     }
9151   /* Or, it might be `typename'.  */
9152   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9153                                            RID_TYPENAME))
9154     {
9155       /* Consume the `typename' token.  */
9156       cp_lexer_consume_token (parser->lexer);
9157       /* Remember that it's a `typename' type.  */
9158       tag_type = typename_type;
9159       /* The `typename' keyword is only allowed in templates.  */
9160       if (!processing_template_decl)
9161         pedwarn ("using `typename' outside of template");
9162     }
9163   /* Otherwise it must be a class-key.  */
9164   else
9165     {
9166       tag_type = cp_parser_class_key (parser);
9167       if (tag_type == none_type)
9168         return error_mark_node;
9169       /* Parse the attributes.  */
9170       attributes = cp_parser_attributes_opt (parser);
9171     }
9172
9173   /* Look for the `::' operator.  */
9174   cp_parser_global_scope_opt (parser, 
9175                               /*current_scope_valid_p=*/false);
9176   /* Look for the nested-name-specifier.  */
9177   if (tag_type == typename_type)
9178     {
9179       if (cp_parser_nested_name_specifier (parser,
9180                                            /*typename_keyword_p=*/true,
9181                                            /*check_dependency_p=*/true,
9182                                            /*type_p=*/true,
9183                                            is_declaration) 
9184           == error_mark_node)
9185         return error_mark_node;
9186     }
9187   else
9188     /* Even though `typename' is not present, the proposed resolution
9189        to Core Issue 180 says that in `class A<T>::B', `B' should be
9190        considered a type-name, even if `A<T>' is dependent.  */
9191     cp_parser_nested_name_specifier_opt (parser,
9192                                          /*typename_keyword_p=*/true,
9193                                          /*check_dependency_p=*/true,
9194                                          /*type_p=*/true,
9195                                          is_declaration);
9196   /* For everything but enumeration types, consider a template-id.  */
9197   if (tag_type != enum_type)
9198     {
9199       bool template_p = false;
9200       tree decl;
9201
9202       /* Allow the `template' keyword.  */
9203       template_p = cp_parser_optional_template_keyword (parser);
9204       /* If we didn't see `template', we don't know if there's a
9205          template-id or not.  */
9206       if (!template_p)
9207         cp_parser_parse_tentatively (parser);
9208       /* Parse the template-id.  */
9209       decl = cp_parser_template_id (parser, template_p,
9210                                     /*check_dependency_p=*/true,
9211                                     is_declaration);
9212       /* If we didn't find a template-id, look for an ordinary
9213          identifier.  */
9214       if (!template_p && !cp_parser_parse_definitely (parser))
9215         ;
9216       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9217          in effect, then we must assume that, upon instantiation, the
9218          template will correspond to a class.  */
9219       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9220                && tag_type == typename_type)
9221         type = make_typename_type (parser->scope, decl,
9222                                    /*complain=*/1);
9223       else 
9224         type = TREE_TYPE (decl);
9225     }
9226
9227   /* For an enumeration type, consider only a plain identifier.  */
9228   if (!type)
9229     {
9230       identifier = cp_parser_identifier (parser);
9231
9232       if (identifier == error_mark_node)
9233         {
9234           parser->scope = NULL_TREE;
9235           return error_mark_node;
9236         }
9237
9238       /* For a `typename', we needn't call xref_tag.  */
9239       if (tag_type == typename_type)
9240         return make_typename_type (parser->scope, identifier, 
9241                                    /*complain=*/1);
9242       /* Look up a qualified name in the usual way.  */
9243       if (parser->scope)
9244         {
9245           tree decl;
9246
9247           /* In an elaborated-type-specifier, names are assumed to name
9248              types, so we set IS_TYPE to TRUE when calling
9249              cp_parser_lookup_name.  */
9250           decl = cp_parser_lookup_name (parser, identifier, 
9251                                         /*is_type=*/true,
9252                                         /*is_template=*/false,
9253                                         /*is_namespace=*/false,
9254                                         /*check_dependency=*/true);
9255
9256           /* If we are parsing friend declaration, DECL may be a
9257              TEMPLATE_DECL tree node here.  However, we need to check
9258              whether this TEMPLATE_DECL results in valid code.  Consider
9259              the following example:
9260
9261                namespace N {
9262                  template <class T> class C {};
9263                }
9264                class X {
9265                  template <class T> friend class N::C; // #1, valid code
9266                };
9267                template <class T> class Y {
9268                  friend class N::C;                    // #2, invalid code
9269                };
9270
9271              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9272              name lookup of `N::C'.  We see that friend declaration must
9273              be template for the code to be valid.  Note that
9274              processing_template_decl does not work here since it is
9275              always 1 for the above two cases.  */
9276
9277           decl = (cp_parser_maybe_treat_template_as_class 
9278                   (decl, /*tag_name_p=*/is_friend
9279                          && parser->num_template_parameter_lists));
9280
9281           if (TREE_CODE (decl) != TYPE_DECL)
9282             {
9283               error ("expected type-name");
9284               return error_mark_node;
9285             }
9286
9287           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9288             check_elaborated_type_specifier 
9289               (tag_type, decl,
9290                (parser->num_template_parameter_lists
9291                 || DECL_SELF_REFERENCE_P (decl)));
9292
9293           type = TREE_TYPE (decl);
9294         }
9295       else 
9296         {
9297           /* An elaborated-type-specifier sometimes introduces a new type and
9298              sometimes names an existing type.  Normally, the rule is that it
9299              introduces a new type only if there is not an existing type of
9300              the same name already in scope.  For example, given:
9301
9302                struct S {};
9303                void f() { struct S s; }
9304
9305              the `struct S' in the body of `f' is the same `struct S' as in
9306              the global scope; the existing definition is used.  However, if
9307              there were no global declaration, this would introduce a new 
9308              local class named `S'.
9309
9310              An exception to this rule applies to the following code:
9311
9312                namespace N { struct S; }
9313
9314              Here, the elaborated-type-specifier names a new type
9315              unconditionally; even if there is already an `S' in the
9316              containing scope this declaration names a new type.
9317              This exception only applies if the elaborated-type-specifier
9318              forms the complete declaration:
9319
9320                [class.name] 
9321
9322                A declaration consisting solely of `class-key identifier ;' is
9323                either a redeclaration of the name in the current scope or a
9324                forward declaration of the identifier as a class name.  It
9325                introduces the name into the current scope.
9326
9327              We are in this situation precisely when the next token is a `;'.
9328
9329              An exception to the exception is that a `friend' declaration does
9330              *not* name a new type; i.e., given:
9331
9332                struct S { friend struct T; };
9333
9334              `T' is not a new type in the scope of `S'.  
9335
9336              Also, `new struct S' or `sizeof (struct S)' never results in the
9337              definition of a new type; a new type can only be declared in a
9338              declaration context.  */
9339
9340           /* Warn about attributes. They are ignored.  */
9341           if (attributes)
9342             warning ("type attributes are honored only at type definition");
9343
9344           type = xref_tag (tag_type, identifier, 
9345                            (is_friend 
9346                             || !is_declaration
9347                             || cp_lexer_next_token_is_not (parser->lexer, 
9348                                                            CPP_SEMICOLON)),
9349                            parser->num_template_parameter_lists);
9350         }
9351     }
9352   if (tag_type != enum_type)
9353     cp_parser_check_class_key (tag_type, type);
9354
9355   /* A "<" cannot follow an elaborated type specifier.  If that
9356      happens, the user was probably trying to form a template-id.  */
9357   cp_parser_check_for_invalid_template_id (parser, type);
9358
9359   return type;
9360 }
9361
9362 /* Parse an enum-specifier.
9363
9364    enum-specifier:
9365      enum identifier [opt] { enumerator-list [opt] }
9366
9367    Returns an ENUM_TYPE representing the enumeration.  */
9368
9369 static tree
9370 cp_parser_enum_specifier (cp_parser* parser)
9371 {
9372   cp_token *token;
9373   tree identifier = NULL_TREE;
9374   tree type;
9375
9376   /* Look for the `enum' keyword.  */
9377   if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9378     return error_mark_node;
9379   /* Peek at the next token.  */
9380   token = cp_lexer_peek_token (parser->lexer);
9381
9382   /* See if it is an identifier.  */
9383   if (token->type == CPP_NAME)
9384     identifier = cp_parser_identifier (parser);
9385
9386   /* Look for the `{'.  */
9387   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9388     return error_mark_node;
9389
9390   /* At this point, we're going ahead with the enum-specifier, even
9391      if some other problem occurs.  */
9392   cp_parser_commit_to_tentative_parse (parser);
9393
9394   /* Issue an error message if type-definitions are forbidden here.  */
9395   cp_parser_check_type_definition (parser);
9396
9397   /* Create the new type.  */
9398   type = start_enum (identifier ? identifier : make_anon_name ());
9399
9400   /* Peek at the next token.  */
9401   token = cp_lexer_peek_token (parser->lexer);
9402   /* If it's not a `}', then there are some enumerators.  */
9403   if (token->type != CPP_CLOSE_BRACE)
9404     cp_parser_enumerator_list (parser, type);
9405   /* Look for the `}'.  */
9406   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9407
9408   /* Finish up the enumeration.  */
9409   finish_enum (type);
9410
9411   return type;
9412 }
9413
9414 /* Parse an enumerator-list.  The enumerators all have the indicated
9415    TYPE.  
9416
9417    enumerator-list:
9418      enumerator-definition
9419      enumerator-list , enumerator-definition  */
9420
9421 static void
9422 cp_parser_enumerator_list (cp_parser* parser, tree type)
9423 {
9424   while (true)
9425     {
9426       cp_token *token;
9427
9428       /* Parse an enumerator-definition.  */
9429       cp_parser_enumerator_definition (parser, type);
9430       /* Peek at the next token.  */
9431       token = cp_lexer_peek_token (parser->lexer);
9432       /* If it's not a `,', then we've reached the end of the 
9433          list.  */
9434       if (token->type != CPP_COMMA)
9435         break;
9436       /* Otherwise, consume the `,' and keep going.  */
9437       cp_lexer_consume_token (parser->lexer);
9438       /* If the next token is a `}', there is a trailing comma.  */
9439       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9440         {
9441           if (pedantic && !in_system_header)
9442             pedwarn ("comma at end of enumerator list");
9443           break;
9444         }
9445     }
9446 }
9447
9448 /* Parse an enumerator-definition.  The enumerator has the indicated
9449    TYPE.
9450
9451    enumerator-definition:
9452      enumerator
9453      enumerator = constant-expression
9454     
9455    enumerator:
9456      identifier  */
9457
9458 static void
9459 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9460 {
9461   cp_token *token;
9462   tree identifier;
9463   tree value;
9464
9465   /* Look for the identifier.  */
9466   identifier = cp_parser_identifier (parser);
9467   if (identifier == error_mark_node)
9468     return;
9469   
9470   /* Peek at the next token.  */
9471   token = cp_lexer_peek_token (parser->lexer);
9472   /* If it's an `=', then there's an explicit value.  */
9473   if (token->type == CPP_EQ)
9474     {
9475       /* Consume the `=' token.  */
9476       cp_lexer_consume_token (parser->lexer);
9477       /* Parse the value.  */
9478       value = cp_parser_constant_expression (parser, 
9479                                              /*allow_non_constant_p=*/false,
9480                                              NULL);
9481     }
9482   else
9483     value = NULL_TREE;
9484
9485   /* Create the enumerator.  */
9486   build_enumerator (identifier, value, type);
9487 }
9488
9489 /* Parse a namespace-name.
9490
9491    namespace-name:
9492      original-namespace-name
9493      namespace-alias
9494
9495    Returns the NAMESPACE_DECL for the namespace.  */
9496
9497 static tree
9498 cp_parser_namespace_name (cp_parser* parser)
9499 {
9500   tree identifier;
9501   tree namespace_decl;
9502
9503   /* Get the name of the namespace.  */
9504   identifier = cp_parser_identifier (parser);
9505   if (identifier == error_mark_node)
9506     return error_mark_node;
9507
9508   /* Look up the identifier in the currently active scope.  Look only
9509      for namespaces, due to:
9510
9511        [basic.lookup.udir]
9512
9513        When looking up a namespace-name in a using-directive or alias
9514        definition, only namespace names are considered.  
9515
9516      And:
9517
9518        [basic.lookup.qual]
9519
9520        During the lookup of a name preceding the :: scope resolution
9521        operator, object, function, and enumerator names are ignored.  
9522
9523      (Note that cp_parser_class_or_namespace_name only calls this
9524      function if the token after the name is the scope resolution
9525      operator.)  */
9526   namespace_decl = cp_parser_lookup_name (parser, identifier,
9527                                           /*is_type=*/false,
9528                                           /*is_template=*/false,
9529                                           /*is_namespace=*/true,
9530                                           /*check_dependency=*/true);
9531   /* If it's not a namespace, issue an error.  */
9532   if (namespace_decl == error_mark_node
9533       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9534     {
9535       cp_parser_error (parser, "expected namespace-name");
9536       namespace_decl = error_mark_node;
9537     }
9538   
9539   return namespace_decl;
9540 }
9541
9542 /* Parse a namespace-definition.
9543
9544    namespace-definition:
9545      named-namespace-definition
9546      unnamed-namespace-definition  
9547
9548    named-namespace-definition:
9549      original-namespace-definition
9550      extension-namespace-definition
9551
9552    original-namespace-definition:
9553      namespace identifier { namespace-body }
9554    
9555    extension-namespace-definition:
9556      namespace original-namespace-name { namespace-body }
9557  
9558    unnamed-namespace-definition:
9559      namespace { namespace-body } */
9560
9561 static void
9562 cp_parser_namespace_definition (cp_parser* parser)
9563 {
9564   tree identifier;
9565
9566   /* Look for the `namespace' keyword.  */
9567   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9568
9569   /* Get the name of the namespace.  We do not attempt to distinguish
9570      between an original-namespace-definition and an
9571      extension-namespace-definition at this point.  The semantic
9572      analysis routines are responsible for that.  */
9573   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9574     identifier = cp_parser_identifier (parser);
9575   else
9576     identifier = NULL_TREE;
9577
9578   /* Look for the `{' to start the namespace.  */
9579   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9580   /* Start the namespace.  */
9581   push_namespace (identifier);
9582   /* Parse the body of the namespace.  */
9583   cp_parser_namespace_body (parser);
9584   /* Finish the namespace.  */
9585   pop_namespace ();
9586   /* Look for the final `}'.  */
9587   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9588 }
9589
9590 /* Parse a namespace-body.
9591
9592    namespace-body:
9593      declaration-seq [opt]  */
9594
9595 static void
9596 cp_parser_namespace_body (cp_parser* parser)
9597 {
9598   cp_parser_declaration_seq_opt (parser);
9599 }
9600
9601 /* Parse a namespace-alias-definition.
9602
9603    namespace-alias-definition:
9604      namespace identifier = qualified-namespace-specifier ;  */
9605
9606 static void
9607 cp_parser_namespace_alias_definition (cp_parser* parser)
9608 {
9609   tree identifier;
9610   tree namespace_specifier;
9611
9612   /* Look for the `namespace' keyword.  */
9613   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9614   /* Look for the identifier.  */
9615   identifier = cp_parser_identifier (parser);
9616   if (identifier == error_mark_node)
9617     return;
9618   /* Look for the `=' token.  */
9619   cp_parser_require (parser, CPP_EQ, "`='");
9620   /* Look for the qualified-namespace-specifier.  */
9621   namespace_specifier 
9622     = cp_parser_qualified_namespace_specifier (parser);
9623   /* Look for the `;' token.  */
9624   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9625
9626   /* Register the alias in the symbol table.  */
9627   do_namespace_alias (identifier, namespace_specifier);
9628 }
9629
9630 /* Parse a qualified-namespace-specifier.
9631
9632    qualified-namespace-specifier:
9633      :: [opt] nested-name-specifier [opt] namespace-name
9634
9635    Returns a NAMESPACE_DECL corresponding to the specified
9636    namespace.  */
9637
9638 static tree
9639 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9640 {
9641   /* Look for the optional `::'.  */
9642   cp_parser_global_scope_opt (parser, 
9643                               /*current_scope_valid_p=*/false);
9644
9645   /* Look for the optional nested-name-specifier.  */
9646   cp_parser_nested_name_specifier_opt (parser,
9647                                        /*typename_keyword_p=*/false,
9648                                        /*check_dependency_p=*/true,
9649                                        /*type_p=*/false,
9650                                        /*is_declaration=*/true);
9651
9652   return cp_parser_namespace_name (parser);
9653 }
9654
9655 /* Parse a using-declaration.
9656
9657    using-declaration:
9658      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9659      using :: unqualified-id ;  */
9660
9661 static void
9662 cp_parser_using_declaration (cp_parser* parser)
9663 {
9664   cp_token *token;
9665   bool typename_p = false;
9666   bool global_scope_p;
9667   tree decl;
9668   tree identifier;
9669   tree scope;
9670   tree qscope;
9671
9672   /* Look for the `using' keyword.  */
9673   cp_parser_require_keyword (parser, RID_USING, "`using'");
9674   
9675   /* Peek at the next token.  */
9676   token = cp_lexer_peek_token (parser->lexer);
9677   /* See if it's `typename'.  */
9678   if (token->keyword == RID_TYPENAME)
9679     {
9680       /* Remember that we've seen it.  */
9681       typename_p = true;
9682       /* Consume the `typename' token.  */
9683       cp_lexer_consume_token (parser->lexer);
9684     }
9685
9686   /* Look for the optional global scope qualification.  */
9687   global_scope_p 
9688     = (cp_parser_global_scope_opt (parser,
9689                                    /*current_scope_valid_p=*/false) 
9690        != NULL_TREE);
9691
9692   /* If we saw `typename', or didn't see `::', then there must be a
9693      nested-name-specifier present.  */
9694   if (typename_p || !global_scope_p)
9695     qscope = cp_parser_nested_name_specifier (parser, typename_p, 
9696                                               /*check_dependency_p=*/true,
9697                                               /*type_p=*/false,
9698                                               /*is_declaration=*/true);
9699   /* Otherwise, we could be in either of the two productions.  In that
9700      case, treat the nested-name-specifier as optional.  */
9701   else
9702     qscope = cp_parser_nested_name_specifier_opt (parser,
9703                                                   /*typename_keyword_p=*/false,
9704                                                   /*check_dependency_p=*/true,
9705                                                   /*type_p=*/false,
9706                                                   /*is_declaration=*/true);
9707   if (!qscope)
9708     qscope = global_namespace;
9709
9710   /* Parse the unqualified-id.  */
9711   identifier = cp_parser_unqualified_id (parser, 
9712                                          /*template_keyword_p=*/false,
9713                                          /*check_dependency_p=*/true,
9714                                          /*declarator_p=*/true);
9715
9716   /* The function we call to handle a using-declaration is different
9717      depending on what scope we are in.  */
9718   if (identifier == error_mark_node)
9719     ;
9720   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9721            && TREE_CODE (identifier) != BIT_NOT_EXPR)
9722     /* [namespace.udecl]
9723
9724        A using declaration shall not name a template-id.  */
9725     error ("a template-id may not appear in a using-declaration");
9726   else
9727     {
9728       scope = current_scope ();
9729       if (scope && TYPE_P (scope))
9730         {
9731           /* Create the USING_DECL.  */
9732           decl = do_class_using_decl (build_nt (SCOPE_REF,
9733                                                 parser->scope,
9734                                                 identifier));
9735           /* Add it to the list of members in this class.  */
9736           finish_member_declaration (decl);
9737         }
9738       else
9739         {
9740           decl = cp_parser_lookup_name_simple (parser, identifier);
9741           if (decl == error_mark_node)
9742             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9743           else if (scope)
9744             do_local_using_decl (decl, qscope, identifier);
9745           else
9746             do_toplevel_using_decl (decl, qscope, identifier);
9747         }
9748     }
9749
9750   /* Look for the final `;'.  */
9751   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9752 }
9753
9754 /* Parse a using-directive.  
9755  
9756    using-directive:
9757      using namespace :: [opt] nested-name-specifier [opt]
9758        namespace-name ;  */
9759
9760 static void
9761 cp_parser_using_directive (cp_parser* parser)
9762 {
9763   tree namespace_decl;
9764   tree attribs;
9765
9766   /* Look for the `using' keyword.  */
9767   cp_parser_require_keyword (parser, RID_USING, "`using'");
9768   /* And the `namespace' keyword.  */
9769   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9770   /* Look for the optional `::' operator.  */
9771   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9772   /* And the optional nested-name-specifier.  */
9773   cp_parser_nested_name_specifier_opt (parser,
9774                                        /*typename_keyword_p=*/false,
9775                                        /*check_dependency_p=*/true,
9776                                        /*type_p=*/false,
9777                                        /*is_declaration=*/true);
9778   /* Get the namespace being used.  */
9779   namespace_decl = cp_parser_namespace_name (parser);
9780   /* And any specified attributes.  */
9781   attribs = cp_parser_attributes_opt (parser);
9782   /* Update the symbol table.  */
9783   parse_using_directive (namespace_decl, attribs);
9784   /* Look for the final `;'.  */
9785   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9786 }
9787
9788 /* Parse an asm-definition.
9789
9790    asm-definition:
9791      asm ( string-literal ) ;  
9792
9793    GNU Extension:
9794
9795    asm-definition:
9796      asm volatile [opt] ( string-literal ) ;
9797      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9798      asm volatile [opt] ( string-literal : asm-operand-list [opt]
9799                           : asm-operand-list [opt] ) ;
9800      asm volatile [opt] ( string-literal : asm-operand-list [opt] 
9801                           : asm-operand-list [opt] 
9802                           : asm-operand-list [opt] ) ;  */
9803
9804 static void
9805 cp_parser_asm_definition (cp_parser* parser)
9806 {
9807   cp_token *token;
9808   tree string;
9809   tree outputs = NULL_TREE;
9810   tree inputs = NULL_TREE;
9811   tree clobbers = NULL_TREE;
9812   tree asm_stmt;
9813   bool volatile_p = false;
9814   bool extended_p = false;
9815
9816   /* Look for the `asm' keyword.  */
9817   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9818   /* See if the next token is `volatile'.  */
9819   if (cp_parser_allow_gnu_extensions_p (parser)
9820       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9821     {
9822       /* Remember that we saw the `volatile' keyword.  */
9823       volatile_p = true;
9824       /* Consume the token.  */
9825       cp_lexer_consume_token (parser->lexer);
9826     }
9827   /* Look for the opening `('.  */
9828   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9829   /* Look for the string.  */
9830   token = cp_parser_require (parser, CPP_STRING, "asm body");
9831   if (!token)
9832     return;
9833   string = token->value;
9834   /* If we're allowing GNU extensions, check for the extended assembly
9835      syntax.  Unfortunately, the `:' tokens need not be separated by 
9836      a space in C, and so, for compatibility, we tolerate that here
9837      too.  Doing that means that we have to treat the `::' operator as
9838      two `:' tokens.  */
9839   if (cp_parser_allow_gnu_extensions_p (parser)
9840       && at_function_scope_p ()
9841       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9842           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9843     {
9844       bool inputs_p = false;
9845       bool clobbers_p = false;
9846
9847       /* The extended syntax was used.  */
9848       extended_p = true;
9849
9850       /* Look for outputs.  */
9851       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9852         {
9853           /* Consume the `:'.  */
9854           cp_lexer_consume_token (parser->lexer);
9855           /* Parse the output-operands.  */
9856           if (cp_lexer_next_token_is_not (parser->lexer, 
9857                                           CPP_COLON)
9858               && cp_lexer_next_token_is_not (parser->lexer,
9859                                              CPP_SCOPE)
9860               && cp_lexer_next_token_is_not (parser->lexer,
9861                                              CPP_CLOSE_PAREN))
9862             outputs = cp_parser_asm_operand_list (parser);
9863         }
9864       /* If the next token is `::', there are no outputs, and the
9865          next token is the beginning of the inputs.  */
9866       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9867         /* The inputs are coming next.  */
9868         inputs_p = true;
9869
9870       /* Look for inputs.  */
9871       if (inputs_p
9872           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9873         {
9874           /* Consume the `:' or `::'.  */
9875           cp_lexer_consume_token (parser->lexer);
9876           /* Parse the output-operands.  */
9877           if (cp_lexer_next_token_is_not (parser->lexer, 
9878                                           CPP_COLON)
9879               && cp_lexer_next_token_is_not (parser->lexer,
9880                                              CPP_CLOSE_PAREN))
9881             inputs = cp_parser_asm_operand_list (parser);
9882         }
9883       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9884         /* The clobbers are coming next.  */
9885         clobbers_p = true;
9886
9887       /* Look for clobbers.  */
9888       if (clobbers_p 
9889           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9890         {
9891           /* Consume the `:' or `::'.  */
9892           cp_lexer_consume_token (parser->lexer);
9893           /* Parse the clobbers.  */
9894           if (cp_lexer_next_token_is_not (parser->lexer,
9895                                           CPP_CLOSE_PAREN))
9896             clobbers = cp_parser_asm_clobber_list (parser);
9897         }
9898     }
9899   /* Look for the closing `)'.  */
9900   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9901     cp_parser_skip_to_closing_parenthesis (parser, true, false,
9902                                            /*consume_paren=*/true);
9903   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9904
9905   /* Create the ASM_STMT.  */
9906   if (at_function_scope_p ())
9907     {
9908       asm_stmt = 
9909         finish_asm_stmt (volatile_p 
9910                          ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9911                          string, outputs, inputs, clobbers);
9912       /* If the extended syntax was not used, mark the ASM_STMT.  */
9913       if (!extended_p)
9914         ASM_INPUT_P (asm_stmt) = 1;
9915     }
9916   else
9917     assemble_asm (string);
9918 }
9919
9920 /* Declarators [gram.dcl.decl] */
9921
9922 /* Parse an init-declarator.
9923
9924    init-declarator:
9925      declarator initializer [opt]
9926
9927    GNU Extension:
9928
9929    init-declarator:
9930      declarator asm-specification [opt] attributes [opt] initializer [opt]
9931
9932    function-definition:
9933      decl-specifier-seq [opt] declarator ctor-initializer [opt]
9934        function-body 
9935      decl-specifier-seq [opt] declarator function-try-block  
9936
9937    GNU Extension:
9938
9939    function-definition:
9940      __extension__ function-definition 
9941
9942    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9943    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
9944    then this declarator appears in a class scope.  The new DECL created
9945    by this declarator is returned.
9946
9947    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9948    for a function-definition here as well.  If the declarator is a
9949    declarator for a function-definition, *FUNCTION_DEFINITION_P will
9950    be TRUE upon return.  By that point, the function-definition will
9951    have been completely parsed.
9952
9953    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9954    is FALSE.  */
9955
9956 static tree
9957 cp_parser_init_declarator (cp_parser* parser, 
9958                            tree decl_specifiers, 
9959                            tree prefix_attributes,
9960                            bool function_definition_allowed_p,
9961                            bool member_p,
9962                            int declares_class_or_enum,
9963                            bool* function_definition_p)
9964 {
9965   cp_token *token;
9966   tree declarator;
9967   tree attributes;
9968   tree asm_specification;
9969   tree initializer;
9970   tree decl = NULL_TREE;
9971   tree scope;
9972   bool is_initialized;
9973   bool is_parenthesized_init;
9974   bool is_non_constant_init;
9975   int ctor_dtor_or_conv_p;
9976   bool friend_p;
9977   bool pop_p = false;
9978
9979   /* Assume that this is not the declarator for a function
9980      definition.  */
9981   if (function_definition_p)
9982     *function_definition_p = false;
9983
9984   /* Defer access checks while parsing the declarator; we cannot know
9985      what names are accessible until we know what is being 
9986      declared.  */
9987   resume_deferring_access_checks ();
9988
9989   /* Parse the declarator.  */
9990   declarator 
9991     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9992                             &ctor_dtor_or_conv_p,
9993                             /*parenthesized_p=*/NULL,
9994                             /*member_p=*/false);
9995   /* Gather up the deferred checks.  */
9996   stop_deferring_access_checks ();
9997
9998   /* If the DECLARATOR was erroneous, there's no need to go
9999      further.  */
10000   if (declarator == error_mark_node)
10001     return error_mark_node;
10002
10003   cp_parser_check_for_definition_in_return_type (declarator,
10004                                                  declares_class_or_enum);
10005
10006   /* Figure out what scope the entity declared by the DECLARATOR is
10007      located in.  `grokdeclarator' sometimes changes the scope, so
10008      we compute it now.  */
10009   scope = get_scope_of_declarator (declarator);
10010
10011   /* If we're allowing GNU extensions, look for an asm-specification
10012      and attributes.  */
10013   if (cp_parser_allow_gnu_extensions_p (parser))
10014     {
10015       /* Look for an asm-specification.  */
10016       asm_specification = cp_parser_asm_specification_opt (parser);
10017       /* And attributes.  */
10018       attributes = cp_parser_attributes_opt (parser);
10019     }
10020   else
10021     {
10022       asm_specification = NULL_TREE;
10023       attributes = NULL_TREE;
10024     }
10025
10026   /* Peek at the next token.  */
10027   token = cp_lexer_peek_token (parser->lexer);
10028   /* Check to see if the token indicates the start of a
10029      function-definition.  */
10030   if (cp_parser_token_starts_function_definition_p (token))
10031     {
10032       if (!function_definition_allowed_p)
10033         {
10034           /* If a function-definition should not appear here, issue an
10035              error message.  */
10036           cp_parser_error (parser,
10037                            "a function-definition is not allowed here");
10038           return error_mark_node;
10039         }
10040       else
10041         {
10042           /* Neither attributes nor an asm-specification are allowed
10043              on a function-definition.  */
10044           if (asm_specification)
10045             error ("an asm-specification is not allowed on a function-definition");
10046           if (attributes)
10047             error ("attributes are not allowed on a function-definition");
10048           /* This is a function-definition.  */
10049           *function_definition_p = true;
10050
10051           /* Parse the function definition.  */
10052           if (member_p)
10053             decl = cp_parser_save_member_function_body (parser,
10054                                                         decl_specifiers,
10055                                                         declarator,
10056                                                         prefix_attributes);
10057           else
10058             decl 
10059               = (cp_parser_function_definition_from_specifiers_and_declarator
10060                  (parser, decl_specifiers, prefix_attributes, declarator));
10061
10062           return decl;
10063         }
10064     }
10065
10066   /* [dcl.dcl]
10067
10068      Only in function declarations for constructors, destructors, and
10069      type conversions can the decl-specifier-seq be omitted.  
10070
10071      We explicitly postpone this check past the point where we handle
10072      function-definitions because we tolerate function-definitions
10073      that are missing their return types in some modes.  */
10074   if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
10075     {
10076       cp_parser_error (parser, 
10077                        "expected constructor, destructor, or type conversion");
10078       return error_mark_node;
10079     }
10080
10081   /* An `=' or an `(' indicates an initializer.  */
10082   is_initialized = (token->type == CPP_EQ 
10083                      || token->type == CPP_OPEN_PAREN);
10084   /* If the init-declarator isn't initialized and isn't followed by a
10085      `,' or `;', it's not a valid init-declarator.  */
10086   if (!is_initialized 
10087       && token->type != CPP_COMMA
10088       && token->type != CPP_SEMICOLON)
10089     {
10090       cp_parser_error (parser, "expected initializer");
10091       return error_mark_node;
10092     }
10093
10094   /* Because start_decl has side-effects, we should only call it if we
10095      know we're going ahead.  By this point, we know that we cannot
10096      possibly be looking at any other construct.  */
10097   cp_parser_commit_to_tentative_parse (parser);
10098
10099   /* If the decl specifiers were bad, issue an error now that we're
10100      sure this was intended to be a declarator.  Then continue
10101      declaring the variable(s), as int, to try to cut down on further
10102      errors.  */
10103   if (decl_specifiers != NULL
10104       && TREE_VALUE (decl_specifiers) == error_mark_node)
10105     {
10106       cp_parser_error (parser, "invalid type in declaration");
10107       TREE_VALUE (decl_specifiers) = integer_type_node;
10108     }
10109
10110   /* Check to see whether or not this declaration is a friend.  */
10111   friend_p = cp_parser_friend_p (decl_specifiers);
10112
10113   /* Check that the number of template-parameter-lists is OK.  */
10114   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10115     return error_mark_node;
10116
10117   /* Enter the newly declared entry in the symbol table.  If we're
10118      processing a declaration in a class-specifier, we wait until
10119      after processing the initializer.  */
10120   if (!member_p)
10121     {
10122       if (parser->in_unbraced_linkage_specification_p)
10123         {
10124           decl_specifiers = tree_cons (error_mark_node,
10125                                        get_identifier ("extern"),
10126                                        decl_specifiers);
10127           have_extern_spec = false;
10128         }
10129       decl = start_decl (declarator, decl_specifiers,
10130                          is_initialized, attributes, prefix_attributes);
10131     }
10132
10133   /* Enter the SCOPE.  That way unqualified names appearing in the
10134      initializer will be looked up in SCOPE.  */
10135   if (scope)
10136     pop_p = push_scope (scope);
10137
10138   /* Perform deferred access control checks, now that we know in which
10139      SCOPE the declared entity resides.  */
10140   if (!member_p && decl) 
10141     {
10142       tree saved_current_function_decl = NULL_TREE;
10143
10144       /* If the entity being declared is a function, pretend that we
10145          are in its scope.  If it is a `friend', it may have access to
10146          things that would not otherwise be accessible.  */
10147       if (TREE_CODE (decl) == FUNCTION_DECL)
10148         {
10149           saved_current_function_decl = current_function_decl;
10150           current_function_decl = decl;
10151         }
10152         
10153       /* Perform the access control checks for the declarator and the
10154          the decl-specifiers.  */
10155       perform_deferred_access_checks ();
10156
10157       /* Restore the saved value.  */
10158       if (TREE_CODE (decl) == FUNCTION_DECL)
10159         current_function_decl = saved_current_function_decl;
10160     }
10161
10162   /* Parse the initializer.  */
10163   if (is_initialized)
10164     initializer = cp_parser_initializer (parser, 
10165                                          &is_parenthesized_init,
10166                                          &is_non_constant_init);
10167   else
10168     {
10169       initializer = NULL_TREE;
10170       is_parenthesized_init = false;
10171       is_non_constant_init = true;
10172     }
10173
10174   /* The old parser allows attributes to appear after a parenthesized
10175      initializer.  Mark Mitchell proposed removing this functionality
10176      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10177      attributes -- but ignores them.  */
10178   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10179     if (cp_parser_attributes_opt (parser))
10180       warning ("attributes after parenthesized initializer ignored");
10181
10182   /* Leave the SCOPE, now that we have processed the initializer.  It
10183      is important to do this before calling cp_finish_decl because it
10184      makes decisions about whether to create DECL_STMTs or not based
10185      on the current scope.  */
10186   if (pop_p)
10187     pop_scope (scope);
10188
10189   /* For an in-class declaration, use `grokfield' to create the
10190      declaration.  */
10191   if (member_p)
10192     {
10193       decl = grokfield (declarator, decl_specifiers,
10194                         initializer, /*asmspec=*/NULL_TREE,
10195                         /*attributes=*/NULL_TREE);
10196       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10197         cp_parser_save_default_args (parser, decl);
10198     }
10199   
10200   /* Finish processing the declaration.  But, skip friend
10201      declarations.  */
10202   if (!friend_p && decl)
10203     cp_finish_decl (decl, 
10204                     initializer, 
10205                     asm_specification,
10206                     /* If the initializer is in parentheses, then this is
10207                        a direct-initialization, which means that an
10208                        `explicit' constructor is OK.  Otherwise, an
10209                        `explicit' constructor cannot be used.  */
10210                     ((is_parenthesized_init || !is_initialized)
10211                      ? 0 : LOOKUP_ONLYCONVERTING));
10212
10213   /* Remember whether or not variables were initialized by
10214      constant-expressions.  */
10215   if (decl && TREE_CODE (decl) == VAR_DECL 
10216       && is_initialized && !is_non_constant_init)
10217     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10218
10219   return decl;
10220 }
10221
10222 /* Parse a declarator.
10223    
10224    declarator:
10225      direct-declarator
10226      ptr-operator declarator  
10227
10228    abstract-declarator:
10229      ptr-operator abstract-declarator [opt]
10230      direct-abstract-declarator
10231
10232    GNU Extensions:
10233
10234    declarator:
10235      attributes [opt] direct-declarator
10236      attributes [opt] ptr-operator declarator  
10237
10238    abstract-declarator:
10239      attributes [opt] ptr-operator abstract-declarator [opt]
10240      attributes [opt] direct-abstract-declarator
10241      
10242    Returns a representation of the declarator.  If the declarator has
10243    the form `* declarator', then an INDIRECT_REF is returned, whose
10244    only operand is the sub-declarator.  Analogously, `& declarator' is
10245    represented as an ADDR_EXPR.  For `X::* declarator', a SCOPE_REF is
10246    used.  The first operand is the TYPE for `X'.  The second operand
10247    is an INDIRECT_REF whose operand is the sub-declarator.
10248
10249    Otherwise, the representation is as for a direct-declarator.
10250
10251    (It would be better to define a structure type to represent
10252    declarators, rather than abusing `tree' nodes to represent
10253    declarators.  That would be much clearer and save some memory.
10254    There is no reason for declarators to be garbage-collected, for
10255    example; they are created during parser and no longer needed after
10256    `grokdeclarator' has been called.)
10257
10258    For a ptr-operator that has the optional cv-qualifier-seq,
10259    cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10260    node.
10261
10262    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10263    detect constructor, destructor or conversion operators. It is set
10264    to -1 if the declarator is a name, and +1 if it is a
10265    function. Otherwise it is set to zero. Usually you just want to
10266    test for >0, but internally the negative value is used.
10267    
10268    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10269    a decl-specifier-seq unless it declares a constructor, destructor,
10270    or conversion.  It might seem that we could check this condition in
10271    semantic analysis, rather than parsing, but that makes it difficult
10272    to handle something like `f()'.  We want to notice that there are
10273    no decl-specifiers, and therefore realize that this is an
10274    expression, not a declaration.)  
10275  
10276    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10277    the declarator is a direct-declarator of the form "(...)".
10278
10279    MEMBER_P is true iff this declarator is a member-declarator.  */
10280
10281 static tree
10282 cp_parser_declarator (cp_parser* parser, 
10283                       cp_parser_declarator_kind dcl_kind, 
10284                       int* ctor_dtor_or_conv_p,
10285                       bool* parenthesized_p,
10286                       bool member_p)
10287 {
10288   cp_token *token;
10289   tree declarator;
10290   enum tree_code code;
10291   tree cv_qualifier_seq;
10292   tree class_type;
10293   tree attributes = NULL_TREE;
10294
10295   /* Assume this is not a constructor, destructor, or type-conversion
10296      operator.  */
10297   if (ctor_dtor_or_conv_p)
10298     *ctor_dtor_or_conv_p = 0;
10299
10300   if (cp_parser_allow_gnu_extensions_p (parser))
10301     attributes = cp_parser_attributes_opt (parser);
10302   
10303   /* Peek at the next token.  */
10304   token = cp_lexer_peek_token (parser->lexer);
10305   
10306   /* Check for the ptr-operator production.  */
10307   cp_parser_parse_tentatively (parser);
10308   /* Parse the ptr-operator.  */
10309   code = cp_parser_ptr_operator (parser, 
10310                                  &class_type, 
10311                                  &cv_qualifier_seq);
10312   /* If that worked, then we have a ptr-operator.  */
10313   if (cp_parser_parse_definitely (parser))
10314     {
10315       /* If a ptr-operator was found, then this declarator was not
10316          parenthesized.  */
10317       if (parenthesized_p)
10318         *parenthesized_p = true;
10319       /* The dependent declarator is optional if we are parsing an
10320          abstract-declarator.  */
10321       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10322         cp_parser_parse_tentatively (parser);
10323
10324       /* Parse the dependent declarator.  */
10325       declarator = cp_parser_declarator (parser, dcl_kind,
10326                                          /*ctor_dtor_or_conv_p=*/NULL,
10327                                          /*parenthesized_p=*/NULL,
10328                                          /*member_p=*/false);
10329
10330       /* If we are parsing an abstract-declarator, we must handle the
10331          case where the dependent declarator is absent.  */
10332       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10333           && !cp_parser_parse_definitely (parser))
10334         declarator = NULL_TREE;
10335         
10336       /* Build the representation of the ptr-operator.  */
10337       if (code == INDIRECT_REF)
10338         declarator = make_pointer_declarator (cv_qualifier_seq, 
10339                                               declarator);
10340       else
10341         declarator = make_reference_declarator (cv_qualifier_seq,
10342                                                 declarator);
10343       /* Handle the pointer-to-member case.  */
10344       if (class_type)
10345         declarator = build_nt (SCOPE_REF, class_type, declarator);
10346     }
10347   /* Everything else is a direct-declarator.  */
10348   else
10349     {
10350       if (parenthesized_p)
10351         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10352                                                    CPP_OPEN_PAREN);
10353       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10354                                                 ctor_dtor_or_conv_p,
10355                                                 member_p);
10356     }
10357
10358   if (attributes && declarator != error_mark_node)
10359     declarator = tree_cons (attributes, declarator, NULL_TREE);
10360   
10361   return declarator;
10362 }
10363
10364 /* Parse a direct-declarator or direct-abstract-declarator.
10365
10366    direct-declarator:
10367      declarator-id
10368      direct-declarator ( parameter-declaration-clause )
10369        cv-qualifier-seq [opt] 
10370        exception-specification [opt]
10371      direct-declarator [ constant-expression [opt] ]
10372      ( declarator )  
10373
10374    direct-abstract-declarator:
10375      direct-abstract-declarator [opt]
10376        ( parameter-declaration-clause ) 
10377        cv-qualifier-seq [opt]
10378        exception-specification [opt]
10379      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10380      ( abstract-declarator )
10381
10382    Returns a representation of the declarator.  DCL_KIND is
10383    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10384    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10385    we are parsing a direct-declarator.  It is
10386    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10387    of ambiguity we prefer an abstract declarator, as per
10388    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
10389    cp_parser_declarator.
10390
10391    For the declarator-id production, the representation is as for an
10392    id-expression, except that a qualified name is represented as a
10393    SCOPE_REF.  A function-declarator is represented as a CALL_EXPR;
10394    see the documentation of the FUNCTION_DECLARATOR_* macros for
10395    information about how to find the various declarator components.
10396    An array-declarator is represented as an ARRAY_REF.  The
10397    direct-declarator is the first operand; the constant-expression
10398    indicating the size of the array is the second operand.  */
10399
10400 static tree
10401 cp_parser_direct_declarator (cp_parser* parser,
10402                              cp_parser_declarator_kind dcl_kind,
10403                              int* ctor_dtor_or_conv_p,
10404                              bool member_p)
10405 {
10406   cp_token *token;
10407   tree declarator = NULL_TREE;
10408   tree scope = NULL_TREE;
10409   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10410   bool saved_in_declarator_p = parser->in_declarator_p;
10411   bool first = true;
10412   bool pop_p = false;
10413
10414   while (true)
10415     {
10416       /* Peek at the next token.  */
10417       token = cp_lexer_peek_token (parser->lexer);
10418       if (token->type == CPP_OPEN_PAREN)
10419         {
10420           /* This is either a parameter-declaration-clause, or a
10421              parenthesized declarator. When we know we are parsing a
10422              named declarator, it must be a parenthesized declarator
10423              if FIRST is true. For instance, `(int)' is a
10424              parameter-declaration-clause, with an omitted
10425              direct-abstract-declarator. But `((*))', is a
10426              parenthesized abstract declarator. Finally, when T is a
10427              template parameter `(T)' is a
10428              parameter-declaration-clause, and not a parenthesized
10429              named declarator.
10430              
10431              We first try and parse a parameter-declaration-clause,
10432              and then try a nested declarator (if FIRST is true).
10433
10434              It is not an error for it not to be a
10435              parameter-declaration-clause, even when FIRST is
10436              false. Consider,
10437
10438                int i (int);
10439                int i (3);
10440
10441              The first is the declaration of a function while the
10442              second is a the definition of a variable, including its
10443              initializer.
10444
10445              Having seen only the parenthesis, we cannot know which of
10446              these two alternatives should be selected.  Even more
10447              complex are examples like:
10448
10449                int i (int (a));
10450                int i (int (3));
10451
10452              The former is a function-declaration; the latter is a
10453              variable initialization.  
10454
10455              Thus again, we try a parameter-declaration-clause, and if
10456              that fails, we back out and return.  */
10457
10458           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10459             {
10460               tree params;
10461               unsigned saved_num_template_parameter_lists;
10462               
10463               /* In a member-declarator, the only valid interpretation
10464                  of a parenthesis is the start of a
10465                  parameter-declaration-clause.  (It is invalid to
10466                  initialize a static data member with a parenthesized
10467                  initializer; only the "=" form of initialization is
10468                  permitted.)  */
10469               if (!member_p)
10470                 cp_parser_parse_tentatively (parser);
10471
10472               /* Consume the `('.  */
10473               cp_lexer_consume_token (parser->lexer);
10474               if (first)
10475                 {
10476                   /* If this is going to be an abstract declarator, we're
10477                      in a declarator and we can't have default args.  */
10478                   parser->default_arg_ok_p = false;
10479                   parser->in_declarator_p = true;
10480                 }
10481           
10482               /* Inside the function parameter list, surrounding
10483                  template-parameter-lists do not apply.  */
10484               saved_num_template_parameter_lists
10485                 = parser->num_template_parameter_lists;
10486               parser->num_template_parameter_lists = 0;
10487
10488               /* Parse the parameter-declaration-clause.  */
10489               params = cp_parser_parameter_declaration_clause (parser);
10490
10491               parser->num_template_parameter_lists
10492                 = saved_num_template_parameter_lists;
10493
10494               /* If all went well, parse the cv-qualifier-seq and the
10495                  exception-specification.  */
10496               if (member_p || cp_parser_parse_definitely (parser))
10497                 {
10498                   tree cv_qualifiers;
10499                   tree exception_specification;
10500
10501                   if (ctor_dtor_or_conv_p)
10502                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10503                   first = false;
10504                   /* Consume the `)'.  */
10505                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10506
10507                   /* Parse the cv-qualifier-seq.  */
10508                   cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10509                   /* And the exception-specification.  */
10510                   exception_specification 
10511                     = cp_parser_exception_specification_opt (parser);
10512
10513                   /* Create the function-declarator.  */
10514                   declarator = make_call_declarator (declarator,
10515                                                      params,
10516                                                      cv_qualifiers,
10517                                                      exception_specification);
10518                   /* Any subsequent parameter lists are to do with
10519                      return type, so are not those of the declared
10520                      function.  */
10521                   parser->default_arg_ok_p = false;
10522                   
10523                   /* Repeat the main loop.  */
10524                   continue;
10525                 }
10526             }
10527           
10528           /* If this is the first, we can try a parenthesized
10529              declarator.  */
10530           if (first)
10531             {
10532               bool saved_in_type_id_in_expr_p;
10533
10534               parser->default_arg_ok_p = saved_default_arg_ok_p;
10535               parser->in_declarator_p = saved_in_declarator_p;
10536               
10537               /* Consume the `('.  */
10538               cp_lexer_consume_token (parser->lexer);
10539               /* Parse the nested declarator.  */
10540               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10541               parser->in_type_id_in_expr_p = true;
10542               declarator 
10543                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10544                                         /*parenthesized_p=*/NULL,
10545                                         member_p);
10546               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10547               first = false;
10548               /* Expect a `)'.  */
10549               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10550                 declarator = error_mark_node;
10551               if (declarator == error_mark_node)
10552                 break;
10553               
10554               goto handle_declarator;
10555             }
10556           /* Otherwise, we must be done.  */
10557           else
10558             break;
10559         }
10560       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10561                && token->type == CPP_OPEN_SQUARE)
10562         {
10563           /* Parse an array-declarator.  */
10564           tree bounds;
10565
10566           if (ctor_dtor_or_conv_p)
10567             *ctor_dtor_or_conv_p = 0;
10568           
10569           first = false;
10570           parser->default_arg_ok_p = false;
10571           parser->in_declarator_p = true;
10572           /* Consume the `['.  */
10573           cp_lexer_consume_token (parser->lexer);
10574           /* Peek at the next token.  */
10575           token = cp_lexer_peek_token (parser->lexer);
10576           /* If the next token is `]', then there is no
10577              constant-expression.  */
10578           if (token->type != CPP_CLOSE_SQUARE)
10579             {
10580               bool non_constant_p;
10581
10582               bounds 
10583                 = cp_parser_constant_expression (parser,
10584                                                  /*allow_non_constant=*/true,
10585                                                  &non_constant_p);
10586               if (!non_constant_p)
10587                 bounds = fold_non_dependent_expr (bounds);
10588             }
10589           else
10590             bounds = NULL_TREE;
10591           /* Look for the closing `]'.  */
10592           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10593             {
10594               declarator = error_mark_node;
10595               break;
10596             }
10597
10598           declarator = build_nt (ARRAY_REF, declarator, bounds);
10599         }
10600       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10601         {
10602           /* Parse a declarator-id */
10603           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10604             cp_parser_parse_tentatively (parser);
10605           declarator = cp_parser_declarator_id (parser);
10606           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10607             {
10608               if (!cp_parser_parse_definitely (parser))
10609                 declarator = error_mark_node;
10610               else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10611                 {
10612                   cp_parser_error (parser, "expected unqualified-id");
10613                   declarator = error_mark_node;
10614                 }
10615             }
10616           
10617           if (declarator == error_mark_node)
10618             break;
10619           
10620           if (TREE_CODE (declarator) == SCOPE_REF
10621               && !current_scope ())
10622             {
10623               tree scope = TREE_OPERAND (declarator, 0);
10624
10625               /* In the declaration of a member of a template class
10626                  outside of the class itself, the SCOPE will sometimes
10627                  be a TYPENAME_TYPE.  For example, given:
10628                   
10629                  template <typename T>
10630                  int S<T>::R::i = 3;
10631                   
10632                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
10633                  this context, we must resolve S<T>::R to an ordinary
10634                  type, rather than a typename type.
10635                   
10636                  The reason we normally avoid resolving TYPENAME_TYPEs
10637                  is that a specialization of `S' might render
10638                  `S<T>::R' not a type.  However, if `S' is
10639                  specialized, then this `i' will not be used, so there
10640                  is no harm in resolving the types here.  */
10641               if (TREE_CODE (scope) == TYPENAME_TYPE)
10642                 {
10643                   tree type;
10644
10645                   /* Resolve the TYPENAME_TYPE.  */
10646                   type = resolve_typename_type (scope,
10647                                                 /*only_current_p=*/false);
10648                   /* If that failed, the declarator is invalid.  */
10649                   if (type == error_mark_node)
10650                     error ("`%T::%D' is not a type",
10651                            TYPE_CONTEXT (scope),
10652                            TYPE_IDENTIFIER (scope));
10653                   /* Build a new DECLARATOR.  */
10654                   declarator = build_nt (SCOPE_REF, 
10655                                          type,
10656                                          TREE_OPERAND (declarator, 1));
10657                 }
10658             }
10659       
10660           /* Check to see whether the declarator-id names a constructor, 
10661              destructor, or conversion.  */
10662           if (declarator && ctor_dtor_or_conv_p 
10663               && ((TREE_CODE (declarator) == SCOPE_REF 
10664                    && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10665                   || (TREE_CODE (declarator) != SCOPE_REF
10666                       && at_class_scope_p ())))
10667             {
10668               tree unqualified_name;
10669               tree class_type;
10670
10671               /* Get the unqualified part of the name.  */
10672               if (TREE_CODE (declarator) == SCOPE_REF)
10673                 {
10674                   class_type = TREE_OPERAND (declarator, 0);
10675                   unqualified_name = TREE_OPERAND (declarator, 1);
10676                 }
10677               else
10678                 {
10679                   class_type = current_class_type;
10680                   unqualified_name = declarator;
10681                 }
10682
10683               /* See if it names ctor, dtor or conv.  */
10684               if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10685                   || IDENTIFIER_TYPENAME_P (unqualified_name)
10686                   || constructor_name_p (unqualified_name, class_type)
10687                   || (TREE_CODE (unqualified_name) == TYPE_DECL
10688                       && same_type_p (TREE_TYPE (unqualified_name),
10689                                       class_type)))
10690                 *ctor_dtor_or_conv_p = -1;
10691             }
10692
10693         handle_declarator:;
10694           scope = get_scope_of_declarator (declarator);
10695           if (scope)
10696             /* Any names that appear after the declarator-id for a
10697                member are looked up in the containing scope.  */
10698             pop_p = push_scope (scope);
10699           parser->in_declarator_p = true;
10700           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10701               || (declarator
10702                   && (TREE_CODE (declarator) == SCOPE_REF
10703                       || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10704             /* Default args are only allowed on function
10705                declarations.  */
10706             parser->default_arg_ok_p = saved_default_arg_ok_p;
10707           else
10708             parser->default_arg_ok_p = false;
10709
10710           first = false;
10711         }
10712       /* We're done.  */
10713       else
10714         break;
10715     }
10716
10717   /* For an abstract declarator, we might wind up with nothing at this
10718      point.  That's an error; the declarator is not optional.  */
10719   if (!declarator)
10720     cp_parser_error (parser, "expected declarator");
10721
10722   /* If we entered a scope, we must exit it now.  */
10723   if (pop_p)
10724     pop_scope (scope);
10725
10726   parser->default_arg_ok_p = saved_default_arg_ok_p;
10727   parser->in_declarator_p = saved_in_declarator_p;
10728   
10729   return declarator;
10730 }
10731
10732 /* Parse a ptr-operator.  
10733
10734    ptr-operator:
10735      * cv-qualifier-seq [opt]
10736      &
10737      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10738
10739    GNU Extension:
10740
10741    ptr-operator:
10742      & cv-qualifier-seq [opt]
10743
10744    Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10745    used.  Returns ADDR_EXPR if a reference was used.  In the
10746    case of a pointer-to-member, *TYPE is filled in with the 
10747    TYPE containing the member.  *CV_QUALIFIER_SEQ is filled in
10748    with the cv-qualifier-seq, or NULL_TREE, if there are no
10749    cv-qualifiers.  Returns ERROR_MARK if an error occurred.  */
10750    
10751 static enum tree_code
10752 cp_parser_ptr_operator (cp_parser* parser, 
10753                         tree* type, 
10754                         tree* cv_qualifier_seq)
10755 {
10756   enum tree_code code = ERROR_MARK;
10757   cp_token *token;
10758
10759   /* Assume that it's not a pointer-to-member.  */
10760   *type = NULL_TREE;
10761   /* And that there are no cv-qualifiers.  */
10762   *cv_qualifier_seq = NULL_TREE;
10763
10764   /* Peek at the next token.  */
10765   token = cp_lexer_peek_token (parser->lexer);
10766   /* If it's a `*' or `&' we have a pointer or reference.  */
10767   if (token->type == CPP_MULT || token->type == CPP_AND)
10768     {
10769       /* Remember which ptr-operator we were processing.  */
10770       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10771
10772       /* Consume the `*' or `&'.  */
10773       cp_lexer_consume_token (parser->lexer);
10774
10775       /* A `*' can be followed by a cv-qualifier-seq, and so can a
10776          `&', if we are allowing GNU extensions.  (The only qualifier
10777          that can legally appear after `&' is `restrict', but that is
10778          enforced during semantic analysis.  */
10779       if (code == INDIRECT_REF 
10780           || cp_parser_allow_gnu_extensions_p (parser))
10781         *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10782     }
10783   else
10784     {
10785       /* Try the pointer-to-member case.  */
10786       cp_parser_parse_tentatively (parser);
10787       /* Look for the optional `::' operator.  */
10788       cp_parser_global_scope_opt (parser,
10789                                   /*current_scope_valid_p=*/false);
10790       /* Look for the nested-name specifier.  */
10791       cp_parser_nested_name_specifier (parser,
10792                                        /*typename_keyword_p=*/false,
10793                                        /*check_dependency_p=*/true,
10794                                        /*type_p=*/false,
10795                                        /*is_declaration=*/false);
10796       /* If we found it, and the next token is a `*', then we are
10797          indeed looking at a pointer-to-member operator.  */
10798       if (!cp_parser_error_occurred (parser)
10799           && cp_parser_require (parser, CPP_MULT, "`*'"))
10800         {
10801           /* The type of which the member is a member is given by the
10802              current SCOPE.  */
10803           *type = parser->scope;
10804           /* The next name will not be qualified.  */
10805           parser->scope = NULL_TREE;
10806           parser->qualifying_scope = NULL_TREE;
10807           parser->object_scope = NULL_TREE;
10808           /* Indicate that the `*' operator was used.  */
10809           code = INDIRECT_REF;
10810           /* Look for the optional cv-qualifier-seq.  */
10811           *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10812         }
10813       /* If that didn't work we don't have a ptr-operator.  */
10814       if (!cp_parser_parse_definitely (parser))
10815         cp_parser_error (parser, "expected ptr-operator");
10816     }
10817
10818   return code;
10819 }
10820
10821 /* Parse an (optional) cv-qualifier-seq.
10822
10823    cv-qualifier-seq:
10824      cv-qualifier cv-qualifier-seq [opt]  
10825
10826    Returns a TREE_LIST.  The TREE_VALUE of each node is the
10827    representation of a cv-qualifier.  */
10828
10829 static tree
10830 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10831 {
10832   tree cv_qualifiers = NULL_TREE;
10833   
10834   while (true)
10835     {
10836       tree cv_qualifier;
10837
10838       /* Look for the next cv-qualifier.  */
10839       cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10840       /* If we didn't find one, we're done.  */
10841       if (!cv_qualifier)
10842         break;
10843
10844       /* Add this cv-qualifier to the list.  */
10845       cv_qualifiers 
10846         = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10847     }
10848
10849   /* We built up the list in reverse order.  */
10850   return nreverse (cv_qualifiers);
10851 }
10852
10853 /* Parse an (optional) cv-qualifier.
10854
10855    cv-qualifier:
10856      const
10857      volatile  
10858
10859    GNU Extension:
10860
10861    cv-qualifier:
10862      __restrict__ */
10863
10864 static tree
10865 cp_parser_cv_qualifier_opt (cp_parser* parser)
10866 {
10867   cp_token *token;
10868   tree cv_qualifier = NULL_TREE;
10869
10870   /* Peek at the next token.  */
10871   token = cp_lexer_peek_token (parser->lexer);
10872   /* See if it's a cv-qualifier.  */
10873   switch (token->keyword)
10874     {
10875     case RID_CONST:
10876     case RID_VOLATILE:
10877     case RID_RESTRICT:
10878       /* Save the value of the token.  */
10879       cv_qualifier = token->value;
10880       /* Consume the token.  */
10881       cp_lexer_consume_token (parser->lexer);
10882       break;
10883
10884     default:
10885       break;
10886     }
10887
10888   return cv_qualifier;
10889 }
10890
10891 /* Parse a declarator-id.
10892
10893    declarator-id:
10894      id-expression
10895      :: [opt] nested-name-specifier [opt] type-name  
10896
10897    In the `id-expression' case, the value returned is as for
10898    cp_parser_id_expression if the id-expression was an unqualified-id.
10899    If the id-expression was a qualified-id, then a SCOPE_REF is
10900    returned.  The first operand is the scope (either a NAMESPACE_DECL
10901    or TREE_TYPE), but the second is still just a representation of an
10902    unqualified-id.  */
10903
10904 static tree
10905 cp_parser_declarator_id (cp_parser* parser)
10906 {
10907   tree id_expression;
10908
10909   /* The expression must be an id-expression.  Assume that qualified
10910      names are the names of types so that:
10911
10912        template <class T>
10913        int S<T>::R::i = 3;
10914
10915      will work; we must treat `S<T>::R' as the name of a type.
10916      Similarly, assume that qualified names are templates, where
10917      required, so that:
10918
10919        template <class T>
10920        int S<T>::R<T>::i = 3;
10921
10922      will work, too.  */
10923   id_expression = cp_parser_id_expression (parser,
10924                                            /*template_keyword_p=*/false,
10925                                            /*check_dependency_p=*/false,
10926                                            /*template_p=*/NULL,
10927                                            /*declarator_p=*/true);
10928   /* If the name was qualified, create a SCOPE_REF to represent 
10929      that.  */
10930   if (parser->scope)
10931     {
10932       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10933       parser->scope = NULL_TREE;
10934     }
10935
10936   return id_expression;
10937 }
10938
10939 /* Parse a type-id.
10940
10941    type-id:
10942      type-specifier-seq abstract-declarator [opt]
10943
10944    Returns the TYPE specified.  */
10945
10946 static tree
10947 cp_parser_type_id (cp_parser* parser)
10948 {
10949   tree type_specifier_seq;
10950   tree abstract_declarator;
10951
10952   /* Parse the type-specifier-seq.  */
10953   type_specifier_seq 
10954     = cp_parser_type_specifier_seq (parser);
10955   if (type_specifier_seq == error_mark_node)
10956     return error_mark_node;
10957
10958   /* There might or might not be an abstract declarator.  */
10959   cp_parser_parse_tentatively (parser);
10960   /* Look for the declarator.  */
10961   abstract_declarator 
10962     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10963                             /*parenthesized_p=*/NULL,
10964                             /*member_p=*/false);
10965   /* Check to see if there really was a declarator.  */
10966   if (!cp_parser_parse_definitely (parser))
10967     abstract_declarator = NULL_TREE;
10968
10969   return groktypename (build_tree_list (type_specifier_seq,
10970                                         abstract_declarator));
10971 }
10972
10973 /* Parse a type-specifier-seq.
10974
10975    type-specifier-seq:
10976      type-specifier type-specifier-seq [opt]
10977
10978    GNU extension:
10979
10980    type-specifier-seq:
10981      attributes type-specifier-seq [opt]
10982
10983    Returns a TREE_LIST.  Either the TREE_VALUE of each node is a
10984    type-specifier, or the TREE_PURPOSE is a list of attributes.  */
10985
10986 static tree
10987 cp_parser_type_specifier_seq (cp_parser* parser)
10988 {
10989   bool seen_type_specifier = false;
10990   tree type_specifier_seq = NULL_TREE;
10991
10992   /* Parse the type-specifiers and attributes.  */
10993   while (true)
10994     {
10995       tree type_specifier;
10996
10997       /* Check for attributes first.  */
10998       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10999         {
11000           type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
11001                                           NULL_TREE,
11002                                           type_specifier_seq);
11003           continue;
11004         }
11005
11006       /* After the first type-specifier, others are optional.  */
11007       if (seen_type_specifier)
11008         cp_parser_parse_tentatively (parser);
11009       /* Look for the type-specifier.  */
11010       type_specifier = cp_parser_type_specifier (parser, 
11011                                                  CP_PARSER_FLAGS_NONE,
11012                                                  /*is_friend=*/false,
11013                                                  /*is_declaration=*/false,
11014                                                  NULL,
11015                                                  NULL);
11016       /* If the first type-specifier could not be found, this is not a
11017          type-specifier-seq at all.  */
11018       if (!seen_type_specifier && type_specifier == error_mark_node)
11019         return error_mark_node;
11020       /* If subsequent type-specifiers could not be found, the
11021          type-specifier-seq is complete.  */
11022       else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
11023         break;
11024
11025       /* Add the new type-specifier to the list.  */
11026       type_specifier_seq 
11027         = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
11028       seen_type_specifier = true;
11029     }
11030
11031   /* We built up the list in reverse order.  */
11032   return nreverse (type_specifier_seq);
11033 }
11034
11035 /* Parse a parameter-declaration-clause.
11036
11037    parameter-declaration-clause:
11038      parameter-declaration-list [opt] ... [opt]
11039      parameter-declaration-list , ...
11040
11041    Returns a representation for the parameter declarations.  Each node
11042    is a TREE_LIST.  (See cp_parser_parameter_declaration for the exact
11043    representation.)  If the parameter-declaration-clause ends with an
11044    ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
11045    list.  A return value of NULL_TREE indicates a
11046    parameter-declaration-clause consisting only of an ellipsis.  */
11047
11048 static tree
11049 cp_parser_parameter_declaration_clause (cp_parser* parser)
11050 {
11051   tree parameters;
11052   cp_token *token;
11053   bool ellipsis_p;
11054
11055   /* Peek at the next token.  */
11056   token = cp_lexer_peek_token (parser->lexer);
11057   /* Check for trivial parameter-declaration-clauses.  */
11058   if (token->type == CPP_ELLIPSIS)
11059     {
11060       /* Consume the `...' token.  */
11061       cp_lexer_consume_token (parser->lexer);
11062       return NULL_TREE;
11063     }
11064   else if (token->type == CPP_CLOSE_PAREN)
11065     /* There are no parameters.  */
11066     {
11067 #ifndef NO_IMPLICIT_EXTERN_C
11068       if (in_system_header && current_class_type == NULL
11069           && current_lang_name == lang_name_c)
11070         return NULL_TREE;
11071       else
11072 #endif
11073         return void_list_node;
11074     }
11075   /* Check for `(void)', too, which is a special case.  */
11076   else if (token->keyword == RID_VOID
11077            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
11078                == CPP_CLOSE_PAREN))
11079     {
11080       /* Consume the `void' token.  */
11081       cp_lexer_consume_token (parser->lexer);
11082       /* There are no parameters.  */
11083       return void_list_node;
11084     }
11085   
11086   /* Parse the parameter-declaration-list.  */
11087   parameters = cp_parser_parameter_declaration_list (parser);
11088   /* If a parse error occurred while parsing the
11089      parameter-declaration-list, then the entire
11090      parameter-declaration-clause is erroneous.  */
11091   if (parameters == error_mark_node)
11092     return error_mark_node;
11093
11094   /* Peek at the next token.  */
11095   token = cp_lexer_peek_token (parser->lexer);
11096   /* If it's a `,', the clause should terminate with an ellipsis.  */
11097   if (token->type == CPP_COMMA)
11098     {
11099       /* Consume the `,'.  */
11100       cp_lexer_consume_token (parser->lexer);
11101       /* Expect an ellipsis.  */
11102       ellipsis_p 
11103         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11104     }
11105   /* It might also be `...' if the optional trailing `,' was 
11106      omitted.  */
11107   else if (token->type == CPP_ELLIPSIS)
11108     {
11109       /* Consume the `...' token.  */
11110       cp_lexer_consume_token (parser->lexer);
11111       /* And remember that we saw it.  */
11112       ellipsis_p = true;
11113     }
11114   else
11115     ellipsis_p = false;
11116
11117   /* Finish the parameter list.  */
11118   return finish_parmlist (parameters, ellipsis_p);
11119 }
11120
11121 /* Parse a parameter-declaration-list.
11122
11123    parameter-declaration-list:
11124      parameter-declaration
11125      parameter-declaration-list , parameter-declaration
11126
11127    Returns a representation of the parameter-declaration-list, as for
11128    cp_parser_parameter_declaration_clause.  However, the
11129    `void_list_node' is never appended to the list.  */
11130
11131 static tree
11132 cp_parser_parameter_declaration_list (cp_parser* parser)
11133 {
11134   tree parameters = NULL_TREE;
11135
11136   /* Look for more parameters.  */
11137   while (true)
11138     {
11139       tree parameter;
11140       bool parenthesized_p;
11141       /* Parse the parameter.  */
11142       parameter 
11143         = cp_parser_parameter_declaration (parser, 
11144                                            /*template_parm_p=*/false,
11145                                            &parenthesized_p);
11146
11147       /* If a parse error occurred parsing the parameter declaration,
11148          then the entire parameter-declaration-list is erroneous.  */
11149       if (parameter == error_mark_node)
11150         {
11151           parameters = error_mark_node;
11152           break;
11153         }
11154       /* Add the new parameter to the list.  */
11155       TREE_CHAIN (parameter) = parameters;
11156       parameters = parameter;
11157
11158       /* Peek at the next token.  */
11159       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11160           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11161         /* The parameter-declaration-list is complete.  */
11162         break;
11163       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11164         {
11165           cp_token *token;
11166
11167           /* Peek at the next token.  */
11168           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11169           /* If it's an ellipsis, then the list is complete.  */
11170           if (token->type == CPP_ELLIPSIS)
11171             break;
11172           /* Otherwise, there must be more parameters.  Consume the
11173              `,'.  */
11174           cp_lexer_consume_token (parser->lexer);
11175           /* When parsing something like:
11176
11177                 int i(float f, double d)
11178                 
11179              we can tell after seeing the declaration for "f" that we
11180              are not looking at an initialization of a variable "i",
11181              but rather at the declaration of a function "i".  
11182
11183              Due to the fact that the parsing of template arguments
11184              (as specified to a template-id) requires backtracking we
11185              cannot use this technique when inside a template argument
11186              list.  */
11187           if (!parser->in_template_argument_list_p
11188               && !parser->in_type_id_in_expr_p
11189               && cp_parser_parsing_tentatively (parser)
11190               && !cp_parser_committed_to_tentative_parse (parser)
11191               /* However, a parameter-declaration of the form
11192                  "foat(f)" (which is a valid declaration of a
11193                  parameter "f") can also be interpreted as an
11194                  expression (the conversion of "f" to "float").  */
11195               && !parenthesized_p)
11196             cp_parser_commit_to_tentative_parse (parser);
11197         }
11198       else
11199         {
11200           cp_parser_error (parser, "expected `,' or `...'");
11201           if (!cp_parser_parsing_tentatively (parser)
11202               || cp_parser_committed_to_tentative_parse (parser))
11203             cp_parser_skip_to_closing_parenthesis (parser, 
11204                                                    /*recovering=*/true,
11205                                                    /*or_comma=*/false,
11206                                                    /*consume_paren=*/false);
11207           break;
11208         }
11209     }
11210
11211   /* We built up the list in reverse order; straighten it out now.  */
11212   return nreverse (parameters);
11213 }
11214
11215 /* Parse a parameter declaration.
11216
11217    parameter-declaration:
11218      decl-specifier-seq declarator
11219      decl-specifier-seq declarator = assignment-expression
11220      decl-specifier-seq abstract-declarator [opt]
11221      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11222
11223    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11224    declares a template parameter.  (In that case, a non-nested `>'
11225    token encountered during the parsing of the assignment-expression
11226    is not interpreted as a greater-than operator.)
11227
11228    Returns a TREE_LIST representing the parameter-declaration.  The
11229    TREE_PURPOSE is the default argument expression, or NULL_TREE if
11230    there is no default argument.  The TREE_VALUE is a representation
11231    of the decl-specifier-seq and declarator.  In particular, the
11232    TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11233    decl-specifier-seq and whose TREE_VALUE represents the declarator.
11234    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11235    the declarator is of the form "(p)".  */
11236
11237 static tree
11238 cp_parser_parameter_declaration (cp_parser *parser, 
11239                                  bool template_parm_p,
11240                                  bool *parenthesized_p)
11241 {
11242   int declares_class_or_enum;
11243   bool greater_than_is_operator_p;
11244   tree decl_specifiers;
11245   tree attributes;
11246   tree declarator;
11247   tree default_argument;
11248   tree parameter;
11249   cp_token *token;
11250   const char *saved_message;
11251
11252   /* In a template parameter, `>' is not an operator.
11253
11254      [temp.param]
11255
11256      When parsing a default template-argument for a non-type
11257      template-parameter, the first non-nested `>' is taken as the end
11258      of the template parameter-list rather than a greater-than
11259      operator.  */
11260   greater_than_is_operator_p = !template_parm_p;
11261
11262   /* Type definitions may not appear in parameter types.  */
11263   saved_message = parser->type_definition_forbidden_message;
11264   parser->type_definition_forbidden_message 
11265     = "types may not be defined in parameter types";
11266
11267   /* Parse the declaration-specifiers.  */
11268   decl_specifiers 
11269     = cp_parser_decl_specifier_seq (parser,
11270                                     CP_PARSER_FLAGS_NONE,
11271                                     &attributes,
11272                                     &declares_class_or_enum);
11273   /* If an error occurred, there's no reason to attempt to parse the
11274      rest of the declaration.  */
11275   if (cp_parser_error_occurred (parser))
11276     {
11277       parser->type_definition_forbidden_message = saved_message;
11278       return error_mark_node;
11279     }
11280
11281   /* Peek at the next token.  */
11282   token = cp_lexer_peek_token (parser->lexer);
11283   /* If the next token is a `)', `,', `=', `>', or `...', then there
11284      is no declarator.  */
11285   if (token->type == CPP_CLOSE_PAREN 
11286       || token->type == CPP_COMMA
11287       || token->type == CPP_EQ
11288       || token->type == CPP_ELLIPSIS
11289       || token->type == CPP_GREATER)
11290     {
11291       declarator = NULL_TREE;
11292       if (parenthesized_p)
11293         *parenthesized_p = false;
11294     }
11295   /* Otherwise, there should be a declarator.  */
11296   else
11297     {
11298       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11299       parser->default_arg_ok_p = false;
11300   
11301       /* After seeing a decl-specifier-seq, if the next token is not a
11302          "(", there is no possibility that the code is a valid
11303          expression.  Therefore, if parsing tentatively, we commit at
11304          this point.  */
11305       if (!parser->in_template_argument_list_p
11306           /* In an expression context, having seen:
11307
11308                (int((char ...
11309
11310              we cannot be sure whether we are looking at a
11311              function-type (taking a "char" as a parameter) or a cast
11312              of some object of type "char" to "int".  */
11313           && !parser->in_type_id_in_expr_p
11314           && cp_parser_parsing_tentatively (parser)
11315           && !cp_parser_committed_to_tentative_parse (parser)
11316           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11317         cp_parser_commit_to_tentative_parse (parser);
11318       /* Parse the declarator.  */
11319       declarator = cp_parser_declarator (parser,
11320                                          CP_PARSER_DECLARATOR_EITHER,
11321                                          /*ctor_dtor_or_conv_p=*/NULL,
11322                                          parenthesized_p,
11323                                          /*member_p=*/false);
11324       parser->default_arg_ok_p = saved_default_arg_ok_p;
11325       /* After the declarator, allow more attributes.  */
11326       attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11327     }
11328
11329   /* The restriction on defining new types applies only to the type
11330      of the parameter, not to the default argument.  */
11331   parser->type_definition_forbidden_message = saved_message;
11332
11333   /* If the next token is `=', then process a default argument.  */
11334   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11335     {
11336       bool saved_greater_than_is_operator_p;
11337       /* Consume the `='.  */
11338       cp_lexer_consume_token (parser->lexer);
11339
11340       /* If we are defining a class, then the tokens that make up the
11341          default argument must be saved and processed later.  */
11342       if (!template_parm_p && at_class_scope_p () 
11343           && TYPE_BEING_DEFINED (current_class_type))
11344         {
11345           unsigned depth = 0;
11346
11347           /* Create a DEFAULT_ARG to represented the unparsed default
11348              argument.  */
11349           default_argument = make_node (DEFAULT_ARG);
11350           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11351
11352           /* Add tokens until we have processed the entire default
11353              argument.  */
11354           while (true)
11355             {
11356               bool done = false;
11357               cp_token *token;
11358
11359               /* Peek at the next token.  */
11360               token = cp_lexer_peek_token (parser->lexer);
11361               /* What we do depends on what token we have.  */
11362               switch (token->type)
11363                 {
11364                   /* In valid code, a default argument must be
11365                      immediately followed by a `,' `)', or `...'.  */
11366                 case CPP_COMMA:
11367                 case CPP_CLOSE_PAREN:
11368                 case CPP_ELLIPSIS:
11369                   /* If we run into a non-nested `;', `}', or `]',
11370                      then the code is invalid -- but the default
11371                      argument is certainly over.  */
11372                 case CPP_SEMICOLON:
11373                 case CPP_CLOSE_BRACE:
11374                 case CPP_CLOSE_SQUARE:
11375                   if (depth == 0)
11376                     done = true;
11377                   /* Update DEPTH, if necessary.  */
11378                   else if (token->type == CPP_CLOSE_PAREN
11379                            || token->type == CPP_CLOSE_BRACE
11380                            || token->type == CPP_CLOSE_SQUARE)
11381                     --depth;
11382                   break;
11383
11384                 case CPP_OPEN_PAREN:
11385                 case CPP_OPEN_SQUARE:
11386                 case CPP_OPEN_BRACE:
11387                   ++depth;
11388                   break;
11389
11390                 case CPP_GREATER:
11391                   /* If we see a non-nested `>', and `>' is not an
11392                      operator, then it marks the end of the default
11393                      argument.  */
11394                   if (!depth && !greater_than_is_operator_p)
11395                     done = true;
11396                   break;
11397
11398                   /* If we run out of tokens, issue an error message.  */
11399                 case CPP_EOF:
11400                   error ("file ends in default argument");
11401                   done = true;
11402                   break;
11403
11404                 case CPP_NAME:
11405                 case CPP_SCOPE:
11406                   /* In these cases, we should look for template-ids.
11407                      For example, if the default argument is 
11408                      `X<int, double>()', we need to do name lookup to
11409                      figure out whether or not `X' is a template; if
11410                      so, the `,' does not end the default argument.
11411
11412                      That is not yet done.  */
11413                   break;
11414
11415                 default:
11416                   break;
11417                 }
11418
11419               /* If we've reached the end, stop.  */
11420               if (done)
11421                 break;
11422               
11423               /* Add the token to the token block.  */
11424               token = cp_lexer_consume_token (parser->lexer);
11425               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11426                                          token);
11427             }
11428         }
11429       /* Outside of a class definition, we can just parse the
11430          assignment-expression.  */
11431       else
11432         {
11433           bool saved_local_variables_forbidden_p;
11434
11435           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11436              set correctly.  */
11437           saved_greater_than_is_operator_p 
11438             = parser->greater_than_is_operator_p;
11439           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11440           /* Local variable names (and the `this' keyword) may not
11441              appear in a default argument.  */
11442           saved_local_variables_forbidden_p 
11443             = parser->local_variables_forbidden_p;
11444           parser->local_variables_forbidden_p = true;
11445           /* Parse the assignment-expression.  */
11446           default_argument = cp_parser_assignment_expression (parser);
11447           /* Restore saved state.  */
11448           parser->greater_than_is_operator_p 
11449             = saved_greater_than_is_operator_p;
11450           parser->local_variables_forbidden_p 
11451             = saved_local_variables_forbidden_p; 
11452         }
11453       if (!parser->default_arg_ok_p)
11454         {
11455           if (!flag_pedantic_errors)
11456             warning ("deprecated use of default argument for parameter of non-function");
11457           else
11458             {
11459               error ("default arguments are only permitted for function parameters");
11460               default_argument = NULL_TREE;
11461             }
11462         }
11463     }
11464   else
11465     default_argument = NULL_TREE;
11466   
11467   /* Create the representation of the parameter.  */
11468   if (attributes)
11469     decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11470   parameter = build_tree_list (default_argument, 
11471                                build_tree_list (decl_specifiers,
11472                                                 declarator));
11473
11474   return parameter;
11475 }
11476
11477 /* Parse a function-body.
11478
11479    function-body:
11480      compound_statement  */
11481
11482 static void
11483 cp_parser_function_body (cp_parser *parser)
11484 {
11485   cp_parser_compound_statement (parser, false);
11486 }
11487
11488 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11489    true if a ctor-initializer was present.  */
11490
11491 static bool
11492 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11493 {
11494   tree body;
11495   bool ctor_initializer_p;
11496
11497   /* Begin the function body.  */
11498   body = begin_function_body ();
11499   /* Parse the optional ctor-initializer.  */
11500   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11501   /* Parse the function-body.  */
11502   cp_parser_function_body (parser);
11503   /* Finish the function body.  */
11504   finish_function_body (body);
11505
11506   return ctor_initializer_p;
11507 }
11508
11509 /* Parse an initializer.
11510
11511    initializer:
11512      = initializer-clause
11513      ( expression-list )  
11514
11515    Returns a expression representing the initializer.  If no
11516    initializer is present, NULL_TREE is returned.  
11517
11518    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11519    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11520    set to FALSE if there is no initializer present.  If there is an
11521    initializer, and it is not a constant-expression, *NON_CONSTANT_P
11522    is set to true; otherwise it is set to false.  */
11523
11524 static tree
11525 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11526                        bool* non_constant_p)
11527 {
11528   cp_token *token;
11529   tree init;
11530
11531   /* Peek at the next token.  */
11532   token = cp_lexer_peek_token (parser->lexer);
11533
11534   /* Let our caller know whether or not this initializer was
11535      parenthesized.  */
11536   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11537   /* Assume that the initializer is constant.  */
11538   *non_constant_p = false;
11539
11540   if (token->type == CPP_EQ)
11541     {
11542       /* Consume the `='.  */
11543       cp_lexer_consume_token (parser->lexer);
11544       /* Parse the initializer-clause.  */
11545       init = cp_parser_initializer_clause (parser, non_constant_p);
11546     }
11547   else if (token->type == CPP_OPEN_PAREN)
11548     init = cp_parser_parenthesized_expression_list (parser, false,
11549                                                     non_constant_p);
11550   else
11551     {
11552       /* Anything else is an error.  */
11553       cp_parser_error (parser, "expected initializer");
11554       init = error_mark_node;
11555     }
11556
11557   return init;
11558 }
11559
11560 /* Parse an initializer-clause.  
11561
11562    initializer-clause:
11563      assignment-expression
11564      { initializer-list , [opt] }
11565      { }
11566
11567    Returns an expression representing the initializer.  
11568
11569    If the `assignment-expression' production is used the value
11570    returned is simply a representation for the expression.  
11571
11572    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11573    the elements of the initializer-list (or NULL_TREE, if the last
11574    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11575    NULL_TREE.  There is no way to detect whether or not the optional
11576    trailing `,' was provided.  NON_CONSTANT_P is as for
11577    cp_parser_initializer.  */
11578
11579 static tree
11580 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11581 {
11582   tree initializer = NULL_TREE;
11583
11584   /* Assume the expression is constant.  */
11585   *non_constant_p = false;
11586
11587   /* If it is not a `{', then we are looking at an
11588      assignment-expression.  */
11589   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11590     {
11591       /* Speed up common initializers (simply a literal).  */
11592       cp_token* token = cp_lexer_peek_token (parser->lexer);
11593       cp_token* token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11594
11595       if (token2->type == CPP_COMMA)
11596         switch (token->type)
11597           {
11598           case CPP_CHAR:
11599           case CPP_WCHAR:
11600           case CPP_NUMBER:
11601             token = cp_lexer_consume_token (parser->lexer);
11602             initializer = token->value;
11603             break;
11604
11605           case CPP_STRING:
11606           case CPP_WSTRING:
11607             token = cp_lexer_consume_token (parser->lexer);
11608             if (TREE_CHAIN (token->value))
11609               initializer = TREE_CHAIN (token->value);
11610             else
11611               initializer = token->value;
11612             break;
11613
11614           default:
11615             break;
11616           }
11617
11618       /* Otherwise, fall back to the generic assignment expression.  */
11619       if (!initializer)
11620         {
11621           initializer
11622             = cp_parser_constant_expression (parser,
11623                                             /*allow_non_constant_p=*/true,
11624                                             non_constant_p);
11625           if (!*non_constant_p)
11626             initializer = fold_non_dependent_expr (initializer);
11627         }
11628     }
11629   else
11630     {
11631       /* Consume the `{' token.  */
11632       cp_lexer_consume_token (parser->lexer);
11633       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11634       initializer = make_node (CONSTRUCTOR);
11635       /* Mark it with TREE_HAS_CONSTRUCTOR.  This should not be
11636          necessary, but check_initializer depends upon it, for 
11637          now.  */
11638       TREE_HAS_CONSTRUCTOR (initializer) = 1;
11639       /* If it's not a `}', then there is a non-trivial initializer.  */
11640       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11641         {
11642           /* Parse the initializer list.  */
11643           CONSTRUCTOR_ELTS (initializer)
11644             = cp_parser_initializer_list (parser, non_constant_p);
11645           /* A trailing `,' token is allowed.  */
11646           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11647             cp_lexer_consume_token (parser->lexer);
11648         }
11649       /* Now, there should be a trailing `}'.  */
11650       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11651     }
11652
11653   return initializer;
11654 }
11655
11656 /* Parse an initializer-list.
11657
11658    initializer-list:
11659      initializer-clause
11660      initializer-list , initializer-clause
11661
11662    GNU Extension:
11663    
11664    initializer-list:
11665      identifier : initializer-clause
11666      initializer-list, identifier : initializer-clause
11667
11668    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
11669    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
11670    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
11671    as for cp_parser_initializer.  */
11672
11673 static tree
11674 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11675 {
11676   tree initializers = NULL_TREE;
11677
11678   /* Assume all of the expressions are constant.  */
11679   *non_constant_p = false;
11680
11681   /* Parse the rest of the list.  */
11682   while (true)
11683     {
11684       cp_token *token;
11685       tree identifier;
11686       tree initializer;
11687       bool clause_non_constant_p;
11688
11689       /* If the next token is an identifier and the following one is a
11690          colon, we are looking at the GNU designated-initializer
11691          syntax.  */
11692       if (cp_parser_allow_gnu_extensions_p (parser)
11693           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11694           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11695         {
11696           /* Consume the identifier.  */
11697           identifier = cp_lexer_consume_token (parser->lexer)->value;
11698           /* Consume the `:'.  */
11699           cp_lexer_consume_token (parser->lexer);
11700         }
11701       else
11702         identifier = NULL_TREE;
11703
11704       /* Parse the initializer.  */
11705       initializer = cp_parser_initializer_clause (parser, 
11706                                                   &clause_non_constant_p);
11707       /* If any clause is non-constant, so is the entire initializer.  */
11708       if (clause_non_constant_p)
11709         *non_constant_p = true;
11710       /* Add it to the list.  */
11711       initializers = tree_cons (identifier, initializer, initializers);
11712
11713       /* If the next token is not a comma, we have reached the end of
11714          the list.  */
11715       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11716         break;
11717
11718       /* Peek at the next token.  */
11719       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11720       /* If the next token is a `}', then we're still done.  An
11721          initializer-clause can have a trailing `,' after the
11722          initializer-list and before the closing `}'.  */
11723       if (token->type == CPP_CLOSE_BRACE)
11724         break;
11725
11726       /* Consume the `,' token.  */
11727       cp_lexer_consume_token (parser->lexer);
11728     }
11729
11730   /* The initializers were built up in reverse order, so we need to
11731      reverse them now.  */
11732   return nreverse (initializers);
11733 }
11734
11735 /* Classes [gram.class] */
11736
11737 /* Parse a class-name.
11738
11739    class-name:
11740      identifier
11741      template-id
11742
11743    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11744    to indicate that names looked up in dependent types should be
11745    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
11746    keyword has been used to indicate that the name that appears next
11747    is a template.  TYPE_P is true iff the next name should be treated
11748    as class-name, even if it is declared to be some other kind of name
11749    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11750    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
11751    being defined in a class-head.
11752
11753    Returns the TYPE_DECL representing the class.  */
11754
11755 static tree
11756 cp_parser_class_name (cp_parser *parser, 
11757                       bool typename_keyword_p, 
11758                       bool template_keyword_p, 
11759                       bool type_p,
11760                       bool check_dependency_p,
11761                       bool class_head_p,
11762                       bool is_declaration)
11763 {
11764   tree decl;
11765   tree scope;
11766   bool typename_p;
11767   cp_token *token;
11768
11769   /* All class-names start with an identifier.  */
11770   token = cp_lexer_peek_token (parser->lexer);
11771   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11772     {
11773       cp_parser_error (parser, "expected class-name");
11774       return error_mark_node;
11775     }
11776     
11777   /* PARSER->SCOPE can be cleared when parsing the template-arguments
11778      to a template-id, so we save it here.  */
11779   scope = parser->scope;
11780   if (scope == error_mark_node)
11781     return error_mark_node;
11782   
11783   /* Any name names a type if we're following the `typename' keyword
11784      in a qualified name where the enclosing scope is type-dependent.  */
11785   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11786                 && dependent_type_p (scope));
11787   /* Handle the common case (an identifier, but not a template-id)
11788      efficiently.  */
11789   if (token->type == CPP_NAME 
11790       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11791     {
11792       tree identifier;
11793
11794       /* Look for the identifier.  */
11795       identifier = cp_parser_identifier (parser);
11796       /* If the next token isn't an identifier, we are certainly not
11797          looking at a class-name.  */
11798       if (identifier == error_mark_node)
11799         decl = error_mark_node;
11800       /* If we know this is a type-name, there's no need to look it
11801          up.  */
11802       else if (typename_p)
11803         decl = identifier;
11804       else
11805         {
11806           /* If the next token is a `::', then the name must be a type
11807              name.
11808
11809              [basic.lookup.qual]
11810
11811              During the lookup for a name preceding the :: scope
11812              resolution operator, object, function, and enumerator
11813              names are ignored.  */
11814           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11815             type_p = true;
11816           /* Look up the name.  */
11817           decl = cp_parser_lookup_name (parser, identifier, 
11818                                         type_p,
11819                                         /*is_template=*/false,
11820                                         /*is_namespace=*/false,
11821                                         check_dependency_p);
11822         }
11823     }
11824   else
11825     {
11826       /* Try a template-id.  */
11827       decl = cp_parser_template_id (parser, template_keyword_p,
11828                                     check_dependency_p,
11829                                     is_declaration);
11830       if (decl == error_mark_node)
11831         return error_mark_node;
11832     }
11833
11834   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11835
11836   /* If this is a typename, create a TYPENAME_TYPE.  */
11837   if (typename_p && decl != error_mark_node)
11838     {
11839       decl = make_typename_type (scope, decl, /*complain=*/1);
11840       if (decl != error_mark_node)
11841         decl = TYPE_NAME (decl);
11842     }
11843
11844   /* Check to see that it is really the name of a class.  */
11845   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 
11846       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11847       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11848     /* Situations like this:
11849
11850          template <typename T> struct A {
11851            typename T::template X<int>::I i; 
11852          };
11853
11854        are problematic.  Is `T::template X<int>' a class-name?  The
11855        standard does not seem to be definitive, but there is no other
11856        valid interpretation of the following `::'.  Therefore, those
11857        names are considered class-names.  */
11858     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11859   else if (decl == error_mark_node
11860            || TREE_CODE (decl) != TYPE_DECL
11861            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11862     {
11863       cp_parser_error (parser, "expected class-name");
11864       return error_mark_node;
11865     }
11866
11867   return decl;
11868 }
11869
11870 /* Parse a class-specifier.
11871
11872    class-specifier:
11873      class-head { member-specification [opt] }
11874
11875    Returns the TREE_TYPE representing the class.  */
11876
11877 static tree
11878 cp_parser_class_specifier (cp_parser* parser)
11879 {
11880   cp_token *token;
11881   tree type;
11882   tree attributes;
11883   int has_trailing_semicolon;
11884   bool nested_name_specifier_p;
11885   unsigned saved_num_template_parameter_lists;
11886   bool pop_p = false;
11887   tree scope = NULL_TREE;
11888
11889   push_deferring_access_checks (dk_no_deferred);
11890
11891   /* Parse the class-head.  */
11892   type = cp_parser_class_head (parser,
11893                                &nested_name_specifier_p,
11894                                &attributes);
11895   /* If the class-head was a semantic disaster, skip the entire body
11896      of the class.  */
11897   if (!type)
11898     {
11899       cp_parser_skip_to_end_of_block_or_statement (parser);
11900       pop_deferring_access_checks ();
11901       return error_mark_node;
11902     }
11903
11904   /* Look for the `{'.  */
11905   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11906     {
11907       pop_deferring_access_checks ();
11908       return error_mark_node;
11909     }
11910
11911   /* Issue an error message if type-definitions are forbidden here.  */
11912   cp_parser_check_type_definition (parser);
11913   /* Remember that we are defining one more class.  */
11914   ++parser->num_classes_being_defined;
11915   /* Inside the class, surrounding template-parameter-lists do not
11916      apply.  */
11917   saved_num_template_parameter_lists 
11918     = parser->num_template_parameter_lists; 
11919   parser->num_template_parameter_lists = 0;
11920
11921   /* Start the class.  */
11922   if (nested_name_specifier_p)
11923     {
11924       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
11925       pop_p = push_scope (scope);
11926     }
11927   type = begin_class_definition (type);
11928   if (type == error_mark_node)
11929     /* If the type is erroneous, skip the entire body of the class.  */
11930     cp_parser_skip_to_closing_brace (parser);
11931   else
11932     /* Parse the member-specification.  */
11933     cp_parser_member_specification_opt (parser);
11934   /* Look for the trailing `}'.  */
11935   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11936   /* We get better error messages by noticing a common problem: a
11937      missing trailing `;'.  */
11938   token = cp_lexer_peek_token (parser->lexer);
11939   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11940   /* Look for trailing attributes to apply to this class.  */
11941   if (cp_parser_allow_gnu_extensions_p (parser))
11942     {
11943       tree sub_attr = cp_parser_attributes_opt (parser);
11944       attributes = chainon (attributes, sub_attr);
11945     }
11946   if (type != error_mark_node)
11947     type = finish_struct (type, attributes);
11948   if (pop_p)
11949     pop_scope (scope);
11950   /* If this class is not itself within the scope of another class,
11951      then we need to parse the bodies of all of the queued function
11952      definitions.  Note that the queued functions defined in a class
11953      are not always processed immediately following the
11954      class-specifier for that class.  Consider:
11955
11956        struct A {
11957          struct B { void f() { sizeof (A); } };
11958        };
11959
11960      If `f' were processed before the processing of `A' were
11961      completed, there would be no way to compute the size of `A'.
11962      Note that the nesting we are interested in here is lexical --
11963      not the semantic nesting given by TYPE_CONTEXT.  In particular,
11964      for:
11965
11966        struct A { struct B; };
11967        struct A::B { void f() { } };
11968
11969      there is no need to delay the parsing of `A::B::f'.  */
11970   if (--parser->num_classes_being_defined == 0) 
11971     {
11972       tree queue_entry;
11973       tree fn;
11974
11975       /* In a first pass, parse default arguments to the functions.
11976          Then, in a second pass, parse the bodies of the functions.
11977          This two-phased approach handles cases like:
11978          
11979             struct S { 
11980               void f() { g(); } 
11981               void g(int i = 3);
11982             };
11983
11984          */
11985       for (TREE_PURPOSE (parser->unparsed_functions_queues)
11986              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11987            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11988            TREE_PURPOSE (parser->unparsed_functions_queues)
11989              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11990         {
11991           fn = TREE_VALUE (queue_entry);
11992           /* Make sure that any template parameters are in scope.  */
11993           maybe_begin_member_template_processing (fn);
11994           /* If there are default arguments that have not yet been processed,
11995              take care of them now.  */
11996           cp_parser_late_parsing_default_args (parser, fn);
11997           /* Remove any template parameters from the symbol table.  */
11998           maybe_end_member_template_processing ();
11999         }
12000       /* Now parse the body of the functions.  */
12001       for (TREE_VALUE (parser->unparsed_functions_queues)
12002              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12003            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12004            TREE_VALUE (parser->unparsed_functions_queues)
12005              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12006         {
12007           /* Figure out which function we need to process.  */
12008           fn = TREE_VALUE (queue_entry);
12009
12010           /* A hack to prevent garbage collection.  */
12011           function_depth++;
12012
12013           /* Parse the function.  */
12014           cp_parser_late_parsing_for_member (parser, fn);
12015           function_depth--;
12016         }
12017
12018     }
12019
12020   /* Put back any saved access checks.  */
12021   pop_deferring_access_checks ();
12022
12023   /* Restore the count of active template-parameter-lists.  */
12024   parser->num_template_parameter_lists
12025     = saved_num_template_parameter_lists;
12026
12027   return type;
12028 }
12029
12030 /* Parse a class-head.
12031
12032    class-head:
12033      class-key identifier [opt] base-clause [opt]
12034      class-key nested-name-specifier identifier base-clause [opt]
12035      class-key nested-name-specifier [opt] template-id 
12036        base-clause [opt]  
12037
12038    GNU Extensions:
12039      class-key attributes identifier [opt] base-clause [opt]
12040      class-key attributes nested-name-specifier identifier base-clause [opt]
12041      class-key attributes nested-name-specifier [opt] template-id 
12042        base-clause [opt]  
12043
12044    Returns the TYPE of the indicated class.  Sets
12045    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12046    involving a nested-name-specifier was used, and FALSE otherwise.
12047
12048    Returns NULL_TREE if the class-head is syntactically valid, but
12049    semantically invalid in a way that means we should skip the entire
12050    body of the class.  */
12051
12052 static tree
12053 cp_parser_class_head (cp_parser* parser, 
12054                       bool* nested_name_specifier_p,
12055                       tree *attributes_p)
12056 {
12057   cp_token *token;
12058   tree nested_name_specifier;
12059   enum tag_types class_key;
12060   tree id = NULL_TREE;
12061   tree type = NULL_TREE;
12062   tree attributes;
12063   bool template_id_p = false;
12064   bool qualified_p = false;
12065   bool invalid_nested_name_p = false;
12066   bool invalid_explicit_specialization_p = false;
12067   bool pop_p = false;
12068   unsigned num_templates;
12069
12070   /* Assume no nested-name-specifier will be present.  */
12071   *nested_name_specifier_p = false;
12072   /* Assume no template parameter lists will be used in defining the
12073      type.  */
12074   num_templates = 0;
12075
12076   /* Look for the class-key.  */
12077   class_key = cp_parser_class_key (parser);
12078   if (class_key == none_type)
12079     return error_mark_node;
12080
12081   /* Parse the attributes.  */
12082   attributes = cp_parser_attributes_opt (parser);
12083
12084   /* If the next token is `::', that is invalid -- but sometimes
12085      people do try to write:
12086
12087        struct ::S {};  
12088
12089      Handle this gracefully by accepting the extra qualifier, and then
12090      issuing an error about it later if this really is a
12091      class-head.  If it turns out just to be an elaborated type
12092      specifier, remain silent.  */
12093   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12094     qualified_p = true;
12095
12096   push_deferring_access_checks (dk_no_check);
12097
12098   /* Determine the name of the class.  Begin by looking for an
12099      optional nested-name-specifier.  */
12100   nested_name_specifier 
12101     = cp_parser_nested_name_specifier_opt (parser,
12102                                            /*typename_keyword_p=*/false,
12103                                            /*check_dependency_p=*/false,
12104                                            /*type_p=*/false,
12105                                            /*is_declaration=*/false);
12106   /* If there was a nested-name-specifier, then there *must* be an
12107      identifier.  */
12108   if (nested_name_specifier)
12109     {
12110       /* Although the grammar says `identifier', it really means
12111          `class-name' or `template-name'.  You are only allowed to
12112          define a class that has already been declared with this
12113          syntax.  
12114
12115          The proposed resolution for Core Issue 180 says that whever
12116          you see `class T::X' you should treat `X' as a type-name.
12117          
12118          It is OK to define an inaccessible class; for example:
12119          
12120            class A { class B; };
12121            class A::B {};
12122          
12123          We do not know if we will see a class-name, or a
12124          template-name.  We look for a class-name first, in case the
12125          class-name is a template-id; if we looked for the
12126          template-name first we would stop after the template-name.  */
12127       cp_parser_parse_tentatively (parser);
12128       type = cp_parser_class_name (parser,
12129                                    /*typename_keyword_p=*/false,
12130                                    /*template_keyword_p=*/false,
12131                                    /*type_p=*/true,
12132                                    /*check_dependency_p=*/false,
12133                                    /*class_head_p=*/true,
12134                                    /*is_declaration=*/false);
12135       /* If that didn't work, ignore the nested-name-specifier.  */
12136       if (!cp_parser_parse_definitely (parser))
12137         {
12138           invalid_nested_name_p = true;
12139           id = cp_parser_identifier (parser);
12140           if (id == error_mark_node)
12141             id = NULL_TREE;
12142         }
12143       /* If we could not find a corresponding TYPE, treat this
12144          declaration like an unqualified declaration.  */
12145       if (type == error_mark_node)
12146         nested_name_specifier = NULL_TREE;
12147       /* Otherwise, count the number of templates used in TYPE and its
12148          containing scopes.  */
12149       else 
12150         {
12151           tree scope;
12152
12153           for (scope = TREE_TYPE (type); 
12154                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12155                scope = (TYPE_P (scope) 
12156                         ? TYPE_CONTEXT (scope)
12157                         : DECL_CONTEXT (scope))) 
12158             if (TYPE_P (scope) 
12159                 && CLASS_TYPE_P (scope)
12160                 && CLASSTYPE_TEMPLATE_INFO (scope)
12161                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12162                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12163               ++num_templates;
12164         }
12165     }
12166   /* Otherwise, the identifier is optional.  */
12167   else
12168     {
12169       /* We don't know whether what comes next is a template-id,
12170          an identifier, or nothing at all.  */
12171       cp_parser_parse_tentatively (parser);
12172       /* Check for a template-id.  */
12173       id = cp_parser_template_id (parser, 
12174                                   /*template_keyword_p=*/false,
12175                                   /*check_dependency_p=*/true,
12176                                   /*is_declaration=*/true);
12177       /* If that didn't work, it could still be an identifier.  */
12178       if (!cp_parser_parse_definitely (parser))
12179         {
12180           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12181             id = cp_parser_identifier (parser);
12182           else
12183             id = NULL_TREE;
12184         }
12185       else
12186         {
12187           template_id_p = true;
12188           ++num_templates;
12189         }
12190     }
12191
12192   pop_deferring_access_checks ();
12193
12194   if (id)
12195     cp_parser_check_for_invalid_template_id (parser, id);
12196
12197   /* If it's not a `:' or a `{' then we can't really be looking at a
12198      class-head, since a class-head only appears as part of a
12199      class-specifier.  We have to detect this situation before calling
12200      xref_tag, since that has irreversible side-effects.  */
12201   if (!cp_parser_next_token_starts_class_definition_p (parser))
12202     {
12203       cp_parser_error (parser, "expected `{' or `:'");
12204       return error_mark_node;
12205     }
12206
12207   /* At this point, we're going ahead with the class-specifier, even
12208      if some other problem occurs.  */
12209   cp_parser_commit_to_tentative_parse (parser);
12210   /* Issue the error about the overly-qualified name now.  */
12211   if (qualified_p)
12212     cp_parser_error (parser,
12213                      "global qualification of class name is invalid");
12214   else if (invalid_nested_name_p)
12215     cp_parser_error (parser,
12216                      "qualified name does not name a class");
12217   else if (nested_name_specifier)
12218     {
12219       tree scope;
12220
12221       /* Reject typedef-names in class heads.  */
12222       if (!DECL_IMPLICIT_TYPEDEF_P (type))
12223         {
12224           error ("invalid class name in declaration of `%D'", type);
12225           type = NULL_TREE;
12226           goto done;
12227         }
12228
12229       /* Figure out in what scope the declaration is being placed.  */
12230       scope = current_scope ();
12231       if (!scope)
12232         scope = current_namespace;
12233       /* If that scope does not contain the scope in which the
12234          class was originally declared, the program is invalid.  */
12235       if (scope && !is_ancestor (scope, nested_name_specifier))
12236         {
12237           error ("declaration of `%D' in `%D' which does not "
12238                  "enclose `%D'", type, scope, nested_name_specifier);
12239           type = NULL_TREE;
12240           goto done;
12241         }
12242       /* [dcl.meaning]
12243
12244          A declarator-id shall not be qualified exception of the
12245          definition of a ... nested class outside of its class
12246          ... [or] a the definition or explicit instantiation of a
12247          class member of a namespace outside of its namespace.  */
12248       if (scope == nested_name_specifier)
12249         {
12250           pedwarn ("extra qualification ignored");
12251           nested_name_specifier = NULL_TREE;
12252           num_templates = 0;
12253         }
12254     }
12255   /* An explicit-specialization must be preceded by "template <>".  If
12256      it is not, try to recover gracefully.  */
12257   if (at_namespace_scope_p () 
12258       && parser->num_template_parameter_lists == 0
12259       && template_id_p)
12260     {
12261       error ("an explicit specialization must be preceded by 'template <>'");
12262       invalid_explicit_specialization_p = true;
12263       /* Take the same action that would have been taken by
12264          cp_parser_explicit_specialization.  */
12265       ++parser->num_template_parameter_lists;
12266       begin_specialization ();
12267     }
12268   /* There must be no "return" statements between this point and the
12269      end of this function; set "type "to the correct return value and
12270      use "goto done;" to return.  */
12271   /* Make sure that the right number of template parameters were
12272      present.  */
12273   if (!cp_parser_check_template_parameters (parser, num_templates))
12274     {
12275       /* If something went wrong, there is no point in even trying to
12276          process the class-definition.  */
12277       type = NULL_TREE;
12278       goto done;
12279     }
12280
12281   /* Look up the type.  */
12282   if (template_id_p)
12283     {
12284       type = TREE_TYPE (id);
12285       maybe_process_partial_specialization (type);
12286     }
12287   else if (!nested_name_specifier)
12288     {
12289       /* If the class was unnamed, create a dummy name.  */
12290       if (!id)
12291         id = make_anon_name ();
12292       type = xref_tag (class_key, id, /*globalize=*/false,
12293                        parser->num_template_parameter_lists);
12294     }
12295   else
12296     {
12297       tree class_type;
12298       bool pop_p = false;
12299
12300       /* Given:
12301
12302             template <typename T> struct S { struct T };
12303             template <typename T> struct S<T>::T { };
12304
12305          we will get a TYPENAME_TYPE when processing the definition of
12306          `S::T'.  We need to resolve it to the actual type before we
12307          try to define it.  */
12308       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12309         {
12310           class_type = resolve_typename_type (TREE_TYPE (type),
12311                                               /*only_current_p=*/false);
12312           if (class_type != error_mark_node)
12313             type = TYPE_NAME (class_type);
12314           else
12315             {
12316               cp_parser_error (parser, "could not resolve typename type");
12317               type = error_mark_node;
12318             }
12319         }
12320
12321       maybe_process_partial_specialization (TREE_TYPE (type));
12322       class_type = current_class_type;
12323       /* Enter the scope indicated by the nested-name-specifier.  */
12324       if (nested_name_specifier)
12325         pop_p = push_scope (nested_name_specifier);
12326       /* Get the canonical version of this type.  */
12327       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12328       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12329           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12330         type = push_template_decl (type);
12331       type = TREE_TYPE (type);
12332       if (nested_name_specifier)
12333         {
12334           *nested_name_specifier_p = true;
12335           if (pop_p)
12336             pop_scope (nested_name_specifier);
12337         }
12338     }
12339   /* Indicate whether this class was declared as a `class' or as a
12340      `struct'.  */
12341   if (TREE_CODE (type) == RECORD_TYPE)
12342     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12343   cp_parser_check_class_key (class_key, type);
12344
12345   /* Enter the scope containing the class; the names of base classes
12346      should be looked up in that context.  For example, given:
12347
12348        struct A { struct B {}; struct C; };
12349        struct A::C : B {};
12350
12351      is valid.  */
12352   if (nested_name_specifier)
12353     pop_p = push_scope (nested_name_specifier);
12354   /* Now, look for the base-clause.  */
12355   token = cp_lexer_peek_token (parser->lexer);
12356   if (token->type == CPP_COLON)
12357     {
12358       tree bases;
12359
12360       /* Get the list of base-classes.  */
12361       bases = cp_parser_base_clause (parser);
12362       /* Process them.  */
12363       xref_basetypes (type, bases);
12364     }
12365   /* Leave the scope given by the nested-name-specifier.  We will
12366      enter the class scope itself while processing the members.  */
12367   if (pop_p)
12368     pop_scope (nested_name_specifier);
12369
12370  done:
12371   if (invalid_explicit_specialization_p)
12372     {
12373       end_specialization ();
12374       --parser->num_template_parameter_lists;
12375     }
12376   *attributes_p = attributes;
12377   return type;
12378 }
12379
12380 /* Parse a class-key.
12381
12382    class-key:
12383      class
12384      struct
12385      union
12386
12387    Returns the kind of class-key specified, or none_type to indicate
12388    error.  */
12389
12390 static enum tag_types
12391 cp_parser_class_key (cp_parser* parser)
12392 {
12393   cp_token *token;
12394   enum tag_types tag_type;
12395
12396   /* Look for the class-key.  */
12397   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12398   if (!token)
12399     return none_type;
12400
12401   /* Check to see if the TOKEN is a class-key.  */
12402   tag_type = cp_parser_token_is_class_key (token);
12403   if (!tag_type)
12404     cp_parser_error (parser, "expected class-key");
12405   return tag_type;
12406 }
12407
12408 /* Parse an (optional) member-specification.
12409
12410    member-specification:
12411      member-declaration member-specification [opt]
12412      access-specifier : member-specification [opt]  */
12413
12414 static void
12415 cp_parser_member_specification_opt (cp_parser* parser)
12416 {
12417   while (true)
12418     {
12419       cp_token *token;
12420       enum rid keyword;
12421
12422       /* Peek at the next token.  */
12423       token = cp_lexer_peek_token (parser->lexer);
12424       /* If it's a `}', or EOF then we've seen all the members.  */
12425       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12426         break;
12427
12428       /* See if this token is a keyword.  */
12429       keyword = token->keyword;
12430       switch (keyword)
12431         {
12432         case RID_PUBLIC:
12433         case RID_PROTECTED:
12434         case RID_PRIVATE:
12435           /* Consume the access-specifier.  */
12436           cp_lexer_consume_token (parser->lexer);
12437           /* Remember which access-specifier is active.  */
12438           current_access_specifier = token->value;
12439           /* Look for the `:'.  */
12440           cp_parser_require (parser, CPP_COLON, "`:'");
12441           break;
12442
12443         default:
12444           /* Otherwise, the next construction must be a
12445              member-declaration.  */
12446           cp_parser_member_declaration (parser);
12447         }
12448     }
12449 }
12450
12451 /* Parse a member-declaration.  
12452
12453    member-declaration:
12454      decl-specifier-seq [opt] member-declarator-list [opt] ;
12455      function-definition ; [opt]
12456      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12457      using-declaration
12458      template-declaration 
12459
12460    member-declarator-list:
12461      member-declarator
12462      member-declarator-list , member-declarator
12463
12464    member-declarator:
12465      declarator pure-specifier [opt] 
12466      declarator constant-initializer [opt]
12467      identifier [opt] : constant-expression 
12468
12469    GNU Extensions:
12470
12471    member-declaration:
12472      __extension__ member-declaration
12473
12474    member-declarator:
12475      declarator attributes [opt] pure-specifier [opt]
12476      declarator attributes [opt] constant-initializer [opt]
12477      identifier [opt] attributes [opt] : constant-expression  */
12478
12479 static void
12480 cp_parser_member_declaration (cp_parser* parser)
12481 {
12482   tree decl_specifiers;
12483   tree prefix_attributes;
12484   tree decl;
12485   int declares_class_or_enum;
12486   bool friend_p;
12487   cp_token *token;
12488   int saved_pedantic;
12489
12490   /* Check for the `__extension__' keyword.  */
12491   if (cp_parser_extension_opt (parser, &saved_pedantic))
12492     {
12493       /* Recurse.  */
12494       cp_parser_member_declaration (parser);
12495       /* Restore the old value of the PEDANTIC flag.  */
12496       pedantic = saved_pedantic;
12497
12498       return;
12499     }
12500
12501   /* Check for a template-declaration.  */
12502   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12503     {
12504       /* Parse the template-declaration.  */
12505       cp_parser_template_declaration (parser, /*member_p=*/true);
12506
12507       return;
12508     }
12509
12510   /* Check for a using-declaration.  */
12511   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12512     {
12513       /* Parse the using-declaration.  */
12514       cp_parser_using_declaration (parser);
12515
12516       return;
12517     }
12518   
12519   /* Parse the decl-specifier-seq.  */
12520   decl_specifiers 
12521     = cp_parser_decl_specifier_seq (parser,
12522                                     CP_PARSER_FLAGS_OPTIONAL,
12523                                     &prefix_attributes,
12524                                     &declares_class_or_enum);
12525   /* Check for an invalid type-name.  */
12526   if (cp_parser_diagnose_invalid_type_name (parser))
12527     return;
12528   /* If there is no declarator, then the decl-specifier-seq should
12529      specify a type.  */
12530   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12531     {
12532       /* If there was no decl-specifier-seq, and the next token is a
12533          `;', then we have something like:
12534
12535            struct S { ; };
12536
12537          [class.mem]
12538
12539          Each member-declaration shall declare at least one member
12540          name of the class.  */
12541       if (!decl_specifiers)
12542         {
12543           if (pedantic)
12544             pedwarn ("extra semicolon");
12545         }
12546       else 
12547         {
12548           tree type;
12549           
12550           /* See if this declaration is a friend.  */
12551           friend_p = cp_parser_friend_p (decl_specifiers);
12552           /* If there were decl-specifiers, check to see if there was
12553              a class-declaration.  */
12554           type = check_tag_decl (decl_specifiers);
12555           /* Nested classes have already been added to the class, but
12556              a `friend' needs to be explicitly registered.  */
12557           if (friend_p)
12558             {
12559               /* If the `friend' keyword was present, the friend must
12560                  be introduced with a class-key.  */
12561                if (!declares_class_or_enum)
12562                  error ("a class-key must be used when declaring a friend");
12563                /* In this case:
12564
12565                     template <typename T> struct A { 
12566                       friend struct A<T>::B; 
12567                     };
12568  
12569                   A<T>::B will be represented by a TYPENAME_TYPE, and
12570                   therefore not recognized by check_tag_decl.  */
12571                if (!type)
12572                  {
12573                    tree specifier;
12574
12575                    for (specifier = decl_specifiers; 
12576                         specifier;
12577                         specifier = TREE_CHAIN (specifier))
12578                      {
12579                        tree s = TREE_VALUE (specifier);
12580
12581                        if (TREE_CODE (s) == IDENTIFIER_NODE)
12582                          get_global_value_if_present (s, &type);
12583                        if (TREE_CODE (s) == TYPE_DECL)
12584                          s = TREE_TYPE (s);
12585                        if (TYPE_P (s))
12586                          {
12587                            type = s;
12588                            break;
12589                          }
12590                      }
12591                  }
12592                if (!type || !TYPE_P (type))
12593                  error ("friend declaration does not name a class or "
12594                         "function");
12595                else
12596                  make_friend_class (current_class_type, type,
12597                                     /*complain=*/true);
12598             }
12599           /* If there is no TYPE, an error message will already have
12600              been issued.  */
12601           else if (!type)
12602             ;
12603           /* An anonymous aggregate has to be handled specially; such
12604              a declaration really declares a data member (with a
12605              particular type), as opposed to a nested class.  */
12606           else if (ANON_AGGR_TYPE_P (type))
12607             {
12608               /* Remove constructors and such from TYPE, now that we
12609                  know it is an anonymous aggregate.  */
12610               fixup_anonymous_aggr (type);
12611               /* And make the corresponding data member.  */
12612               decl = build_decl (FIELD_DECL, NULL_TREE, type);
12613               /* Add it to the class.  */
12614               finish_member_declaration (decl);
12615             }
12616           else
12617             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12618         }
12619     }
12620   else
12621     {
12622       /* See if these declarations will be friends.  */
12623       friend_p = cp_parser_friend_p (decl_specifiers);
12624
12625       /* Keep going until we hit the `;' at the end of the 
12626          declaration.  */
12627       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12628         {
12629           tree attributes = NULL_TREE;
12630           tree first_attribute;
12631
12632           /* Peek at the next token.  */
12633           token = cp_lexer_peek_token (parser->lexer);
12634
12635           /* Check for a bitfield declaration.  */
12636           if (token->type == CPP_COLON
12637               || (token->type == CPP_NAME
12638                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type 
12639                   == CPP_COLON))
12640             {
12641               tree identifier;
12642               tree width;
12643
12644               /* Get the name of the bitfield.  Note that we cannot just
12645                  check TOKEN here because it may have been invalidated by
12646                  the call to cp_lexer_peek_nth_token above.  */
12647               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12648                 identifier = cp_parser_identifier (parser);
12649               else
12650                 identifier = NULL_TREE;
12651
12652               /* Consume the `:' token.  */
12653               cp_lexer_consume_token (parser->lexer);
12654               /* Get the width of the bitfield.  */
12655               width 
12656                 = cp_parser_constant_expression (parser,
12657                                                  /*allow_non_constant=*/false,
12658                                                  NULL);
12659
12660               /* Look for attributes that apply to the bitfield.  */
12661               attributes = cp_parser_attributes_opt (parser);
12662               /* Remember which attributes are prefix attributes and
12663                  which are not.  */
12664               first_attribute = attributes;
12665               /* Combine the attributes.  */
12666               attributes = chainon (prefix_attributes, attributes);
12667
12668               /* Create the bitfield declaration.  */
12669               decl = grokbitfield (identifier, 
12670                                    decl_specifiers,
12671                                    width);
12672               /* Apply the attributes.  */
12673               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12674             }
12675           else
12676             {
12677               tree declarator;
12678               tree initializer;
12679               tree asm_specification;
12680               int ctor_dtor_or_conv_p;
12681
12682               /* Parse the declarator.  */
12683               declarator 
12684                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12685                                         &ctor_dtor_or_conv_p,
12686                                         /*parenthesized_p=*/NULL,
12687                                         /*member_p=*/true);
12688
12689               /* If something went wrong parsing the declarator, make sure
12690                  that we at least consume some tokens.  */
12691               if (declarator == error_mark_node)
12692                 {
12693                   /* Skip to the end of the statement.  */
12694                   cp_parser_skip_to_end_of_statement (parser);
12695                   /* If the next token is not a semicolon, that is
12696                      probably because we just skipped over the body of
12697                      a function.  So, we consume a semicolon if
12698                      present, but do not issue an error message if it
12699                      is not present.  */
12700                   if (cp_lexer_next_token_is (parser->lexer,
12701                                               CPP_SEMICOLON))
12702                     cp_lexer_consume_token (parser->lexer);
12703                   return;
12704                 }
12705
12706               cp_parser_check_for_definition_in_return_type 
12707                 (declarator, declares_class_or_enum);
12708
12709               /* Look for an asm-specification.  */
12710               asm_specification = cp_parser_asm_specification_opt (parser);
12711               /* Look for attributes that apply to the declaration.  */
12712               attributes = cp_parser_attributes_opt (parser);
12713               /* Remember which attributes are prefix attributes and
12714                  which are not.  */
12715               first_attribute = attributes;
12716               /* Combine the attributes.  */
12717               attributes = chainon (prefix_attributes, attributes);
12718
12719               /* If it's an `=', then we have a constant-initializer or a
12720                  pure-specifier.  It is not correct to parse the
12721                  initializer before registering the member declaration
12722                  since the member declaration should be in scope while
12723                  its initializer is processed.  However, the rest of the
12724                  front end does not yet provide an interface that allows
12725                  us to handle this correctly.  */
12726               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12727                 {
12728                   /* In [class.mem]:
12729
12730                      A pure-specifier shall be used only in the declaration of
12731                      a virtual function.  
12732
12733                      A member-declarator can contain a constant-initializer
12734                      only if it declares a static member of integral or
12735                      enumeration type.  
12736
12737                      Therefore, if the DECLARATOR is for a function, we look
12738                      for a pure-specifier; otherwise, we look for a
12739                      constant-initializer.  When we call `grokfield', it will
12740                      perform more stringent semantics checks.  */
12741                   if (TREE_CODE (declarator) == CALL_EXPR)
12742                     initializer = cp_parser_pure_specifier (parser);
12743                   else
12744                     /* Parse the initializer.  */
12745                     initializer = cp_parser_constant_initializer (parser);
12746                 }
12747               /* Otherwise, there is no initializer.  */
12748               else
12749                 initializer = NULL_TREE;
12750
12751               /* See if we are probably looking at a function
12752                  definition.  We are certainly not looking at at a
12753                  member-declarator.  Calling `grokfield' has
12754                  side-effects, so we must not do it unless we are sure
12755                  that we are looking at a member-declarator.  */
12756               if (cp_parser_token_starts_function_definition_p 
12757                   (cp_lexer_peek_token (parser->lexer)))
12758                 {
12759                   /* The grammar does not allow a pure-specifier to be
12760                      used when a member function is defined.  (It is
12761                      possible that this fact is an oversight in the
12762                      standard, since a pure function may be defined
12763                      outside of the class-specifier.  */
12764                   if (initializer)
12765                     error ("pure-specifier on function-definition");
12766                   decl = cp_parser_save_member_function_body (parser,
12767                                                               decl_specifiers,
12768                                                               declarator,
12769                                                               attributes);
12770                   /* If the member was not a friend, declare it here.  */
12771                   if (!friend_p)
12772                     finish_member_declaration (decl);
12773                   /* Peek at the next token.  */
12774                   token = cp_lexer_peek_token (parser->lexer);
12775                   /* If the next token is a semicolon, consume it.  */
12776                   if (token->type == CPP_SEMICOLON)
12777                     cp_lexer_consume_token (parser->lexer);
12778                   return;
12779                 }
12780               else
12781                 {
12782                   /* Create the declaration.  */
12783                   decl = grokfield (declarator, decl_specifiers, 
12784                                     initializer, asm_specification,
12785                                     attributes);
12786                   /* Any initialization must have been from a
12787                      constant-expression.  */
12788                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12789                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12790                 }
12791             }
12792
12793           /* Reset PREFIX_ATTRIBUTES.  */
12794           while (attributes && TREE_CHAIN (attributes) != first_attribute)
12795             attributes = TREE_CHAIN (attributes);
12796           if (attributes)
12797             TREE_CHAIN (attributes) = NULL_TREE;
12798
12799           /* If there is any qualification still in effect, clear it
12800              now; we will be starting fresh with the next declarator.  */
12801           parser->scope = NULL_TREE;
12802           parser->qualifying_scope = NULL_TREE;
12803           parser->object_scope = NULL_TREE;
12804           /* If it's a `,', then there are more declarators.  */
12805           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12806             cp_lexer_consume_token (parser->lexer);
12807           /* If the next token isn't a `;', then we have a parse error.  */
12808           else if (cp_lexer_next_token_is_not (parser->lexer,
12809                                                CPP_SEMICOLON))
12810             {
12811               cp_parser_error (parser, "expected `;'");
12812               /* Skip tokens until we find a `;'.  */
12813               cp_parser_skip_to_end_of_statement (parser);
12814
12815               break;
12816             }
12817
12818           if (decl)
12819             {
12820               /* Add DECL to the list of members.  */
12821               if (!friend_p)
12822                 finish_member_declaration (decl);
12823
12824               if (TREE_CODE (decl) == FUNCTION_DECL)
12825                 cp_parser_save_default_args (parser, decl);
12826             }
12827         }
12828     }
12829
12830   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12831 }
12832
12833 /* Parse a pure-specifier.
12834
12835    pure-specifier:
12836      = 0
12837
12838    Returns INTEGER_ZERO_NODE if a pure specifier is found.
12839    Otherwise, ERROR_MARK_NODE is returned.  */
12840
12841 static tree
12842 cp_parser_pure_specifier (cp_parser* parser)
12843 {
12844   cp_token *token;
12845
12846   /* Look for the `=' token.  */
12847   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12848     return error_mark_node;
12849   /* Look for the `0' token.  */
12850   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12851   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
12852      to get information from the lexer about how the number was
12853      spelled in order to fix this problem.  */
12854   if (!token || !integer_zerop (token->value))
12855     return error_mark_node;
12856
12857   return integer_zero_node;
12858 }
12859
12860 /* Parse a constant-initializer.
12861
12862    constant-initializer:
12863      = constant-expression
12864
12865    Returns a representation of the constant-expression.  */
12866
12867 static tree
12868 cp_parser_constant_initializer (cp_parser* parser)
12869 {
12870   /* Look for the `=' token.  */
12871   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12872     return error_mark_node;
12873
12874   /* It is invalid to write:
12875
12876        struct S { static const int i = { 7 }; };
12877
12878      */
12879   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12880     {
12881       cp_parser_error (parser,
12882                        "a brace-enclosed initializer is not allowed here");
12883       /* Consume the opening brace.  */
12884       cp_lexer_consume_token (parser->lexer);
12885       /* Skip the initializer.  */
12886       cp_parser_skip_to_closing_brace (parser);
12887       /* Look for the trailing `}'.  */
12888       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12889       
12890       return error_mark_node;
12891     }
12892
12893   return cp_parser_constant_expression (parser, 
12894                                         /*allow_non_constant=*/false,
12895                                         NULL);
12896 }
12897
12898 /* Derived classes [gram.class.derived] */
12899
12900 /* Parse a base-clause.
12901
12902    base-clause:
12903      : base-specifier-list  
12904
12905    base-specifier-list:
12906      base-specifier
12907      base-specifier-list , base-specifier
12908
12909    Returns a TREE_LIST representing the base-classes, in the order in
12910    which they were declared.  The representation of each node is as
12911    described by cp_parser_base_specifier.  
12912
12913    In the case that no bases are specified, this function will return
12914    NULL_TREE, not ERROR_MARK_NODE.  */
12915
12916 static tree
12917 cp_parser_base_clause (cp_parser* parser)
12918 {
12919   tree bases = NULL_TREE;
12920
12921   /* Look for the `:' that begins the list.  */
12922   cp_parser_require (parser, CPP_COLON, "`:'");
12923
12924   /* Scan the base-specifier-list.  */
12925   while (true)
12926     {
12927       cp_token *token;
12928       tree base;
12929
12930       /* Look for the base-specifier.  */
12931       base = cp_parser_base_specifier (parser);
12932       /* Add BASE to the front of the list.  */
12933       if (base != error_mark_node)
12934         {
12935           TREE_CHAIN (base) = bases;
12936           bases = base;
12937         }
12938       /* Peek at the next token.  */
12939       token = cp_lexer_peek_token (parser->lexer);
12940       /* If it's not a comma, then the list is complete.  */
12941       if (token->type != CPP_COMMA)
12942         break;
12943       /* Consume the `,'.  */
12944       cp_lexer_consume_token (parser->lexer);
12945     }
12946
12947   /* PARSER->SCOPE may still be non-NULL at this point, if the last
12948      base class had a qualified name.  However, the next name that
12949      appears is certainly not qualified.  */
12950   parser->scope = NULL_TREE;
12951   parser->qualifying_scope = NULL_TREE;
12952   parser->object_scope = NULL_TREE;
12953
12954   return nreverse (bases);
12955 }
12956
12957 /* Parse a base-specifier.
12958
12959    base-specifier:
12960      :: [opt] nested-name-specifier [opt] class-name
12961      virtual access-specifier [opt] :: [opt] nested-name-specifier
12962        [opt] class-name
12963      access-specifier virtual [opt] :: [opt] nested-name-specifier
12964        [opt] class-name
12965
12966    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
12967    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12968    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
12969    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
12970        
12971 static tree
12972 cp_parser_base_specifier (cp_parser* parser)
12973 {
12974   cp_token *token;
12975   bool done = false;
12976   bool virtual_p = false;
12977   bool duplicate_virtual_error_issued_p = false;
12978   bool duplicate_access_error_issued_p = false;
12979   bool class_scope_p, template_p;
12980   tree access = access_default_node;
12981   tree type;
12982
12983   /* Process the optional `virtual' and `access-specifier'.  */
12984   while (!done)
12985     {
12986       /* Peek at the next token.  */
12987       token = cp_lexer_peek_token (parser->lexer);
12988       /* Process `virtual'.  */
12989       switch (token->keyword)
12990         {
12991         case RID_VIRTUAL:
12992           /* If `virtual' appears more than once, issue an error.  */
12993           if (virtual_p && !duplicate_virtual_error_issued_p)
12994             {
12995               cp_parser_error (parser,
12996                                "`virtual' specified more than once in base-specified");
12997               duplicate_virtual_error_issued_p = true;
12998             }
12999
13000           virtual_p = true;
13001
13002           /* Consume the `virtual' token.  */
13003           cp_lexer_consume_token (parser->lexer);
13004
13005           break;
13006
13007         case RID_PUBLIC:
13008         case RID_PROTECTED:
13009         case RID_PRIVATE:
13010           /* If more than one access specifier appears, issue an
13011              error.  */
13012           if (access != access_default_node
13013               && !duplicate_access_error_issued_p)
13014             {
13015               cp_parser_error (parser,
13016                                "more than one access specifier in base-specified");
13017               duplicate_access_error_issued_p = true;
13018             }
13019
13020           access = ridpointers[(int) token->keyword];
13021
13022           /* Consume the access-specifier.  */
13023           cp_lexer_consume_token (parser->lexer);
13024
13025           break;
13026
13027         default:
13028           done = true;
13029           break;
13030         }
13031     }
13032   /* It is not uncommon to see programs mechanically, errouneously, use
13033      the 'typename' keyword to denote (dependent) qualified types
13034      as base classes.  */
13035   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13036     {
13037       if (!processing_template_decl)
13038         error ("keyword `typename' not allowed outside of templates");
13039       else
13040         error ("keyword `typename' not allowed in this context "
13041                "(the base class is implicitly a type)");
13042       cp_lexer_consume_token (parser->lexer);
13043     }
13044
13045   /* Look for the optional `::' operator.  */
13046   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13047   /* Look for the nested-name-specifier.  The simplest way to
13048      implement:
13049
13050        [temp.res]
13051
13052        The keyword `typename' is not permitted in a base-specifier or
13053        mem-initializer; in these contexts a qualified name that
13054        depends on a template-parameter is implicitly assumed to be a
13055        type name.
13056
13057      is to pretend that we have seen the `typename' keyword at this
13058      point.  */ 
13059   cp_parser_nested_name_specifier_opt (parser,
13060                                        /*typename_keyword_p=*/true,
13061                                        /*check_dependency_p=*/true,
13062                                        /*type_p=*/true,
13063                                        /*is_declaration=*/true);
13064   /* If the base class is given by a qualified name, assume that names
13065      we see are type names or templates, as appropriate.  */
13066   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13067   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13068   
13069   /* Finally, look for the class-name.  */
13070   type = cp_parser_class_name (parser, 
13071                                class_scope_p,
13072                                template_p,
13073                                /*type_p=*/true,
13074                                /*check_dependency_p=*/true,
13075                                /*class_head_p=*/false,
13076                                /*is_declaration=*/true);
13077
13078   if (type == error_mark_node)
13079     return error_mark_node;
13080
13081   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13082 }
13083
13084 /* Exception handling [gram.exception] */
13085
13086 /* Parse an (optional) exception-specification.
13087
13088    exception-specification:
13089      throw ( type-id-list [opt] )
13090
13091    Returns a TREE_LIST representing the exception-specification.  The
13092    TREE_VALUE of each node is a type.  */
13093
13094 static tree
13095 cp_parser_exception_specification_opt (cp_parser* parser)
13096 {
13097   cp_token *token;
13098   tree type_id_list;
13099
13100   /* Peek at the next token.  */
13101   token = cp_lexer_peek_token (parser->lexer);
13102   /* If it's not `throw', then there's no exception-specification.  */
13103   if (!cp_parser_is_keyword (token, RID_THROW))
13104     return NULL_TREE;
13105
13106   /* Consume the `throw'.  */
13107   cp_lexer_consume_token (parser->lexer);
13108
13109   /* Look for the `('.  */
13110   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13111
13112   /* Peek at the next token.  */
13113   token = cp_lexer_peek_token (parser->lexer);
13114   /* If it's not a `)', then there is a type-id-list.  */
13115   if (token->type != CPP_CLOSE_PAREN)
13116     {
13117       const char *saved_message;
13118
13119       /* Types may not be defined in an exception-specification.  */
13120       saved_message = parser->type_definition_forbidden_message;
13121       parser->type_definition_forbidden_message
13122         = "types may not be defined in an exception-specification";
13123       /* Parse the type-id-list.  */
13124       type_id_list = cp_parser_type_id_list (parser);
13125       /* Restore the saved message.  */
13126       parser->type_definition_forbidden_message = saved_message;
13127     }
13128   else
13129     type_id_list = empty_except_spec;
13130
13131   /* Look for the `)'.  */
13132   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13133
13134   return type_id_list;
13135 }
13136
13137 /* Parse an (optional) type-id-list.
13138
13139    type-id-list:
13140      type-id
13141      type-id-list , type-id
13142
13143    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13144    in the order that the types were presented.  */
13145
13146 static tree
13147 cp_parser_type_id_list (cp_parser* parser)
13148 {
13149   tree types = NULL_TREE;
13150
13151   while (true)
13152     {
13153       cp_token *token;
13154       tree type;
13155
13156       /* Get the next type-id.  */
13157       type = cp_parser_type_id (parser);
13158       /* Add it to the list.  */
13159       types = add_exception_specifier (types, type, /*complain=*/1);
13160       /* Peek at the next token.  */
13161       token = cp_lexer_peek_token (parser->lexer);
13162       /* If it is not a `,', we are done.  */
13163       if (token->type != CPP_COMMA)
13164         break;
13165       /* Consume the `,'.  */
13166       cp_lexer_consume_token (parser->lexer);
13167     }
13168
13169   return nreverse (types);
13170 }
13171
13172 /* Parse a try-block.
13173
13174    try-block:
13175      try compound-statement handler-seq  */
13176
13177 static tree
13178 cp_parser_try_block (cp_parser* parser)
13179 {
13180   tree try_block;
13181
13182   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13183   try_block = begin_try_block ();
13184   cp_parser_compound_statement (parser, false);
13185   finish_try_block (try_block);
13186   cp_parser_handler_seq (parser);
13187   finish_handler_sequence (try_block);
13188
13189   return try_block;
13190 }
13191
13192 /* Parse a function-try-block.
13193
13194    function-try-block:
13195      try ctor-initializer [opt] function-body handler-seq  */
13196
13197 static bool
13198 cp_parser_function_try_block (cp_parser* parser)
13199 {
13200   tree try_block;
13201   bool ctor_initializer_p;
13202
13203   /* Look for the `try' keyword.  */
13204   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13205     return false;
13206   /* Let the rest of the front-end know where we are.  */
13207   try_block = begin_function_try_block ();
13208   /* Parse the function-body.  */
13209   ctor_initializer_p 
13210     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13211   /* We're done with the `try' part.  */
13212   finish_function_try_block (try_block);
13213   /* Parse the handlers.  */
13214   cp_parser_handler_seq (parser);
13215   /* We're done with the handlers.  */
13216   finish_function_handler_sequence (try_block);
13217
13218   return ctor_initializer_p;
13219 }
13220
13221 /* Parse a handler-seq.
13222
13223    handler-seq:
13224      handler handler-seq [opt]  */
13225
13226 static void
13227 cp_parser_handler_seq (cp_parser* parser)
13228 {
13229   while (true)
13230     {
13231       cp_token *token;
13232
13233       /* Parse the handler.  */
13234       cp_parser_handler (parser);
13235       /* Peek at the next token.  */
13236       token = cp_lexer_peek_token (parser->lexer);
13237       /* If it's not `catch' then there are no more handlers.  */
13238       if (!cp_parser_is_keyword (token, RID_CATCH))
13239         break;
13240     }
13241 }
13242
13243 /* Parse a handler.
13244
13245    handler:
13246      catch ( exception-declaration ) compound-statement  */
13247
13248 static void
13249 cp_parser_handler (cp_parser* parser)
13250 {
13251   tree handler;
13252   tree declaration;
13253
13254   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13255   handler = begin_handler ();
13256   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13257   declaration = cp_parser_exception_declaration (parser);
13258   finish_handler_parms (declaration, handler);
13259   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13260   cp_parser_compound_statement (parser, false);
13261   finish_handler (handler);
13262 }
13263
13264 /* Parse an exception-declaration.
13265
13266    exception-declaration:
13267      type-specifier-seq declarator
13268      type-specifier-seq abstract-declarator
13269      type-specifier-seq
13270      ...  
13271
13272    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13273    ellipsis variant is used.  */
13274
13275 static tree
13276 cp_parser_exception_declaration (cp_parser* parser)
13277 {
13278   tree type_specifiers;
13279   tree declarator;
13280   const char *saved_message;
13281
13282   /* If it's an ellipsis, it's easy to handle.  */
13283   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13284     {
13285       /* Consume the `...' token.  */
13286       cp_lexer_consume_token (parser->lexer);
13287       return NULL_TREE;
13288     }
13289
13290   /* Types may not be defined in exception-declarations.  */
13291   saved_message = parser->type_definition_forbidden_message;
13292   parser->type_definition_forbidden_message
13293     = "types may not be defined in exception-declarations";
13294
13295   /* Parse the type-specifier-seq.  */
13296   type_specifiers = cp_parser_type_specifier_seq (parser);
13297   /* If it's a `)', then there is no declarator.  */
13298   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13299     declarator = NULL_TREE;
13300   else
13301     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13302                                        /*ctor_dtor_or_conv_p=*/NULL,
13303                                        /*parenthesized_p=*/NULL,
13304                                        /*member_p=*/false);
13305
13306   /* Restore the saved message.  */
13307   parser->type_definition_forbidden_message = saved_message;
13308
13309   return start_handler_parms (type_specifiers, declarator);
13310 }
13311
13312 /* Parse a throw-expression. 
13313
13314    throw-expression:
13315      throw assignment-expression [opt]
13316
13317    Returns a THROW_EXPR representing the throw-expression.  */
13318
13319 static tree
13320 cp_parser_throw_expression (cp_parser* parser)
13321 {
13322   tree expression;
13323   cp_token* token;
13324
13325   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13326   token = cp_lexer_peek_token (parser->lexer);
13327   /* Figure out whether or not there is an assignment-expression
13328      following the "throw" keyword.  */
13329   if (token->type == CPP_COMMA
13330       || token->type == CPP_SEMICOLON
13331       || token->type == CPP_CLOSE_PAREN
13332       || token->type == CPP_CLOSE_SQUARE
13333       || token->type == CPP_CLOSE_BRACE
13334       || token->type == CPP_COLON)
13335     expression = NULL_TREE;
13336   else
13337     expression = cp_parser_assignment_expression (parser);
13338
13339   return build_throw (expression);
13340 }
13341
13342 /* GNU Extensions */
13343
13344 /* Parse an (optional) asm-specification.
13345
13346    asm-specification:
13347      asm ( string-literal )
13348
13349    If the asm-specification is present, returns a STRING_CST
13350    corresponding to the string-literal.  Otherwise, returns
13351    NULL_TREE.  */
13352
13353 static tree
13354 cp_parser_asm_specification_opt (cp_parser* parser)
13355 {
13356   cp_token *token;
13357   tree asm_specification;
13358
13359   /* Peek at the next token.  */
13360   token = cp_lexer_peek_token (parser->lexer);
13361   /* If the next token isn't the `asm' keyword, then there's no 
13362      asm-specification.  */
13363   if (!cp_parser_is_keyword (token, RID_ASM))
13364     return NULL_TREE;
13365
13366   /* Consume the `asm' token.  */
13367   cp_lexer_consume_token (parser->lexer);
13368   /* Look for the `('.  */
13369   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13370
13371   /* Look for the string-literal.  */
13372   token = cp_parser_require (parser, CPP_STRING, "string-literal");
13373   if (token)
13374     asm_specification = token->value;
13375   else
13376     asm_specification = NULL_TREE;
13377
13378   /* Look for the `)'.  */
13379   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13380
13381   return asm_specification;
13382 }
13383
13384 /* Parse an asm-operand-list.  
13385
13386    asm-operand-list:
13387      asm-operand
13388      asm-operand-list , asm-operand
13389      
13390    asm-operand:
13391      string-literal ( expression )  
13392      [ string-literal ] string-literal ( expression )
13393
13394    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13395    each node is the expression.  The TREE_PURPOSE is itself a
13396    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13397    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13398    is a STRING_CST for the string literal before the parenthesis.  */
13399
13400 static tree
13401 cp_parser_asm_operand_list (cp_parser* parser)
13402 {
13403   tree asm_operands = NULL_TREE;
13404
13405   while (true)
13406     {
13407       tree string_literal;
13408       tree expression;
13409       tree name;
13410       cp_token *token;
13411       
13412       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 
13413         {
13414           /* Consume the `[' token.  */
13415           cp_lexer_consume_token (parser->lexer);
13416           /* Read the operand name.  */
13417           name = cp_parser_identifier (parser);
13418           if (name != error_mark_node) 
13419             name = build_string (IDENTIFIER_LENGTH (name),
13420                                  IDENTIFIER_POINTER (name));
13421           /* Look for the closing `]'.  */
13422           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13423         }
13424       else
13425         name = NULL_TREE;
13426       /* Look for the string-literal.  */
13427       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13428       string_literal = token ? token->value : error_mark_node;
13429       /* Look for the `('.  */
13430       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13431       /* Parse the expression.  */
13432       expression = cp_parser_expression (parser);
13433       /* Look for the `)'.  */
13434       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13435       /* Add this operand to the list.  */
13436       asm_operands = tree_cons (build_tree_list (name, string_literal),
13437                                 expression, 
13438                                 asm_operands);
13439       /* If the next token is not a `,', there are no more 
13440          operands.  */
13441       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13442         break;
13443       /* Consume the `,'.  */
13444       cp_lexer_consume_token (parser->lexer);
13445     }
13446
13447   return nreverse (asm_operands);
13448 }
13449
13450 /* Parse an asm-clobber-list.  
13451
13452    asm-clobber-list:
13453      string-literal
13454      asm-clobber-list , string-literal  
13455
13456    Returns a TREE_LIST, indicating the clobbers in the order that they
13457    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13458
13459 static tree
13460 cp_parser_asm_clobber_list (cp_parser* parser)
13461 {
13462   tree clobbers = NULL_TREE;
13463
13464   while (true)
13465     {
13466       cp_token *token;
13467       tree string_literal;
13468
13469       /* Look for the string literal.  */
13470       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13471       string_literal = token ? token->value : error_mark_node;
13472       /* Add it to the list.  */
13473       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13474       /* If the next token is not a `,', then the list is 
13475          complete.  */
13476       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13477         break;
13478       /* Consume the `,' token.  */
13479       cp_lexer_consume_token (parser->lexer);
13480     }
13481
13482   return clobbers;
13483 }
13484
13485 /* Parse an (optional) series of attributes.
13486
13487    attributes:
13488      attributes attribute
13489
13490    attribute:
13491      __attribute__ (( attribute-list [opt] ))  
13492
13493    The return value is as for cp_parser_attribute_list.  */
13494      
13495 static tree
13496 cp_parser_attributes_opt (cp_parser* parser)
13497 {
13498   tree attributes = NULL_TREE;
13499
13500   while (true)
13501     {
13502       cp_token *token;
13503       tree attribute_list;
13504
13505       /* Peek at the next token.  */
13506       token = cp_lexer_peek_token (parser->lexer);
13507       /* If it's not `__attribute__', then we're done.  */
13508       if (token->keyword != RID_ATTRIBUTE)
13509         break;
13510
13511       /* Consume the `__attribute__' keyword.  */
13512       cp_lexer_consume_token (parser->lexer);
13513       /* Look for the two `(' tokens.  */
13514       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13515       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13516
13517       /* Peek at the next token.  */
13518       token = cp_lexer_peek_token (parser->lexer);
13519       if (token->type != CPP_CLOSE_PAREN)
13520         /* Parse the attribute-list.  */
13521         attribute_list = cp_parser_attribute_list (parser);
13522       else
13523         /* If the next token is a `)', then there is no attribute
13524            list.  */
13525         attribute_list = NULL;
13526
13527       /* Look for the two `)' tokens.  */
13528       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13529       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13530
13531       /* Add these new attributes to the list.  */
13532       attributes = chainon (attributes, attribute_list);
13533     }
13534
13535   return attributes;
13536 }
13537
13538 /* Parse an attribute-list.  
13539
13540    attribute-list:  
13541      attribute 
13542      attribute-list , attribute
13543
13544    attribute:
13545      identifier     
13546      identifier ( identifier )
13547      identifier ( identifier , expression-list )
13548      identifier ( expression-list ) 
13549
13550    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13551    TREE_PURPOSE of each node is the identifier indicating which
13552    attribute is in use.  The TREE_VALUE represents the arguments, if
13553    any.  */
13554
13555 static tree
13556 cp_parser_attribute_list (cp_parser* parser)
13557 {
13558   tree attribute_list = NULL_TREE;
13559
13560   while (true)
13561     {
13562       cp_token *token;
13563       tree identifier;
13564       tree attribute;
13565
13566       /* Look for the identifier.  We also allow keywords here; for
13567          example `__attribute__ ((const))' is legal.  */
13568       token = cp_lexer_peek_token (parser->lexer);
13569       if (token->type == CPP_NAME 
13570           || token->type == CPP_KEYWORD)
13571         {
13572           /* Consume the token.  */
13573           token = cp_lexer_consume_token (parser->lexer);
13574
13575           /* Save away the identifier that indicates which attribute 
13576              this is.  */
13577           identifier = token->value;
13578           attribute = build_tree_list (identifier, NULL_TREE);
13579
13580           /* Peek at the next token.  */
13581           token = cp_lexer_peek_token (parser->lexer);
13582           /* If it's an `(', then parse the attribute arguments.  */
13583           if (token->type == CPP_OPEN_PAREN)
13584             {
13585               tree arguments;
13586
13587               arguments = (cp_parser_parenthesized_expression_list 
13588                            (parser, true, /*non_constant_p=*/NULL));
13589               /* Save the identifier and arguments away.  */
13590               TREE_VALUE (attribute) = arguments;
13591             }
13592
13593           /* Add this attribute to the list.  */
13594           TREE_CHAIN (attribute) = attribute_list;
13595           attribute_list = attribute;
13596
13597           /* Now, look for more attributes.  */
13598           token = cp_lexer_peek_token (parser->lexer);
13599         }
13600       /* Now, look for more attributes.  If the next token isn't a
13601          `,', we're done.  */
13602       if (token->type != CPP_COMMA)
13603         break;
13604
13605       /* Consume the comma and keep going.  */
13606       cp_lexer_consume_token (parser->lexer);
13607     }
13608
13609   /* We built up the list in reverse order.  */
13610   return nreverse (attribute_list);
13611 }
13612
13613 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13614    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13615    current value of the PEDANTIC flag, regardless of whether or not
13616    the `__extension__' keyword is present.  The caller is responsible
13617    for restoring the value of the PEDANTIC flag.  */
13618
13619 static bool
13620 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13621 {
13622   /* Save the old value of the PEDANTIC flag.  */
13623   *saved_pedantic = pedantic;
13624
13625   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13626     {
13627       /* Consume the `__extension__' token.  */
13628       cp_lexer_consume_token (parser->lexer);
13629       /* We're not being pedantic while the `__extension__' keyword is
13630          in effect.  */
13631       pedantic = 0;
13632
13633       return true;
13634     }
13635
13636   return false;
13637 }
13638
13639 /* Parse a label declaration.
13640
13641    label-declaration:
13642      __label__ label-declarator-seq ;
13643
13644    label-declarator-seq:
13645      identifier , label-declarator-seq
13646      identifier  */
13647
13648 static void
13649 cp_parser_label_declaration (cp_parser* parser)
13650 {
13651   /* Look for the `__label__' keyword.  */
13652   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13653
13654   while (true)
13655     {
13656       tree identifier;
13657
13658       /* Look for an identifier.  */
13659       identifier = cp_parser_identifier (parser);
13660       /* If we failed, stop.  */
13661       if (identifier == error_mark_node)
13662         break;
13663       /* Declare it as a label.  */
13664       finish_label_decl (identifier);
13665       /* If the next token is a `;', stop.  */
13666       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13667         break;
13668       /* Look for the `,' separating the label declarations.  */
13669       cp_parser_require (parser, CPP_COMMA, "`,'");
13670     }
13671
13672   /* Look for the final `;'.  */
13673   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13674 }
13675
13676 /* Support Functions */
13677
13678 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13679    NAME should have one of the representations used for an
13680    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13681    is returned.  If PARSER->SCOPE is a dependent type, then a
13682    SCOPE_REF is returned.
13683
13684    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13685    returned; the name was already resolved when the TEMPLATE_ID_EXPR
13686    was formed.  Abstractly, such entities should not be passed to this
13687    function, because they do not need to be looked up, but it is
13688    simpler to check for this special case here, rather than at the
13689    call-sites.
13690
13691    In cases not explicitly covered above, this function returns a
13692    DECL, OVERLOAD, or baselink representing the result of the lookup.
13693    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13694    is returned.
13695
13696    If IS_TYPE is TRUE, bindings that do not refer to types are
13697    ignored.
13698
13699    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13700    ignored.
13701
13702    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13703    are ignored.
13704
13705    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13706    types.  */
13707
13708 static tree
13709 cp_parser_lookup_name (cp_parser *parser, tree name, 
13710                        bool is_type, bool is_template, bool is_namespace,
13711                        bool check_dependency)
13712 {
13713   tree decl;
13714   tree object_type = parser->context->object_type;
13715
13716   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13717      no longer valid.  Note that if we are parsing tentatively, and
13718      the parse fails, OBJECT_TYPE will be automatically restored.  */
13719   parser->context->object_type = NULL_TREE;
13720
13721   if (name == error_mark_node)
13722     return error_mark_node;
13723
13724   /* A template-id has already been resolved; there is no lookup to
13725      do.  */
13726   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13727     return name;
13728   if (BASELINK_P (name))
13729     {
13730       my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13731                            == TEMPLATE_ID_EXPR),
13732                           20020909);
13733       return name;
13734     }
13735
13736   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
13737      it should already have been checked to make sure that the name
13738      used matches the type being destroyed.  */
13739   if (TREE_CODE (name) == BIT_NOT_EXPR)
13740     {
13741       tree type;
13742
13743       /* Figure out to which type this destructor applies.  */
13744       if (parser->scope)
13745         type = parser->scope;
13746       else if (object_type)
13747         type = object_type;
13748       else
13749         type = current_class_type;
13750       /* If that's not a class type, there is no destructor.  */
13751       if (!type || !CLASS_TYPE_P (type))
13752         return error_mark_node;
13753       if (!CLASSTYPE_DESTRUCTORS (type))
13754           return error_mark_node;
13755       /* If it was a class type, return the destructor.  */
13756       return CLASSTYPE_DESTRUCTORS (type);
13757     }
13758
13759   /* By this point, the NAME should be an ordinary identifier.  If
13760      the id-expression was a qualified name, the qualifying scope is
13761      stored in PARSER->SCOPE at this point.  */
13762   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13763                       20000619);
13764   
13765   /* Perform the lookup.  */
13766   if (parser->scope)
13767     { 
13768       bool dependent_p;
13769
13770       if (parser->scope == error_mark_node)
13771         return error_mark_node;
13772
13773       /* If the SCOPE is dependent, the lookup must be deferred until
13774          the template is instantiated -- unless we are explicitly
13775          looking up names in uninstantiated templates.  Even then, we
13776          cannot look up the name if the scope is not a class type; it
13777          might, for example, be a template type parameter.  */
13778       dependent_p = (TYPE_P (parser->scope)
13779                      && !(parser->in_declarator_p
13780                           && currently_open_class (parser->scope))
13781                      && dependent_type_p (parser->scope));
13782       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13783            && dependent_p)
13784         {
13785           if (is_type)
13786             /* The resolution to Core Issue 180 says that `struct A::B'
13787                should be considered a type-name, even if `A' is
13788                dependent.  */
13789             decl = TYPE_NAME (make_typename_type (parser->scope,
13790                                                   name,
13791                                                   /*complain=*/1));
13792           else if (is_template)
13793             decl = make_unbound_class_template (parser->scope,
13794                                                 name,
13795                                                 /*complain=*/1);
13796           else
13797             decl = build_nt (SCOPE_REF, parser->scope, name);
13798         }
13799       else
13800         {
13801           bool pop_p = false;
13802
13803           /* If PARSER->SCOPE is a dependent type, then it must be a
13804              class type, and we must not be checking dependencies;
13805              otherwise, we would have processed this lookup above.  So
13806              that PARSER->SCOPE is not considered a dependent base by
13807              lookup_member, we must enter the scope here.  */
13808           if (dependent_p)
13809             pop_p = push_scope (parser->scope);
13810           /* If the PARSER->SCOPE is a a template specialization, it
13811              may be instantiated during name lookup.  In that case,
13812              errors may be issued.  Even if we rollback the current
13813              tentative parse, those errors are valid.  */
13814           decl = lookup_qualified_name (parser->scope, name, is_type,
13815                                         /*complain=*/true);
13816           if (pop_p)
13817             pop_scope (parser->scope);
13818         }
13819       parser->qualifying_scope = parser->scope;
13820       parser->object_scope = NULL_TREE;
13821     }
13822   else if (object_type)
13823     {
13824       tree object_decl = NULL_TREE;
13825       /* Look up the name in the scope of the OBJECT_TYPE, unless the
13826          OBJECT_TYPE is not a class.  */
13827       if (CLASS_TYPE_P (object_type))
13828         /* If the OBJECT_TYPE is a template specialization, it may
13829            be instantiated during name lookup.  In that case, errors
13830            may be issued.  Even if we rollback the current tentative
13831            parse, those errors are valid.  */
13832         object_decl = lookup_member (object_type,
13833                                      name,
13834                                      /*protect=*/0, is_type);
13835       /* Look it up in the enclosing context, too.  */
13836       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13837                                is_namespace,
13838                                /*flags=*/0);
13839       parser->object_scope = object_type;
13840       parser->qualifying_scope = NULL_TREE;
13841       if (object_decl)
13842         decl = object_decl;
13843     }
13844   else
13845     {
13846       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13847                                is_namespace,
13848                                /*flags=*/0);
13849       parser->qualifying_scope = NULL_TREE;
13850       parser->object_scope = NULL_TREE;
13851     }
13852
13853   /* If the lookup failed, let our caller know.  */
13854   if (!decl 
13855       || decl == error_mark_node
13856       || (TREE_CODE (decl) == FUNCTION_DECL 
13857           && DECL_ANTICIPATED (decl)))
13858     return error_mark_node;
13859
13860   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
13861   if (TREE_CODE (decl) == TREE_LIST)
13862     {
13863       /* The error message we have to print is too complicated for
13864          cp_parser_error, so we incorporate its actions directly.  */
13865       if (!cp_parser_simulate_error (parser))
13866         {
13867           error ("reference to `%D' is ambiguous", name);
13868           print_candidates (decl);
13869         }
13870       return error_mark_node;
13871     }
13872
13873   my_friendly_assert (DECL_P (decl) 
13874                       || TREE_CODE (decl) == OVERLOAD
13875                       || TREE_CODE (decl) == SCOPE_REF
13876                       || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13877                       || BASELINK_P (decl),
13878                       20000619);
13879
13880   /* If we have resolved the name of a member declaration, check to
13881      see if the declaration is accessible.  When the name resolves to
13882      set of overloaded functions, accessibility is checked when
13883      overload resolution is done.  
13884
13885      During an explicit instantiation, access is not checked at all,
13886      as per [temp.explicit].  */
13887   if (DECL_P (decl))
13888     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13889
13890   return decl;
13891 }
13892
13893 /* Like cp_parser_lookup_name, but for use in the typical case where
13894    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13895    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
13896
13897 static tree
13898 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13899 {
13900   return cp_parser_lookup_name (parser, name, 
13901                                 /*is_type=*/false,
13902                                 /*is_template=*/false,
13903                                 /*is_namespace=*/false,
13904                                 /*check_dependency=*/true);
13905 }
13906
13907 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13908    the current context, return the TYPE_DECL.  If TAG_NAME_P is
13909    true, the DECL indicates the class being defined in a class-head,
13910    or declared in an elaborated-type-specifier.
13911
13912    Otherwise, return DECL.  */
13913
13914 static tree
13915 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13916 {
13917   /* If the TEMPLATE_DECL is being declared as part of a class-head,
13918      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13919
13920        struct A { 
13921          template <typename T> struct B;
13922        };
13923
13924        template <typename T> struct A::B {}; 
13925    
13926      Similarly, in a elaborated-type-specifier:
13927
13928        namespace N { struct X{}; }
13929
13930        struct A {
13931          template <typename T> friend struct N::X;
13932        };
13933
13934      However, if the DECL refers to a class type, and we are in
13935      the scope of the class, then the name lookup automatically
13936      finds the TYPE_DECL created by build_self_reference rather
13937      than a TEMPLATE_DECL.  For example, in:
13938
13939        template <class T> struct S {
13940          S s;
13941        };
13942
13943      there is no need to handle such case.  */
13944
13945   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13946     return DECL_TEMPLATE_RESULT (decl);
13947
13948   return decl;
13949 }
13950
13951 /* If too many, or too few, template-parameter lists apply to the
13952    declarator, issue an error message.  Returns TRUE if all went well,
13953    and FALSE otherwise.  */
13954
13955 static bool
13956 cp_parser_check_declarator_template_parameters (cp_parser* parser, 
13957                                                 tree declarator)
13958 {
13959   unsigned num_templates;
13960
13961   /* We haven't seen any classes that involve template parameters yet.  */
13962   num_templates = 0;
13963
13964   switch (TREE_CODE (declarator))
13965     {
13966     case CALL_EXPR:
13967     case ARRAY_REF:
13968     case INDIRECT_REF:
13969     case ADDR_EXPR:
13970       {
13971         tree main_declarator = TREE_OPERAND (declarator, 0);
13972         return
13973           cp_parser_check_declarator_template_parameters (parser, 
13974                                                           main_declarator);
13975       }
13976
13977     case SCOPE_REF:
13978       {
13979         tree scope;
13980         tree member;
13981
13982         scope = TREE_OPERAND (declarator, 0);
13983         member = TREE_OPERAND (declarator, 1);
13984
13985         /* If this is a pointer-to-member, then we are not interested
13986            in the SCOPE, because it does not qualify the thing that is
13987            being declared.  */
13988         if (TREE_CODE (member) == INDIRECT_REF)
13989           return (cp_parser_check_declarator_template_parameters
13990                   (parser, member));
13991
13992         while (scope && CLASS_TYPE_P (scope))
13993           {
13994             /* You're supposed to have one `template <...>'
13995                for every template class, but you don't need one
13996                for a full specialization.  For example:
13997                
13998                template <class T> struct S{};
13999                template <> struct S<int> { void f(); };
14000                void S<int>::f () {}
14001                
14002                is correct; there shouldn't be a `template <>' for
14003                the definition of `S<int>::f'.  */
14004             if (CLASSTYPE_TEMPLATE_INFO (scope)
14005                 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14006                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14007                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14008               ++num_templates;
14009
14010             scope = TYPE_CONTEXT (scope);
14011           }
14012       }
14013
14014       /* Fall through.  */
14015
14016     default:
14017       /* If the DECLARATOR has the form `X<y>' then it uses one
14018          additional level of template parameters.  */
14019       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
14020         ++num_templates;
14021
14022       return cp_parser_check_template_parameters (parser, 
14023                                                   num_templates);
14024     }
14025 }
14026
14027 /* NUM_TEMPLATES were used in the current declaration.  If that is
14028    invalid, return FALSE and issue an error messages.  Otherwise,
14029    return TRUE.  */
14030
14031 static bool
14032 cp_parser_check_template_parameters (cp_parser* parser,
14033                                      unsigned num_templates)
14034 {
14035   /* If there are more template classes than parameter lists, we have
14036      something like:
14037      
14038        template <class T> void S<T>::R<T>::f ();  */
14039   if (parser->num_template_parameter_lists < num_templates)
14040     {
14041       error ("too few template-parameter-lists");
14042       return false;
14043     }
14044   /* If there are the same number of template classes and parameter
14045      lists, that's OK.  */
14046   if (parser->num_template_parameter_lists == num_templates)
14047     return true;
14048   /* If there are more, but only one more, then we are referring to a
14049      member template.  That's OK too.  */
14050   if (parser->num_template_parameter_lists == num_templates + 1)
14051       return true;
14052   /* Otherwise, there are too many template parameter lists.  We have
14053      something like:
14054
14055      template <class T> template <class U> void S::f();  */
14056   error ("too many template-parameter-lists");
14057   return false;
14058 }
14059
14060 /* Parse a binary-expression of the general form:
14061
14062    binary-expression:
14063      <expr>
14064      binary-expression <token> <expr>
14065
14066    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
14067    to parser the <expr>s.  If the first production is used, then the
14068    value returned by FN is returned directly.  Otherwise, a node with
14069    the indicated EXPR_TYPE is returned, with operands corresponding to
14070    the two sub-expressions.  */
14071
14072 static tree
14073 cp_parser_binary_expression (cp_parser* parser, 
14074                              const cp_parser_token_tree_map token_tree_map, 
14075                              cp_parser_expression_fn fn)
14076 {
14077   tree lhs;
14078
14079   /* Parse the first expression.  */
14080   lhs = (*fn) (parser);
14081   /* Now, look for more expressions.  */
14082   while (true)
14083     {
14084       cp_token *token;
14085       const cp_parser_token_tree_map_node *map_node;
14086       tree rhs;
14087
14088       /* Peek at the next token.  */
14089       token = cp_lexer_peek_token (parser->lexer);
14090       /* If the token is `>', and that's not an operator at the
14091          moment, then we're done.  */
14092       if (token->type == CPP_GREATER
14093           && !parser->greater_than_is_operator_p)
14094         break;
14095       /* If we find one of the tokens we want, build the corresponding
14096          tree representation.  */
14097       for (map_node = token_tree_map; 
14098            map_node->token_type != CPP_EOF;
14099            ++map_node)
14100         if (map_node->token_type == token->type)
14101           {
14102             /* Assume that an overloaded operator will not be used.  */
14103             bool overloaded_p = false;
14104
14105             /* Consume the operator token.  */
14106             cp_lexer_consume_token (parser->lexer);
14107             /* Parse the right-hand side of the expression.  */
14108             rhs = (*fn) (parser);
14109             /* Build the binary tree node.  */
14110             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs, 
14111                                      &overloaded_p);
14112             /* If the binary operator required the use of an
14113                overloaded operator, then this expression cannot be an
14114                integral constant-expression.  An overloaded operator
14115                can be used even if both operands are otherwise
14116                permissible in an integral constant-expression if at
14117                least one of the operands is of enumeration type.  */
14118             if (overloaded_p
14119                 && (cp_parser_non_integral_constant_expression 
14120                     (parser, "calls to overloaded operators")))
14121               lhs = error_mark_node;
14122             break;
14123           }
14124
14125       /* If the token wasn't one of the ones we want, we're done.  */
14126       if (map_node->token_type == CPP_EOF)
14127         break;
14128     }
14129
14130   return lhs;
14131 }
14132
14133 /* Parse an optional `::' token indicating that the following name is
14134    from the global namespace.  If so, PARSER->SCOPE is set to the
14135    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14136    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14137    Returns the new value of PARSER->SCOPE, if the `::' token is
14138    present, and NULL_TREE otherwise.  */
14139
14140 static tree
14141 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14142 {
14143   cp_token *token;
14144
14145   /* Peek at the next token.  */
14146   token = cp_lexer_peek_token (parser->lexer);
14147   /* If we're looking at a `::' token then we're starting from the
14148      global namespace, not our current location.  */
14149   if (token->type == CPP_SCOPE)
14150     {
14151       /* Consume the `::' token.  */
14152       cp_lexer_consume_token (parser->lexer);
14153       /* Set the SCOPE so that we know where to start the lookup.  */
14154       parser->scope = global_namespace;
14155       parser->qualifying_scope = global_namespace;
14156       parser->object_scope = NULL_TREE;
14157
14158       return parser->scope;
14159     }
14160   else if (!current_scope_valid_p)
14161     {
14162       parser->scope = NULL_TREE;
14163       parser->qualifying_scope = NULL_TREE;
14164       parser->object_scope = NULL_TREE;
14165     }
14166
14167   return NULL_TREE;
14168 }
14169
14170 /* Returns TRUE if the upcoming token sequence is the start of a
14171    constructor declarator.  If FRIEND_P is true, the declarator is
14172    preceded by the `friend' specifier.  */
14173
14174 static bool
14175 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14176 {
14177   bool constructor_p;
14178   tree type_decl = NULL_TREE;
14179   bool nested_name_p;
14180   cp_token *next_token;
14181
14182   /* The common case is that this is not a constructor declarator, so
14183      try to avoid doing lots of work if at all possible.  It's not
14184      valid declare a constructor at function scope.  */
14185   if (at_function_scope_p ())
14186     return false;
14187   /* And only certain tokens can begin a constructor declarator.  */
14188   next_token = cp_lexer_peek_token (parser->lexer);
14189   if (next_token->type != CPP_NAME
14190       && next_token->type != CPP_SCOPE
14191       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14192       && next_token->type != CPP_TEMPLATE_ID)
14193     return false;
14194
14195   /* Parse tentatively; we are going to roll back all of the tokens
14196      consumed here.  */
14197   cp_parser_parse_tentatively (parser);
14198   /* Assume that we are looking at a constructor declarator.  */
14199   constructor_p = true;
14200
14201   /* Look for the optional `::' operator.  */
14202   cp_parser_global_scope_opt (parser,
14203                               /*current_scope_valid_p=*/false);
14204   /* Look for the nested-name-specifier.  */
14205   nested_name_p 
14206     = (cp_parser_nested_name_specifier_opt (parser,
14207                                             /*typename_keyword_p=*/false,
14208                                             /*check_dependency_p=*/false,
14209                                             /*type_p=*/false,
14210                                             /*is_declaration=*/false)
14211        != NULL_TREE);
14212   /* Outside of a class-specifier, there must be a
14213      nested-name-specifier.  */
14214   if (!nested_name_p && 
14215       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14216        || friend_p))
14217     constructor_p = false;
14218   /* If we still think that this might be a constructor-declarator,
14219      look for a class-name.  */
14220   if (constructor_p)
14221     {
14222       /* If we have:
14223
14224            template <typename T> struct S { S(); };
14225            template <typename T> S<T>::S ();
14226
14227          we must recognize that the nested `S' names a class.
14228          Similarly, for:
14229
14230            template <typename T> S<T>::S<T> ();
14231
14232          we must recognize that the nested `S' names a template.  */
14233       type_decl = cp_parser_class_name (parser,
14234                                         /*typename_keyword_p=*/false,
14235                                         /*template_keyword_p=*/false,
14236                                         /*type_p=*/false,
14237                                         /*check_dependency_p=*/false,
14238                                         /*class_head_p=*/false,
14239                                         /*is_declaration=*/false);
14240       /* If there was no class-name, then this is not a constructor.  */
14241       constructor_p = !cp_parser_error_occurred (parser);
14242     }
14243
14244   /* If we're still considering a constructor, we have to see a `(',
14245      to begin the parameter-declaration-clause, followed by either a
14246      `)', an `...', or a decl-specifier.  We need to check for a
14247      type-specifier to avoid being fooled into thinking that:
14248
14249        S::S (f) (int);
14250
14251      is a constructor.  (It is actually a function named `f' that
14252      takes one parameter (of type `int') and returns a value of type
14253      `S::S'.  */
14254   if (constructor_p 
14255       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14256     {
14257       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14258           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14259           /* A parameter declaration begins with a decl-specifier,
14260              which is either the "attribute" keyword, a storage class
14261              specifier, or (usually) a type-specifier.  */
14262           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14263           && !cp_parser_storage_class_specifier_opt (parser))
14264         {
14265           tree type;
14266           bool pop_p = false;
14267           unsigned saved_num_template_parameter_lists;
14268
14269           /* Names appearing in the type-specifier should be looked up
14270              in the scope of the class.  */
14271           if (current_class_type)
14272             type = NULL_TREE;
14273           else
14274             {
14275               type = TREE_TYPE (type_decl);
14276               if (TREE_CODE (type) == TYPENAME_TYPE)
14277                 {
14278                   type = resolve_typename_type (type, 
14279                                                 /*only_current_p=*/false);
14280                   if (type == error_mark_node)
14281                     {
14282                       cp_parser_abort_tentative_parse (parser);
14283                       return false;
14284                     }
14285                 }
14286               pop_p = push_scope (type);
14287             }
14288
14289           /* Inside the constructor parameter list, surrounding
14290              template-parameter-lists do not apply.  */
14291           saved_num_template_parameter_lists
14292             = parser->num_template_parameter_lists;
14293           parser->num_template_parameter_lists = 0;
14294
14295           /* Look for the type-specifier.  */
14296           cp_parser_type_specifier (parser,
14297                                     CP_PARSER_FLAGS_NONE,
14298                                     /*is_friend=*/false,
14299                                     /*is_declarator=*/true,
14300                                     /*declares_class_or_enum=*/NULL,
14301                                     /*is_cv_qualifier=*/NULL);
14302
14303           parser->num_template_parameter_lists
14304             = saved_num_template_parameter_lists;
14305
14306           /* Leave the scope of the class.  */
14307           if (pop_p)
14308             pop_scope (type);
14309
14310           constructor_p = !cp_parser_error_occurred (parser);
14311         }
14312     }
14313   else
14314     constructor_p = false;
14315   /* We did not really want to consume any tokens.  */
14316   cp_parser_abort_tentative_parse (parser);
14317
14318   return constructor_p;
14319 }
14320
14321 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14322    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14323    they must be performed once we are in the scope of the function.
14324
14325    Returns the function defined.  */
14326
14327 static tree
14328 cp_parser_function_definition_from_specifiers_and_declarator
14329   (cp_parser* parser,
14330    tree decl_specifiers,
14331    tree attributes,
14332    tree declarator)
14333 {
14334   tree fn;
14335   bool success_p;
14336
14337   /* Begin the function-definition.  */
14338   success_p = begin_function_definition (decl_specifiers, 
14339                                          attributes, 
14340                                          declarator);
14341
14342   /* If there were names looked up in the decl-specifier-seq that we
14343      did not check, check them now.  We must wait until we are in the
14344      scope of the function to perform the checks, since the function
14345      might be a friend.  */
14346   perform_deferred_access_checks ();
14347
14348   if (!success_p)
14349     {
14350       /* If begin_function_definition didn't like the definition, skip
14351          the entire function.  */
14352       error ("invalid function declaration");
14353       cp_parser_skip_to_end_of_block_or_statement (parser);
14354       fn = error_mark_node;
14355     }
14356   else
14357     fn = cp_parser_function_definition_after_declarator (parser,
14358                                                          /*inline_p=*/false);
14359
14360   return fn;
14361 }
14362
14363 /* Parse the part of a function-definition that follows the
14364    declarator.  INLINE_P is TRUE iff this function is an inline
14365    function defined with a class-specifier.
14366
14367    Returns the function defined.  */
14368
14369 static tree 
14370 cp_parser_function_definition_after_declarator (cp_parser* parser, 
14371                                                 bool inline_p)
14372 {
14373   tree fn;
14374   bool ctor_initializer_p = false;
14375   bool saved_in_unbraced_linkage_specification_p;
14376   unsigned saved_num_template_parameter_lists;
14377
14378   /* If the next token is `return', then the code may be trying to
14379      make use of the "named return value" extension that G++ used to
14380      support.  */
14381   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14382     {
14383       /* Consume the `return' keyword.  */
14384       cp_lexer_consume_token (parser->lexer);
14385       /* Look for the identifier that indicates what value is to be
14386          returned.  */
14387       cp_parser_identifier (parser);
14388       /* Issue an error message.  */
14389       error ("named return values are no longer supported");
14390       /* Skip tokens until we reach the start of the function body.  */
14391       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14392              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14393         cp_lexer_consume_token (parser->lexer);
14394     }
14395   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14396      anything declared inside `f'.  */
14397   saved_in_unbraced_linkage_specification_p 
14398     = parser->in_unbraced_linkage_specification_p;
14399   parser->in_unbraced_linkage_specification_p = false;
14400   /* Inside the function, surrounding template-parameter-lists do not
14401      apply.  */
14402   saved_num_template_parameter_lists 
14403     = parser->num_template_parameter_lists; 
14404   parser->num_template_parameter_lists = 0;
14405   /* If the next token is `try', then we are looking at a
14406      function-try-block.  */
14407   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14408     ctor_initializer_p = cp_parser_function_try_block (parser);
14409   /* A function-try-block includes the function-body, so we only do
14410      this next part if we're not processing a function-try-block.  */
14411   else
14412     ctor_initializer_p 
14413       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14414
14415   /* Finish the function.  */
14416   fn = finish_function ((ctor_initializer_p ? 1 : 0) | 
14417                         (inline_p ? 2 : 0));
14418   /* Generate code for it, if necessary.  */
14419   expand_or_defer_fn (fn);
14420   /* Restore the saved values.  */
14421   parser->in_unbraced_linkage_specification_p 
14422     = saved_in_unbraced_linkage_specification_p;
14423   parser->num_template_parameter_lists 
14424     = saved_num_template_parameter_lists;
14425
14426   return fn;
14427 }
14428
14429 /* Parse a template-declaration, assuming that the `export' (and
14430    `extern') keywords, if present, has already been scanned.  MEMBER_P
14431    is as for cp_parser_template_declaration.  */
14432
14433 static void
14434 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14435 {
14436   tree decl = NULL_TREE;
14437   tree parameter_list;
14438   bool friend_p = false;
14439
14440   /* Look for the `template' keyword.  */
14441   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14442     return;
14443       
14444   /* And the `<'.  */
14445   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14446     return;
14447       
14448   /* If the next token is `>', then we have an invalid
14449      specialization.  Rather than complain about an invalid template
14450      parameter, issue an error message here.  */
14451   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14452     {
14453       cp_parser_error (parser, "invalid explicit specialization");
14454       begin_specialization ();
14455       parameter_list = NULL_TREE;
14456     }
14457   else
14458     {
14459       /* Parse the template parameters.  */
14460       begin_template_parm_list ();
14461       parameter_list = cp_parser_template_parameter_list (parser);
14462       parameter_list = end_template_parm_list (parameter_list);
14463     }
14464
14465   /* Look for the `>'.  */
14466   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14467   /* We just processed one more parameter list.  */
14468   ++parser->num_template_parameter_lists;
14469   /* If the next token is `template', there are more template
14470      parameters.  */
14471   if (cp_lexer_next_token_is_keyword (parser->lexer, 
14472                                       RID_TEMPLATE))
14473     cp_parser_template_declaration_after_export (parser, member_p);
14474   else
14475     {
14476       decl = cp_parser_single_declaration (parser,
14477                                            member_p,
14478                                            &friend_p);
14479
14480       /* If this is a member template declaration, let the front
14481          end know.  */
14482       if (member_p && !friend_p && decl)
14483         {
14484           if (TREE_CODE (decl) == TYPE_DECL)
14485             cp_parser_check_access_in_redeclaration (decl);
14486
14487           decl = finish_member_template_decl (decl);
14488         }
14489       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14490         make_friend_class (current_class_type, TREE_TYPE (decl),
14491                            /*complain=*/true);
14492     }
14493   /* We are done with the current parameter list.  */
14494   --parser->num_template_parameter_lists;
14495
14496   /* Finish up.  */
14497   finish_template_decl (parameter_list);
14498
14499   /* Register member declarations.  */
14500   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14501     finish_member_declaration (decl);
14502
14503   /* If DECL is a function template, we must return to parse it later.
14504      (Even though there is no definition, there might be default
14505      arguments that need handling.)  */
14506   if (member_p && decl 
14507       && (TREE_CODE (decl) == FUNCTION_DECL
14508           || DECL_FUNCTION_TEMPLATE_P (decl)))
14509     TREE_VALUE (parser->unparsed_functions_queues)
14510       = tree_cons (NULL_TREE, decl, 
14511                    TREE_VALUE (parser->unparsed_functions_queues));
14512 }
14513
14514 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14515    `function-definition' sequence.  MEMBER_P is true, this declaration
14516    appears in a class scope.
14517
14518    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14519    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14520
14521 static tree
14522 cp_parser_single_declaration (cp_parser* parser, 
14523                               bool member_p,
14524                               bool* friend_p)
14525 {
14526   int declares_class_or_enum;
14527   tree decl = NULL_TREE;
14528   tree decl_specifiers;
14529   tree attributes;
14530   bool function_definition_p = false;
14531
14532   /* This function is only used when processing a template
14533      declaration.  */
14534   if (innermost_scope_kind () != sk_template_parms
14535       && innermost_scope_kind () != sk_template_spec)
14536     abort ();
14537
14538   /* Defer access checks until we know what is being declared.  */
14539   push_deferring_access_checks (dk_deferred);
14540
14541   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14542      alternative.  */
14543   decl_specifiers 
14544     = cp_parser_decl_specifier_seq (parser,
14545                                     CP_PARSER_FLAGS_OPTIONAL,
14546                                     &attributes,
14547                                     &declares_class_or_enum);
14548   if (friend_p)
14549     *friend_p = cp_parser_friend_p (decl_specifiers);
14550
14551   /* There are no template typedefs.  */
14552   if (cp_parser_typedef_p (decl_specifiers))
14553     {
14554       error ("template declaration of `typedef'");
14555       decl = error_mark_node;
14556     }
14557
14558   /* Gather up the access checks that occurred the
14559      decl-specifier-seq.  */
14560   stop_deferring_access_checks ();
14561
14562   /* Check for the declaration of a template class.  */
14563   if (declares_class_or_enum)
14564     {
14565       if (cp_parser_declares_only_class_p (parser))
14566         {
14567           decl = shadow_tag (decl_specifiers);
14568           if (decl)
14569             decl = TYPE_NAME (decl);
14570           else
14571             decl = error_mark_node;
14572         }
14573     }
14574   /* If it's not a template class, try for a template function.  If
14575      the next token is a `;', then this declaration does not declare
14576      anything.  But, if there were errors in the decl-specifiers, then
14577      the error might well have come from an attempted class-specifier.
14578      In that case, there's no need to warn about a missing declarator.  */
14579   if (!decl
14580       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14581           || !value_member (error_mark_node, decl_specifiers)))
14582     decl = cp_parser_init_declarator (parser, 
14583                                       decl_specifiers,
14584                                       attributes,
14585                                       /*function_definition_allowed_p=*/true,
14586                                       member_p,
14587                                       declares_class_or_enum,
14588                                       &function_definition_p);
14589
14590   pop_deferring_access_checks ();
14591
14592   /* Clear any current qualification; whatever comes next is the start
14593      of something new.  */
14594   parser->scope = NULL_TREE;
14595   parser->qualifying_scope = NULL_TREE;
14596   parser->object_scope = NULL_TREE;
14597   /* Look for a trailing `;' after the declaration.  */
14598   if (!function_definition_p
14599       && (decl == error_mark_node
14600           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
14601     cp_parser_skip_to_end_of_block_or_statement (parser);
14602
14603   return decl;
14604 }
14605
14606 /* Parse a cast-expression that is not the operand of a unary "&".  */
14607
14608 static tree
14609 cp_parser_simple_cast_expression (cp_parser *parser)
14610 {
14611   return cp_parser_cast_expression (parser, /*address_p=*/false);
14612 }
14613
14614 /* Parse a functional cast to TYPE.  Returns an expression
14615    representing the cast.  */
14616
14617 static tree
14618 cp_parser_functional_cast (cp_parser* parser, tree type)
14619 {
14620   tree expression_list;
14621   tree cast;
14622
14623   expression_list
14624     = cp_parser_parenthesized_expression_list (parser, false,
14625                                                /*non_constant_p=*/NULL);
14626
14627   cast = build_functional_cast (type, expression_list);
14628   /* [expr.const]/1: In an integral constant expression "only type
14629      conversions to integral or enumeration type can be used".  */
14630   if (cast != error_mark_node && !type_dependent_expression_p (type) 
14631       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14632     {
14633       if (cp_parser_non_integral_constant_expression 
14634           (parser, "a call to a constructor"))
14635         return error_mark_node;
14636     }
14637   return cast;
14638 }
14639
14640 /* Save the tokens that make up the body of a member function defined
14641    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
14642    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
14643    specifiers applied to the declaration.  Returns the FUNCTION_DECL
14644    for the member function.  */
14645
14646 static tree
14647 cp_parser_save_member_function_body (cp_parser* parser,
14648                                      tree decl_specifiers,
14649                                      tree declarator,
14650                                      tree attributes)
14651 {
14652   cp_token_cache *cache;
14653   tree fn;
14654
14655   /* Create the function-declaration.  */
14656   fn = start_method (decl_specifiers, declarator, attributes);
14657   /* If something went badly wrong, bail out now.  */
14658   if (fn == error_mark_node)
14659     {
14660       /* If there's a function-body, skip it.  */
14661       if (cp_parser_token_starts_function_definition_p 
14662           (cp_lexer_peek_token (parser->lexer)))
14663         cp_parser_skip_to_end_of_block_or_statement (parser);
14664       return error_mark_node;
14665     }
14666
14667   /* Remember it, if there default args to post process.  */
14668   cp_parser_save_default_args (parser, fn);
14669
14670   /* Create a token cache.  */
14671   cache = cp_token_cache_new ();
14672   /* Save away the tokens that make up the body of the 
14673      function.  */
14674   cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14675   /* Handle function try blocks.  */
14676   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14677     cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14678
14679   /* Save away the inline definition; we will process it when the
14680      class is complete.  */
14681   DECL_PENDING_INLINE_INFO (fn) = cache;
14682   DECL_PENDING_INLINE_P (fn) = 1;
14683
14684   /* We need to know that this was defined in the class, so that
14685      friend templates are handled correctly.  */
14686   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14687
14688   /* We're done with the inline definition.  */
14689   finish_method (fn);
14690
14691   /* Add FN to the queue of functions to be parsed later.  */
14692   TREE_VALUE (parser->unparsed_functions_queues)
14693     = tree_cons (NULL_TREE, fn, 
14694                  TREE_VALUE (parser->unparsed_functions_queues));
14695
14696   return fn;
14697 }
14698
14699 /* Parse a template-argument-list, as well as the trailing ">" (but
14700    not the opening ">").  See cp_parser_template_argument_list for the
14701    return value.  */
14702
14703 static tree
14704 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14705 {
14706   tree arguments;
14707   tree saved_scope;
14708   tree saved_qualifying_scope;
14709   tree saved_object_scope;
14710   bool saved_greater_than_is_operator_p;
14711
14712   /* [temp.names]
14713
14714      When parsing a template-id, the first non-nested `>' is taken as
14715      the end of the template-argument-list rather than a greater-than
14716      operator.  */
14717   saved_greater_than_is_operator_p 
14718     = parser->greater_than_is_operator_p;
14719   parser->greater_than_is_operator_p = false;
14720   /* Parsing the argument list may modify SCOPE, so we save it
14721      here.  */
14722   saved_scope = parser->scope;
14723   saved_qualifying_scope = parser->qualifying_scope;
14724   saved_object_scope = parser->object_scope;
14725   /* Parse the template-argument-list itself.  */
14726   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14727     arguments = NULL_TREE;
14728   else
14729     arguments = cp_parser_template_argument_list (parser);
14730   /* Look for the `>' that ends the template-argument-list. If we find
14731      a '>>' instead, it's probably just a typo.  */
14732   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14733     {
14734       if (!saved_greater_than_is_operator_p)
14735         {
14736           /* If we're in a nested template argument list, the '>>' has to be
14737             a typo for '> >'. We emit the error message, but we continue
14738             parsing and we push a '>' as next token, so that the argument
14739             list will be parsed correctly..  */
14740           cp_token* token;
14741           error ("`>>' should be `> >' within a nested template argument list");
14742           token = cp_lexer_peek_token (parser->lexer);
14743           token->type = CPP_GREATER;
14744         }
14745       else
14746         {
14747           /* If this is not a nested template argument list, the '>>' is
14748             a typo for '>'. Emit an error message and continue.  */
14749           error ("spurious `>>', use `>' to terminate a template argument list");
14750           cp_lexer_consume_token (parser->lexer);
14751         }
14752     }
14753   else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14754     error ("missing `>' to terminate the template argument list");
14755   /* The `>' token might be a greater-than operator again now.  */
14756   parser->greater_than_is_operator_p 
14757     = saved_greater_than_is_operator_p;
14758   /* Restore the SAVED_SCOPE.  */
14759   parser->scope = saved_scope;
14760   parser->qualifying_scope = saved_qualifying_scope;
14761   parser->object_scope = saved_object_scope;
14762
14763   return arguments;
14764 }
14765
14766 /* MEMBER_FUNCTION is a member function, or a friend.  If default
14767    arguments, or the body of the function have not yet been parsed,
14768    parse them now.  */
14769
14770 static void
14771 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14772 {
14773   cp_lexer *saved_lexer;
14774
14775   /* If this member is a template, get the underlying
14776      FUNCTION_DECL.  */
14777   if (DECL_FUNCTION_TEMPLATE_P (member_function))
14778     member_function = DECL_TEMPLATE_RESULT (member_function);
14779
14780   /* There should not be any class definitions in progress at this
14781      point; the bodies of members are only parsed outside of all class
14782      definitions.  */
14783   my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14784   /* While we're parsing the member functions we might encounter more
14785      classes.  We want to handle them right away, but we don't want
14786      them getting mixed up with functions that are currently in the
14787      queue.  */
14788   parser->unparsed_functions_queues
14789     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14790
14791   /* Make sure that any template parameters are in scope.  */
14792   maybe_begin_member_template_processing (member_function);
14793
14794   /* If the body of the function has not yet been parsed, parse it
14795      now.  */
14796   if (DECL_PENDING_INLINE_P (member_function))
14797     {
14798       tree function_scope;
14799       cp_token_cache *tokens;
14800
14801       /* The function is no longer pending; we are processing it.  */
14802       tokens = DECL_PENDING_INLINE_INFO (member_function);
14803       DECL_PENDING_INLINE_INFO (member_function) = NULL;
14804       DECL_PENDING_INLINE_P (member_function) = 0;
14805       
14806       /* If this is a local class, enter the scope of the containing
14807          function.  */
14808       function_scope = current_function_decl;
14809       if (function_scope)
14810         push_function_context_to (function_scope);
14811       
14812       /* Save away the current lexer.  */
14813       saved_lexer = parser->lexer;
14814       /* Make a new lexer to feed us the tokens saved for this function.  */
14815       parser->lexer = cp_lexer_new_from_tokens (tokens);
14816       parser->lexer->next = saved_lexer;
14817       
14818       /* Set the current source position to be the location of the first
14819          token in the saved inline body.  */
14820       cp_lexer_peek_token (parser->lexer);
14821       
14822       /* Let the front end know that we going to be defining this
14823          function.  */
14824       start_function (NULL_TREE, member_function, NULL_TREE,
14825                       SF_PRE_PARSED | SF_INCLASS_INLINE);
14826       
14827       /* Now, parse the body of the function.  */
14828       cp_parser_function_definition_after_declarator (parser,
14829                                                       /*inline_p=*/true);
14830       
14831       /* Leave the scope of the containing function.  */
14832       if (function_scope)
14833         pop_function_context_from (function_scope);
14834       /* Restore the lexer.  */
14835       parser->lexer = saved_lexer;
14836     }
14837
14838   /* Remove any template parameters from the symbol table.  */
14839   maybe_end_member_template_processing ();
14840
14841   /* Restore the queue.  */
14842   parser->unparsed_functions_queues 
14843     = TREE_CHAIN (parser->unparsed_functions_queues);
14844 }
14845
14846 /* If DECL contains any default args, remember it on the unparsed
14847    functions queue.  */
14848
14849 static void
14850 cp_parser_save_default_args (cp_parser* parser, tree decl)
14851 {
14852   tree probe;
14853
14854   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14855        probe;
14856        probe = TREE_CHAIN (probe))
14857     if (TREE_PURPOSE (probe))
14858       {
14859         TREE_PURPOSE (parser->unparsed_functions_queues)
14860           = tree_cons (NULL_TREE, decl, 
14861                        TREE_PURPOSE (parser->unparsed_functions_queues));
14862         break;
14863       }
14864   return;
14865 }
14866
14867 /* FN is a FUNCTION_DECL which may contains a parameter with an
14868    unparsed DEFAULT_ARG.  Parse the default args now.  */
14869
14870 static void
14871 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14872 {
14873   cp_lexer *saved_lexer;
14874   cp_token_cache *tokens;
14875   bool saved_local_variables_forbidden_p;
14876   tree parameters;
14877
14878   /* While we're parsing the default args, we might (due to the
14879      statement expression extension) encounter more classes.  We want
14880      to handle them right away, but we don't want them getting mixed
14881      up with default args that are currently in the queue.  */
14882   parser->unparsed_functions_queues
14883     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14884
14885   for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14886        parameters;
14887        parameters = TREE_CHAIN (parameters))
14888     {
14889       tree default_arg = TREE_PURPOSE (parameters);
14890       tree parsed_arg;
14891
14892       if (!default_arg)
14893         continue;
14894
14895       if (TREE_CODE (default_arg) != DEFAULT_ARG)
14896         /* This can happen for a friend declaration for a function
14897            already declared with default arguments.  */
14898         continue;
14899   
14900       /* Save away the current lexer.  */
14901       saved_lexer = parser->lexer;
14902       /* Create a new one, using the tokens we have saved.  */
14903       tokens =  DEFARG_TOKENS (default_arg);
14904       parser->lexer = cp_lexer_new_from_tokens (tokens);
14905
14906       /* Set the current source position to be the location of the
14907          first token in the default argument.  */
14908       cp_lexer_peek_token (parser->lexer);
14909
14910       /* Local variable names (and the `this' keyword) may not appear
14911          in a default argument.  */
14912       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14913       parser->local_variables_forbidden_p = true;
14914       
14915       /* Parse the assignment-expression.  */
14916       if (DECL_FRIEND_CONTEXT (fn))
14917         push_nested_class (DECL_FRIEND_CONTEXT (fn));
14918       else if (DECL_CLASS_SCOPE_P (fn))
14919         push_nested_class (DECL_CONTEXT (fn));
14920       parsed_arg = cp_parser_assignment_expression (parser);
14921       if (DECL_FRIEND_CONTEXT (fn) || DECL_CLASS_SCOPE_P (fn))
14922         pop_nested_class ();
14923       
14924       TREE_PURPOSE (parameters) = parsed_arg;
14925       
14926       /* Update any instantiations we've already created.  */
14927       for (default_arg = TREE_CHAIN (default_arg);
14928            default_arg;
14929            default_arg = TREE_CHAIN (default_arg))
14930         TREE_PURPOSE (TREE_PURPOSE (default_arg)) = parsed_arg;
14931      
14932       /* If the token stream has not been completely used up, then
14933          there was extra junk after the end of the default
14934          argument.  */
14935       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14936         cp_parser_error (parser, "expected `,'");
14937
14938        /* Restore saved state.  */
14939       parser->lexer = saved_lexer;
14940       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14941     }
14942
14943   /* Restore the queue.  */
14944   parser->unparsed_functions_queues 
14945     = TREE_CHAIN (parser->unparsed_functions_queues);
14946 }
14947
14948 /* Parse the operand of `sizeof' (or a similar operator).  Returns
14949    either a TYPE or an expression, depending on the form of the
14950    input.  The KEYWORD indicates which kind of expression we have
14951    encountered.  */
14952
14953 static tree
14954 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14955 {
14956   static const char *format;
14957   tree expr = NULL_TREE;
14958   const char *saved_message;
14959   bool saved_integral_constant_expression_p;
14960
14961   /* Initialize FORMAT the first time we get here.  */
14962   if (!format)
14963     format = "types may not be defined in `%s' expressions";
14964
14965   /* Types cannot be defined in a `sizeof' expression.  Save away the
14966      old message.  */
14967   saved_message = parser->type_definition_forbidden_message;
14968   /* And create the new one.  */
14969   parser->type_definition_forbidden_message 
14970     = xmalloc (strlen (format) 
14971                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14972                + 1 /* `\0' */);
14973   sprintf ((char *) parser->type_definition_forbidden_message,
14974            format, IDENTIFIER_POINTER (ridpointers[keyword]));
14975
14976   /* The restrictions on constant-expressions do not apply inside
14977      sizeof expressions.  */
14978   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14979   parser->integral_constant_expression_p = false;
14980
14981   /* Do not actually evaluate the expression.  */
14982   ++skip_evaluation;
14983   /* If it's a `(', then we might be looking at the type-id
14984      construction.  */
14985   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14986     {
14987       tree type;
14988       bool saved_in_type_id_in_expr_p;
14989
14990       /* We can't be sure yet whether we're looking at a type-id or an
14991          expression.  */
14992       cp_parser_parse_tentatively (parser);
14993       /* Consume the `('.  */
14994       cp_lexer_consume_token (parser->lexer);
14995       /* Parse the type-id.  */
14996       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14997       parser->in_type_id_in_expr_p = true;
14998       type = cp_parser_type_id (parser);
14999       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15000       /* Now, look for the trailing `)'.  */
15001       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15002       /* If all went well, then we're done.  */
15003       if (cp_parser_parse_definitely (parser))
15004         {
15005           /* Build a list of decl-specifiers; right now, we have only
15006              a single type-specifier.  */
15007           type = build_tree_list (NULL_TREE,
15008                                   type);
15009
15010           /* Call grokdeclarator to figure out what type this is.  */
15011           expr = grokdeclarator (NULL_TREE,
15012                                  type,
15013                                  TYPENAME,
15014                                  /*initialized=*/0,
15015                                  /*attrlist=*/NULL);
15016         }
15017     }
15018
15019   /* If the type-id production did not work out, then we must be
15020      looking at the unary-expression production.  */
15021   if (!expr)
15022     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15023   /* Go back to evaluating expressions.  */
15024   --skip_evaluation;
15025
15026   /* Free the message we created.  */
15027   free ((char *) parser->type_definition_forbidden_message);
15028   /* And restore the old one.  */
15029   parser->type_definition_forbidden_message = saved_message;
15030   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15031
15032   return expr;
15033 }
15034
15035 /* If the current declaration has no declarator, return true.  */
15036
15037 static bool
15038 cp_parser_declares_only_class_p (cp_parser *parser)
15039 {
15040   /* If the next token is a `;' or a `,' then there is no 
15041      declarator.  */
15042   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15043           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15044 }
15045
15046 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15047    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15048
15049 static bool
15050 cp_parser_friend_p (tree decl_specifiers)
15051 {
15052   while (decl_specifiers)
15053     {
15054       /* See if this decl-specifier is `friend'.  */
15055       if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
15056           && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
15057         return true;
15058
15059       /* Go on to the next decl-specifier.  */
15060       decl_specifiers = TREE_CHAIN (decl_specifiers);
15061     }
15062
15063   return false;
15064 }
15065
15066 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15067    Returns TRUE iff `typedef' appears among the DECL_SPECIFIERS.  */
15068
15069 static bool
15070 cp_parser_typedef_p (tree decl_specifiers)
15071 {
15072   while (decl_specifiers)
15073     {
15074       /* See if this decl-specifier is `typedef'.  */
15075       if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
15076           && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_TYPEDEF)
15077         return true;
15078
15079       /* Go on to the next decl-specifier.  */
15080       decl_specifiers = TREE_CHAIN (decl_specifiers);
15081     }
15082
15083   return false;
15084 }
15085
15086
15087 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15088    issue an error message indicating that TOKEN_DESC was expected.
15089    
15090    Returns the token consumed, if the token had the appropriate type.
15091    Otherwise, returns NULL.  */
15092
15093 static cp_token *
15094 cp_parser_require (cp_parser* parser,
15095                    enum cpp_ttype type,
15096                    const char* token_desc)
15097 {
15098   if (cp_lexer_next_token_is (parser->lexer, type))
15099     return cp_lexer_consume_token (parser->lexer);
15100   else
15101     {
15102       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15103       if (!cp_parser_simulate_error (parser))
15104         {
15105           char *message = concat ("expected ", token_desc, NULL);
15106           cp_parser_error (parser, message);
15107           free (message);
15108         }
15109       return NULL;
15110     }
15111 }
15112
15113 /* Like cp_parser_require, except that tokens will be skipped until
15114    the desired token is found.  An error message is still produced if
15115    the next token is not as expected.  */
15116
15117 static void
15118 cp_parser_skip_until_found (cp_parser* parser, 
15119                             enum cpp_ttype type, 
15120                             const char* token_desc)
15121 {
15122   cp_token *token;
15123   unsigned nesting_depth = 0;
15124
15125   if (cp_parser_require (parser, type, token_desc))
15126     return;
15127
15128   /* Skip tokens until the desired token is found.  */
15129   while (true)
15130     {
15131       /* Peek at the next token.  */
15132       token = cp_lexer_peek_token (parser->lexer);
15133       /* If we've reached the token we want, consume it and 
15134          stop.  */
15135       if (token->type == type && !nesting_depth)
15136         {
15137           cp_lexer_consume_token (parser->lexer);
15138           return;
15139         }
15140       /* If we've run out of tokens, stop.  */
15141       if (token->type == CPP_EOF)
15142         return;
15143       if (token->type == CPP_OPEN_BRACE 
15144           || token->type == CPP_OPEN_PAREN
15145           || token->type == CPP_OPEN_SQUARE)
15146         ++nesting_depth;
15147       else if (token->type == CPP_CLOSE_BRACE 
15148                || token->type == CPP_CLOSE_PAREN
15149                || token->type == CPP_CLOSE_SQUARE)
15150         {
15151           if (nesting_depth-- == 0)
15152             return;
15153         }
15154       /* Consume this token.  */
15155       cp_lexer_consume_token (parser->lexer);
15156     }
15157 }
15158
15159 /* If the next token is the indicated keyword, consume it.  Otherwise,
15160    issue an error message indicating that TOKEN_DESC was expected.
15161    
15162    Returns the token consumed, if the token had the appropriate type.
15163    Otherwise, returns NULL.  */
15164
15165 static cp_token *
15166 cp_parser_require_keyword (cp_parser* parser,
15167                            enum rid keyword,
15168                            const char* token_desc)
15169 {
15170   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15171
15172   if (token && token->keyword != keyword)
15173     {
15174       dyn_string_t error_msg;
15175
15176       /* Format the error message.  */
15177       error_msg = dyn_string_new (0);
15178       dyn_string_append_cstr (error_msg, "expected ");
15179       dyn_string_append_cstr (error_msg, token_desc);
15180       cp_parser_error (parser, error_msg->s);
15181       dyn_string_delete (error_msg);
15182       return NULL;
15183     }
15184
15185   return token;
15186 }
15187
15188 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15189    function-definition.  */
15190
15191 static bool 
15192 cp_parser_token_starts_function_definition_p (cp_token* token)
15193 {
15194   return (/* An ordinary function-body begins with an `{'.  */
15195           token->type == CPP_OPEN_BRACE
15196           /* A ctor-initializer begins with a `:'.  */
15197           || token->type == CPP_COLON
15198           /* A function-try-block begins with `try'.  */
15199           || token->keyword == RID_TRY
15200           /* The named return value extension begins with `return'.  */
15201           || token->keyword == RID_RETURN);
15202 }
15203
15204 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15205    definition.  */
15206
15207 static bool
15208 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15209 {
15210   cp_token *token;
15211
15212   token = cp_lexer_peek_token (parser->lexer);
15213   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15214 }
15215
15216 /* Returns TRUE iff the next token is the "," or ">" ending a
15217    template-argument.   */
15218
15219 static bool
15220 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15221 {
15222   cp_token *token;
15223
15224   token = cp_lexer_peek_token (parser->lexer);
15225   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15226 }
15227
15228 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15229    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15230
15231 static bool
15232 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser, 
15233                                                      size_t n)
15234 {
15235   cp_token *token;
15236
15237   token = cp_lexer_peek_nth_token (parser->lexer, n);
15238   if (token->type == CPP_LESS)
15239     return true;
15240   /* Check for the sequence `<::' in the original code. It would be lexed as
15241      `[:', where `[' is a digraph, and there is no whitespace before
15242      `:'.  */
15243   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15244     {
15245       cp_token *token2;
15246       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15247       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15248         return true;
15249     }
15250   return false;
15251 }
15252  
15253 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15254    or none_type otherwise.  */
15255
15256 static enum tag_types
15257 cp_parser_token_is_class_key (cp_token* token)
15258 {
15259   switch (token->keyword)
15260     {
15261     case RID_CLASS:
15262       return class_type;
15263     case RID_STRUCT:
15264       return record_type;
15265     case RID_UNION:
15266       return union_type;
15267       
15268     default:
15269       return none_type;
15270     }
15271 }
15272
15273 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15274
15275 static void
15276 cp_parser_check_class_key (enum tag_types class_key, tree type)
15277 {
15278   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15279     pedwarn ("`%s' tag used in naming `%#T'",
15280             class_key == union_type ? "union"
15281              : class_key == record_type ? "struct" : "class", 
15282              type);
15283 }
15284                            
15285 /* Issue an error message if DECL is redeclared with different
15286    access than its original declaration [class.access.spec/3].
15287    This applies to nested classes and nested class templates.
15288    [class.mem/1].  */
15289
15290 static void cp_parser_check_access_in_redeclaration (tree decl)
15291 {
15292   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15293     return;
15294
15295   if ((TREE_PRIVATE (decl)
15296        != (current_access_specifier == access_private_node))
15297       || (TREE_PROTECTED (decl)
15298           != (current_access_specifier == access_protected_node)))
15299     error ("%D redeclared with different access", decl);
15300 }
15301
15302 /* Look for the `template' keyword, as a syntactic disambiguator.
15303    Return TRUE iff it is present, in which case it will be 
15304    consumed.  */
15305
15306 static bool
15307 cp_parser_optional_template_keyword (cp_parser *parser)
15308 {
15309   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15310     {
15311       /* The `template' keyword can only be used within templates;
15312          outside templates the parser can always figure out what is a
15313          template and what is not.  */
15314       if (!processing_template_decl)
15315         {
15316           error ("`template' (as a disambiguator) is only allowed "
15317                  "within templates");
15318           /* If this part of the token stream is rescanned, the same
15319              error message would be generated.  So, we purge the token
15320              from the stream.  */
15321           cp_lexer_purge_token (parser->lexer);
15322           return false;
15323         }
15324       else
15325         {
15326           /* Consume the `template' keyword.  */
15327           cp_lexer_consume_token (parser->lexer);
15328           return true;
15329         }
15330     }
15331
15332   return false;
15333 }
15334
15335 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15336    set PARSER->SCOPE, and perform other related actions.  */
15337
15338 static void
15339 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15340 {
15341   tree value;
15342   tree check;
15343
15344   /* Get the stored value.  */
15345   value = cp_lexer_consume_token (parser->lexer)->value;
15346   /* Perform any access checks that were deferred.  */
15347   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15348     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15349   /* Set the scope from the stored value.  */
15350   parser->scope = TREE_VALUE (value);
15351   parser->qualifying_scope = TREE_TYPE (value);
15352   parser->object_scope = NULL_TREE;
15353 }
15354
15355 /* Add tokens to CACHE until an non-nested END token appears.  */
15356
15357 static void
15358 cp_parser_cache_group (cp_parser *parser, 
15359                        cp_token_cache *cache,
15360                        enum cpp_ttype end,
15361                        unsigned depth)
15362 {
15363   while (true)
15364     {
15365       cp_token *token;
15366
15367       /* Abort a parenthesized expression if we encounter a brace.  */
15368       if ((end == CPP_CLOSE_PAREN || depth == 0)
15369           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15370         return;
15371       /* If we've reached the end of the file, stop.  */
15372       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15373         return;
15374       /* Consume the next token.  */
15375       token = cp_lexer_consume_token (parser->lexer);
15376       /* Add this token to the tokens we are saving.  */
15377       cp_token_cache_push_token (cache, token);
15378       /* See if it starts a new group.  */
15379       if (token->type == CPP_OPEN_BRACE)
15380         {
15381           cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15382           if (depth == 0)
15383             return;
15384         }
15385       else if (token->type == CPP_OPEN_PAREN)
15386         cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15387       else if (token->type == end)
15388         return;
15389     }
15390 }
15391
15392 /* Begin parsing tentatively.  We always save tokens while parsing
15393    tentatively so that if the tentative parsing fails we can restore the
15394    tokens.  */
15395
15396 static void
15397 cp_parser_parse_tentatively (cp_parser* parser)
15398 {
15399   /* Enter a new parsing context.  */
15400   parser->context = cp_parser_context_new (parser->context);
15401   /* Begin saving tokens.  */
15402   cp_lexer_save_tokens (parser->lexer);
15403   /* In order to avoid repetitive access control error messages,
15404      access checks are queued up until we are no longer parsing
15405      tentatively.  */
15406   push_deferring_access_checks (dk_deferred);
15407 }
15408
15409 /* Commit to the currently active tentative parse.  */
15410
15411 static void
15412 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15413 {
15414   cp_parser_context *context;
15415   cp_lexer *lexer;
15416
15417   /* Mark all of the levels as committed.  */
15418   lexer = parser->lexer;
15419   for (context = parser->context; context->next; context = context->next)
15420     {
15421       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15422         break;
15423       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15424       while (!cp_lexer_saving_tokens (lexer))
15425         lexer = lexer->next;
15426       cp_lexer_commit_tokens (lexer);
15427     }
15428 }
15429
15430 /* Abort the currently active tentative parse.  All consumed tokens
15431    will be rolled back, and no diagnostics will be issued.  */
15432
15433 static void
15434 cp_parser_abort_tentative_parse (cp_parser* parser)
15435 {
15436   cp_parser_simulate_error (parser);
15437   /* Now, pretend that we want to see if the construct was
15438      successfully parsed.  */
15439   cp_parser_parse_definitely (parser);
15440 }
15441
15442 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15443    token stream.  Otherwise, commit to the tokens we have consumed.
15444    Returns true if no error occurred; false otherwise.  */
15445
15446 static bool
15447 cp_parser_parse_definitely (cp_parser* parser)
15448 {
15449   bool error_occurred;
15450   cp_parser_context *context;
15451
15452   /* Remember whether or not an error occurred, since we are about to
15453      destroy that information.  */
15454   error_occurred = cp_parser_error_occurred (parser);
15455   /* Remove the topmost context from the stack.  */
15456   context = parser->context;
15457   parser->context = context->next;
15458   /* If no parse errors occurred, commit to the tentative parse.  */
15459   if (!error_occurred)
15460     {
15461       /* Commit to the tokens read tentatively, unless that was
15462          already done.  */
15463       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15464         cp_lexer_commit_tokens (parser->lexer);
15465
15466       pop_to_parent_deferring_access_checks ();
15467     }
15468   /* Otherwise, if errors occurred, roll back our state so that things
15469      are just as they were before we began the tentative parse.  */
15470   else
15471     {
15472       cp_lexer_rollback_tokens (parser->lexer);
15473       pop_deferring_access_checks ();
15474     }
15475   /* Add the context to the front of the free list.  */
15476   context->next = cp_parser_context_free_list;
15477   cp_parser_context_free_list = context;
15478
15479   return !error_occurred;
15480 }
15481
15482 /* Returns true if we are parsing tentatively -- but have decided that
15483    we will stick with this tentative parse, even if errors occur.  */
15484
15485 static bool
15486 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15487 {
15488   return (cp_parser_parsing_tentatively (parser)
15489           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15490 }
15491
15492 /* Returns nonzero iff an error has occurred during the most recent
15493    tentative parse.  */
15494    
15495 static bool
15496 cp_parser_error_occurred (cp_parser* parser)
15497 {
15498   return (cp_parser_parsing_tentatively (parser)
15499           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15500 }
15501
15502 /* Returns nonzero if GNU extensions are allowed.  */
15503
15504 static bool
15505 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15506 {
15507   return parser->allow_gnu_extensions_p;
15508 }
15509
15510 \f
15511
15512 /* The parser.  */
15513
15514 static GTY (()) cp_parser *the_parser;
15515
15516 /* External interface.  */
15517
15518 /* Parse one entire translation unit.  */
15519
15520 void
15521 c_parse_file (void)
15522 {
15523   bool error_occurred;
15524
15525   the_parser = cp_parser_new ();
15526   push_deferring_access_checks (flag_access_control
15527                                 ? dk_no_deferred : dk_no_check);
15528   error_occurred = cp_parser_translation_unit (the_parser);
15529   the_parser = NULL;
15530 }
15531
15532 /* This variable must be provided by every front end.  */
15533
15534 int yydebug;
15535
15536 #include "gt-cp-parser.h"