OSDN Git Service

PR c++/10771
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>.
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GCC is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37
38 \f
39 /* The lexer.  */
40
41 /* Overview
42    --------
43
44    A cp_lexer represents a stream of cp_tokens.  It allows arbitrary
45    look-ahead.
46
47    Methodology
48    -----------
49
50    We use a circular buffer to store incoming tokens.
51
52    Some artifacts of the C++ language (such as the
53    expression/declaration ambiguity) require arbitrary look-ahead.
54    The strategy we adopt for dealing with these problems is to attempt
55    to parse one construct (e.g., the declaration) and fall back to the
56    other (e.g., the expression) if that attempt does not succeed.
57    Therefore, we must sometimes store an arbitrary number of tokens.
58
59    The parser routinely peeks at the next token, and then consumes it
60    later.  That also requires a buffer in which to store the tokens.
61      
62    In order to easily permit adding tokens to the end of the buffer,
63    while removing them from the beginning of the buffer, we use a
64    circular buffer.  */
65
66 /* A C++ token.  */
67
68 typedef struct cp_token GTY (())
69 {
70   /* The kind of token.  */
71   ENUM_BITFIELD (cpp_ttype) type : 8;
72   /* If this token is a keyword, this value indicates which keyword.
73      Otherwise, this value is RID_MAX.  */
74   ENUM_BITFIELD (rid) keyword : 8;
75   /* The value associated with this token, if any.  */
76   tree value;
77   /* The location at which this token was found.  */
78   location_t location;
79 } cp_token;
80
81 /* The number of tokens in a single token block.
82    Computed so that cp_token_block fits in a 512B allocation unit.  */
83
84 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
85
86 /* A group of tokens.  These groups are chained together to store
87    large numbers of tokens.  (For example, a token block is created
88    when the body of an inline member function is first encountered;
89    the tokens are processed later after the class definition is
90    complete.)  
91
92    This somewhat ungainly data structure (as opposed to, say, a
93    variable-length array), is used due to constraints imposed by the
94    current garbage-collection methodology.  If it is made more
95    flexible, we could perhaps simplify the data structures involved.  */
96
97 typedef struct cp_token_block GTY (())
98 {
99   /* The tokens.  */
100   cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
101   /* The number of tokens in this block.  */
102   size_t num_tokens;
103   /* The next token block in the chain.  */
104   struct cp_token_block *next;
105   /* The previous block in the chain.  */
106   struct cp_token_block *prev;
107 } cp_token_block;
108
109 typedef struct cp_token_cache GTY (())
110 {
111   /* The first block in the cache.  NULL if there are no tokens in the
112      cache.  */
113   cp_token_block *first;
114   /* The last block in the cache.  NULL If there are no tokens in the
115      cache.  */
116   cp_token_block *last;
117 } cp_token_cache;
118
119 /* Prototypes.  */
120
121 static cp_token_cache *cp_token_cache_new 
122   (void);
123 static void cp_token_cache_push_token
124   (cp_token_cache *, cp_token *);
125
126 /* Create a new cp_token_cache.  */
127
128 static cp_token_cache *
129 cp_token_cache_new (void)
130 {
131   return ggc_alloc_cleared (sizeof (cp_token_cache));
132 }
133
134 /* Add *TOKEN to *CACHE.  */
135
136 static void
137 cp_token_cache_push_token (cp_token_cache *cache,
138                            cp_token *token)
139 {
140   cp_token_block *b = cache->last;
141
142   /* See if we need to allocate a new token block.  */
143   if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
144     {
145       b = ggc_alloc_cleared (sizeof (cp_token_block));
146       b->prev = cache->last;
147       if (cache->last)
148         {
149           cache->last->next = b;
150           cache->last = b;
151         }
152       else
153         cache->first = cache->last = b;
154     }
155   /* Add this token to the current token block.  */
156   b->tokens[b->num_tokens++] = *token;
157 }
158
159 /* The cp_lexer structure represents the C++ lexer.  It is responsible
160    for managing the token stream from the preprocessor and supplying
161    it to the parser.  */
162
163 typedef struct cp_lexer GTY (())
164 {
165   /* The memory allocated for the buffer.  Never NULL.  */
166   cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
167   /* A pointer just past the end of the memory allocated for the buffer.  */
168   cp_token * GTY ((skip (""))) buffer_end;
169   /* The first valid token in the buffer, or NULL if none.  */
170   cp_token * GTY ((skip (""))) first_token;
171   /* The next available token.  If NEXT_TOKEN is NULL, then there are
172      no more available tokens.  */
173   cp_token * GTY ((skip (""))) next_token;
174   /* A pointer just past the last available token.  If FIRST_TOKEN is
175      NULL, however, there are no available tokens, and then this
176      location is simply the place in which the next token read will be
177      placed.  If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
178      When the LAST_TOKEN == BUFFER, then the last token is at the
179      highest memory address in the BUFFER.  */
180   cp_token * GTY ((skip (""))) last_token;
181
182   /* A stack indicating positions at which cp_lexer_save_tokens was
183      called.  The top entry is the most recent position at which we
184      began saving tokens.  The entries are differences in token
185      position between FIRST_TOKEN and the first saved token.
186
187      If the stack is non-empty, we are saving tokens.  When a token is
188      consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
189      pointer will not.  The token stream will be preserved so that it
190      can be reexamined later.
191
192      If the stack is empty, then we are not saving tokens.  Whenever a
193      token is consumed, the FIRST_TOKEN pointer will be moved, and the
194      consumed token will be gone forever.  */
195   varray_type saved_tokens;
196
197   /* The STRING_CST tokens encountered while processing the current
198      string literal.  */
199   varray_type string_tokens;
200
201   /* True if we should obtain more tokens from the preprocessor; false
202      if we are processing a saved token cache.  */
203   bool main_lexer_p;
204
205   /* True if we should output debugging information.  */
206   bool debugging_p;
207
208   /* The next lexer in a linked list of lexers.  */
209   struct cp_lexer *next;
210 } cp_lexer;
211
212 /* Prototypes.  */
213
214 static cp_lexer *cp_lexer_new_main
215   (void);
216 static cp_lexer *cp_lexer_new_from_tokens
217   (struct cp_token_cache *);
218 static int cp_lexer_saving_tokens
219   (const cp_lexer *);
220 static cp_token *cp_lexer_next_token
221   (cp_lexer *, cp_token *);
222 static cp_token *cp_lexer_prev_token
223   (cp_lexer *, cp_token *);
224 static ptrdiff_t cp_lexer_token_difference 
225   (cp_lexer *, cp_token *, cp_token *);
226 static cp_token *cp_lexer_read_token
227   (cp_lexer *);
228 static void cp_lexer_maybe_grow_buffer
229   (cp_lexer *);
230 static void cp_lexer_get_preprocessor_token
231   (cp_lexer *, cp_token *);
232 static cp_token *cp_lexer_peek_token
233   (cp_lexer *);
234 static cp_token *cp_lexer_peek_nth_token
235   (cp_lexer *, size_t);
236 static inline bool cp_lexer_next_token_is
237   (cp_lexer *, enum cpp_ttype);
238 static bool cp_lexer_next_token_is_not
239   (cp_lexer *, enum cpp_ttype);
240 static bool cp_lexer_next_token_is_keyword
241   (cp_lexer *, enum rid);
242 static cp_token *cp_lexer_consume_token 
243   (cp_lexer *);
244 static void cp_lexer_purge_token
245   (cp_lexer *);
246 static void cp_lexer_purge_tokens_after
247   (cp_lexer *, cp_token *);
248 static void cp_lexer_save_tokens
249   (cp_lexer *);
250 static void cp_lexer_commit_tokens
251   (cp_lexer *);
252 static void cp_lexer_rollback_tokens
253   (cp_lexer *);
254 static inline void cp_lexer_set_source_position_from_token 
255   (cp_lexer *, const cp_token *);
256 static void cp_lexer_print_token
257   (FILE *, cp_token *);
258 static inline bool cp_lexer_debugging_p 
259   (cp_lexer *);
260 static void cp_lexer_start_debugging
261   (cp_lexer *) ATTRIBUTE_UNUSED;
262 static void cp_lexer_stop_debugging
263   (cp_lexer *) ATTRIBUTE_UNUSED;
264
265 /* Manifest constants.  */
266
267 #define CP_TOKEN_BUFFER_SIZE 5
268 #define CP_SAVED_TOKENS_SIZE 5
269
270 /* A token type for keywords, as opposed to ordinary identifiers.  */
271 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
272
273 /* A token type for template-ids.  If a template-id is processed while
274    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
275    the value of the CPP_TEMPLATE_ID is whatever was returned by
276    cp_parser_template_id.  */
277 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
278
279 /* A token type for nested-name-specifiers.  If a
280    nested-name-specifier is processed while parsing tentatively, it is
281    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
282    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
283    cp_parser_nested_name_specifier_opt.  */
284 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
285
286 /* A token type for tokens that are not tokens at all; these are used
287    to mark the end of a token block.  */
288 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
289
290 /* Variables.  */
291
292 /* The stream to which debugging output should be written.  */
293 static FILE *cp_lexer_debug_stream;
294
295 /* Create a new main C++ lexer, the lexer that gets tokens from the
296    preprocessor.  */
297
298 static cp_lexer *
299 cp_lexer_new_main (void)
300 {
301   cp_lexer *lexer;
302   cp_token first_token;
303
304   /* It's possible that lexing the first token will load a PCH file,
305      which is a GC collection point.  So we have to grab the first
306      token before allocating any memory.  */
307   cp_lexer_get_preprocessor_token (NULL, &first_token);
308   c_common_no_more_pch ();
309
310   /* Allocate the memory.  */
311   lexer = ggc_alloc_cleared (sizeof (cp_lexer));
312
313   /* Create the circular buffer.  */
314   lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
315   lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
316
317   /* There is one token in the buffer.  */
318   lexer->last_token = lexer->buffer + 1;
319   lexer->first_token = lexer->buffer;
320   lexer->next_token = lexer->buffer;
321   memcpy (lexer->buffer, &first_token, sizeof (cp_token));
322
323   /* This lexer obtains more tokens by calling c_lex.  */
324   lexer->main_lexer_p = true;
325
326   /* Create the SAVED_TOKENS stack.  */
327   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
328   
329   /* Create the STRINGS array.  */
330   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
331
332   /* Assume we are not debugging.  */
333   lexer->debugging_p = false;
334
335   return lexer;
336 }
337
338 /* Create a new lexer whose token stream is primed with the TOKENS.
339    When these tokens are exhausted, no new tokens will be read.  */
340
341 static cp_lexer *
342 cp_lexer_new_from_tokens (cp_token_cache *tokens)
343 {
344   cp_lexer *lexer;
345   cp_token *token;
346   cp_token_block *block;
347   ptrdiff_t num_tokens;
348
349   /* Allocate the memory.  */
350   lexer = ggc_alloc_cleared (sizeof (cp_lexer));
351
352   /* Create a new buffer, appropriately sized.  */
353   num_tokens = 0;
354   for (block = tokens->first; block != NULL; block = block->next)
355     num_tokens += block->num_tokens;
356   lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
357   lexer->buffer_end = lexer->buffer + num_tokens;
358   
359   /* Install the tokens.  */
360   token = lexer->buffer;
361   for (block = tokens->first; block != NULL; block = block->next)
362     {
363       memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
364       token += block->num_tokens;
365     }
366
367   /* The FIRST_TOKEN is the beginning of the buffer.  */
368   lexer->first_token = lexer->buffer;
369   /* The next available token is also at the beginning of the buffer.  */
370   lexer->next_token = lexer->buffer;
371   /* The buffer is full.  */
372   lexer->last_token = lexer->first_token;
373
374   /* This lexer doesn't obtain more tokens.  */
375   lexer->main_lexer_p = false;
376
377   /* Create the SAVED_TOKENS stack.  */
378   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
379   
380   /* Create the STRINGS array.  */
381   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
382
383   /* Assume we are not debugging.  */
384   lexer->debugging_p = false;
385
386   return lexer;
387 }
388
389 /* Returns nonzero if debugging information should be output.  */
390
391 static inline bool
392 cp_lexer_debugging_p (cp_lexer *lexer)
393 {
394   return lexer->debugging_p;
395 }
396
397 /* Set the current source position from the information stored in
398    TOKEN.  */
399
400 static inline void
401 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
402                                          const cp_token *token)
403 {
404   /* Ideally, the source position information would not be a global
405      variable, but it is.  */
406
407   /* Update the line number.  */
408   if (token->type != CPP_EOF)
409     input_location = token->location;
410 }
411
412 /* TOKEN points into the circular token buffer.  Return a pointer to
413    the next token in the buffer.  */
414
415 static inline cp_token *
416 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
417 {
418   token++;
419   if (token == lexer->buffer_end)
420     token = lexer->buffer;
421   return token;
422 }
423
424 /* TOKEN points into the circular token buffer.  Return a pointer to
425    the previous token in the buffer.  */
426
427 static inline cp_token *
428 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
429 {
430   if (token == lexer->buffer)
431     token = lexer->buffer_end;
432   return token - 1;
433 }
434
435 /* nonzero if we are presently saving tokens.  */
436
437 static int
438 cp_lexer_saving_tokens (const cp_lexer* lexer)
439 {
440   return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
441 }
442
443 /* Return a pointer to the token that is N tokens beyond TOKEN in the
444    buffer.  */
445
446 static cp_token *
447 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
448 {
449   token += n;
450   if (token >= lexer->buffer_end)
451     token = lexer->buffer + (token - lexer->buffer_end);
452   return token;
453 }
454
455 /* Returns the number of times that START would have to be incremented
456    to reach FINISH.  If START and FINISH are the same, returns zero.  */
457
458 static ptrdiff_t
459 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
460 {
461   if (finish >= start)
462     return finish - start;
463   else
464     return ((lexer->buffer_end - lexer->buffer)
465             - (start - finish));
466 }
467
468 /* Obtain another token from the C preprocessor and add it to the
469    token buffer.  Returns the newly read token.  */
470
471 static cp_token *
472 cp_lexer_read_token (cp_lexer* lexer)
473 {
474   cp_token *token;
475
476   /* Make sure there is room in the buffer.  */
477   cp_lexer_maybe_grow_buffer (lexer);
478
479   /* If there weren't any tokens, then this one will be the first.  */
480   if (!lexer->first_token)
481     lexer->first_token = lexer->last_token;
482   /* Similarly, if there were no available tokens, there is one now.  */
483   if (!lexer->next_token)
484     lexer->next_token = lexer->last_token;
485
486   /* Figure out where we're going to store the new token.  */
487   token = lexer->last_token;
488
489   /* Get a new token from the preprocessor.  */
490   cp_lexer_get_preprocessor_token (lexer, token);
491
492   /* Increment LAST_TOKEN.  */
493   lexer->last_token = cp_lexer_next_token (lexer, token);
494
495   /* Strings should have type `const char []'.  Right now, we will
496      have an ARRAY_TYPE that is constant rather than an array of
497      constant elements.
498      FIXME: Make fix_string_type get this right in the first place.  */
499   if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
500       && flag_const_strings)
501     {
502       tree type;
503
504       /* Get the current type.  It will be an ARRAY_TYPE.  */
505       type = TREE_TYPE (token->value);
506       /* Use build_cplus_array_type to rebuild the array, thereby
507          getting the right type.  */
508       type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
509       /* Reset the type of the token.  */
510       TREE_TYPE (token->value) = type;
511     }
512
513   return token;
514 }
515
516 /* If the circular buffer is full, make it bigger.  */
517
518 static void
519 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
520 {
521   /* If the buffer is full, enlarge it.  */
522   if (lexer->last_token == lexer->first_token)
523     {
524       cp_token *new_buffer;
525       cp_token *old_buffer;
526       cp_token *new_first_token;
527       ptrdiff_t buffer_length;
528       size_t num_tokens_to_copy;
529
530       /* Remember the current buffer pointer.  It will become invalid,
531          but we will need to do pointer arithmetic involving this
532          value.  */
533       old_buffer = lexer->buffer;
534       /* Compute the current buffer size.  */
535       buffer_length = lexer->buffer_end - lexer->buffer;
536       /* Allocate a buffer twice as big.  */
537       new_buffer = ggc_realloc (lexer->buffer, 
538                                 2 * buffer_length * sizeof (cp_token));
539       
540       /* Because the buffer is circular, logically consecutive tokens
541          are not necessarily placed consecutively in memory.
542          Therefore, we must keep move the tokens that were before
543          FIRST_TOKEN to the second half of the newly allocated
544          buffer.  */
545       num_tokens_to_copy = (lexer->first_token - old_buffer);
546       memcpy (new_buffer + buffer_length,
547               new_buffer,
548               num_tokens_to_copy * sizeof (cp_token));
549       /* Clear the rest of the buffer.  We never look at this storage,
550          but the garbage collector may.  */
551       memset (new_buffer + buffer_length + num_tokens_to_copy, 0, 
552               (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
553
554       /* Now recompute all of the buffer pointers.  */
555       new_first_token 
556         = new_buffer + (lexer->first_token - old_buffer);
557       if (lexer->next_token != NULL)
558         {
559           ptrdiff_t next_token_delta;
560
561           if (lexer->next_token > lexer->first_token)
562             next_token_delta = lexer->next_token - lexer->first_token;
563           else
564             next_token_delta = 
565               buffer_length - (lexer->first_token - lexer->next_token);
566           lexer->next_token = new_first_token + next_token_delta;
567         }
568       lexer->last_token = new_first_token + buffer_length;
569       lexer->buffer = new_buffer;
570       lexer->buffer_end = new_buffer + buffer_length * 2;
571       lexer->first_token = new_first_token;
572     }
573 }
574
575 /* Store the next token from the preprocessor in *TOKEN.  */
576
577 static void 
578 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
579                                  cp_token *token)
580 {
581   bool done;
582
583   /* If this not the main lexer, return a terminating CPP_EOF token.  */
584   if (lexer != NULL && !lexer->main_lexer_p)
585     {
586       token->type = CPP_EOF;
587       token->location.line = 0;
588       token->location.file = NULL;
589       token->value = NULL_TREE;
590       token->keyword = RID_MAX;
591
592       return;
593     }
594
595   done = false;
596   /* Keep going until we get a token we like.  */
597   while (!done)
598     {
599       /* Get a new token from the preprocessor.  */
600       token->type = c_lex (&token->value);
601       /* Issue messages about tokens we cannot process.  */
602       switch (token->type)
603         {
604         case CPP_ATSIGN:
605         case CPP_HASH:
606         case CPP_PASTE:
607           error ("invalid token");
608           break;
609
610         default:
611           /* This is a good token, so we exit the loop.  */
612           done = true;
613           break;
614         }
615     }
616   /* Now we've got our token.  */
617   token->location = input_location;
618
619   /* Check to see if this token is a keyword.  */
620   if (token->type == CPP_NAME 
621       && C_IS_RESERVED_WORD (token->value))
622     {
623       /* Mark this token as a keyword.  */
624       token->type = CPP_KEYWORD;
625       /* Record which keyword.  */
626       token->keyword = C_RID_CODE (token->value);
627       /* Update the value.  Some keywords are mapped to particular
628          entities, rather than simply having the value of the
629          corresponding IDENTIFIER_NODE.  For example, `__const' is
630          mapped to `const'.  */
631       token->value = ridpointers[token->keyword];
632     }
633   else
634     token->keyword = RID_MAX;
635 }
636
637 /* Return a pointer to the next token in the token stream, but do not
638    consume it.  */
639
640 static cp_token *
641 cp_lexer_peek_token (cp_lexer* lexer)
642 {
643   cp_token *token;
644
645   /* If there are no tokens, read one now.  */
646   if (!lexer->next_token)
647     cp_lexer_read_token (lexer);
648
649   /* Provide debugging output.  */
650   if (cp_lexer_debugging_p (lexer))
651     {
652       fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
653       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
654       fprintf (cp_lexer_debug_stream, "\n");
655     }
656
657   token = lexer->next_token;
658   cp_lexer_set_source_position_from_token (lexer, token);
659   return token;
660 }
661
662 /* Return true if the next token has the indicated TYPE.  */
663
664 static bool
665 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
666 {
667   cp_token *token;
668
669   /* Peek at the next token.  */
670   token = cp_lexer_peek_token (lexer);
671   /* Check to see if it has the indicated TYPE.  */
672   return token->type == type;
673 }
674
675 /* Return true if the next token does not have the indicated TYPE.  */
676
677 static bool
678 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
679 {
680   return !cp_lexer_next_token_is (lexer, type);
681 }
682
683 /* Return true if the next token is the indicated KEYWORD.  */
684
685 static bool
686 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
687 {
688   cp_token *token;
689
690   /* Peek at the next token.  */
691   token = cp_lexer_peek_token (lexer);
692   /* Check to see if it is the indicated keyword.  */
693   return token->keyword == keyword;
694 }
695
696 /* Return a pointer to the Nth token in the token stream.  If N is 1,
697    then this is precisely equivalent to cp_lexer_peek_token.  */
698
699 static cp_token *
700 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
701 {
702   cp_token *token;
703
704   /* N is 1-based, not zero-based.  */
705   my_friendly_assert (n > 0, 20000224);
706
707   /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary.  */
708   token = lexer->next_token;
709   /* If there are no tokens in the buffer, get one now.  */
710   if (!token)
711     {
712       cp_lexer_read_token (lexer);
713       token = lexer->next_token;
714     }
715
716   /* Now, read tokens until we have enough.  */
717   while (--n > 0)
718     {
719       /* Advance to the next token.  */
720       token = cp_lexer_next_token (lexer, token);
721       /* If that's all the tokens we have, read a new one.  */
722       if (token == lexer->last_token)
723         token = cp_lexer_read_token (lexer);
724     }
725
726   return token;
727 }
728
729 /* Consume the next token.  The pointer returned is valid only until
730    another token is read.  Callers should preserve copy the token
731    explicitly if they will need its value for a longer period of
732    time.  */
733
734 static cp_token *
735 cp_lexer_consume_token (cp_lexer* lexer)
736 {
737   cp_token *token;
738
739   /* If there are no tokens, read one now.  */
740   if (!lexer->next_token)
741     cp_lexer_read_token (lexer);
742
743   /* Remember the token we'll be returning.  */
744   token = lexer->next_token;
745
746   /* Increment NEXT_TOKEN.  */
747   lexer->next_token = cp_lexer_next_token (lexer, 
748                                            lexer->next_token);
749   /* Check to see if we're all out of tokens.  */
750   if (lexer->next_token == lexer->last_token)
751     lexer->next_token = NULL;
752
753   /* If we're not saving tokens, then move FIRST_TOKEN too.  */
754   if (!cp_lexer_saving_tokens (lexer))
755     {
756       /* If there are no tokens available, set FIRST_TOKEN to NULL.  */
757       if (!lexer->next_token)
758         lexer->first_token = NULL;
759       else
760         lexer->first_token = lexer->next_token;
761     }
762
763   /* Provide debugging output.  */
764   if (cp_lexer_debugging_p (lexer))
765     {
766       fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
767       cp_lexer_print_token (cp_lexer_debug_stream, token);
768       fprintf (cp_lexer_debug_stream, "\n");
769     }
770
771   return token;
772 }
773
774 /* Permanently remove the next token from the token stream.  There
775    must be a valid next token already; this token never reads
776    additional tokens from the preprocessor.  */
777
778 static void
779 cp_lexer_purge_token (cp_lexer *lexer)
780 {
781   cp_token *token;
782   cp_token *next_token;
783
784   token = lexer->next_token;
785   while (true) 
786     {
787       next_token = cp_lexer_next_token (lexer, token);
788       if (next_token == lexer->last_token)
789         break;
790       *token = *next_token;
791       token = next_token;
792     }
793
794   lexer->last_token = token;
795   /* The token purged may have been the only token remaining; if so,
796      clear NEXT_TOKEN.  */
797   if (lexer->next_token == token)
798     lexer->next_token = NULL;
799 }
800
801 /* Permanently remove all tokens after TOKEN, up to, but not
802    including, the token that will be returned next by
803    cp_lexer_peek_token.  */
804
805 static void
806 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
807 {
808   cp_token *peek;
809   cp_token *t1;
810   cp_token *t2;
811
812   if (lexer->next_token)
813     {
814       /* Copy the tokens that have not yet been read to the location
815          immediately following TOKEN.  */
816       t1 = cp_lexer_next_token (lexer, token);
817       t2 = peek = cp_lexer_peek_token (lexer);
818       /* Move tokens into the vacant area between TOKEN and PEEK.  */
819       while (t2 != lexer->last_token)
820         {
821           *t1 = *t2;
822           t1 = cp_lexer_next_token (lexer, t1);
823           t2 = cp_lexer_next_token (lexer, t2);
824         }
825       /* Now, the next available token is right after TOKEN.  */
826       lexer->next_token = cp_lexer_next_token (lexer, token);
827       /* And the last token is wherever we ended up.  */
828       lexer->last_token = t1;
829     }
830   else
831     {
832       /* There are no tokens in the buffer, so there is nothing to
833          copy.  The last token in the buffer is TOKEN itself.  */
834       lexer->last_token = cp_lexer_next_token (lexer, token);
835     }
836 }
837
838 /* Begin saving tokens.  All tokens consumed after this point will be
839    preserved.  */
840
841 static void
842 cp_lexer_save_tokens (cp_lexer* lexer)
843 {
844   /* Provide debugging output.  */
845   if (cp_lexer_debugging_p (lexer))
846     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
847
848   /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
849      restore the tokens if required.  */
850   if (!lexer->next_token)
851     cp_lexer_read_token (lexer);
852
853   VARRAY_PUSH_INT (lexer->saved_tokens,
854                    cp_lexer_token_difference (lexer,
855                                               lexer->first_token,
856                                               lexer->next_token));
857 }
858
859 /* Commit to the portion of the token stream most recently saved.  */
860
861 static void
862 cp_lexer_commit_tokens (cp_lexer* lexer)
863 {
864   /* Provide debugging output.  */
865   if (cp_lexer_debugging_p (lexer))
866     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
867
868   VARRAY_POP (lexer->saved_tokens);
869 }
870
871 /* Return all tokens saved since the last call to cp_lexer_save_tokens
872    to the token stream.  Stop saving tokens.  */
873
874 static void
875 cp_lexer_rollback_tokens (cp_lexer* lexer)
876 {
877   size_t delta;
878
879   /* Provide debugging output.  */
880   if (cp_lexer_debugging_p (lexer))
881     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
882
883   /* Find the token that was the NEXT_TOKEN when we started saving
884      tokens.  */
885   delta = VARRAY_TOP_INT(lexer->saved_tokens);
886   /* Make it the next token again now.  */
887   lexer->next_token = cp_lexer_advance_token (lexer,
888                                               lexer->first_token, 
889                                               delta);
890   /* It might be the case that there were no tokens when we started
891      saving tokens, but that there are some tokens now.  */
892   if (!lexer->next_token && lexer->first_token)
893     lexer->next_token = lexer->first_token;
894
895   /* Stop saving tokens.  */
896   VARRAY_POP (lexer->saved_tokens);
897 }
898
899 /* Print a representation of the TOKEN on the STREAM.  */
900
901 static void
902 cp_lexer_print_token (FILE * stream, cp_token* token)
903 {
904   const char *token_type = NULL;
905
906   /* Figure out what kind of token this is.  */
907   switch (token->type)
908     {
909     case CPP_EQ:
910       token_type = "EQ";
911       break;
912
913     case CPP_COMMA:
914       token_type = "COMMA";
915       break;
916
917     case CPP_OPEN_PAREN:
918       token_type = "OPEN_PAREN";
919       break;
920
921     case CPP_CLOSE_PAREN:
922       token_type = "CLOSE_PAREN";
923       break;
924
925     case CPP_OPEN_BRACE:
926       token_type = "OPEN_BRACE";
927       break;
928
929     case CPP_CLOSE_BRACE:
930       token_type = "CLOSE_BRACE";
931       break;
932
933     case CPP_SEMICOLON:
934       token_type = "SEMICOLON";
935       break;
936
937     case CPP_NAME:
938       token_type = "NAME";
939       break;
940
941     case CPP_EOF:
942       token_type = "EOF";
943       break;
944
945     case CPP_KEYWORD:
946       token_type = "keyword";
947       break;
948
949       /* This is not a token that we know how to handle yet.  */
950     default:
951       break;
952     }
953
954   /* If we have a name for the token, print it out.  Otherwise, we
955      simply give the numeric code.  */
956   if (token_type)
957     fprintf (stream, "%s", token_type);
958   else
959     fprintf (stream, "%d", token->type);
960   /* And, for an identifier, print the identifier name.  */
961   if (token->type == CPP_NAME 
962       /* Some keywords have a value that is not an IDENTIFIER_NODE.
963          For example, `struct' is mapped to an INTEGER_CST.  */
964       || (token->type == CPP_KEYWORD 
965           && TREE_CODE (token->value) == IDENTIFIER_NODE))
966     fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
967 }
968
969 /* Start emitting debugging information.  */
970
971 static void
972 cp_lexer_start_debugging (cp_lexer* lexer)
973 {
974   ++lexer->debugging_p;
975 }
976   
977 /* Stop emitting debugging information.  */
978
979 static void
980 cp_lexer_stop_debugging (cp_lexer* lexer)
981 {
982   --lexer->debugging_p;
983 }
984
985 \f
986 /* The parser.  */
987
988 /* Overview
989    --------
990
991    A cp_parser parses the token stream as specified by the C++
992    grammar.  Its job is purely parsing, not semantic analysis.  For
993    example, the parser breaks the token stream into declarators,
994    expressions, statements, and other similar syntactic constructs.
995    It does not check that the types of the expressions on either side
996    of an assignment-statement are compatible, or that a function is
997    not declared with a parameter of type `void'.
998
999    The parser invokes routines elsewhere in the compiler to perform
1000    semantic analysis and to build up the abstract syntax tree for the
1001    code processed.  
1002
1003    The parser (and the template instantiation code, which is, in a
1004    way, a close relative of parsing) are the only parts of the
1005    compiler that should be calling push_scope and pop_scope, or
1006    related functions.  The parser (and template instantiation code)
1007    keeps track of what scope is presently active; everything else
1008    should simply honor that.  (The code that generates static
1009    initializers may also need to set the scope, in order to check
1010    access control correctly when emitting the initializers.)
1011
1012    Methodology
1013    -----------
1014    
1015    The parser is of the standard recursive-descent variety.  Upcoming
1016    tokens in the token stream are examined in order to determine which
1017    production to use when parsing a non-terminal.  Some C++ constructs
1018    require arbitrary look ahead to disambiguate.  For example, it is
1019    impossible, in the general case, to tell whether a statement is an
1020    expression or declaration without scanning the entire statement.
1021    Therefore, the parser is capable of "parsing tentatively."  When the
1022    parser is not sure what construct comes next, it enters this mode.
1023    Then, while we attempt to parse the construct, the parser queues up
1024    error messages, rather than issuing them immediately, and saves the
1025    tokens it consumes.  If the construct is parsed successfully, the
1026    parser "commits", i.e., it issues any queued error messages and
1027    the tokens that were being preserved are permanently discarded.
1028    If, however, the construct is not parsed successfully, the parser
1029    rolls back its state completely so that it can resume parsing using
1030    a different alternative.
1031
1032    Future Improvements
1033    -------------------
1034    
1035    The performance of the parser could probably be improved
1036    substantially.  Some possible improvements include:
1037
1038      - The expression parser recurses through the various levels of
1039        precedence as specified in the grammar, rather than using an
1040        operator-precedence technique.  Therefore, parsing a simple
1041        identifier requires multiple recursive calls.
1042
1043      - We could often eliminate the need to parse tentatively by
1044        looking ahead a little bit.  In some places, this approach
1045        might not entirely eliminate the need to parse tentatively, but
1046        it might still speed up the average case.  */
1047
1048 /* Flags that are passed to some parsing functions.  These values can
1049    be bitwise-ored together.  */
1050
1051 typedef enum cp_parser_flags
1052 {
1053   /* No flags.  */
1054   CP_PARSER_FLAGS_NONE = 0x0,
1055   /* The construct is optional.  If it is not present, then no error
1056      should be issued.  */
1057   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1058   /* When parsing a type-specifier, do not allow user-defined types.  */
1059   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1060 } cp_parser_flags;
1061
1062 /* The different kinds of declarators we want to parse.  */
1063
1064 typedef enum cp_parser_declarator_kind
1065 {
1066   /* We want an abstract declartor.  */
1067   CP_PARSER_DECLARATOR_ABSTRACT,
1068   /* We want a named declarator.  */
1069   CP_PARSER_DECLARATOR_NAMED,
1070   /* We don't mind, but the name must be an unqualified-id.  */
1071   CP_PARSER_DECLARATOR_EITHER
1072 } cp_parser_declarator_kind;
1073
1074 /* A mapping from a token type to a corresponding tree node type.  */
1075
1076 typedef struct cp_parser_token_tree_map_node
1077 {
1078   /* The token type.  */
1079   ENUM_BITFIELD (cpp_ttype) token_type : 8;
1080   /* The corresponding tree code.  */
1081   ENUM_BITFIELD (tree_code) tree_type : 8;
1082 } cp_parser_token_tree_map_node;
1083
1084 /* A complete map consists of several ordinary entries, followed by a
1085    terminator.  The terminating entry has a token_type of CPP_EOF.  */
1086
1087 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1088
1089 /* The status of a tentative parse.  */
1090
1091 typedef enum cp_parser_status_kind
1092 {
1093   /* No errors have occurred.  */
1094   CP_PARSER_STATUS_KIND_NO_ERROR,
1095   /* An error has occurred.  */
1096   CP_PARSER_STATUS_KIND_ERROR,
1097   /* We are committed to this tentative parse, whether or not an error
1098      has occurred.  */
1099   CP_PARSER_STATUS_KIND_COMMITTED
1100 } cp_parser_status_kind;
1101
1102 /* Context that is saved and restored when parsing tentatively.  */
1103
1104 typedef struct cp_parser_context GTY (())
1105 {
1106   /* If this is a tentative parsing context, the status of the
1107      tentative parse.  */
1108   enum cp_parser_status_kind status;
1109   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1110      that are looked up in this context must be looked up both in the
1111      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112      the context of the containing expression.  */
1113   tree object_type;
1114   /* The next parsing context in the stack.  */
1115   struct cp_parser_context *next;
1116 } cp_parser_context;
1117
1118 /* Prototypes.  */
1119
1120 /* Constructors and destructors.  */
1121
1122 static cp_parser_context *cp_parser_context_new
1123   (cp_parser_context *);
1124
1125 /* Class variables.  */
1126
1127 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1128
1129 /* Constructors and destructors.  */
1130
1131 /* Construct a new context.  The context below this one on the stack
1132    is given by NEXT.  */
1133
1134 static cp_parser_context *
1135 cp_parser_context_new (cp_parser_context* next)
1136 {
1137   cp_parser_context *context;
1138
1139   /* Allocate the storage.  */
1140   if (cp_parser_context_free_list != NULL)
1141     {
1142       /* Pull the first entry from the free list.  */
1143       context = cp_parser_context_free_list;
1144       cp_parser_context_free_list = context->next;
1145       memset (context, 0, sizeof (*context));
1146     }
1147   else
1148     context = ggc_alloc_cleared (sizeof (cp_parser_context));
1149   /* No errors have occurred yet in this context.  */
1150   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1151   /* If this is not the bottomost context, copy information that we
1152      need from the previous context.  */
1153   if (next)
1154     {
1155       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1156          expression, then we are parsing one in this context, too.  */
1157       context->object_type = next->object_type;
1158       /* Thread the stack.  */
1159       context->next = next;
1160     }
1161
1162   return context;
1163 }
1164
1165 /* The cp_parser structure represents the C++ parser.  */
1166
1167 typedef struct cp_parser GTY(())
1168 {
1169   /* The lexer from which we are obtaining tokens.  */
1170   cp_lexer *lexer;
1171
1172   /* The scope in which names should be looked up.  If NULL_TREE, then
1173      we look up names in the scope that is currently open in the
1174      source program.  If non-NULL, this is either a TYPE or
1175      NAMESPACE_DECL for the scope in which we should look.  
1176
1177      This value is not cleared automatically after a name is looked
1178      up, so we must be careful to clear it before starting a new look
1179      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1180      will look up `Z' in the scope of `X', rather than the current
1181      scope.)  Unfortunately, it is difficult to tell when name lookup
1182      is complete, because we sometimes peek at a token, look it up,
1183      and then decide not to consume it.  */
1184   tree scope;
1185
1186   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1187      last lookup took place.  OBJECT_SCOPE is used if an expression
1188      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1189      respectively.  QUALIFYING_SCOPE is used for an expression of the 
1190      form "X::Y"; it refers to X.  */
1191   tree object_scope;
1192   tree qualifying_scope;
1193
1194   /* A stack of parsing contexts.  All but the bottom entry on the
1195      stack will be tentative contexts.
1196
1197      We parse tentatively in order to determine which construct is in
1198      use in some situations.  For example, in order to determine
1199      whether a statement is an expression-statement or a
1200      declaration-statement we parse it tentatively as a
1201      declaration-statement.  If that fails, we then reparse the same
1202      token stream as an expression-statement.  */
1203   cp_parser_context *context;
1204
1205   /* True if we are parsing GNU C++.  If this flag is not set, then
1206      GNU extensions are not recognized.  */
1207   bool allow_gnu_extensions_p;
1208
1209   /* TRUE if the `>' token should be interpreted as the greater-than
1210      operator.  FALSE if it is the end of a template-id or
1211      template-parameter-list.  */
1212   bool greater_than_is_operator_p;
1213
1214   /* TRUE if default arguments are allowed within a parameter list
1215      that starts at this point. FALSE if only a gnu extension makes
1216      them permissible.  */
1217   bool default_arg_ok_p;
1218   
1219   /* TRUE if we are parsing an integral constant-expression.  See
1220      [expr.const] for a precise definition.  */
1221   bool constant_expression_p;
1222
1223   /* TRUE if we are parsing an integral constant-expression -- but a
1224      non-constant expression should be permitted as well.  This flag
1225      is used when parsing an array bound so that GNU variable-length
1226      arrays are tolerated.  */
1227   bool allow_non_constant_expression_p;
1228
1229   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1230      been seen that makes the expression non-constant.  */
1231   bool non_constant_expression_p;
1232
1233   /* TRUE if local variable names and `this' are forbidden in the
1234      current context.  */
1235   bool local_variables_forbidden_p;
1236
1237   /* TRUE if the declaration we are parsing is part of a
1238      linkage-specification of the form `extern string-literal
1239      declaration'.  */
1240   bool in_unbraced_linkage_specification_p;
1241
1242   /* TRUE if we are presently parsing a declarator, after the
1243      direct-declarator.  */
1244   bool in_declarator_p;
1245
1246   /* TRUE if we are presently parsing the body of an
1247      iteration-statement.  */
1248   bool in_iteration_statement_p;
1249
1250   /* TRUE if we are presently parsing the body of a switch
1251      statement.  */
1252   bool in_switch_statement_p;
1253
1254   /* If non-NULL, then we are parsing a construct where new type
1255      definitions are not permitted.  The string stored here will be
1256      issued as an error message if a type is defined.  */
1257   const char *type_definition_forbidden_message;
1258
1259   /* A list of lists. The outer list is a stack, used for member
1260      functions of local classes. At each level there are two sub-list,
1261      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1262      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1263      TREE_VALUE's. The functions are chained in reverse declaration
1264      order.
1265
1266      The TREE_PURPOSE sublist contains those functions with default
1267      arguments that need post processing, and the TREE_VALUE sublist
1268      contains those functions with definitions that need post
1269      processing.
1270
1271      These lists can only be processed once the outermost class being
1272      defined is complete.  */
1273   tree unparsed_functions_queues;
1274
1275   /* The number of classes whose definitions are currently in
1276      progress.  */
1277   unsigned num_classes_being_defined;
1278
1279   /* The number of template parameter lists that apply directly to the
1280      current declaration.  */
1281   unsigned num_template_parameter_lists;
1282 } cp_parser;
1283
1284 /* The type of a function that parses some kind of expression.  */
1285 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1286
1287 /* Prototypes.  */
1288
1289 /* Constructors and destructors.  */
1290
1291 static cp_parser *cp_parser_new
1292   (void);
1293
1294 /* Routines to parse various constructs.  
1295
1296    Those that return `tree' will return the error_mark_node (rather
1297    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1298    Sometimes, they will return an ordinary node if error-recovery was
1299    attempted, even though a parse error occurred.  So, to check
1300    whether or not a parse error occurred, you should always use
1301    cp_parser_error_occurred.  If the construct is optional (indicated
1302    either by an `_opt' in the name of the function that does the
1303    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1304    the construct is not present.  */
1305
1306 /* Lexical conventions [gram.lex]  */
1307
1308 static tree cp_parser_identifier
1309   (cp_parser *);
1310
1311 /* Basic concepts [gram.basic]  */
1312
1313 static bool cp_parser_translation_unit
1314   (cp_parser *);
1315
1316 /* Expressions [gram.expr]  */
1317
1318 static tree cp_parser_primary_expression
1319   (cp_parser *, cp_id_kind *, tree *);
1320 static tree cp_parser_id_expression
1321   (cp_parser *, bool, bool, bool *, bool);
1322 static tree cp_parser_unqualified_id
1323   (cp_parser *, bool, bool, bool);
1324 static tree cp_parser_nested_name_specifier_opt
1325   (cp_parser *, bool, bool, bool, bool);
1326 static tree cp_parser_nested_name_specifier
1327   (cp_parser *, bool, bool, bool, bool);
1328 static tree cp_parser_class_or_namespace_name
1329   (cp_parser *, bool, bool, bool, bool, bool);
1330 static tree cp_parser_postfix_expression
1331   (cp_parser *, bool);
1332 static tree cp_parser_parenthesized_expression_list
1333   (cp_parser *, bool, bool *);
1334 static void cp_parser_pseudo_destructor_name
1335   (cp_parser *, tree *, tree *);
1336 static tree cp_parser_unary_expression
1337   (cp_parser *, bool);
1338 static enum tree_code cp_parser_unary_operator
1339   (cp_token *);
1340 static tree cp_parser_new_expression
1341   (cp_parser *);
1342 static tree cp_parser_new_placement
1343   (cp_parser *);
1344 static tree cp_parser_new_type_id
1345   (cp_parser *);
1346 static tree cp_parser_new_declarator_opt
1347   (cp_parser *);
1348 static tree cp_parser_direct_new_declarator
1349   (cp_parser *);
1350 static tree cp_parser_new_initializer
1351   (cp_parser *);
1352 static tree cp_parser_delete_expression
1353   (cp_parser *);
1354 static tree cp_parser_cast_expression 
1355   (cp_parser *, bool);
1356 static tree cp_parser_pm_expression
1357   (cp_parser *);
1358 static tree cp_parser_multiplicative_expression
1359   (cp_parser *);
1360 static tree cp_parser_additive_expression
1361   (cp_parser *);
1362 static tree cp_parser_shift_expression
1363   (cp_parser *);
1364 static tree cp_parser_relational_expression
1365   (cp_parser *);
1366 static tree cp_parser_equality_expression
1367   (cp_parser *);
1368 static tree cp_parser_and_expression
1369   (cp_parser *);
1370 static tree cp_parser_exclusive_or_expression
1371   (cp_parser *);
1372 static tree cp_parser_inclusive_or_expression
1373   (cp_parser *);
1374 static tree cp_parser_logical_and_expression
1375   (cp_parser *);
1376 static tree cp_parser_logical_or_expression 
1377   (cp_parser *);
1378 static tree cp_parser_question_colon_clause
1379   (cp_parser *, tree);
1380 static tree cp_parser_assignment_expression
1381   (cp_parser *);
1382 static enum tree_code cp_parser_assignment_operator_opt
1383   (cp_parser *);
1384 static tree cp_parser_expression
1385   (cp_parser *);
1386 static tree cp_parser_constant_expression
1387   (cp_parser *, bool, bool *);
1388
1389 /* Statements [gram.stmt.stmt]  */
1390
1391 static void cp_parser_statement
1392   (cp_parser *, bool);
1393 static tree cp_parser_labeled_statement
1394   (cp_parser *, bool);
1395 static tree cp_parser_expression_statement
1396   (cp_parser *, bool);
1397 static tree cp_parser_compound_statement
1398   (cp_parser *, bool);
1399 static void cp_parser_statement_seq_opt
1400   (cp_parser *, bool);
1401 static tree cp_parser_selection_statement
1402   (cp_parser *);
1403 static tree cp_parser_condition
1404   (cp_parser *);
1405 static tree cp_parser_iteration_statement
1406   (cp_parser *);
1407 static void cp_parser_for_init_statement
1408   (cp_parser *);
1409 static tree cp_parser_jump_statement
1410   (cp_parser *);
1411 static void cp_parser_declaration_statement
1412   (cp_parser *);
1413
1414 static tree cp_parser_implicitly_scoped_statement
1415   (cp_parser *);
1416 static void cp_parser_already_scoped_statement
1417   (cp_parser *);
1418
1419 /* Declarations [gram.dcl.dcl] */
1420
1421 static void cp_parser_declaration_seq_opt
1422   (cp_parser *);
1423 static void cp_parser_declaration
1424   (cp_parser *);
1425 static void cp_parser_block_declaration
1426   (cp_parser *, bool);
1427 static void cp_parser_simple_declaration
1428   (cp_parser *, bool);
1429 static tree cp_parser_decl_specifier_seq 
1430   (cp_parser *, cp_parser_flags, tree *, int *);
1431 static tree cp_parser_storage_class_specifier_opt
1432   (cp_parser *);
1433 static tree cp_parser_function_specifier_opt
1434   (cp_parser *);
1435 static tree cp_parser_type_specifier
1436   (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1437 static tree cp_parser_simple_type_specifier
1438   (cp_parser *, cp_parser_flags, bool);
1439 static tree cp_parser_type_name
1440   (cp_parser *);
1441 static tree cp_parser_elaborated_type_specifier
1442   (cp_parser *, bool, bool);
1443 static tree cp_parser_enum_specifier
1444   (cp_parser *);
1445 static void cp_parser_enumerator_list
1446   (cp_parser *, tree);
1447 static void cp_parser_enumerator_definition 
1448   (cp_parser *, tree);
1449 static tree cp_parser_namespace_name
1450   (cp_parser *);
1451 static void cp_parser_namespace_definition
1452   (cp_parser *);
1453 static void cp_parser_namespace_body
1454   (cp_parser *);
1455 static tree cp_parser_qualified_namespace_specifier
1456   (cp_parser *);
1457 static void cp_parser_namespace_alias_definition
1458   (cp_parser *);
1459 static void cp_parser_using_declaration
1460   (cp_parser *);
1461 static void cp_parser_using_directive
1462   (cp_parser *);
1463 static void cp_parser_asm_definition
1464   (cp_parser *);
1465 static void cp_parser_linkage_specification
1466   (cp_parser *);
1467
1468 /* Declarators [gram.dcl.decl] */
1469
1470 static tree cp_parser_init_declarator
1471   (cp_parser *, tree, tree, bool, bool, int, bool *);
1472 static tree cp_parser_declarator
1473   (cp_parser *, cp_parser_declarator_kind, int *);
1474 static tree cp_parser_direct_declarator
1475   (cp_parser *, cp_parser_declarator_kind, int *);
1476 static enum tree_code cp_parser_ptr_operator
1477   (cp_parser *, tree *, tree *);
1478 static tree cp_parser_cv_qualifier_seq_opt
1479   (cp_parser *);
1480 static tree cp_parser_cv_qualifier_opt
1481   (cp_parser *);
1482 static tree cp_parser_declarator_id
1483   (cp_parser *);
1484 static tree cp_parser_type_id
1485   (cp_parser *);
1486 static tree cp_parser_type_specifier_seq
1487   (cp_parser *);
1488 static tree cp_parser_parameter_declaration_clause
1489   (cp_parser *);
1490 static tree cp_parser_parameter_declaration_list
1491   (cp_parser *);
1492 static tree cp_parser_parameter_declaration
1493   (cp_parser *, bool);
1494 static tree cp_parser_function_definition
1495   (cp_parser *, bool *);
1496 static void cp_parser_function_body
1497   (cp_parser *);
1498 static tree cp_parser_initializer
1499   (cp_parser *, bool *, bool *);
1500 static tree cp_parser_initializer_clause
1501   (cp_parser *, bool *);
1502 static tree cp_parser_initializer_list
1503   (cp_parser *, bool *);
1504
1505 static bool cp_parser_ctor_initializer_opt_and_function_body
1506   (cp_parser *);
1507
1508 /* Classes [gram.class] */
1509
1510 static tree cp_parser_class_name
1511   (cp_parser *, bool, bool, bool, bool, bool, bool);
1512 static tree cp_parser_class_specifier
1513   (cp_parser *);
1514 static tree cp_parser_class_head
1515   (cp_parser *, bool *);
1516 static enum tag_types cp_parser_class_key
1517   (cp_parser *);
1518 static void cp_parser_member_specification_opt
1519   (cp_parser *);
1520 static void cp_parser_member_declaration
1521   (cp_parser *);
1522 static tree cp_parser_pure_specifier
1523   (cp_parser *);
1524 static tree cp_parser_constant_initializer
1525   (cp_parser *);
1526
1527 /* Derived classes [gram.class.derived] */
1528
1529 static tree cp_parser_base_clause
1530   (cp_parser *);
1531 static tree cp_parser_base_specifier
1532   (cp_parser *);
1533
1534 /* Special member functions [gram.special] */
1535
1536 static tree cp_parser_conversion_function_id
1537   (cp_parser *);
1538 static tree cp_parser_conversion_type_id
1539   (cp_parser *);
1540 static tree cp_parser_conversion_declarator_opt
1541   (cp_parser *);
1542 static bool cp_parser_ctor_initializer_opt
1543   (cp_parser *);
1544 static void cp_parser_mem_initializer_list
1545   (cp_parser *);
1546 static tree cp_parser_mem_initializer
1547   (cp_parser *);
1548 static tree cp_parser_mem_initializer_id
1549   (cp_parser *);
1550
1551 /* Overloading [gram.over] */
1552
1553 static tree cp_parser_operator_function_id
1554   (cp_parser *);
1555 static tree cp_parser_operator
1556   (cp_parser *);
1557
1558 /* Templates [gram.temp] */
1559
1560 static void cp_parser_template_declaration
1561   (cp_parser *, bool);
1562 static tree cp_parser_template_parameter_list
1563   (cp_parser *);
1564 static tree cp_parser_template_parameter
1565   (cp_parser *);
1566 static tree cp_parser_type_parameter
1567   (cp_parser *);
1568 static tree cp_parser_template_id
1569   (cp_parser *, bool, bool, bool);
1570 static tree cp_parser_template_name
1571   (cp_parser *, bool, bool, bool, bool *);
1572 static tree cp_parser_template_argument_list
1573   (cp_parser *);
1574 static tree cp_parser_template_argument
1575   (cp_parser *);
1576 static void cp_parser_explicit_instantiation
1577   (cp_parser *);
1578 static void cp_parser_explicit_specialization
1579   (cp_parser *);
1580
1581 /* Exception handling [gram.exception] */
1582
1583 static tree cp_parser_try_block 
1584   (cp_parser *);
1585 static bool cp_parser_function_try_block
1586   (cp_parser *);
1587 static void cp_parser_handler_seq
1588   (cp_parser *);
1589 static void cp_parser_handler
1590   (cp_parser *);
1591 static tree cp_parser_exception_declaration
1592   (cp_parser *);
1593 static tree cp_parser_throw_expression
1594   (cp_parser *);
1595 static tree cp_parser_exception_specification_opt
1596   (cp_parser *);
1597 static tree cp_parser_type_id_list
1598   (cp_parser *);
1599
1600 /* GNU Extensions */
1601
1602 static tree cp_parser_asm_specification_opt
1603   (cp_parser *);
1604 static tree cp_parser_asm_operand_list
1605   (cp_parser *);
1606 static tree cp_parser_asm_clobber_list
1607   (cp_parser *);
1608 static tree cp_parser_attributes_opt
1609   (cp_parser *);
1610 static tree cp_parser_attribute_list
1611   (cp_parser *);
1612 static bool cp_parser_extension_opt
1613   (cp_parser *, int *);
1614 static void cp_parser_label_declaration
1615   (cp_parser *);
1616
1617 /* Utility Routines */
1618
1619 static tree cp_parser_lookup_name
1620   (cp_parser *, tree, bool, bool, bool);
1621 static tree cp_parser_lookup_name_simple
1622   (cp_parser *, tree);
1623 static tree cp_parser_maybe_treat_template_as_class
1624   (tree, bool);
1625 static bool cp_parser_check_declarator_template_parameters
1626   (cp_parser *, tree);
1627 static bool cp_parser_check_template_parameters
1628   (cp_parser *, unsigned);
1629 static tree cp_parser_simple_cast_expression
1630   (cp_parser *);
1631 static tree cp_parser_binary_expression
1632   (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1633 static tree cp_parser_global_scope_opt
1634   (cp_parser *, bool);
1635 static bool cp_parser_constructor_declarator_p
1636   (cp_parser *, bool);
1637 static tree cp_parser_function_definition_from_specifiers_and_declarator
1638   (cp_parser *, tree, tree, tree);
1639 static tree cp_parser_function_definition_after_declarator
1640   (cp_parser *, bool);
1641 static void cp_parser_template_declaration_after_export
1642   (cp_parser *, bool);
1643 static tree cp_parser_single_declaration
1644   (cp_parser *, bool, bool *);
1645 static tree cp_parser_functional_cast
1646   (cp_parser *, tree);
1647 static tree cp_parser_enclosed_template_argument_list
1648   (cp_parser *);
1649 static void cp_parser_save_default_args
1650   (cp_parser *, tree);
1651 static void cp_parser_late_parsing_for_member
1652   (cp_parser *, tree);
1653 static void cp_parser_late_parsing_default_args
1654   (cp_parser *, tree);
1655 static tree cp_parser_sizeof_operand
1656   (cp_parser *, enum rid);
1657 static bool cp_parser_declares_only_class_p
1658   (cp_parser *);
1659 static tree cp_parser_fold_non_dependent_expr
1660   (tree);
1661 static bool cp_parser_friend_p
1662   (tree);
1663 static cp_token *cp_parser_require
1664   (cp_parser *, enum cpp_ttype, const char *);
1665 static cp_token *cp_parser_require_keyword
1666   (cp_parser *, enum rid, const char *);
1667 static bool cp_parser_token_starts_function_definition_p 
1668   (cp_token *);
1669 static bool cp_parser_next_token_starts_class_definition_p
1670   (cp_parser *);
1671 static bool cp_parser_next_token_ends_template_argument_p
1672   (cp_parser *);
1673 static enum tag_types cp_parser_token_is_class_key
1674   (cp_token *);
1675 static void cp_parser_check_class_key
1676   (enum tag_types, tree type);
1677 static void cp_parser_check_access_in_redeclaration
1678   (tree type);
1679 static bool cp_parser_optional_template_keyword
1680   (cp_parser *);
1681 static void cp_parser_pre_parsed_nested_name_specifier 
1682   (cp_parser *);
1683 static void cp_parser_cache_group
1684   (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1685 static void cp_parser_parse_tentatively 
1686   (cp_parser *);
1687 static void cp_parser_commit_to_tentative_parse
1688   (cp_parser *);
1689 static void cp_parser_abort_tentative_parse
1690   (cp_parser *);
1691 static bool cp_parser_parse_definitely
1692   (cp_parser *);
1693 static inline bool cp_parser_parsing_tentatively
1694   (cp_parser *);
1695 static bool cp_parser_committed_to_tentative_parse
1696   (cp_parser *);
1697 static void cp_parser_error
1698   (cp_parser *, const char *);
1699 static bool cp_parser_simulate_error
1700   (cp_parser *);
1701 static void cp_parser_check_type_definition
1702   (cp_parser *);
1703 static void cp_parser_check_for_definition_in_return_type
1704   (tree, int);
1705 static void cp_parser_check_for_invalid_template_id
1706   (cp_parser *, tree);
1707 static tree cp_parser_non_constant_expression
1708   (const char *);
1709 static bool cp_parser_diagnose_invalid_type_name
1710   (cp_parser *);
1711 static int cp_parser_skip_to_closing_parenthesis
1712   (cp_parser *, bool, bool, bool);
1713 static void cp_parser_skip_to_end_of_statement
1714   (cp_parser *);
1715 static void cp_parser_consume_semicolon_at_end_of_statement
1716   (cp_parser *);
1717 static void cp_parser_skip_to_end_of_block_or_statement
1718   (cp_parser *);
1719 static void cp_parser_skip_to_closing_brace
1720   (cp_parser *);
1721 static void cp_parser_skip_until_found
1722   (cp_parser *, enum cpp_ttype, const char *);
1723 static bool cp_parser_error_occurred
1724   (cp_parser *);
1725 static bool cp_parser_allow_gnu_extensions_p
1726   (cp_parser *);
1727 static bool cp_parser_is_string_literal
1728   (cp_token *);
1729 static bool cp_parser_is_keyword 
1730   (cp_token *, enum rid);
1731
1732 /* Returns nonzero if we are parsing tentatively.  */
1733
1734 static inline bool
1735 cp_parser_parsing_tentatively (cp_parser* parser)
1736 {
1737   return parser->context->next != NULL;
1738 }
1739
1740 /* Returns nonzero if TOKEN is a string literal.  */
1741
1742 static bool
1743 cp_parser_is_string_literal (cp_token* token)
1744 {
1745   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1746 }
1747
1748 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1749
1750 static bool
1751 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1752 {
1753   return token->keyword == keyword;
1754 }
1755
1756 /* Issue the indicated error MESSAGE.  */
1757
1758 static void
1759 cp_parser_error (cp_parser* parser, const char* message)
1760 {
1761   /* Output the MESSAGE -- unless we're parsing tentatively.  */
1762   if (!cp_parser_simulate_error (parser))
1763     error (message);
1764 }
1765
1766 /* If we are parsing tentatively, remember that an error has occurred
1767    during this tentative parse.  Returns true if the error was
1768    simulated; false if a messgae should be issued by the caller.  */
1769
1770 static bool
1771 cp_parser_simulate_error (cp_parser* parser)
1772 {
1773   if (cp_parser_parsing_tentatively (parser)
1774       && !cp_parser_committed_to_tentative_parse (parser))
1775     {
1776       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1777       return true;
1778     }
1779   return false;
1780 }
1781
1782 /* This function is called when a type is defined.  If type
1783    definitions are forbidden at this point, an error message is
1784    issued.  */
1785
1786 static void
1787 cp_parser_check_type_definition (cp_parser* parser)
1788 {
1789   /* If types are forbidden here, issue a message.  */
1790   if (parser->type_definition_forbidden_message)
1791     /* Use `%s' to print the string in case there are any escape
1792        characters in the message.  */
1793     error ("%s", parser->type_definition_forbidden_message);
1794 }
1795
1796 /* This function is called when a declaration is parsed.  If
1797    DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1798    indicates that a type was defined in the decl-specifiers for DECL,
1799    then an error is issued.  */
1800
1801 static void
1802 cp_parser_check_for_definition_in_return_type (tree declarator, 
1803                                                int declares_class_or_enum)
1804 {
1805   /* [dcl.fct] forbids type definitions in return types.
1806      Unfortunately, it's not easy to know whether or not we are
1807      processing a return type until after the fact.  */
1808   while (declarator
1809          && (TREE_CODE (declarator) == INDIRECT_REF
1810              || TREE_CODE (declarator) == ADDR_EXPR))
1811     declarator = TREE_OPERAND (declarator, 0);
1812   if (declarator
1813       && TREE_CODE (declarator) == CALL_EXPR 
1814       && declares_class_or_enum & 2)
1815     error ("new types may not be defined in a return type");
1816 }
1817
1818 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1819    "<" in any valid C++ program.  If the next token is indeed "<",
1820    issue a message warning the user about what appears to be an
1821    invalid attempt to form a template-id.  */
1822
1823 static void
1824 cp_parser_check_for_invalid_template_id (cp_parser* parser, 
1825                                          tree type)
1826 {
1827   ptrdiff_t start;
1828   cp_token *token;
1829
1830   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1831     {
1832       if (TYPE_P (type))
1833         error ("`%T' is not a template", type);
1834       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1835         error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1836       else
1837         error ("invalid template-id");
1838       /* Remember the location of the invalid "<".  */
1839       if (cp_parser_parsing_tentatively (parser)
1840           && !cp_parser_committed_to_tentative_parse (parser))
1841         {
1842           token = cp_lexer_peek_token (parser->lexer);
1843           token = cp_lexer_prev_token (parser->lexer, token);
1844           start = cp_lexer_token_difference (parser->lexer,
1845                                              parser->lexer->first_token,
1846                                              token);
1847         }
1848       else
1849         start = -1;
1850       /* Consume the "<".  */
1851       cp_lexer_consume_token (parser->lexer);
1852       /* Parse the template arguments.  */
1853       cp_parser_enclosed_template_argument_list (parser);
1854       /* Permanently remove the invalid template arugments so that
1855          this error message is not issued again.  */
1856       if (start >= 0)
1857         {
1858           token = cp_lexer_advance_token (parser->lexer,
1859                                           parser->lexer->first_token,
1860                                           start);
1861           cp_lexer_purge_tokens_after (parser->lexer, token);
1862         }
1863     }
1864 }
1865
1866 /* Issue an error message about the fact that THING appeared in a
1867    constant-expression.  Returns ERROR_MARK_NODE.  */
1868
1869 static tree
1870 cp_parser_non_constant_expression (const char *thing)
1871 {
1872   error ("%s cannot appear in a constant-expression", thing);
1873   return error_mark_node;
1874 }
1875
1876 /* Check for a common situation where a type-name should be present,
1877    but is not, and issue a sensible error message.  Returns true if an
1878    invalid type-name was detected.  */
1879
1880 static bool
1881 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1882 {
1883   /* If the next two tokens are both identifiers, the code is
1884      erroneous. The usual cause of this situation is code like:
1885
1886        T t;
1887
1888      where "T" should name a type -- but does not.  */
1889   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1890       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1891     {
1892       tree name;
1893
1894       /* If parsing tentatively, we should commit; we really are
1895          looking at a declaration.  */
1896       /* Consume the first identifier.  */
1897       name = cp_lexer_consume_token (parser->lexer)->value;
1898       /* Issue an error message.  */
1899       error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1900       /* If we're in a template class, it's possible that the user was
1901          referring to a type from a base class.  For example:
1902
1903            template <typename T> struct A { typedef T X; };
1904            template <typename T> struct B : public A<T> { X x; };
1905
1906          The user should have said "typename A<T>::X".  */
1907       if (processing_template_decl && current_class_type)
1908         {
1909           tree b;
1910
1911           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1912                b;
1913                b = TREE_CHAIN (b))
1914             {
1915               tree base_type = BINFO_TYPE (b);
1916               if (CLASS_TYPE_P (base_type) 
1917                   && dependent_type_p (base_type))
1918                 {
1919                   tree field;
1920                   /* Go from a particular instantiation of the
1921                      template (which will have an empty TYPE_FIELDs),
1922                      to the main version.  */
1923                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1924                   for (field = TYPE_FIELDS (base_type);
1925                        field;
1926                        field = TREE_CHAIN (field))
1927                     if (TREE_CODE (field) == TYPE_DECL
1928                         && DECL_NAME (field) == name)
1929                       {
1930                         error ("(perhaps `typename %T::%s' was intended)",
1931                                BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1932                         break;
1933                       }
1934                   if (field)
1935                     break;
1936                 }
1937             }
1938         }
1939       /* Skip to the end of the declaration; there's no point in
1940          trying to process it.  */
1941       cp_parser_skip_to_end_of_statement (parser);
1942       
1943       return true;
1944     }
1945
1946   return false;
1947 }
1948
1949 /* Consume tokens up to, and including, the next non-nested closing `)'. 
1950    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
1951    are doing error recovery. Returns -1 if OR_COMMA is true and we
1952    found an unnested comma.  */
1953
1954 static int
1955 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
1956                                        bool recovering, 
1957                                        bool or_comma,
1958                                        bool consume_paren)
1959 {
1960   unsigned paren_depth = 0;
1961   unsigned brace_depth = 0;
1962
1963   if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
1964       && !cp_parser_committed_to_tentative_parse (parser))
1965     return 0;
1966   
1967   while (true)
1968     {
1969       cp_token *token;
1970       
1971       /* If we've run out of tokens, then there is no closing `)'.  */
1972       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
1973         return 0;
1974
1975       token = cp_lexer_peek_token (parser->lexer);
1976       
1977       /* This matches the processing in skip_to_end_of_statement */
1978       if (token->type == CPP_SEMICOLON && !brace_depth)
1979         return 0;
1980       if (token->type == CPP_OPEN_BRACE)
1981         ++brace_depth;
1982       if (token->type == CPP_CLOSE_BRACE)
1983         {
1984           if (!brace_depth--)
1985             return 0;
1986         }
1987       if (recovering && or_comma && token->type == CPP_COMMA
1988           && !brace_depth && !paren_depth)
1989         return -1;
1990       
1991       if (!brace_depth)
1992         {
1993           /* If it is an `(', we have entered another level of nesting.  */
1994           if (token->type == CPP_OPEN_PAREN)
1995             ++paren_depth;
1996           /* If it is a `)', then we might be done.  */
1997           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
1998             {
1999               if (consume_paren)
2000                 cp_lexer_consume_token (parser->lexer);
2001               return 1;
2002             }
2003         }
2004       
2005       /* Consume the token.  */
2006       cp_lexer_consume_token (parser->lexer);
2007     }
2008 }
2009
2010 /* Consume tokens until we reach the end of the current statement.
2011    Normally, that will be just before consuming a `;'.  However, if a
2012    non-nested `}' comes first, then we stop before consuming that.  */
2013
2014 static void
2015 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2016 {
2017   unsigned nesting_depth = 0;
2018
2019   while (true)
2020     {
2021       cp_token *token;
2022
2023       /* Peek at the next token.  */
2024       token = cp_lexer_peek_token (parser->lexer);
2025       /* If we've run out of tokens, stop.  */
2026       if (token->type == CPP_EOF)
2027         break;
2028       /* If the next token is a `;', we have reached the end of the
2029          statement.  */
2030       if (token->type == CPP_SEMICOLON && !nesting_depth)
2031         break;
2032       /* If the next token is a non-nested `}', then we have reached
2033          the end of the current block.  */
2034       if (token->type == CPP_CLOSE_BRACE)
2035         {
2036           /* If this is a non-nested `}', stop before consuming it.
2037              That way, when confronted with something like:
2038
2039                { 3 + } 
2040
2041              we stop before consuming the closing `}', even though we
2042              have not yet reached a `;'.  */
2043           if (nesting_depth == 0)
2044             break;
2045           /* If it is the closing `}' for a block that we have
2046              scanned, stop -- but only after consuming the token.
2047              That way given:
2048
2049                 void f g () { ... }
2050                 typedef int I;
2051
2052              we will stop after the body of the erroneously declared
2053              function, but before consuming the following `typedef'
2054              declaration.  */
2055           if (--nesting_depth == 0)
2056             {
2057               cp_lexer_consume_token (parser->lexer);
2058               break;
2059             }
2060         }
2061       /* If it the next token is a `{', then we are entering a new
2062          block.  Consume the entire block.  */
2063       else if (token->type == CPP_OPEN_BRACE)
2064         ++nesting_depth;
2065       /* Consume the token.  */
2066       cp_lexer_consume_token (parser->lexer);
2067     }
2068 }
2069
2070 /* This function is called at the end of a statement or declaration.
2071    If the next token is a semicolon, it is consumed; otherwise, error
2072    recovery is attempted.  */
2073
2074 static void
2075 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2076 {
2077   /* Look for the trailing `;'.  */
2078   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2079     {
2080       /* If there is additional (erroneous) input, skip to the end of
2081          the statement.  */
2082       cp_parser_skip_to_end_of_statement (parser);
2083       /* If the next token is now a `;', consume it.  */
2084       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2085         cp_lexer_consume_token (parser->lexer);
2086     }
2087 }
2088
2089 /* Skip tokens until we have consumed an entire block, or until we
2090    have consumed a non-nested `;'.  */
2091
2092 static void
2093 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2094 {
2095   unsigned nesting_depth = 0;
2096
2097   while (true)
2098     {
2099       cp_token *token;
2100
2101       /* Peek at the next token.  */
2102       token = cp_lexer_peek_token (parser->lexer);
2103       /* If we've run out of tokens, stop.  */
2104       if (token->type == CPP_EOF)
2105         break;
2106       /* If the next token is a `;', we have reached the end of the
2107          statement.  */
2108       if (token->type == CPP_SEMICOLON && !nesting_depth)
2109         {
2110           /* Consume the `;'.  */
2111           cp_lexer_consume_token (parser->lexer);
2112           break;
2113         }
2114       /* Consume the token.  */
2115       token = cp_lexer_consume_token (parser->lexer);
2116       /* If the next token is a non-nested `}', then we have reached
2117          the end of the current block.  */
2118       if (token->type == CPP_CLOSE_BRACE 
2119           && (nesting_depth == 0 || --nesting_depth == 0))
2120         break;
2121       /* If it the next token is a `{', then we are entering a new
2122          block.  Consume the entire block.  */
2123       if (token->type == CPP_OPEN_BRACE)
2124         ++nesting_depth;
2125     }
2126 }
2127
2128 /* Skip tokens until a non-nested closing curly brace is the next
2129    token.  */
2130
2131 static void
2132 cp_parser_skip_to_closing_brace (cp_parser *parser)
2133 {
2134   unsigned nesting_depth = 0;
2135
2136   while (true)
2137     {
2138       cp_token *token;
2139
2140       /* Peek at the next token.  */
2141       token = cp_lexer_peek_token (parser->lexer);
2142       /* If we've run out of tokens, stop.  */
2143       if (token->type == CPP_EOF)
2144         break;
2145       /* If the next token is a non-nested `}', then we have reached
2146          the end of the current block.  */
2147       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2148         break;
2149       /* If it the next token is a `{', then we are entering a new
2150          block.  Consume the entire block.  */
2151       else if (token->type == CPP_OPEN_BRACE)
2152         ++nesting_depth;
2153       /* Consume the token.  */
2154       cp_lexer_consume_token (parser->lexer);
2155     }
2156 }
2157
2158 /* Create a new C++ parser.  */
2159
2160 static cp_parser *
2161 cp_parser_new (void)
2162 {
2163   cp_parser *parser;
2164   cp_lexer *lexer;
2165
2166   /* cp_lexer_new_main is called before calling ggc_alloc because
2167      cp_lexer_new_main might load a PCH file.  */
2168   lexer = cp_lexer_new_main ();
2169
2170   parser = ggc_alloc_cleared (sizeof (cp_parser));
2171   parser->lexer = lexer;
2172   parser->context = cp_parser_context_new (NULL);
2173
2174   /* For now, we always accept GNU extensions.  */
2175   parser->allow_gnu_extensions_p = 1;
2176
2177   /* The `>' token is a greater-than operator, not the end of a
2178      template-id.  */
2179   parser->greater_than_is_operator_p = true;
2180
2181   parser->default_arg_ok_p = true;
2182   
2183   /* We are not parsing a constant-expression.  */
2184   parser->constant_expression_p = false;
2185   parser->allow_non_constant_expression_p = false;
2186   parser->non_constant_expression_p = false;
2187
2188   /* Local variable names are not forbidden.  */
2189   parser->local_variables_forbidden_p = false;
2190
2191   /* We are not processing an `extern "C"' declaration.  */
2192   parser->in_unbraced_linkage_specification_p = false;
2193
2194   /* We are not processing a declarator.  */
2195   parser->in_declarator_p = false;
2196
2197   /* We are not in an iteration statement.  */
2198   parser->in_iteration_statement_p = false;
2199
2200   /* We are not in a switch statement.  */
2201   parser->in_switch_statement_p = false;
2202
2203   /* The unparsed function queue is empty.  */
2204   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2205
2206   /* There are no classes being defined.  */
2207   parser->num_classes_being_defined = 0;
2208
2209   /* No template parameters apply.  */
2210   parser->num_template_parameter_lists = 0;
2211
2212   return parser;
2213 }
2214
2215 /* Lexical conventions [gram.lex]  */
2216
2217 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2218    identifier.  */
2219
2220 static tree 
2221 cp_parser_identifier (cp_parser* parser)
2222 {
2223   cp_token *token;
2224
2225   /* Look for the identifier.  */
2226   token = cp_parser_require (parser, CPP_NAME, "identifier");
2227   /* Return the value.  */
2228   return token ? token->value : error_mark_node;
2229 }
2230
2231 /* Basic concepts [gram.basic]  */
2232
2233 /* Parse a translation-unit.
2234
2235    translation-unit:
2236      declaration-seq [opt]  
2237
2238    Returns TRUE if all went well.  */
2239
2240 static bool
2241 cp_parser_translation_unit (cp_parser* parser)
2242 {
2243   while (true)
2244     {
2245       cp_parser_declaration_seq_opt (parser);
2246
2247       /* If there are no tokens left then all went well.  */
2248       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2249         break;
2250       
2251       /* Otherwise, issue an error message.  */
2252       cp_parser_error (parser, "expected declaration");
2253       return false;
2254     }
2255
2256   /* Consume the EOF token.  */
2257   cp_parser_require (parser, CPP_EOF, "end-of-file");
2258   
2259   /* Finish up.  */
2260   finish_translation_unit ();
2261
2262   /* All went well.  */
2263   return true;
2264 }
2265
2266 /* Expressions [gram.expr] */
2267
2268 /* Parse a primary-expression.
2269
2270    primary-expression:
2271      literal
2272      this
2273      ( expression )
2274      id-expression
2275
2276    GNU Extensions:
2277
2278    primary-expression:
2279      ( compound-statement )
2280      __builtin_va_arg ( assignment-expression , type-id )
2281
2282    literal:
2283      __null
2284
2285    Returns a representation of the expression.  
2286
2287    *IDK indicates what kind of id-expression (if any) was present.  
2288
2289    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2290    used as the operand of a pointer-to-member.  In that case,
2291    *QUALIFYING_CLASS gives the class that is used as the qualifying
2292    class in the pointer-to-member.  */
2293
2294 static tree
2295 cp_parser_primary_expression (cp_parser *parser, 
2296                               cp_id_kind *idk,
2297                               tree *qualifying_class)
2298 {
2299   cp_token *token;
2300
2301   /* Assume the primary expression is not an id-expression.  */
2302   *idk = CP_ID_KIND_NONE;
2303   /* And that it cannot be used as pointer-to-member.  */
2304   *qualifying_class = NULL_TREE;
2305
2306   /* Peek at the next token.  */
2307   token = cp_lexer_peek_token (parser->lexer);
2308   switch (token->type)
2309     {
2310       /* literal:
2311            integer-literal
2312            character-literal
2313            floating-literal
2314            string-literal
2315            boolean-literal  */
2316     case CPP_CHAR:
2317     case CPP_WCHAR:
2318     case CPP_STRING:
2319     case CPP_WSTRING:
2320     case CPP_NUMBER:
2321       token = cp_lexer_consume_token (parser->lexer);
2322       return token->value;
2323
2324     case CPP_OPEN_PAREN:
2325       {
2326         tree expr;
2327         bool saved_greater_than_is_operator_p;
2328
2329         /* Consume the `('.  */
2330         cp_lexer_consume_token (parser->lexer);
2331         /* Within a parenthesized expression, a `>' token is always
2332            the greater-than operator.  */
2333         saved_greater_than_is_operator_p 
2334           = parser->greater_than_is_operator_p;
2335         parser->greater_than_is_operator_p = true;
2336         /* If we see `( { ' then we are looking at the beginning of
2337            a GNU statement-expression.  */
2338         if (cp_parser_allow_gnu_extensions_p (parser)
2339             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2340           {
2341             /* Statement-expressions are not allowed by the standard.  */
2342             if (pedantic)
2343               pedwarn ("ISO C++ forbids braced-groups within expressions");  
2344             
2345             /* And they're not allowed outside of a function-body; you
2346                cannot, for example, write:
2347                
2348                  int i = ({ int j = 3; j + 1; });
2349                
2350                at class or namespace scope.  */
2351             if (!at_function_scope_p ())
2352               error ("statement-expressions are allowed only inside functions");
2353             /* Start the statement-expression.  */
2354             expr = begin_stmt_expr ();
2355             /* Parse the compound-statement.  */
2356             cp_parser_compound_statement (parser, true);
2357             /* Finish up.  */
2358             expr = finish_stmt_expr (expr, false);
2359           }
2360         else
2361           {
2362             /* Parse the parenthesized expression.  */
2363             expr = cp_parser_expression (parser);
2364             /* Let the front end know that this expression was
2365                enclosed in parentheses. This matters in case, for
2366                example, the expression is of the form `A::B', since
2367                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2368                not.  */
2369             finish_parenthesized_expr (expr);
2370           }
2371         /* The `>' token might be the end of a template-id or
2372            template-parameter-list now.  */
2373         parser->greater_than_is_operator_p 
2374           = saved_greater_than_is_operator_p;
2375         /* Consume the `)'.  */
2376         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2377           cp_parser_skip_to_end_of_statement (parser);
2378
2379         return expr;
2380       }
2381
2382     case CPP_KEYWORD:
2383       switch (token->keyword)
2384         {
2385           /* These two are the boolean literals.  */
2386         case RID_TRUE:
2387           cp_lexer_consume_token (parser->lexer);
2388           return boolean_true_node;
2389         case RID_FALSE:
2390           cp_lexer_consume_token (parser->lexer);
2391           return boolean_false_node;
2392           
2393           /* The `__null' literal.  */
2394         case RID_NULL:
2395           cp_lexer_consume_token (parser->lexer);
2396           return null_node;
2397
2398           /* Recognize the `this' keyword.  */
2399         case RID_THIS:
2400           cp_lexer_consume_token (parser->lexer);
2401           if (parser->local_variables_forbidden_p)
2402             {
2403               error ("`this' may not be used in this context");
2404               return error_mark_node;
2405             }
2406           /* Pointers cannot appear in constant-expressions.  */
2407           if (parser->constant_expression_p)
2408             {
2409               if (!parser->allow_non_constant_expression_p)
2410                 return cp_parser_non_constant_expression ("`this'");
2411               parser->non_constant_expression_p = true;
2412             }
2413           return finish_this_expr ();
2414
2415           /* The `operator' keyword can be the beginning of an
2416              id-expression.  */
2417         case RID_OPERATOR:
2418           goto id_expression;
2419
2420         case RID_FUNCTION_NAME:
2421         case RID_PRETTY_FUNCTION_NAME:
2422         case RID_C99_FUNCTION_NAME:
2423           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2424              __func__ are the names of variables -- but they are
2425              treated specially.  Therefore, they are handled here,
2426              rather than relying on the generic id-expression logic
2427              below.  Grammatically, these names are id-expressions.  
2428
2429              Consume the token.  */
2430           token = cp_lexer_consume_token (parser->lexer);
2431           /* Look up the name.  */
2432           return finish_fname (token->value);
2433
2434         case RID_VA_ARG:
2435           {
2436             tree expression;
2437             tree type;
2438
2439             /* The `__builtin_va_arg' construct is used to handle
2440                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2441             cp_lexer_consume_token (parser->lexer);
2442             /* Look for the opening `('.  */
2443             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2444             /* Now, parse the assignment-expression.  */
2445             expression = cp_parser_assignment_expression (parser);
2446             /* Look for the `,'.  */
2447             cp_parser_require (parser, CPP_COMMA, "`,'");
2448             /* Parse the type-id.  */
2449             type = cp_parser_type_id (parser);
2450             /* Look for the closing `)'.  */
2451             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2452             /* Using `va_arg' in a constant-expression is not
2453                allowed.  */
2454             if (parser->constant_expression_p)
2455               {
2456                 if (!parser->allow_non_constant_expression_p)
2457                   return cp_parser_non_constant_expression ("`va_arg'");
2458                 parser->non_constant_expression_p = true;
2459               }
2460             return build_x_va_arg (expression, type);
2461           }
2462
2463         default:
2464           cp_parser_error (parser, "expected primary-expression");
2465           return error_mark_node;
2466         }
2467
2468       /* An id-expression can start with either an identifier, a
2469          `::' as the beginning of a qualified-id, or the "operator"
2470          keyword.  */
2471     case CPP_NAME:
2472     case CPP_SCOPE:
2473     case CPP_TEMPLATE_ID:
2474     case CPP_NESTED_NAME_SPECIFIER:
2475       {
2476         tree id_expression;
2477         tree decl;
2478         const char *error_msg;
2479
2480       id_expression:
2481         /* Parse the id-expression.  */
2482         id_expression 
2483           = cp_parser_id_expression (parser, 
2484                                      /*template_keyword_p=*/false,
2485                                      /*check_dependency_p=*/true,
2486                                      /*template_p=*/NULL,
2487                                      /*declarator_p=*/false);
2488         if (id_expression == error_mark_node)
2489           return error_mark_node;
2490         /* If we have a template-id, then no further lookup is
2491            required.  If the template-id was for a template-class, we
2492            will sometimes have a TYPE_DECL at this point.  */
2493         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2494             || TREE_CODE (id_expression) == TYPE_DECL)
2495           decl = id_expression;
2496         /* Look up the name.  */
2497         else 
2498           {
2499             decl = cp_parser_lookup_name_simple (parser, id_expression);
2500             /* If name lookup gives us a SCOPE_REF, then the
2501                qualifying scope was dependent.  Just propagate the
2502                name.  */
2503             if (TREE_CODE (decl) == SCOPE_REF)
2504               {
2505                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2506                   *qualifying_class = TREE_OPERAND (decl, 0);
2507                 return decl;
2508               }
2509             /* Check to see if DECL is a local variable in a context
2510                where that is forbidden.  */
2511             if (parser->local_variables_forbidden_p
2512                 && local_variable_p (decl))
2513               {
2514                 /* It might be that we only found DECL because we are
2515                    trying to be generous with pre-ISO scoping rules.
2516                    For example, consider:
2517
2518                      int i;
2519                      void g() {
2520                        for (int i = 0; i < 10; ++i) {}
2521                        extern void f(int j = i);
2522                      }
2523
2524                    Here, name look up will originally find the out 
2525                    of scope `i'.  We need to issue a warning message,
2526                    but then use the global `i'.  */
2527                 decl = check_for_out_of_scope_variable (decl);
2528                 if (local_variable_p (decl))
2529                   {
2530                     error ("local variable `%D' may not appear in this context",
2531                            decl);
2532                     return error_mark_node;
2533                   }
2534               }
2535           }
2536         
2537         decl = finish_id_expression (id_expression, decl, parser->scope, 
2538                                      idk, qualifying_class,
2539                                      parser->constant_expression_p,
2540                                      parser->allow_non_constant_expression_p,
2541                                      &parser->non_constant_expression_p,
2542                                      &error_msg);
2543         if (error_msg)
2544           cp_parser_error (parser, error_msg);
2545         return decl;
2546       }
2547
2548       /* Anything else is an error.  */
2549     default:
2550       cp_parser_error (parser, "expected primary-expression");
2551       return error_mark_node;
2552     }
2553 }
2554
2555 /* Parse an id-expression.
2556
2557    id-expression:
2558      unqualified-id
2559      qualified-id
2560
2561    qualified-id:
2562      :: [opt] nested-name-specifier template [opt] unqualified-id
2563      :: identifier
2564      :: operator-function-id
2565      :: template-id
2566
2567    Return a representation of the unqualified portion of the
2568    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2569    a `::' or nested-name-specifier.
2570
2571    Often, if the id-expression was a qualified-id, the caller will
2572    want to make a SCOPE_REF to represent the qualified-id.  This
2573    function does not do this in order to avoid wastefully creating
2574    SCOPE_REFs when they are not required.
2575
2576    If TEMPLATE_KEYWORD_P is true, then we have just seen the
2577    `template' keyword.
2578
2579    If CHECK_DEPENDENCY_P is false, then names are looked up inside
2580    uninstantiated templates.  
2581
2582    If *TEMPLATE_P is non-NULL, it is set to true iff the
2583    `template' keyword is used to explicitly indicate that the entity
2584    named is a template.  
2585
2586    If DECLARATOR_P is true, the id-expression is appearing as part of
2587    a declarator, rather than as part of an expression.  */
2588
2589 static tree
2590 cp_parser_id_expression (cp_parser *parser,
2591                          bool template_keyword_p,
2592                          bool check_dependency_p,
2593                          bool *template_p,
2594                          bool declarator_p)
2595 {
2596   bool global_scope_p;
2597   bool nested_name_specifier_p;
2598
2599   /* Assume the `template' keyword was not used.  */
2600   if (template_p)
2601     *template_p = false;
2602
2603   /* Look for the optional `::' operator.  */
2604   global_scope_p 
2605     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) 
2606        != NULL_TREE);
2607   /* Look for the optional nested-name-specifier.  */
2608   nested_name_specifier_p 
2609     = (cp_parser_nested_name_specifier_opt (parser,
2610                                             /*typename_keyword_p=*/false,
2611                                             check_dependency_p,
2612                                             /*type_p=*/false,
2613                                             /*is_declarator=*/false)
2614        != NULL_TREE);
2615   /* If there is a nested-name-specifier, then we are looking at
2616      the first qualified-id production.  */
2617   if (nested_name_specifier_p)
2618     {
2619       tree saved_scope;
2620       tree saved_object_scope;
2621       tree saved_qualifying_scope;
2622       tree unqualified_id;
2623       bool is_template;
2624
2625       /* See if the next token is the `template' keyword.  */
2626       if (!template_p)
2627         template_p = &is_template;
2628       *template_p = cp_parser_optional_template_keyword (parser);
2629       /* Name lookup we do during the processing of the
2630          unqualified-id might obliterate SCOPE.  */
2631       saved_scope = parser->scope;
2632       saved_object_scope = parser->object_scope;
2633       saved_qualifying_scope = parser->qualifying_scope;
2634       /* Process the final unqualified-id.  */
2635       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2636                                                  check_dependency_p,
2637                                                  declarator_p);
2638       /* Restore the SAVED_SCOPE for our caller.  */
2639       parser->scope = saved_scope;
2640       parser->object_scope = saved_object_scope;
2641       parser->qualifying_scope = saved_qualifying_scope;
2642
2643       return unqualified_id;
2644     }
2645   /* Otherwise, if we are in global scope, then we are looking at one
2646      of the other qualified-id productions.  */
2647   else if (global_scope_p)
2648     {
2649       cp_token *token;
2650       tree id;
2651
2652       /* Peek at the next token.  */
2653       token = cp_lexer_peek_token (parser->lexer);
2654
2655       /* If it's an identifier, and the next token is not a "<", then
2656          we can avoid the template-id case.  This is an optimization
2657          for this common case.  */
2658       if (token->type == CPP_NAME 
2659           && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2660         return cp_parser_identifier (parser);
2661
2662       cp_parser_parse_tentatively (parser);
2663       /* Try a template-id.  */
2664       id = cp_parser_template_id (parser, 
2665                                   /*template_keyword_p=*/false,
2666                                   /*check_dependency_p=*/true,
2667                                   declarator_p);
2668       /* If that worked, we're done.  */
2669       if (cp_parser_parse_definitely (parser))
2670         return id;
2671
2672       /* Peek at the next token.  (Changes in the token buffer may
2673          have invalidated the pointer obtained above.)  */
2674       token = cp_lexer_peek_token (parser->lexer);
2675
2676       switch (token->type)
2677         {
2678         case CPP_NAME:
2679           return cp_parser_identifier (parser);
2680
2681         case CPP_KEYWORD:
2682           if (token->keyword == RID_OPERATOR)
2683             return cp_parser_operator_function_id (parser);
2684           /* Fall through.  */
2685           
2686         default:
2687           cp_parser_error (parser, "expected id-expression");
2688           return error_mark_node;
2689         }
2690     }
2691   else
2692     return cp_parser_unqualified_id (parser, template_keyword_p,
2693                                      /*check_dependency_p=*/true,
2694                                      declarator_p);
2695 }
2696
2697 /* Parse an unqualified-id.
2698
2699    unqualified-id:
2700      identifier
2701      operator-function-id
2702      conversion-function-id
2703      ~ class-name
2704      template-id
2705
2706    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2707    keyword, in a construct like `A::template ...'.
2708
2709    Returns a representation of unqualified-id.  For the `identifier'
2710    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
2711    production a BIT_NOT_EXPR is returned; the operand of the
2712    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
2713    other productions, see the documentation accompanying the
2714    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
2715    names are looked up in uninstantiated templates.  If DECLARATOR_P
2716    is true, the unqualified-id is appearing as part of a declarator,
2717    rather than as part of an expression.  */
2718
2719 static tree
2720 cp_parser_unqualified_id (cp_parser* parser, 
2721                           bool template_keyword_p,
2722                           bool check_dependency_p,
2723                           bool declarator_p)
2724 {
2725   cp_token *token;
2726
2727   /* Peek at the next token.  */
2728   token = cp_lexer_peek_token (parser->lexer);
2729   
2730   switch (token->type)
2731     {
2732     case CPP_NAME:
2733       {
2734         tree id;
2735
2736         /* We don't know yet whether or not this will be a
2737            template-id.  */
2738         cp_parser_parse_tentatively (parser);
2739         /* Try a template-id.  */
2740         id = cp_parser_template_id (parser, template_keyword_p,
2741                                     check_dependency_p,
2742                                     declarator_p);
2743         /* If it worked, we're done.  */
2744         if (cp_parser_parse_definitely (parser))
2745           return id;
2746         /* Otherwise, it's an ordinary identifier.  */
2747         return cp_parser_identifier (parser);
2748       }
2749
2750     case CPP_TEMPLATE_ID:
2751       return cp_parser_template_id (parser, template_keyword_p,
2752                                     check_dependency_p,
2753                                     declarator_p);
2754
2755     case CPP_COMPL:
2756       {
2757         tree type_decl;
2758         tree qualifying_scope;
2759         tree object_scope;
2760         tree scope;
2761
2762         /* Consume the `~' token.  */
2763         cp_lexer_consume_token (parser->lexer);
2764         /* Parse the class-name.  The standard, as written, seems to
2765            say that:
2766
2767              template <typename T> struct S { ~S (); };
2768              template <typename T> S<T>::~S() {}
2769
2770            is invalid, since `~' must be followed by a class-name, but
2771            `S<T>' is dependent, and so not known to be a class.
2772            That's not right; we need to look in uninstantiated
2773            templates.  A further complication arises from:
2774
2775              template <typename T> void f(T t) {
2776                t.T::~T();
2777              } 
2778
2779            Here, it is not possible to look up `T' in the scope of `T'
2780            itself.  We must look in both the current scope, and the
2781            scope of the containing complete expression.  
2782
2783            Yet another issue is:
2784
2785              struct S {
2786                int S;
2787                ~S();
2788              };
2789
2790              S::~S() {}
2791
2792            The standard does not seem to say that the `S' in `~S'
2793            should refer to the type `S' and not the data member
2794            `S::S'.  */
2795
2796         /* DR 244 says that we look up the name after the "~" in the
2797            same scope as we looked up the qualifying name.  That idea
2798            isn't fully worked out; it's more complicated than that.  */
2799         scope = parser->scope;
2800         object_scope = parser->object_scope;
2801         qualifying_scope = parser->qualifying_scope;
2802
2803         /* If the name is of the form "X::~X" it's OK.  */
2804         if (scope && TYPE_P (scope)
2805             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2806             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
2807                 == CPP_OPEN_PAREN)
2808             && (cp_lexer_peek_token (parser->lexer)->value 
2809                 == TYPE_IDENTIFIER (scope)))
2810           {
2811             cp_lexer_consume_token (parser->lexer);
2812             return build_nt (BIT_NOT_EXPR, scope);
2813           }
2814
2815         /* If there was an explicit qualification (S::~T), first look
2816            in the scope given by the qualification (i.e., S).  */
2817         if (scope)
2818           {
2819             cp_parser_parse_tentatively (parser);
2820             type_decl = cp_parser_class_name (parser, 
2821                                               /*typename_keyword_p=*/false,
2822                                               /*template_keyword_p=*/false,
2823                                               /*type_p=*/false,
2824                                               /*check_dependency=*/false,
2825                                               /*class_head_p=*/false,
2826                                               declarator_p);
2827             if (cp_parser_parse_definitely (parser))
2828               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2829           }
2830         /* In "N::S::~S", look in "N" as well.  */
2831         if (scope && qualifying_scope)
2832           {
2833             cp_parser_parse_tentatively (parser);
2834             parser->scope = qualifying_scope;
2835             parser->object_scope = NULL_TREE;
2836             parser->qualifying_scope = NULL_TREE;
2837             type_decl 
2838               = cp_parser_class_name (parser, 
2839                                       /*typename_keyword_p=*/false,
2840                                       /*template_keyword_p=*/false,
2841                                       /*type_p=*/false,
2842                                       /*check_dependency=*/false,
2843                                       /*class_head_p=*/false,
2844                                       declarator_p);
2845             if (cp_parser_parse_definitely (parser))
2846               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2847           }
2848         /* In "p->S::~T", look in the scope given by "*p" as well.  */
2849         else if (object_scope)
2850           {
2851             cp_parser_parse_tentatively (parser);
2852             parser->scope = object_scope;
2853             parser->object_scope = NULL_TREE;
2854             parser->qualifying_scope = NULL_TREE;
2855             type_decl 
2856               = cp_parser_class_name (parser, 
2857                                       /*typename_keyword_p=*/false,
2858                                       /*template_keyword_p=*/false,
2859                                       /*type_p=*/false,
2860                                       /*check_dependency=*/false,
2861                                       /*class_head_p=*/false,
2862                                       declarator_p);
2863             if (cp_parser_parse_definitely (parser))
2864               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2865           }
2866         /* Look in the surrounding context.  */
2867         parser->scope = NULL_TREE;
2868         parser->object_scope = NULL_TREE;
2869         parser->qualifying_scope = NULL_TREE;
2870         type_decl 
2871           = cp_parser_class_name (parser, 
2872                                   /*typename_keyword_p=*/false,
2873                                   /*template_keyword_p=*/false,
2874                                   /*type_p=*/false,
2875                                   /*check_dependency=*/false,
2876                                   /*class_head_p=*/false,
2877                                   declarator_p);
2878         /* If an error occurred, assume that the name of the
2879            destructor is the same as the name of the qualifying
2880            class.  That allows us to keep parsing after running
2881            into ill-formed destructor names.  */
2882         if (type_decl == error_mark_node && scope && TYPE_P (scope))
2883           return build_nt (BIT_NOT_EXPR, scope);
2884         else if (type_decl == error_mark_node)
2885           return error_mark_node;
2886
2887         /* [class.dtor]
2888
2889            A typedef-name that names a class shall not be used as the
2890            identifier in the declarator for a destructor declaration.  */
2891         if (declarator_p 
2892             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2893             && !DECL_SELF_REFERENCE_P (type_decl))
2894           error ("typedef-name `%D' used as destructor declarator",
2895                  type_decl);
2896
2897         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2898       }
2899
2900     case CPP_KEYWORD:
2901       if (token->keyword == RID_OPERATOR)
2902         {
2903           tree id;
2904
2905           /* This could be a template-id, so we try that first.  */
2906           cp_parser_parse_tentatively (parser);
2907           /* Try a template-id.  */
2908           id = cp_parser_template_id (parser, template_keyword_p,
2909                                       /*check_dependency_p=*/true,
2910                                       declarator_p);
2911           /* If that worked, we're done.  */
2912           if (cp_parser_parse_definitely (parser))
2913             return id;
2914           /* We still don't know whether we're looking at an
2915              operator-function-id or a conversion-function-id.  */
2916           cp_parser_parse_tentatively (parser);
2917           /* Try an operator-function-id.  */
2918           id = cp_parser_operator_function_id (parser);
2919           /* If that didn't work, try a conversion-function-id.  */
2920           if (!cp_parser_parse_definitely (parser))
2921             id = cp_parser_conversion_function_id (parser);
2922
2923           return id;
2924         }
2925       /* Fall through.  */
2926
2927     default:
2928       cp_parser_error (parser, "expected unqualified-id");
2929       return error_mark_node;
2930     }
2931 }
2932
2933 /* Parse an (optional) nested-name-specifier.
2934
2935    nested-name-specifier:
2936      class-or-namespace-name :: nested-name-specifier [opt]
2937      class-or-namespace-name :: template nested-name-specifier [opt]
2938
2939    PARSER->SCOPE should be set appropriately before this function is
2940    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
2941    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
2942    in name lookups.
2943
2944    Sets PARSER->SCOPE to the class (TYPE) or namespace
2945    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
2946    it unchanged if there is no nested-name-specifier.  Returns the new
2947    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.  
2948
2949    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
2950    part of a declaration and/or decl-specifier.  */
2951
2952 static tree
2953 cp_parser_nested_name_specifier_opt (cp_parser *parser, 
2954                                      bool typename_keyword_p, 
2955                                      bool check_dependency_p,
2956                                      bool type_p,
2957                                      bool is_declaration)
2958 {
2959   bool success = false;
2960   tree access_check = NULL_TREE;
2961   ptrdiff_t start;
2962   cp_token* token;
2963
2964   /* If the next token corresponds to a nested name specifier, there
2965      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
2966      false, it may have been true before, in which case something 
2967      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
2968      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
2969      CHECK_DEPENDENCY_P is false, we have to fall through into the
2970      main loop.  */
2971   if (check_dependency_p
2972       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
2973     {
2974       cp_parser_pre_parsed_nested_name_specifier (parser);
2975       return parser->scope;
2976     }
2977
2978   /* Remember where the nested-name-specifier starts.  */
2979   if (cp_parser_parsing_tentatively (parser)
2980       && !cp_parser_committed_to_tentative_parse (parser))
2981     {
2982       token = cp_lexer_peek_token (parser->lexer);
2983       start = cp_lexer_token_difference (parser->lexer,
2984                                          parser->lexer->first_token,
2985                                          token);
2986     }
2987   else
2988     start = -1;
2989
2990   push_deferring_access_checks (dk_deferred);
2991
2992   while (true)
2993     {
2994       tree new_scope;
2995       tree old_scope;
2996       tree saved_qualifying_scope;
2997       bool template_keyword_p;
2998
2999       /* Spot cases that cannot be the beginning of a
3000          nested-name-specifier.  */
3001       token = cp_lexer_peek_token (parser->lexer);
3002
3003       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3004          the already parsed nested-name-specifier.  */
3005       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3006         {
3007           /* Grab the nested-name-specifier and continue the loop.  */
3008           cp_parser_pre_parsed_nested_name_specifier (parser);
3009           success = true;
3010           continue;
3011         }
3012
3013       /* Spot cases that cannot be the beginning of a
3014          nested-name-specifier.  On the second and subsequent times
3015          through the loop, we look for the `template' keyword.  */
3016       if (success && token->keyword == RID_TEMPLATE)
3017         ;
3018       /* A template-id can start a nested-name-specifier.  */
3019       else if (token->type == CPP_TEMPLATE_ID)
3020         ;
3021       else
3022         {
3023           /* If the next token is not an identifier, then it is
3024              definitely not a class-or-namespace-name.  */
3025           if (token->type != CPP_NAME)
3026             break;
3027           /* If the following token is neither a `<' (to begin a
3028              template-id), nor a `::', then we are not looking at a
3029              nested-name-specifier.  */
3030           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3031           if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3032             break;
3033         }
3034
3035       /* The nested-name-specifier is optional, so we parse
3036          tentatively.  */
3037       cp_parser_parse_tentatively (parser);
3038
3039       /* Look for the optional `template' keyword, if this isn't the
3040          first time through the loop.  */
3041       if (success)
3042         template_keyword_p = cp_parser_optional_template_keyword (parser);
3043       else
3044         template_keyword_p = false;
3045
3046       /* Save the old scope since the name lookup we are about to do
3047          might destroy it.  */
3048       old_scope = parser->scope;
3049       saved_qualifying_scope = parser->qualifying_scope;
3050       /* Parse the qualifying entity.  */
3051       new_scope 
3052         = cp_parser_class_or_namespace_name (parser,
3053                                              typename_keyword_p,
3054                                              template_keyword_p,
3055                                              check_dependency_p,
3056                                              type_p,
3057                                              is_declaration);
3058       /* Look for the `::' token.  */
3059       cp_parser_require (parser, CPP_SCOPE, "`::'");
3060
3061       /* If we found what we wanted, we keep going; otherwise, we're
3062          done.  */
3063       if (!cp_parser_parse_definitely (parser))
3064         {
3065           bool error_p = false;
3066
3067           /* Restore the OLD_SCOPE since it was valid before the
3068              failed attempt at finding the last
3069              class-or-namespace-name.  */
3070           parser->scope = old_scope;
3071           parser->qualifying_scope = saved_qualifying_scope;
3072           /* If the next token is an identifier, and the one after
3073              that is a `::', then any valid interpretation would have
3074              found a class-or-namespace-name.  */
3075           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3076                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
3077                      == CPP_SCOPE)
3078                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
3079                      != CPP_COMPL))
3080             {
3081               token = cp_lexer_consume_token (parser->lexer);
3082               if (!error_p) 
3083                 {
3084                   tree decl;
3085
3086                   decl = cp_parser_lookup_name_simple (parser, token->value);
3087                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3088                     error ("`%D' used without template parameters",
3089                            decl);
3090                   else if (parser->scope)
3091                     {
3092                       if (TYPE_P (parser->scope))
3093                         error ("`%T::%D' is not a class-name or "
3094                                "namespace-name",
3095                                parser->scope, token->value);
3096                       else if (parser->scope == global_namespace)
3097                         error ("`::%D' is not a class-name or "
3098                                "namespace-name",
3099                                token->value);
3100                       else
3101                         error ("`%D::%D' is not a class-name or "
3102                                "namespace-name",
3103                                parser->scope, token->value);
3104                     }
3105                   else
3106                     error ("`%D' is not a class-name or namespace-name",
3107                            token->value);
3108                   parser->scope = NULL_TREE;
3109                   error_p = true;
3110                   /* Treat this as a successful nested-name-specifier
3111                      due to:
3112
3113                      [basic.lookup.qual]
3114
3115                      If the name found is not a class-name (clause
3116                      _class_) or namespace-name (_namespace.def_), the
3117                      program is ill-formed.  */
3118                   success = true;
3119                 }
3120               cp_lexer_consume_token (parser->lexer);
3121             }
3122           break;
3123         }
3124
3125       /* We've found one valid nested-name-specifier.  */
3126       success = true;
3127       /* Make sure we look in the right scope the next time through
3128          the loop.  */
3129       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL 
3130                        ? TREE_TYPE (new_scope)
3131                        : new_scope);
3132       /* If it is a class scope, try to complete it; we are about to
3133          be looking up names inside the class.  */
3134       if (TYPE_P (parser->scope)
3135           /* Since checking types for dependency can be expensive,
3136              avoid doing it if the type is already complete.  */
3137           && !COMPLETE_TYPE_P (parser->scope)
3138           /* Do not try to complete dependent types.  */
3139           && !dependent_type_p (parser->scope))
3140         complete_type (parser->scope);
3141     }
3142
3143   /* Retrieve any deferred checks.  Do not pop this access checks yet
3144      so the memory will not be reclaimed during token replacing below.  */
3145   access_check = get_deferred_access_checks ();
3146
3147   /* If parsing tentatively, replace the sequence of tokens that makes
3148      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3149      token.  That way, should we re-parse the token stream, we will
3150      not have to repeat the effort required to do the parse, nor will
3151      we issue duplicate error messages.  */
3152   if (success && start >= 0)
3153     {
3154       /* Find the token that corresponds to the start of the
3155          template-id.  */
3156       token = cp_lexer_advance_token (parser->lexer, 
3157                                       parser->lexer->first_token,
3158                                       start);
3159
3160       /* Reset the contents of the START token.  */
3161       token->type = CPP_NESTED_NAME_SPECIFIER;
3162       token->value = build_tree_list (access_check, parser->scope);
3163       TREE_TYPE (token->value) = parser->qualifying_scope;
3164       token->keyword = RID_MAX;
3165       /* Purge all subsequent tokens.  */
3166       cp_lexer_purge_tokens_after (parser->lexer, token);
3167     }
3168
3169   pop_deferring_access_checks ();
3170   return success ? parser->scope : NULL_TREE;
3171 }
3172
3173 /* Parse a nested-name-specifier.  See
3174    cp_parser_nested_name_specifier_opt for details.  This function
3175    behaves identically, except that it will an issue an error if no
3176    nested-name-specifier is present, and it will return
3177    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3178    is present.  */
3179
3180 static tree
3181 cp_parser_nested_name_specifier (cp_parser *parser, 
3182                                  bool typename_keyword_p, 
3183                                  bool check_dependency_p,
3184                                  bool type_p,
3185                                  bool is_declaration)
3186 {
3187   tree scope;
3188
3189   /* Look for the nested-name-specifier.  */
3190   scope = cp_parser_nested_name_specifier_opt (parser,
3191                                                typename_keyword_p,
3192                                                check_dependency_p,
3193                                                type_p,
3194                                                is_declaration);
3195   /* If it was not present, issue an error message.  */
3196   if (!scope)
3197     {
3198       cp_parser_error (parser, "expected nested-name-specifier");
3199       parser->scope = NULL_TREE;
3200       return error_mark_node;
3201     }
3202
3203   return scope;
3204 }
3205
3206 /* Parse a class-or-namespace-name.
3207
3208    class-or-namespace-name:
3209      class-name
3210      namespace-name
3211
3212    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3213    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3214    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3215    TYPE_P is TRUE iff the next name should be taken as a class-name,
3216    even the same name is declared to be another entity in the same
3217    scope.
3218
3219    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3220    specified by the class-or-namespace-name.  If neither is found the
3221    ERROR_MARK_NODE is returned.  */
3222
3223 static tree
3224 cp_parser_class_or_namespace_name (cp_parser *parser, 
3225                                    bool typename_keyword_p,
3226                                    bool template_keyword_p,
3227                                    bool check_dependency_p,
3228                                    bool type_p,
3229                                    bool is_declaration)
3230 {
3231   tree saved_scope;
3232   tree saved_qualifying_scope;
3233   tree saved_object_scope;
3234   tree scope;
3235   bool only_class_p;
3236
3237   /* Before we try to parse the class-name, we must save away the
3238      current PARSER->SCOPE since cp_parser_class_name will destroy
3239      it.  */
3240   saved_scope = parser->scope;
3241   saved_qualifying_scope = parser->qualifying_scope;
3242   saved_object_scope = parser->object_scope;
3243   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3244      there is no need to look for a namespace-name.  */
3245   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3246   if (!only_class_p)
3247     cp_parser_parse_tentatively (parser);
3248   scope = cp_parser_class_name (parser, 
3249                                 typename_keyword_p,
3250                                 template_keyword_p,
3251                                 type_p,
3252                                 check_dependency_p,
3253                                 /*class_head_p=*/false,
3254                                 is_declaration);
3255   /* If that didn't work, try for a namespace-name.  */
3256   if (!only_class_p && !cp_parser_parse_definitely (parser))
3257     {
3258       /* Restore the saved scope.  */
3259       parser->scope = saved_scope;
3260       parser->qualifying_scope = saved_qualifying_scope;
3261       parser->object_scope = saved_object_scope;
3262       /* If we are not looking at an identifier followed by the scope
3263          resolution operator, then this is not part of a
3264          nested-name-specifier.  (Note that this function is only used
3265          to parse the components of a nested-name-specifier.)  */
3266       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3267           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3268         return error_mark_node;
3269       scope = cp_parser_namespace_name (parser);
3270     }
3271
3272   return scope;
3273 }
3274
3275 /* Parse a postfix-expression.
3276
3277    postfix-expression:
3278      primary-expression
3279      postfix-expression [ expression ]
3280      postfix-expression ( expression-list [opt] )
3281      simple-type-specifier ( expression-list [opt] )
3282      typename :: [opt] nested-name-specifier identifier 
3283        ( expression-list [opt] )
3284      typename :: [opt] nested-name-specifier template [opt] template-id
3285        ( expression-list [opt] )
3286      postfix-expression . template [opt] id-expression
3287      postfix-expression -> template [opt] id-expression
3288      postfix-expression . pseudo-destructor-name
3289      postfix-expression -> pseudo-destructor-name
3290      postfix-expression ++
3291      postfix-expression --
3292      dynamic_cast < type-id > ( expression )
3293      static_cast < type-id > ( expression )
3294      reinterpret_cast < type-id > ( expression )
3295      const_cast < type-id > ( expression )
3296      typeid ( expression )
3297      typeid ( type-id )
3298
3299    GNU Extension:
3300      
3301    postfix-expression:
3302      ( type-id ) { initializer-list , [opt] }
3303
3304    This extension is a GNU version of the C99 compound-literal
3305    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3306    but they are essentially the same concept.)
3307
3308    If ADDRESS_P is true, the postfix expression is the operand of the
3309    `&' operator.
3310
3311    Returns a representation of the expression.  */
3312
3313 static tree
3314 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3315 {
3316   cp_token *token;
3317   enum rid keyword;
3318   cp_id_kind idk = CP_ID_KIND_NONE;
3319   tree postfix_expression = NULL_TREE;
3320   /* Non-NULL only if the current postfix-expression can be used to
3321      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3322      class used to qualify the member.  */
3323   tree qualifying_class = NULL_TREE;
3324
3325   /* Peek at the next token.  */
3326   token = cp_lexer_peek_token (parser->lexer);
3327   /* Some of the productions are determined by keywords.  */
3328   keyword = token->keyword;
3329   switch (keyword)
3330     {
3331     case RID_DYNCAST:
3332     case RID_STATCAST:
3333     case RID_REINTCAST:
3334     case RID_CONSTCAST:
3335       {
3336         tree type;
3337         tree expression;
3338         const char *saved_message;
3339
3340         /* All of these can be handled in the same way from the point
3341            of view of parsing.  Begin by consuming the token
3342            identifying the cast.  */
3343         cp_lexer_consume_token (parser->lexer);
3344         
3345         /* New types cannot be defined in the cast.  */
3346         saved_message = parser->type_definition_forbidden_message;
3347         parser->type_definition_forbidden_message
3348           = "types may not be defined in casts";
3349
3350         /* Look for the opening `<'.  */
3351         cp_parser_require (parser, CPP_LESS, "`<'");
3352         /* Parse the type to which we are casting.  */
3353         type = cp_parser_type_id (parser);
3354         /* Look for the closing `>'.  */
3355         cp_parser_require (parser, CPP_GREATER, "`>'");
3356         /* Restore the old message.  */
3357         parser->type_definition_forbidden_message = saved_message;
3358
3359         /* And the expression which is being cast.  */
3360         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3361         expression = cp_parser_expression (parser);
3362         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3363
3364         /* Only type conversions to integral or enumeration types
3365            can be used in constant-expressions.  */
3366         if (parser->constant_expression_p
3367             && !dependent_type_p (type)
3368             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3369           {
3370             if (!parser->allow_non_constant_expression_p)
3371               return (cp_parser_non_constant_expression 
3372                       ("a cast to a type other than an integral or "
3373                        "enumeration type"));
3374             parser->non_constant_expression_p = true;
3375           }
3376
3377         switch (keyword)
3378           {
3379           case RID_DYNCAST:
3380             postfix_expression
3381               = build_dynamic_cast (type, expression);
3382             break;
3383           case RID_STATCAST:
3384             postfix_expression
3385               = build_static_cast (type, expression);
3386             break;
3387           case RID_REINTCAST:
3388             postfix_expression
3389               = build_reinterpret_cast (type, expression);
3390             break;
3391           case RID_CONSTCAST:
3392             postfix_expression
3393               = build_const_cast (type, expression);
3394             break;
3395           default:
3396             abort ();
3397           }
3398       }
3399       break;
3400
3401     case RID_TYPEID:
3402       {
3403         tree type;
3404         const char *saved_message;
3405
3406         /* Consume the `typeid' token.  */
3407         cp_lexer_consume_token (parser->lexer);
3408         /* Look for the `(' token.  */
3409         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3410         /* Types cannot be defined in a `typeid' expression.  */
3411         saved_message = parser->type_definition_forbidden_message;
3412         parser->type_definition_forbidden_message
3413           = "types may not be defined in a `typeid\' expression";
3414         /* We can't be sure yet whether we're looking at a type-id or an
3415            expression.  */
3416         cp_parser_parse_tentatively (parser);
3417         /* Try a type-id first.  */
3418         type = cp_parser_type_id (parser);
3419         /* Look for the `)' token.  Otherwise, we can't be sure that
3420            we're not looking at an expression: consider `typeid (int
3421            (3))', for example.  */
3422         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3423         /* If all went well, simply lookup the type-id.  */
3424         if (cp_parser_parse_definitely (parser))
3425           postfix_expression = get_typeid (type);
3426         /* Otherwise, fall back to the expression variant.  */
3427         else
3428           {
3429             tree expression;
3430
3431             /* Look for an expression.  */
3432             expression = cp_parser_expression (parser);
3433             /* Compute its typeid.  */
3434             postfix_expression = build_typeid (expression);
3435             /* Look for the `)' token.  */
3436             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3437           }
3438
3439         /* Restore the saved message.  */
3440         parser->type_definition_forbidden_message = saved_message;
3441       }
3442       break;
3443       
3444     case RID_TYPENAME:
3445       {
3446         bool template_p = false;
3447         tree id;
3448         tree type;
3449
3450         /* Consume the `typename' token.  */
3451         cp_lexer_consume_token (parser->lexer);
3452         /* Look for the optional `::' operator.  */
3453         cp_parser_global_scope_opt (parser, 
3454                                     /*current_scope_valid_p=*/false);
3455         /* Look for the nested-name-specifier.  */
3456         cp_parser_nested_name_specifier (parser,
3457                                          /*typename_keyword_p=*/true,
3458                                          /*check_dependency_p=*/true,
3459                                          /*type_p=*/true,
3460                                          /*is_declaration=*/true);
3461         /* Look for the optional `template' keyword.  */
3462         template_p = cp_parser_optional_template_keyword (parser);
3463         /* We don't know whether we're looking at a template-id or an
3464            identifier.  */
3465         cp_parser_parse_tentatively (parser);
3466         /* Try a template-id.  */
3467         id = cp_parser_template_id (parser, template_p,
3468                                     /*check_dependency_p=*/true,
3469                                     /*is_declaration=*/true);
3470         /* If that didn't work, try an identifier.  */
3471         if (!cp_parser_parse_definitely (parser))
3472           id = cp_parser_identifier (parser);
3473         /* Create a TYPENAME_TYPE to represent the type to which the
3474            functional cast is being performed.  */
3475         type = make_typename_type (parser->scope, id, 
3476                                    /*complain=*/1);
3477
3478         postfix_expression = cp_parser_functional_cast (parser, type);
3479       }
3480       break;
3481
3482     default:
3483       {
3484         tree type;
3485
3486         /* If the next thing is a simple-type-specifier, we may be
3487            looking at a functional cast.  We could also be looking at
3488            an id-expression.  So, we try the functional cast, and if
3489            that doesn't work we fall back to the primary-expression.  */
3490         cp_parser_parse_tentatively (parser);
3491         /* Look for the simple-type-specifier.  */
3492         type = cp_parser_simple_type_specifier (parser, 
3493                                                 CP_PARSER_FLAGS_NONE,
3494                                                 /*identifier_p=*/false);
3495         /* Parse the cast itself.  */
3496         if (!cp_parser_error_occurred (parser))
3497           postfix_expression 
3498             = cp_parser_functional_cast (parser, type);
3499         /* If that worked, we're done.  */
3500         if (cp_parser_parse_definitely (parser))
3501           break;
3502
3503         /* If the functional-cast didn't work out, try a
3504            compound-literal.  */
3505         if (cp_parser_allow_gnu_extensions_p (parser)
3506             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3507           {
3508             tree initializer_list = NULL_TREE;
3509
3510             cp_parser_parse_tentatively (parser);
3511             /* Consume the `('.  */
3512             cp_lexer_consume_token (parser->lexer);
3513             /* Parse the type.  */
3514             type = cp_parser_type_id (parser);
3515             /* Look for the `)'.  */
3516             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3517             /* Look for the `{'.  */
3518             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3519             /* If things aren't going well, there's no need to
3520                keep going.  */
3521             if (!cp_parser_error_occurred (parser))
3522               {
3523                 bool non_constant_p;
3524                 /* Parse the initializer-list.  */
3525                 initializer_list 
3526                   = cp_parser_initializer_list (parser, &non_constant_p);
3527                 /* Allow a trailing `,'.  */
3528                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3529                   cp_lexer_consume_token (parser->lexer);
3530                 /* Look for the final `}'.  */
3531                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3532               }
3533             /* If that worked, we're definitely looking at a
3534                compound-literal expression.  */
3535             if (cp_parser_parse_definitely (parser))
3536               {
3537                 /* Warn the user that a compound literal is not
3538                    allowed in standard C++.  */
3539                 if (pedantic)
3540                   pedwarn ("ISO C++ forbids compound-literals");
3541                 /* Form the representation of the compound-literal.  */
3542                 postfix_expression 
3543                   = finish_compound_literal (type, initializer_list);
3544                 break;
3545               }
3546           }
3547
3548         /* It must be a primary-expression.  */
3549         postfix_expression = cp_parser_primary_expression (parser, 
3550                                                            &idk,
3551                                                            &qualifying_class);
3552       }
3553       break;
3554     }
3555
3556   /* If we were avoiding committing to the processing of a
3557      qualified-id until we knew whether or not we had a
3558      pointer-to-member, we now know.  */
3559   if (qualifying_class)
3560     {
3561       bool done;
3562
3563       /* Peek at the next token.  */
3564       token = cp_lexer_peek_token (parser->lexer);
3565       done = (token->type != CPP_OPEN_SQUARE
3566               && token->type != CPP_OPEN_PAREN
3567               && token->type != CPP_DOT
3568               && token->type != CPP_DEREF
3569               && token->type != CPP_PLUS_PLUS
3570               && token->type != CPP_MINUS_MINUS);
3571
3572       postfix_expression = finish_qualified_id_expr (qualifying_class,
3573                                                      postfix_expression,
3574                                                      done,
3575                                                      address_p);
3576       if (done)
3577         return postfix_expression;
3578     }
3579
3580   /* Keep looping until the postfix-expression is complete.  */
3581   while (true)
3582     {
3583       if (idk == CP_ID_KIND_UNQUALIFIED
3584           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3585           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3586         /* It is not a Koenig lookup function call.  */
3587         postfix_expression 
3588           = unqualified_name_lookup_error (postfix_expression);
3589       
3590       /* Peek at the next token.  */
3591       token = cp_lexer_peek_token (parser->lexer);
3592
3593       switch (token->type)
3594         {
3595         case CPP_OPEN_SQUARE:
3596           /* postfix-expression [ expression ] */
3597           {
3598             tree index;
3599
3600             /* Consume the `[' token.  */
3601             cp_lexer_consume_token (parser->lexer);
3602             /* Parse the index expression.  */
3603             index = cp_parser_expression (parser);
3604             /* Look for the closing `]'.  */
3605             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3606
3607             /* Build the ARRAY_REF.  */
3608             postfix_expression 
3609               = grok_array_decl (postfix_expression, index);
3610             idk = CP_ID_KIND_NONE;
3611           }
3612           break;
3613
3614         case CPP_OPEN_PAREN:
3615           /* postfix-expression ( expression-list [opt] ) */
3616           {
3617             bool koenig_p;
3618             tree args = (cp_parser_parenthesized_expression_list 
3619                          (parser, false, /*non_constant_p=*/NULL));
3620
3621             if (args == error_mark_node)
3622               {
3623                 postfix_expression = error_mark_node;
3624                 break;
3625               }
3626             
3627             /* Function calls are not permitted in
3628                constant-expressions.  */
3629             if (parser->constant_expression_p)
3630               {
3631                 if (!parser->allow_non_constant_expression_p)
3632                   return cp_parser_non_constant_expression ("a function call");
3633                 parser->non_constant_expression_p = true;
3634               }
3635
3636             koenig_p = false;
3637             if (idk == CP_ID_KIND_UNQUALIFIED)
3638               {
3639                 if (args
3640                     && (is_overloaded_fn (postfix_expression)
3641                         || DECL_P (postfix_expression)
3642                         || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3643                   {
3644                     koenig_p = true;
3645                     postfix_expression 
3646                       = perform_koenig_lookup (postfix_expression, args);
3647                   }
3648                 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3649                   postfix_expression
3650                     = unqualified_fn_lookup_error (postfix_expression);
3651               }
3652           
3653             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3654               {
3655                 tree instance = TREE_OPERAND (postfix_expression, 0);
3656                 tree fn = TREE_OPERAND (postfix_expression, 1);
3657
3658                 if (processing_template_decl
3659                     && (type_dependent_expression_p (instance)
3660                         || (!BASELINK_P (fn)
3661                             && TREE_CODE (fn) != FIELD_DECL)
3662                         || type_dependent_expression_p (fn)
3663                         || any_type_dependent_arguments_p (args)))
3664                   {
3665                     postfix_expression
3666                       = build_min_nt (CALL_EXPR, postfix_expression, args);
3667                     break;
3668                   }
3669                   
3670                 postfix_expression
3671                   = (build_new_method_call 
3672                      (instance, fn, args, NULL_TREE, 
3673                       (idk == CP_ID_KIND_QUALIFIED 
3674                        ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3675               }
3676             else if (TREE_CODE (postfix_expression) == OFFSET_REF
3677                      || TREE_CODE (postfix_expression) == MEMBER_REF
3678                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3679               postfix_expression = (build_offset_ref_call_from_tree
3680                                     (postfix_expression, args));
3681             else if (idk == CP_ID_KIND_QUALIFIED)
3682               /* A call to a static class member, or a namespace-scope
3683                  function.  */
3684               postfix_expression
3685                 = finish_call_expr (postfix_expression, args,
3686                                     /*disallow_virtual=*/true,
3687                                     koenig_p);
3688             else
3689               /* All other function calls.  */
3690               postfix_expression 
3691                 = finish_call_expr (postfix_expression, args, 
3692                                     /*disallow_virtual=*/false,
3693                                     koenig_p);
3694
3695             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
3696             idk = CP_ID_KIND_NONE;
3697           }
3698           break;
3699           
3700         case CPP_DOT:
3701         case CPP_DEREF:
3702           /* postfix-expression . template [opt] id-expression  
3703              postfix-expression . pseudo-destructor-name 
3704              postfix-expression -> template [opt] id-expression
3705              postfix-expression -> pseudo-destructor-name */
3706           {
3707             tree name;
3708             bool dependent_p;
3709             bool template_p;
3710             tree scope = NULL_TREE;
3711
3712             /* If this is a `->' operator, dereference the pointer.  */
3713             if (token->type == CPP_DEREF)
3714               postfix_expression = build_x_arrow (postfix_expression);
3715             /* Check to see whether or not the expression is
3716                type-dependent.  */
3717             dependent_p = type_dependent_expression_p (postfix_expression);
3718             /* The identifier following the `->' or `.' is not
3719                qualified.  */
3720             parser->scope = NULL_TREE;
3721             parser->qualifying_scope = NULL_TREE;
3722             parser->object_scope = NULL_TREE;
3723             idk = CP_ID_KIND_NONE;
3724             /* Enter the scope corresponding to the type of the object
3725                given by the POSTFIX_EXPRESSION.  */
3726             if (!dependent_p 
3727                 && TREE_TYPE (postfix_expression) != NULL_TREE)
3728               {
3729                 scope = TREE_TYPE (postfix_expression);
3730                 /* According to the standard, no expression should
3731                    ever have reference type.  Unfortunately, we do not
3732                    currently match the standard in this respect in
3733                    that our internal representation of an expression
3734                    may have reference type even when the standard says
3735                    it does not.  Therefore, we have to manually obtain
3736                    the underlying type here.  */
3737                 scope = non_reference (scope);
3738                 /* The type of the POSTFIX_EXPRESSION must be
3739                    complete.  */
3740                 scope = complete_type_or_else (scope, NULL_TREE);
3741                 /* Let the name lookup machinery know that we are
3742                    processing a class member access expression.  */
3743                 parser->context->object_type = scope;
3744                 /* If something went wrong, we want to be able to
3745                    discern that case, as opposed to the case where
3746                    there was no SCOPE due to the type of expression
3747                    being dependent.  */
3748                 if (!scope)
3749                   scope = error_mark_node;
3750               }
3751
3752             /* Consume the `.' or `->' operator.  */
3753             cp_lexer_consume_token (parser->lexer);
3754             /* If the SCOPE is not a scalar type, we are looking at an
3755                ordinary class member access expression, rather than a
3756                pseudo-destructor-name.  */
3757             if (!scope || !SCALAR_TYPE_P (scope))
3758               {
3759                 template_p = cp_parser_optional_template_keyword (parser);
3760                 /* Parse the id-expression.  */
3761                 name = cp_parser_id_expression (parser,
3762                                                 template_p,
3763                                                 /*check_dependency_p=*/true,
3764                                                 /*template_p=*/NULL,
3765                                                 /*declarator_p=*/false);
3766                 /* In general, build a SCOPE_REF if the member name is
3767                    qualified.  However, if the name was not dependent
3768                    and has already been resolved; there is no need to
3769                    build the SCOPE_REF.  For example;
3770
3771                      struct X { void f(); };
3772                      template <typename T> void f(T* t) { t->X::f(); }
3773  
3774                    Even though "t" is dependent, "X::f" is not and has
3775                    been resolved to a BASELINK; there is no need to
3776                    include scope information.  */
3777
3778                 /* But we do need to remember that there was an explicit
3779                    scope for virtual function calls.  */
3780                 if (parser->scope)
3781                   idk = CP_ID_KIND_QUALIFIED;
3782
3783                 if (name != error_mark_node 
3784                     && !BASELINK_P (name)
3785                     && parser->scope)
3786                   {
3787                     name = build_nt (SCOPE_REF, parser->scope, name);
3788                     parser->scope = NULL_TREE;
3789                     parser->qualifying_scope = NULL_TREE;
3790                     parser->object_scope = NULL_TREE;
3791                   }
3792                 postfix_expression 
3793                   = finish_class_member_access_expr (postfix_expression, name);
3794               }
3795             /* Otherwise, try the pseudo-destructor-name production.  */
3796             else
3797               {
3798                 tree s = NULL_TREE;
3799                 tree type;
3800
3801                 /* Parse the pseudo-destructor-name.  */
3802                 cp_parser_pseudo_destructor_name (parser, &s, &type);
3803                 /* Form the call.  */
3804                 postfix_expression 
3805                   = finish_pseudo_destructor_expr (postfix_expression,
3806                                                    s, TREE_TYPE (type));
3807               }
3808
3809             /* We no longer need to look up names in the scope of the
3810                object on the left-hand side of the `.' or `->'
3811                operator.  */
3812             parser->context->object_type = NULL_TREE;
3813           }
3814           break;
3815
3816         case CPP_PLUS_PLUS:
3817           /* postfix-expression ++  */
3818           /* Consume the `++' token.  */
3819           cp_lexer_consume_token (parser->lexer);
3820           /* Increments may not appear in constant-expressions.  */
3821           if (parser->constant_expression_p)
3822             {
3823               if (!parser->allow_non_constant_expression_p)
3824                 return cp_parser_non_constant_expression ("an increment");
3825               parser->non_constant_expression_p = true;
3826             }
3827           /* Generate a representation for the complete expression.  */
3828           postfix_expression 
3829             = finish_increment_expr (postfix_expression, 
3830                                      POSTINCREMENT_EXPR);
3831           idk = CP_ID_KIND_NONE;
3832           break;
3833
3834         case CPP_MINUS_MINUS:
3835           /* postfix-expression -- */
3836           /* Consume the `--' token.  */
3837           cp_lexer_consume_token (parser->lexer);
3838           /* Decrements may not appear in constant-expressions.  */
3839           if (parser->constant_expression_p)
3840             {
3841               if (!parser->allow_non_constant_expression_p)
3842                 return cp_parser_non_constant_expression ("a decrement");
3843               parser->non_constant_expression_p = true;
3844             }
3845           /* Generate a representation for the complete expression.  */
3846           postfix_expression 
3847             = finish_increment_expr (postfix_expression, 
3848                                      POSTDECREMENT_EXPR);
3849           idk = CP_ID_KIND_NONE;
3850           break;
3851
3852         default:
3853           return postfix_expression;
3854         }
3855     }
3856
3857   /* We should never get here.  */
3858   abort ();
3859   return error_mark_node;
3860 }
3861
3862 /* Parse a parenthesized expression-list.
3863
3864    expression-list:
3865      assignment-expression
3866      expression-list, assignment-expression
3867
3868    attribute-list:
3869      expression-list
3870      identifier
3871      identifier, expression-list
3872
3873    Returns a TREE_LIST.  The TREE_VALUE of each node is a
3874    representation of an assignment-expression.  Note that a TREE_LIST
3875    is returned even if there is only a single expression in the list.
3876    error_mark_node is returned if the ( and or ) are
3877    missing. NULL_TREE is returned on no expressions. The parentheses
3878    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
3879    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3880    indicates whether or not all of the expressions in the list were
3881    constant.  */
3882
3883 static tree
3884 cp_parser_parenthesized_expression_list (cp_parser* parser, 
3885                                          bool is_attribute_list,
3886                                          bool *non_constant_p)
3887 {
3888   tree expression_list = NULL_TREE;
3889   tree identifier = NULL_TREE;
3890
3891   /* Assume all the expressions will be constant.  */
3892   if (non_constant_p)
3893     *non_constant_p = false;
3894
3895   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3896     return error_mark_node;
3897   
3898   /* Consume expressions until there are no more.  */
3899   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
3900     while (true)
3901       {
3902         tree expr;
3903         
3904         /* At the beginning of attribute lists, check to see if the
3905            next token is an identifier.  */
3906         if (is_attribute_list
3907             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
3908           {
3909             cp_token *token;
3910             
3911             /* Consume the identifier.  */
3912             token = cp_lexer_consume_token (parser->lexer);
3913             /* Save the identifier.  */
3914             identifier = token->value;
3915           }
3916         else
3917           {
3918             /* Parse the next assignment-expression.  */
3919             if (non_constant_p)
3920               {
3921                 bool expr_non_constant_p;
3922                 expr = (cp_parser_constant_expression 
3923                         (parser, /*allow_non_constant_p=*/true,
3924                          &expr_non_constant_p));
3925                 if (expr_non_constant_p)
3926                   *non_constant_p = true;
3927               }
3928             else
3929               expr = cp_parser_assignment_expression (parser);
3930
3931              /* Add it to the list.  We add error_mark_node
3932                 expressions to the list, so that we can still tell if
3933                 the correct form for a parenthesized expression-list
3934                 is found. That gives better errors.  */
3935             expression_list = tree_cons (NULL_TREE, expr, expression_list);
3936
3937             if (expr == error_mark_node)
3938               goto skip_comma;
3939           }
3940
3941         /* After the first item, attribute lists look the same as
3942            expression lists.  */
3943         is_attribute_list = false;
3944         
3945       get_comma:;
3946         /* If the next token isn't a `,', then we are done.  */
3947         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
3948           break;
3949
3950         /* Otherwise, consume the `,' and keep going.  */
3951         cp_lexer_consume_token (parser->lexer);
3952       }
3953   
3954   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3955     {
3956       int ending;
3957       
3958     skip_comma:;
3959       /* We try and resync to an unnested comma, as that will give the
3960          user better diagnostics.  */
3961       ending = cp_parser_skip_to_closing_parenthesis (parser, true, true,
3962                                                       /*consume_paren=*/true);
3963       if (ending < 0)
3964         goto get_comma;
3965       if (!ending)
3966         return error_mark_node;
3967     }
3968
3969   /* We built up the list in reverse order so we must reverse it now.  */
3970   expression_list = nreverse (expression_list);
3971   if (identifier)
3972     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
3973   
3974   return expression_list;
3975 }
3976
3977 /* Parse a pseudo-destructor-name.
3978
3979    pseudo-destructor-name:
3980      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
3981      :: [opt] nested-name-specifier template template-id :: ~ type-name
3982      :: [opt] nested-name-specifier [opt] ~ type-name
3983
3984    If either of the first two productions is used, sets *SCOPE to the
3985    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
3986    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
3987    or ERROR_MARK_NODE if no type-name is present.  */
3988
3989 static void
3990 cp_parser_pseudo_destructor_name (cp_parser* parser, 
3991                                   tree* scope, 
3992                                   tree* type)
3993 {
3994   bool nested_name_specifier_p;
3995
3996   /* Look for the optional `::' operator.  */
3997   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
3998   /* Look for the optional nested-name-specifier.  */
3999   nested_name_specifier_p 
4000     = (cp_parser_nested_name_specifier_opt (parser,
4001                                             /*typename_keyword_p=*/false,
4002                                             /*check_dependency_p=*/true,
4003                                             /*type_p=*/false,
4004                                             /*is_declaration=*/true) 
4005        != NULL_TREE);
4006   /* Now, if we saw a nested-name-specifier, we might be doing the
4007      second production.  */
4008   if (nested_name_specifier_p 
4009       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4010     {
4011       /* Consume the `template' keyword.  */
4012       cp_lexer_consume_token (parser->lexer);
4013       /* Parse the template-id.  */
4014       cp_parser_template_id (parser, 
4015                              /*template_keyword_p=*/true,
4016                              /*check_dependency_p=*/false,
4017                              /*is_declaration=*/true);
4018       /* Look for the `::' token.  */
4019       cp_parser_require (parser, CPP_SCOPE, "`::'");
4020     }
4021   /* If the next token is not a `~', then there might be some
4022      additional qualification.  */
4023   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4024     {
4025       /* Look for the type-name.  */
4026       *scope = TREE_TYPE (cp_parser_type_name (parser));
4027       /* Look for the `::' token.  */
4028       cp_parser_require (parser, CPP_SCOPE, "`::'");
4029     }
4030   else
4031     *scope = NULL_TREE;
4032
4033   /* Look for the `~'.  */
4034   cp_parser_require (parser, CPP_COMPL, "`~'");
4035   /* Look for the type-name again.  We are not responsible for
4036      checking that it matches the first type-name.  */
4037   *type = cp_parser_type_name (parser);
4038 }
4039
4040 /* Parse a unary-expression.
4041
4042    unary-expression:
4043      postfix-expression
4044      ++ cast-expression
4045      -- cast-expression
4046      unary-operator cast-expression
4047      sizeof unary-expression
4048      sizeof ( type-id )
4049      new-expression
4050      delete-expression
4051
4052    GNU Extensions:
4053
4054    unary-expression:
4055      __extension__ cast-expression
4056      __alignof__ unary-expression
4057      __alignof__ ( type-id )
4058      __real__ cast-expression
4059      __imag__ cast-expression
4060      && identifier
4061
4062    ADDRESS_P is true iff the unary-expression is appearing as the
4063    operand of the `&' operator.
4064
4065    Returns a representation of the expression.  */
4066
4067 static tree
4068 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4069 {
4070   cp_token *token;
4071   enum tree_code unary_operator;
4072
4073   /* Peek at the next token.  */
4074   token = cp_lexer_peek_token (parser->lexer);
4075   /* Some keywords give away the kind of expression.  */
4076   if (token->type == CPP_KEYWORD)
4077     {
4078       enum rid keyword = token->keyword;
4079
4080       switch (keyword)
4081         {
4082         case RID_ALIGNOF:
4083         case RID_SIZEOF:
4084           {
4085             tree operand;
4086             enum tree_code op;
4087             
4088             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4089             /* Consume the token.  */
4090             cp_lexer_consume_token (parser->lexer);
4091             /* Parse the operand.  */
4092             operand = cp_parser_sizeof_operand (parser, keyword);
4093
4094             if (TYPE_P (operand))
4095               return cxx_sizeof_or_alignof_type (operand, op, true);
4096             else
4097               return cxx_sizeof_or_alignof_expr (operand, op);
4098           }
4099
4100         case RID_NEW:
4101           return cp_parser_new_expression (parser);
4102
4103         case RID_DELETE:
4104           return cp_parser_delete_expression (parser);
4105           
4106         case RID_EXTENSION:
4107           {
4108             /* The saved value of the PEDANTIC flag.  */
4109             int saved_pedantic;
4110             tree expr;
4111
4112             /* Save away the PEDANTIC flag.  */
4113             cp_parser_extension_opt (parser, &saved_pedantic);
4114             /* Parse the cast-expression.  */
4115             expr = cp_parser_simple_cast_expression (parser);
4116             /* Restore the PEDANTIC flag.  */
4117             pedantic = saved_pedantic;
4118
4119             return expr;
4120           }
4121
4122         case RID_REALPART:
4123         case RID_IMAGPART:
4124           {
4125             tree expression;
4126
4127             /* Consume the `__real__' or `__imag__' token.  */
4128             cp_lexer_consume_token (parser->lexer);
4129             /* Parse the cast-expression.  */
4130             expression = cp_parser_simple_cast_expression (parser);
4131             /* Create the complete representation.  */
4132             return build_x_unary_op ((keyword == RID_REALPART
4133                                       ? REALPART_EXPR : IMAGPART_EXPR),
4134                                      expression);
4135           }
4136           break;
4137
4138         default:
4139           break;
4140         }
4141     }
4142
4143   /* Look for the `:: new' and `:: delete', which also signal the
4144      beginning of a new-expression, or delete-expression,
4145      respectively.  If the next token is `::', then it might be one of
4146      these.  */
4147   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4148     {
4149       enum rid keyword;
4150
4151       /* See if the token after the `::' is one of the keywords in
4152          which we're interested.  */
4153       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4154       /* If it's `new', we have a new-expression.  */
4155       if (keyword == RID_NEW)
4156         return cp_parser_new_expression (parser);
4157       /* Similarly, for `delete'.  */
4158       else if (keyword == RID_DELETE)
4159         return cp_parser_delete_expression (parser);
4160     }
4161
4162   /* Look for a unary operator.  */
4163   unary_operator = cp_parser_unary_operator (token);
4164   /* The `++' and `--' operators can be handled similarly, even though
4165      they are not technically unary-operators in the grammar.  */
4166   if (unary_operator == ERROR_MARK)
4167     {
4168       if (token->type == CPP_PLUS_PLUS)
4169         unary_operator = PREINCREMENT_EXPR;
4170       else if (token->type == CPP_MINUS_MINUS)
4171         unary_operator = PREDECREMENT_EXPR;
4172       /* Handle the GNU address-of-label extension.  */
4173       else if (cp_parser_allow_gnu_extensions_p (parser)
4174                && token->type == CPP_AND_AND)
4175         {
4176           tree identifier;
4177
4178           /* Consume the '&&' token.  */
4179           cp_lexer_consume_token (parser->lexer);
4180           /* Look for the identifier.  */
4181           identifier = cp_parser_identifier (parser);
4182           /* Create an expression representing the address.  */
4183           return finish_label_address_expr (identifier);
4184         }
4185     }
4186   if (unary_operator != ERROR_MARK)
4187     {
4188       tree cast_expression;
4189
4190       /* Consume the operator token.  */
4191       token = cp_lexer_consume_token (parser->lexer);
4192       /* Parse the cast-expression.  */
4193       cast_expression 
4194         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4195       /* Now, build an appropriate representation.  */
4196       switch (unary_operator)
4197         {
4198         case INDIRECT_REF:
4199           return build_x_indirect_ref (cast_expression, "unary *");
4200           
4201         case ADDR_EXPR:
4202         case BIT_NOT_EXPR:
4203           return build_x_unary_op (unary_operator, cast_expression);
4204           
4205         case PREINCREMENT_EXPR:
4206         case PREDECREMENT_EXPR:
4207           if (parser->constant_expression_p)
4208             {
4209               if (!parser->allow_non_constant_expression_p)
4210                 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4211                                                           ? "an increment"
4212                                                           : "a decrement");
4213               parser->non_constant_expression_p = true;
4214             }
4215           /* Fall through.  */
4216         case CONVERT_EXPR:
4217         case NEGATE_EXPR:
4218         case TRUTH_NOT_EXPR:
4219           return finish_unary_op_expr (unary_operator, cast_expression);
4220
4221         default:
4222           abort ();
4223           return error_mark_node;
4224         }
4225     }
4226
4227   return cp_parser_postfix_expression (parser, address_p);
4228 }
4229
4230 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4231    unary-operator, the corresponding tree code is returned.  */
4232
4233 static enum tree_code
4234 cp_parser_unary_operator (cp_token* token)
4235 {
4236   switch (token->type)
4237     {
4238     case CPP_MULT:
4239       return INDIRECT_REF;
4240
4241     case CPP_AND:
4242       return ADDR_EXPR;
4243
4244     case CPP_PLUS:
4245       return CONVERT_EXPR;
4246
4247     case CPP_MINUS:
4248       return NEGATE_EXPR;
4249
4250     case CPP_NOT:
4251       return TRUTH_NOT_EXPR;
4252       
4253     case CPP_COMPL:
4254       return BIT_NOT_EXPR;
4255
4256     default:
4257       return ERROR_MARK;
4258     }
4259 }
4260
4261 /* Parse a new-expression.
4262
4263    new-expression:
4264      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4265      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4266
4267    Returns a representation of the expression.  */
4268
4269 static tree
4270 cp_parser_new_expression (cp_parser* parser)
4271 {
4272   bool global_scope_p;
4273   tree placement;
4274   tree type;
4275   tree initializer;
4276
4277   /* Look for the optional `::' operator.  */
4278   global_scope_p 
4279     = (cp_parser_global_scope_opt (parser,
4280                                    /*current_scope_valid_p=*/false)
4281        != NULL_TREE);
4282   /* Look for the `new' operator.  */
4283   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4284   /* There's no easy way to tell a new-placement from the
4285      `( type-id )' construct.  */
4286   cp_parser_parse_tentatively (parser);
4287   /* Look for a new-placement.  */
4288   placement = cp_parser_new_placement (parser);
4289   /* If that didn't work out, there's no new-placement.  */
4290   if (!cp_parser_parse_definitely (parser))
4291     placement = NULL_TREE;
4292
4293   /* If the next token is a `(', then we have a parenthesized
4294      type-id.  */
4295   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4296     {
4297       /* Consume the `('.  */
4298       cp_lexer_consume_token (parser->lexer);
4299       /* Parse the type-id.  */
4300       type = cp_parser_type_id (parser);
4301       /* Look for the closing `)'.  */
4302       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4303     }
4304   /* Otherwise, there must be a new-type-id.  */
4305   else
4306     type = cp_parser_new_type_id (parser);
4307
4308   /* If the next token is a `(', then we have a new-initializer.  */
4309   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4310     initializer = cp_parser_new_initializer (parser);
4311   else
4312     initializer = NULL_TREE;
4313
4314   /* Create a representation of the new-expression.  */
4315   return build_new (placement, type, initializer, global_scope_p);
4316 }
4317
4318 /* Parse a new-placement.
4319
4320    new-placement:
4321      ( expression-list )
4322
4323    Returns the same representation as for an expression-list.  */
4324
4325 static tree
4326 cp_parser_new_placement (cp_parser* parser)
4327 {
4328   tree expression_list;
4329
4330   /* Parse the expression-list.  */
4331   expression_list = (cp_parser_parenthesized_expression_list 
4332                      (parser, false, /*non_constant_p=*/NULL));
4333
4334   return expression_list;
4335 }
4336
4337 /* Parse a new-type-id.
4338
4339    new-type-id:
4340      type-specifier-seq new-declarator [opt]
4341
4342    Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4343    and whose TREE_VALUE is the new-declarator.  */
4344
4345 static tree
4346 cp_parser_new_type_id (cp_parser* parser)
4347 {
4348   tree type_specifier_seq;
4349   tree declarator;
4350   const char *saved_message;
4351
4352   /* The type-specifier sequence must not contain type definitions.
4353      (It cannot contain declarations of new types either, but if they
4354      are not definitions we will catch that because they are not
4355      complete.)  */
4356   saved_message = parser->type_definition_forbidden_message;
4357   parser->type_definition_forbidden_message
4358     = "types may not be defined in a new-type-id";
4359   /* Parse the type-specifier-seq.  */
4360   type_specifier_seq = cp_parser_type_specifier_seq (parser);
4361   /* Restore the old message.  */
4362   parser->type_definition_forbidden_message = saved_message;
4363   /* Parse the new-declarator.  */
4364   declarator = cp_parser_new_declarator_opt (parser);
4365
4366   return build_tree_list (type_specifier_seq, declarator);
4367 }
4368
4369 /* Parse an (optional) new-declarator.
4370
4371    new-declarator:
4372      ptr-operator new-declarator [opt]
4373      direct-new-declarator
4374
4375    Returns a representation of the declarator.  See
4376    cp_parser_declarator for the representations used.  */
4377
4378 static tree
4379 cp_parser_new_declarator_opt (cp_parser* parser)
4380 {
4381   enum tree_code code;
4382   tree type;
4383   tree cv_qualifier_seq;
4384
4385   /* We don't know if there's a ptr-operator next, or not.  */
4386   cp_parser_parse_tentatively (parser);
4387   /* Look for a ptr-operator.  */
4388   code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4389   /* If that worked, look for more new-declarators.  */
4390   if (cp_parser_parse_definitely (parser))
4391     {
4392       tree declarator;
4393
4394       /* Parse another optional declarator.  */
4395       declarator = cp_parser_new_declarator_opt (parser);
4396
4397       /* Create the representation of the declarator.  */
4398       if (code == INDIRECT_REF)
4399         declarator = make_pointer_declarator (cv_qualifier_seq,
4400                                               declarator);
4401       else
4402         declarator = make_reference_declarator (cv_qualifier_seq,
4403                                                 declarator);
4404
4405      /* Handle the pointer-to-member case.  */
4406      if (type)
4407        declarator = build_nt (SCOPE_REF, type, declarator);
4408
4409       return declarator;
4410     }
4411
4412   /* If the next token is a `[', there is a direct-new-declarator.  */
4413   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4414     return cp_parser_direct_new_declarator (parser);
4415
4416   return NULL_TREE;
4417 }
4418
4419 /* Parse a direct-new-declarator.
4420
4421    direct-new-declarator:
4422      [ expression ]
4423      direct-new-declarator [constant-expression]  
4424
4425    Returns an ARRAY_REF, following the same conventions as are
4426    documented for cp_parser_direct_declarator.  */
4427
4428 static tree
4429 cp_parser_direct_new_declarator (cp_parser* parser)
4430 {
4431   tree declarator = NULL_TREE;
4432
4433   while (true)
4434     {
4435       tree expression;
4436
4437       /* Look for the opening `['.  */
4438       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4439       /* The first expression is not required to be constant.  */
4440       if (!declarator)
4441         {
4442           expression = cp_parser_expression (parser);
4443           /* The standard requires that the expression have integral
4444              type.  DR 74 adds enumeration types.  We believe that the
4445              real intent is that these expressions be handled like the
4446              expression in a `switch' condition, which also allows
4447              classes with a single conversion to integral or
4448              enumeration type.  */
4449           if (!processing_template_decl)
4450             {
4451               expression 
4452                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4453                                               expression,
4454                                               /*complain=*/true);
4455               if (!expression)
4456                 {
4457                   error ("expression in new-declarator must have integral or enumeration type");
4458                   expression = error_mark_node;
4459                 }
4460             }
4461         }
4462       /* But all the other expressions must be.  */
4463       else
4464         expression 
4465           = cp_parser_constant_expression (parser, 
4466                                            /*allow_non_constant=*/false,
4467                                            NULL);
4468       /* Look for the closing `]'.  */
4469       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4470
4471       /* Add this bound to the declarator.  */
4472       declarator = build_nt (ARRAY_REF, declarator, expression);
4473
4474       /* If the next token is not a `[', then there are no more
4475          bounds.  */
4476       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4477         break;
4478     }
4479
4480   return declarator;
4481 }
4482
4483 /* Parse a new-initializer.
4484
4485    new-initializer:
4486      ( expression-list [opt] )
4487
4488    Returns a representation of the expression-list.  If there is no
4489    expression-list, VOID_ZERO_NODE is returned.  */
4490
4491 static tree
4492 cp_parser_new_initializer (cp_parser* parser)
4493 {
4494   tree expression_list;
4495
4496   expression_list = (cp_parser_parenthesized_expression_list 
4497                      (parser, false, /*non_constant_p=*/NULL));
4498   if (!expression_list)
4499     expression_list = void_zero_node;
4500
4501   return expression_list;
4502 }
4503
4504 /* Parse a delete-expression.
4505
4506    delete-expression:
4507      :: [opt] delete cast-expression
4508      :: [opt] delete [ ] cast-expression
4509
4510    Returns a representation of the expression.  */
4511
4512 static tree
4513 cp_parser_delete_expression (cp_parser* parser)
4514 {
4515   bool global_scope_p;
4516   bool array_p;
4517   tree expression;
4518
4519   /* Look for the optional `::' operator.  */
4520   global_scope_p
4521     = (cp_parser_global_scope_opt (parser,
4522                                    /*current_scope_valid_p=*/false)
4523        != NULL_TREE);
4524   /* Look for the `delete' keyword.  */
4525   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4526   /* See if the array syntax is in use.  */
4527   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4528     {
4529       /* Consume the `[' token.  */
4530       cp_lexer_consume_token (parser->lexer);
4531       /* Look for the `]' token.  */
4532       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4533       /* Remember that this is the `[]' construct.  */
4534       array_p = true;
4535     }
4536   else
4537     array_p = false;
4538
4539   /* Parse the cast-expression.  */
4540   expression = cp_parser_simple_cast_expression (parser);
4541
4542   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4543 }
4544
4545 /* Parse a cast-expression.
4546
4547    cast-expression:
4548      unary-expression
4549      ( type-id ) cast-expression
4550
4551    Returns a representation of the expression.  */
4552
4553 static tree
4554 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4555 {
4556   /* If it's a `(', then we might be looking at a cast.  */
4557   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4558     {
4559       tree type = NULL_TREE;
4560       tree expr = NULL_TREE;
4561       bool compound_literal_p;
4562       const char *saved_message;
4563
4564       /* There's no way to know yet whether or not this is a cast.
4565          For example, `(int (3))' is a unary-expression, while `(int)
4566          3' is a cast.  So, we resort to parsing tentatively.  */
4567       cp_parser_parse_tentatively (parser);
4568       /* Types may not be defined in a cast.  */
4569       saved_message = parser->type_definition_forbidden_message;
4570       parser->type_definition_forbidden_message
4571         = "types may not be defined in casts";
4572       /* Consume the `('.  */
4573       cp_lexer_consume_token (parser->lexer);
4574       /* A very tricky bit is that `(struct S) { 3 }' is a
4575          compound-literal (which we permit in C++ as an extension).
4576          But, that construct is not a cast-expression -- it is a
4577          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
4578          is legal; if the compound-literal were a cast-expression,
4579          you'd need an extra set of parentheses.)  But, if we parse
4580          the type-id, and it happens to be a class-specifier, then we
4581          will commit to the parse at that point, because we cannot
4582          undo the action that is done when creating a new class.  So,
4583          then we cannot back up and do a postfix-expression.  
4584
4585          Therefore, we scan ahead to the closing `)', and check to see
4586          if the token after the `)' is a `{'.  If so, we are not
4587          looking at a cast-expression.  
4588
4589          Save tokens so that we can put them back.  */
4590       cp_lexer_save_tokens (parser->lexer);
4591       /* Skip tokens until the next token is a closing parenthesis.
4592          If we find the closing `)', and the next token is a `{', then
4593          we are looking at a compound-literal.  */
4594       compound_literal_p 
4595         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4596                                                   /*consume_paren=*/true)
4597            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4598       /* Roll back the tokens we skipped.  */
4599       cp_lexer_rollback_tokens (parser->lexer);
4600       /* If we were looking at a compound-literal, simulate an error
4601          so that the call to cp_parser_parse_definitely below will
4602          fail.  */
4603       if (compound_literal_p)
4604         cp_parser_simulate_error (parser);
4605       else
4606         {
4607           /* Look for the type-id.  */
4608           type = cp_parser_type_id (parser);
4609           /* Look for the closing `)'.  */
4610           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4611         }
4612
4613       /* Restore the saved message.  */
4614       parser->type_definition_forbidden_message = saved_message;
4615
4616       /* If ok so far, parse the dependent expression. We cannot be
4617          sure it is a cast. Consider `(T ())'.  It is a parenthesized
4618          ctor of T, but looks like a cast to function returning T
4619          without a dependent expression.  */
4620       if (!cp_parser_error_occurred (parser))
4621         expr = cp_parser_simple_cast_expression (parser);
4622
4623       if (cp_parser_parse_definitely (parser))
4624         {
4625           /* Warn about old-style casts, if so requested.  */
4626           if (warn_old_style_cast 
4627               && !in_system_header 
4628               && !VOID_TYPE_P (type) 
4629               && current_lang_name != lang_name_c)
4630             warning ("use of old-style cast");
4631
4632           /* Only type conversions to integral or enumeration types
4633              can be used in constant-expressions.  */
4634           if (parser->constant_expression_p
4635               && !dependent_type_p (type)
4636               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4637             {
4638               if (!parser->allow_non_constant_expression_p)
4639                 return (cp_parser_non_constant_expression 
4640                         ("a casts to a type other than an integral or "
4641                          "enumeration type"));
4642               parser->non_constant_expression_p = true;
4643             }
4644           /* Perform the cast.  */
4645           expr = build_c_cast (type, expr);
4646           return expr;
4647         }
4648     }
4649
4650   /* If we get here, then it's not a cast, so it must be a
4651      unary-expression.  */
4652   return cp_parser_unary_expression (parser, address_p);
4653 }
4654
4655 /* Parse a pm-expression.
4656
4657    pm-expression:
4658      cast-expression
4659      pm-expression .* cast-expression
4660      pm-expression ->* cast-expression
4661
4662      Returns a representation of the expression.  */
4663
4664 static tree
4665 cp_parser_pm_expression (cp_parser* parser)
4666 {
4667   static const cp_parser_token_tree_map map = {
4668     { CPP_DEREF_STAR, MEMBER_REF },
4669     { CPP_DOT_STAR, DOTSTAR_EXPR },
4670     { CPP_EOF, ERROR_MARK }
4671   };
4672
4673   return cp_parser_binary_expression (parser, map, 
4674                                       cp_parser_simple_cast_expression);
4675 }
4676
4677 /* Parse a multiplicative-expression.
4678
4679    mulitplicative-expression:
4680      pm-expression
4681      multiplicative-expression * pm-expression
4682      multiplicative-expression / pm-expression
4683      multiplicative-expression % pm-expression
4684
4685    Returns a representation of the expression.  */
4686
4687 static tree
4688 cp_parser_multiplicative_expression (cp_parser* parser)
4689 {
4690   static const cp_parser_token_tree_map map = {
4691     { CPP_MULT, MULT_EXPR },
4692     { CPP_DIV, TRUNC_DIV_EXPR },
4693     { CPP_MOD, TRUNC_MOD_EXPR },
4694     { CPP_EOF, ERROR_MARK }
4695   };
4696
4697   return cp_parser_binary_expression (parser,
4698                                       map,
4699                                       cp_parser_pm_expression);
4700 }
4701
4702 /* Parse an additive-expression.
4703
4704    additive-expression:
4705      multiplicative-expression
4706      additive-expression + multiplicative-expression
4707      additive-expression - multiplicative-expression
4708
4709    Returns a representation of the expression.  */
4710
4711 static tree
4712 cp_parser_additive_expression (cp_parser* parser)
4713 {
4714   static const cp_parser_token_tree_map map = {
4715     { CPP_PLUS, PLUS_EXPR },
4716     { CPP_MINUS, MINUS_EXPR },
4717     { CPP_EOF, ERROR_MARK }
4718   };
4719
4720   return cp_parser_binary_expression (parser,
4721                                       map,
4722                                       cp_parser_multiplicative_expression);
4723 }
4724
4725 /* Parse a shift-expression.
4726
4727    shift-expression:
4728      additive-expression
4729      shift-expression << additive-expression
4730      shift-expression >> additive-expression
4731
4732    Returns a representation of the expression.  */
4733
4734 static tree
4735 cp_parser_shift_expression (cp_parser* parser)
4736 {
4737   static const cp_parser_token_tree_map map = {
4738     { CPP_LSHIFT, LSHIFT_EXPR },
4739     { CPP_RSHIFT, RSHIFT_EXPR },
4740     { CPP_EOF, ERROR_MARK }
4741   };
4742
4743   return cp_parser_binary_expression (parser,
4744                                       map,
4745                                       cp_parser_additive_expression);
4746 }
4747
4748 /* Parse a relational-expression.
4749
4750    relational-expression:
4751      shift-expression
4752      relational-expression < shift-expression
4753      relational-expression > shift-expression
4754      relational-expression <= shift-expression
4755      relational-expression >= shift-expression
4756
4757    GNU Extension:
4758
4759    relational-expression:
4760      relational-expression <? shift-expression
4761      relational-expression >? shift-expression
4762
4763    Returns a representation of the expression.  */
4764
4765 static tree
4766 cp_parser_relational_expression (cp_parser* parser)
4767 {
4768   static const cp_parser_token_tree_map map = {
4769     { CPP_LESS, LT_EXPR },
4770     { CPP_GREATER, GT_EXPR },
4771     { CPP_LESS_EQ, LE_EXPR },
4772     { CPP_GREATER_EQ, GE_EXPR },
4773     { CPP_MIN, MIN_EXPR },
4774     { CPP_MAX, MAX_EXPR },
4775     { CPP_EOF, ERROR_MARK }
4776   };
4777
4778   return cp_parser_binary_expression (parser,
4779                                       map,
4780                                       cp_parser_shift_expression);
4781 }
4782
4783 /* Parse an equality-expression.
4784
4785    equality-expression:
4786      relational-expression
4787      equality-expression == relational-expression
4788      equality-expression != relational-expression
4789
4790    Returns a representation of the expression.  */
4791
4792 static tree
4793 cp_parser_equality_expression (cp_parser* parser)
4794 {
4795   static const cp_parser_token_tree_map map = {
4796     { CPP_EQ_EQ, EQ_EXPR },
4797     { CPP_NOT_EQ, NE_EXPR },
4798     { CPP_EOF, ERROR_MARK }
4799   };
4800
4801   return cp_parser_binary_expression (parser,
4802                                       map,
4803                                       cp_parser_relational_expression);
4804 }
4805
4806 /* Parse an and-expression.
4807
4808    and-expression:
4809      equality-expression
4810      and-expression & equality-expression
4811
4812    Returns a representation of the expression.  */
4813
4814 static tree
4815 cp_parser_and_expression (cp_parser* parser)
4816 {
4817   static const cp_parser_token_tree_map map = {
4818     { CPP_AND, BIT_AND_EXPR },
4819     { CPP_EOF, ERROR_MARK }
4820   };
4821
4822   return cp_parser_binary_expression (parser,
4823                                       map,
4824                                       cp_parser_equality_expression);
4825 }
4826
4827 /* Parse an exclusive-or-expression.
4828
4829    exclusive-or-expression:
4830      and-expression
4831      exclusive-or-expression ^ and-expression
4832
4833    Returns a representation of the expression.  */
4834
4835 static tree
4836 cp_parser_exclusive_or_expression (cp_parser* parser)
4837 {
4838   static const cp_parser_token_tree_map map = {
4839     { CPP_XOR, BIT_XOR_EXPR },
4840     { CPP_EOF, ERROR_MARK }
4841   };
4842
4843   return cp_parser_binary_expression (parser,
4844                                       map,
4845                                       cp_parser_and_expression);
4846 }
4847
4848
4849 /* Parse an inclusive-or-expression.
4850
4851    inclusive-or-expression:
4852      exclusive-or-expression
4853      inclusive-or-expression | exclusive-or-expression
4854
4855    Returns a representation of the expression.  */
4856
4857 static tree
4858 cp_parser_inclusive_or_expression (cp_parser* parser)
4859 {
4860   static const cp_parser_token_tree_map map = {
4861     { CPP_OR, BIT_IOR_EXPR },
4862     { CPP_EOF, ERROR_MARK }
4863   };
4864
4865   return cp_parser_binary_expression (parser,
4866                                       map,
4867                                       cp_parser_exclusive_or_expression);
4868 }
4869
4870 /* Parse a logical-and-expression.
4871
4872    logical-and-expression:
4873      inclusive-or-expression
4874      logical-and-expression && inclusive-or-expression
4875
4876    Returns a representation of the expression.  */
4877
4878 static tree
4879 cp_parser_logical_and_expression (cp_parser* parser)
4880 {
4881   static const cp_parser_token_tree_map map = {
4882     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
4883     { CPP_EOF, ERROR_MARK }
4884   };
4885
4886   return cp_parser_binary_expression (parser,
4887                                       map,
4888                                       cp_parser_inclusive_or_expression);
4889 }
4890
4891 /* Parse a logical-or-expression.
4892
4893    logical-or-expression:
4894      logical-and-expression
4895      logical-or-expression || logical-and-expression
4896
4897    Returns a representation of the expression.  */
4898
4899 static tree
4900 cp_parser_logical_or_expression (cp_parser* parser)
4901 {
4902   static const cp_parser_token_tree_map map = {
4903     { CPP_OR_OR, TRUTH_ORIF_EXPR },
4904     { CPP_EOF, ERROR_MARK }
4905   };
4906
4907   return cp_parser_binary_expression (parser,
4908                                       map,
4909                                       cp_parser_logical_and_expression);
4910 }
4911
4912 /* Parse the `? expression : assignment-expression' part of a
4913    conditional-expression.  The LOGICAL_OR_EXPR is the
4914    logical-or-expression that started the conditional-expression.
4915    Returns a representation of the entire conditional-expression.
4916
4917    This routine is used by cp_parser_assignment_expression.
4918
4919      ? expression : assignment-expression
4920    
4921    GNU Extensions:
4922    
4923      ? : assignment-expression */
4924
4925 static tree
4926 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
4927 {
4928   tree expr;
4929   tree assignment_expr;
4930
4931   /* Consume the `?' token.  */
4932   cp_lexer_consume_token (parser->lexer);
4933   if (cp_parser_allow_gnu_extensions_p (parser)
4934       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
4935     /* Implicit true clause.  */
4936     expr = NULL_TREE;
4937   else
4938     /* Parse the expression.  */
4939     expr = cp_parser_expression (parser);
4940   
4941   /* The next token should be a `:'.  */
4942   cp_parser_require (parser, CPP_COLON, "`:'");
4943   /* Parse the assignment-expression.  */
4944   assignment_expr = cp_parser_assignment_expression (parser);
4945
4946   /* Build the conditional-expression.  */
4947   return build_x_conditional_expr (logical_or_expr,
4948                                    expr,
4949                                    assignment_expr);
4950 }
4951
4952 /* Parse an assignment-expression.
4953
4954    assignment-expression:
4955      conditional-expression
4956      logical-or-expression assignment-operator assignment_expression
4957      throw-expression
4958
4959    Returns a representation for the expression.  */
4960
4961 static tree
4962 cp_parser_assignment_expression (cp_parser* parser)
4963 {
4964   tree expr;
4965
4966   /* If the next token is the `throw' keyword, then we're looking at
4967      a throw-expression.  */
4968   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
4969     expr = cp_parser_throw_expression (parser);
4970   /* Otherwise, it must be that we are looking at a
4971      logical-or-expression.  */
4972   else
4973     {
4974       /* Parse the logical-or-expression.  */
4975       expr = cp_parser_logical_or_expression (parser);
4976       /* If the next token is a `?' then we're actually looking at a
4977          conditional-expression.  */
4978       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
4979         return cp_parser_question_colon_clause (parser, expr);
4980       else 
4981         {
4982           enum tree_code assignment_operator;
4983
4984           /* If it's an assignment-operator, we're using the second
4985              production.  */
4986           assignment_operator 
4987             = cp_parser_assignment_operator_opt (parser);
4988           if (assignment_operator != ERROR_MARK)
4989             {
4990               tree rhs;
4991
4992               /* Parse the right-hand side of the assignment.  */
4993               rhs = cp_parser_assignment_expression (parser);
4994               /* An assignment may not appear in a
4995                  constant-expression.  */
4996               if (parser->constant_expression_p)
4997                 {
4998                   if (!parser->allow_non_constant_expression_p)
4999                     return cp_parser_non_constant_expression ("an assignment");
5000                   parser->non_constant_expression_p = true;
5001                 }
5002               /* Build the assignment expression.  */
5003               expr = build_x_modify_expr (expr, 
5004                                           assignment_operator, 
5005                                           rhs);
5006             }
5007         }
5008     }
5009
5010   return expr;
5011 }
5012
5013 /* Parse an (optional) assignment-operator.
5014
5015    assignment-operator: one of 
5016      = *= /= %= += -= >>= <<= &= ^= |=  
5017
5018    GNU Extension:
5019    
5020    assignment-operator: one of
5021      <?= >?=
5022
5023    If the next token is an assignment operator, the corresponding tree
5024    code is returned, and the token is consumed.  For example, for
5025    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5026    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5027    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5028    operator, ERROR_MARK is returned.  */
5029
5030 static enum tree_code
5031 cp_parser_assignment_operator_opt (cp_parser* parser)
5032 {
5033   enum tree_code op;
5034   cp_token *token;
5035
5036   /* Peek at the next toen.  */
5037   token = cp_lexer_peek_token (parser->lexer);
5038
5039   switch (token->type)
5040     {
5041     case CPP_EQ:
5042       op = NOP_EXPR;
5043       break;
5044
5045     case CPP_MULT_EQ:
5046       op = MULT_EXPR;
5047       break;
5048
5049     case CPP_DIV_EQ:
5050       op = TRUNC_DIV_EXPR;
5051       break;
5052
5053     case CPP_MOD_EQ:
5054       op = TRUNC_MOD_EXPR;
5055       break;
5056
5057     case CPP_PLUS_EQ:
5058       op = PLUS_EXPR;
5059       break;
5060
5061     case CPP_MINUS_EQ:
5062       op = MINUS_EXPR;
5063       break;
5064
5065     case CPP_RSHIFT_EQ:
5066       op = RSHIFT_EXPR;
5067       break;
5068
5069     case CPP_LSHIFT_EQ:
5070       op = LSHIFT_EXPR;
5071       break;
5072
5073     case CPP_AND_EQ:
5074       op = BIT_AND_EXPR;
5075       break;
5076
5077     case CPP_XOR_EQ:
5078       op = BIT_XOR_EXPR;
5079       break;
5080
5081     case CPP_OR_EQ:
5082       op = BIT_IOR_EXPR;
5083       break;
5084
5085     case CPP_MIN_EQ:
5086       op = MIN_EXPR;
5087       break;
5088
5089     case CPP_MAX_EQ:
5090       op = MAX_EXPR;
5091       break;
5092
5093     default: 
5094       /* Nothing else is an assignment operator.  */
5095       op = ERROR_MARK;
5096     }
5097
5098   /* If it was an assignment operator, consume it.  */
5099   if (op != ERROR_MARK)
5100     cp_lexer_consume_token (parser->lexer);
5101
5102   return op;
5103 }
5104
5105 /* Parse an expression.
5106
5107    expression:
5108      assignment-expression
5109      expression , assignment-expression
5110
5111    Returns a representation of the expression.  */
5112
5113 static tree
5114 cp_parser_expression (cp_parser* parser)
5115 {
5116   tree expression = NULL_TREE;
5117
5118   while (true)
5119     {
5120       tree assignment_expression;
5121
5122       /* Parse the next assignment-expression.  */
5123       assignment_expression 
5124         = cp_parser_assignment_expression (parser);
5125       /* If this is the first assignment-expression, we can just
5126          save it away.  */
5127       if (!expression)
5128         expression = assignment_expression;
5129       else
5130         expression = build_x_compound_expr (expression,
5131                                             assignment_expression);
5132       /* If the next token is not a comma, then we are done with the
5133          expression.  */
5134       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5135         break;
5136       /* Consume the `,'.  */
5137       cp_lexer_consume_token (parser->lexer);
5138       /* A comma operator cannot appear in a constant-expression.  */
5139       if (parser->constant_expression_p)
5140         {
5141           if (!parser->allow_non_constant_expression_p)
5142             expression 
5143               = cp_parser_non_constant_expression ("a comma operator");
5144           parser->non_constant_expression_p = true;
5145         }
5146     }
5147
5148   return expression;
5149 }
5150
5151 /* Parse a constant-expression. 
5152
5153    constant-expression:
5154      conditional-expression  
5155
5156   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5157   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5158   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5159   is false, NON_CONSTANT_P should be NULL.  */
5160
5161 static tree
5162 cp_parser_constant_expression (cp_parser* parser, 
5163                                bool allow_non_constant_p,
5164                                bool *non_constant_p)
5165 {
5166   bool saved_constant_expression_p;
5167   bool saved_allow_non_constant_expression_p;
5168   bool saved_non_constant_expression_p;
5169   tree expression;
5170
5171   /* It might seem that we could simply parse the
5172      conditional-expression, and then check to see if it were
5173      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5174      one that the compiler can figure out is constant, possibly after
5175      doing some simplifications or optimizations.  The standard has a
5176      precise definition of constant-expression, and we must honor
5177      that, even though it is somewhat more restrictive.
5178
5179      For example:
5180
5181        int i[(2, 3)];
5182
5183      is not a legal declaration, because `(2, 3)' is not a
5184      constant-expression.  The `,' operator is forbidden in a
5185      constant-expression.  However, GCC's constant-folding machinery
5186      will fold this operation to an INTEGER_CST for `3'.  */
5187
5188   /* Save the old settings.  */
5189   saved_constant_expression_p = parser->constant_expression_p;
5190   saved_allow_non_constant_expression_p 
5191     = parser->allow_non_constant_expression_p;
5192   saved_non_constant_expression_p = parser->non_constant_expression_p;
5193   /* We are now parsing a constant-expression.  */
5194   parser->constant_expression_p = true;
5195   parser->allow_non_constant_expression_p = allow_non_constant_p;
5196   parser->non_constant_expression_p = false;
5197   /* Although the grammar says "conditional-expression", we parse an
5198      "assignment-expression", which also permits "throw-expression"
5199      and the use of assignment operators.  In the case that
5200      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5201      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5202      actually essential that we look for an assignment-expression.
5203      For example, cp_parser_initializer_clauses uses this function to
5204      determine whether a particular assignment-expression is in fact
5205      constant.  */
5206   expression = cp_parser_assignment_expression (parser);
5207   /* Restore the old settings.  */
5208   parser->constant_expression_p = saved_constant_expression_p;
5209   parser->allow_non_constant_expression_p 
5210     = saved_allow_non_constant_expression_p;
5211   if (allow_non_constant_p)
5212     *non_constant_p = parser->non_constant_expression_p;
5213   parser->non_constant_expression_p = saved_non_constant_expression_p;
5214
5215   return expression;
5216 }
5217
5218 /* Statements [gram.stmt.stmt]  */
5219
5220 /* Parse a statement.  
5221
5222    statement:
5223      labeled-statement
5224      expression-statement
5225      compound-statement
5226      selection-statement
5227      iteration-statement
5228      jump-statement
5229      declaration-statement
5230      try-block  */
5231
5232 static void
5233 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5234 {
5235   tree statement;
5236   cp_token *token;
5237   int statement_line_number;
5238
5239   /* There is no statement yet.  */
5240   statement = NULL_TREE;
5241   /* Peek at the next token.  */
5242   token = cp_lexer_peek_token (parser->lexer);
5243   /* Remember the line number of the first token in the statement.  */
5244   statement_line_number = token->location.line;
5245   /* If this is a keyword, then that will often determine what kind of
5246      statement we have.  */
5247   if (token->type == CPP_KEYWORD)
5248     {
5249       enum rid keyword = token->keyword;
5250
5251       switch (keyword)
5252         {
5253         case RID_CASE:
5254         case RID_DEFAULT:
5255           statement = cp_parser_labeled_statement (parser,
5256                                                    in_statement_expr_p);
5257           break;
5258
5259         case RID_IF:
5260         case RID_SWITCH:
5261           statement = cp_parser_selection_statement (parser);
5262           break;
5263
5264         case RID_WHILE:
5265         case RID_DO:
5266         case RID_FOR:
5267           statement = cp_parser_iteration_statement (parser);
5268           break;
5269
5270         case RID_BREAK:
5271         case RID_CONTINUE:
5272         case RID_RETURN:
5273         case RID_GOTO:
5274           statement = cp_parser_jump_statement (parser);
5275           break;
5276
5277         case RID_TRY:
5278           statement = cp_parser_try_block (parser);
5279           break;
5280
5281         default:
5282           /* It might be a keyword like `int' that can start a
5283              declaration-statement.  */
5284           break;
5285         }
5286     }
5287   else if (token->type == CPP_NAME)
5288     {
5289       /* If the next token is a `:', then we are looking at a
5290          labeled-statement.  */
5291       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5292       if (token->type == CPP_COLON)
5293         statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5294     }
5295   /* Anything that starts with a `{' must be a compound-statement.  */
5296   else if (token->type == CPP_OPEN_BRACE)
5297     statement = cp_parser_compound_statement (parser, false);
5298
5299   /* Everything else must be a declaration-statement or an
5300      expression-statement.  Try for the declaration-statement 
5301      first, unless we are looking at a `;', in which case we know that
5302      we have an expression-statement.  */
5303   if (!statement)
5304     {
5305       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5306         {
5307           cp_parser_parse_tentatively (parser);
5308           /* Try to parse the declaration-statement.  */
5309           cp_parser_declaration_statement (parser);
5310           /* If that worked, we're done.  */
5311           if (cp_parser_parse_definitely (parser))
5312             return;
5313         }
5314       /* Look for an expression-statement instead.  */
5315       statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5316     }
5317
5318   /* Set the line number for the statement.  */
5319   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5320     STMT_LINENO (statement) = statement_line_number;
5321 }
5322
5323 /* Parse a labeled-statement.
5324
5325    labeled-statement:
5326      identifier : statement
5327      case constant-expression : statement
5328      default : statement  
5329
5330    Returns the new CASE_LABEL, for a `case' or `default' label.  For
5331    an ordinary label, returns a LABEL_STMT.  */
5332
5333 static tree
5334 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5335 {
5336   cp_token *token;
5337   tree statement = error_mark_node;
5338
5339   /* The next token should be an identifier.  */
5340   token = cp_lexer_peek_token (parser->lexer);
5341   if (token->type != CPP_NAME
5342       && token->type != CPP_KEYWORD)
5343     {
5344       cp_parser_error (parser, "expected labeled-statement");
5345       return error_mark_node;
5346     }
5347
5348   switch (token->keyword)
5349     {
5350     case RID_CASE:
5351       {
5352         tree expr;
5353
5354         /* Consume the `case' token.  */
5355         cp_lexer_consume_token (parser->lexer);
5356         /* Parse the constant-expression.  */
5357         expr = cp_parser_constant_expression (parser, 
5358                                               /*allow_non_constant_p=*/false,
5359                                               NULL);
5360         if (!parser->in_switch_statement_p)
5361           error ("case label `%E' not within a switch statement", expr);
5362         else
5363           statement = finish_case_label (expr, NULL_TREE);
5364       }
5365       break;
5366
5367     case RID_DEFAULT:
5368       /* Consume the `default' token.  */
5369       cp_lexer_consume_token (parser->lexer);
5370       if (!parser->in_switch_statement_p)
5371         error ("case label not within a switch statement");
5372       else
5373         statement = finish_case_label (NULL_TREE, NULL_TREE);
5374       break;
5375
5376     default:
5377       /* Anything else must be an ordinary label.  */
5378       statement = finish_label_stmt (cp_parser_identifier (parser));
5379       break;
5380     }
5381
5382   /* Require the `:' token.  */
5383   cp_parser_require (parser, CPP_COLON, "`:'");
5384   /* Parse the labeled statement.  */
5385   cp_parser_statement (parser, in_statement_expr_p);
5386
5387   /* Return the label, in the case of a `case' or `default' label.  */
5388   return statement;
5389 }
5390
5391 /* Parse an expression-statement.
5392
5393    expression-statement:
5394      expression [opt] ;
5395
5396    Returns the new EXPR_STMT -- or NULL_TREE if the expression
5397    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5398    indicates whether this expression-statement is part of an
5399    expression statement.  */
5400
5401 static tree
5402 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5403 {
5404   tree statement = NULL_TREE;
5405
5406   /* If the next token is a ';', then there is no expression
5407      statement.  */
5408   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5409     statement = cp_parser_expression (parser);
5410   
5411   /* Consume the final `;'.  */
5412   cp_parser_consume_semicolon_at_end_of_statement (parser);
5413
5414   if (in_statement_expr_p
5415       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5416     {
5417       /* This is the final expression statement of a statement
5418          expression.  */
5419       statement = finish_stmt_expr_expr (statement);
5420     }
5421   else if (statement)
5422     statement = finish_expr_stmt (statement);
5423   else
5424     finish_stmt ();
5425   
5426   return statement;
5427 }
5428
5429 /* Parse a compound-statement.
5430
5431    compound-statement:
5432      { statement-seq [opt] }
5433      
5434    Returns a COMPOUND_STMT representing the statement.  */
5435
5436 static tree
5437 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5438 {
5439   tree compound_stmt;
5440
5441   /* Consume the `{'.  */
5442   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5443     return error_mark_node;
5444   /* Begin the compound-statement.  */
5445   compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5446   /* Parse an (optional) statement-seq.  */
5447   cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5448   /* Finish the compound-statement.  */
5449   finish_compound_stmt (compound_stmt);
5450   /* Consume the `}'.  */
5451   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5452
5453   return compound_stmt;
5454 }
5455
5456 /* Parse an (optional) statement-seq.
5457
5458    statement-seq:
5459      statement
5460      statement-seq [opt] statement  */
5461
5462 static void
5463 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5464 {
5465   /* Scan statements until there aren't any more.  */
5466   while (true)
5467     {
5468       /* If we're looking at a `}', then we've run out of statements.  */
5469       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5470           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5471         break;
5472
5473       /* Parse the statement.  */
5474       cp_parser_statement (parser, in_statement_expr_p);
5475     }
5476 }
5477
5478 /* Parse a selection-statement.
5479
5480    selection-statement:
5481      if ( condition ) statement
5482      if ( condition ) statement else statement
5483      switch ( condition ) statement  
5484
5485    Returns the new IF_STMT or SWITCH_STMT.  */
5486
5487 static tree
5488 cp_parser_selection_statement (cp_parser* parser)
5489 {
5490   cp_token *token;
5491   enum rid keyword;
5492
5493   /* Peek at the next token.  */
5494   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5495
5496   /* See what kind of keyword it is.  */
5497   keyword = token->keyword;
5498   switch (keyword)
5499     {
5500     case RID_IF:
5501     case RID_SWITCH:
5502       {
5503         tree statement;
5504         tree condition;
5505
5506         /* Look for the `('.  */
5507         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5508           {
5509             cp_parser_skip_to_end_of_statement (parser);
5510             return error_mark_node;
5511           }
5512
5513         /* Begin the selection-statement.  */
5514         if (keyword == RID_IF)
5515           statement = begin_if_stmt ();
5516         else
5517           statement = begin_switch_stmt ();
5518
5519         /* Parse the condition.  */
5520         condition = cp_parser_condition (parser);
5521         /* Look for the `)'.  */
5522         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5523           cp_parser_skip_to_closing_parenthesis (parser, true, false,
5524                                                  /*consume_paren=*/true);
5525
5526         if (keyword == RID_IF)
5527           {
5528             tree then_stmt;
5529
5530             /* Add the condition.  */
5531             finish_if_stmt_cond (condition, statement);
5532
5533             /* Parse the then-clause.  */
5534             then_stmt = cp_parser_implicitly_scoped_statement (parser);
5535             finish_then_clause (statement);
5536
5537             /* If the next token is `else', parse the else-clause.  */
5538             if (cp_lexer_next_token_is_keyword (parser->lexer,
5539                                                 RID_ELSE))
5540               {
5541                 tree else_stmt;
5542
5543                 /* Consume the `else' keyword.  */
5544                 cp_lexer_consume_token (parser->lexer);
5545                 /* Parse the else-clause.  */
5546                 else_stmt 
5547                   = cp_parser_implicitly_scoped_statement (parser);
5548                 finish_else_clause (statement);
5549               }
5550
5551             /* Now we're all done with the if-statement.  */
5552             finish_if_stmt ();
5553           }
5554         else
5555           {
5556             tree body;
5557             bool in_switch_statement_p;
5558
5559             /* Add the condition.  */
5560             finish_switch_cond (condition, statement);
5561
5562             /* Parse the body of the switch-statement.  */
5563             in_switch_statement_p = parser->in_switch_statement_p;
5564             parser->in_switch_statement_p = true;
5565             body = cp_parser_implicitly_scoped_statement (parser);
5566             parser->in_switch_statement_p = in_switch_statement_p;
5567
5568             /* Now we're all done with the switch-statement.  */
5569             finish_switch_stmt (statement);
5570           }
5571
5572         return statement;
5573       }
5574       break;
5575
5576     default:
5577       cp_parser_error (parser, "expected selection-statement");
5578       return error_mark_node;
5579     }
5580 }
5581
5582 /* Parse a condition. 
5583
5584    condition:
5585      expression
5586      type-specifier-seq declarator = assignment-expression  
5587
5588    GNU Extension:
5589    
5590    condition:
5591      type-specifier-seq declarator asm-specification [opt] 
5592        attributes [opt] = assignment-expression
5593  
5594    Returns the expression that should be tested.  */
5595
5596 static tree
5597 cp_parser_condition (cp_parser* parser)
5598 {
5599   tree type_specifiers;
5600   const char *saved_message;
5601
5602   /* Try the declaration first.  */
5603   cp_parser_parse_tentatively (parser);
5604   /* New types are not allowed in the type-specifier-seq for a
5605      condition.  */
5606   saved_message = parser->type_definition_forbidden_message;
5607   parser->type_definition_forbidden_message
5608     = "types may not be defined in conditions";
5609   /* Parse the type-specifier-seq.  */
5610   type_specifiers = cp_parser_type_specifier_seq (parser);
5611   /* Restore the saved message.  */
5612   parser->type_definition_forbidden_message = saved_message;
5613   /* If all is well, we might be looking at a declaration.  */
5614   if (!cp_parser_error_occurred (parser))
5615     {
5616       tree decl;
5617       tree asm_specification;
5618       tree attributes;
5619       tree declarator;
5620       tree initializer = NULL_TREE;
5621       
5622       /* Parse the declarator.  */
5623       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5624                                          /*ctor_dtor_or_conv_p=*/NULL);
5625       /* Parse the attributes.  */
5626       attributes = cp_parser_attributes_opt (parser);
5627       /* Parse the asm-specification.  */
5628       asm_specification = cp_parser_asm_specification_opt (parser);
5629       /* If the next token is not an `=', then we might still be
5630          looking at an expression.  For example:
5631          
5632            if (A(a).x)
5633           
5634          looks like a decl-specifier-seq and a declarator -- but then
5635          there is no `=', so this is an expression.  */
5636       cp_parser_require (parser, CPP_EQ, "`='");
5637       /* If we did see an `=', then we are looking at a declaration
5638          for sure.  */
5639       if (cp_parser_parse_definitely (parser))
5640         {
5641           /* Create the declaration.  */
5642           decl = start_decl (declarator, type_specifiers, 
5643                              /*initialized_p=*/true,
5644                              attributes, /*prefix_attributes=*/NULL_TREE);
5645           /* Parse the assignment-expression.  */
5646           initializer = cp_parser_assignment_expression (parser);
5647           
5648           /* Process the initializer.  */
5649           cp_finish_decl (decl, 
5650                           initializer, 
5651                           asm_specification, 
5652                           LOOKUP_ONLYCONVERTING);
5653           
5654           return convert_from_reference (decl);
5655         }
5656     }
5657   /* If we didn't even get past the declarator successfully, we are
5658      definitely not looking at a declaration.  */
5659   else
5660     cp_parser_abort_tentative_parse (parser);
5661
5662   /* Otherwise, we are looking at an expression.  */
5663   return cp_parser_expression (parser);
5664 }
5665
5666 /* Parse an iteration-statement.
5667
5668    iteration-statement:
5669      while ( condition ) statement
5670      do statement while ( expression ) ;
5671      for ( for-init-statement condition [opt] ; expression [opt] )
5672        statement
5673
5674    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
5675
5676 static tree
5677 cp_parser_iteration_statement (cp_parser* parser)
5678 {
5679   cp_token *token;
5680   enum rid keyword;
5681   tree statement;
5682   bool in_iteration_statement_p;
5683
5684
5685   /* Peek at the next token.  */
5686   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5687   if (!token)
5688     return error_mark_node;
5689
5690   /* Remember whether or not we are already within an iteration
5691      statement.  */ 
5692   in_iteration_statement_p = parser->in_iteration_statement_p;
5693
5694   /* See what kind of keyword it is.  */
5695   keyword = token->keyword;
5696   switch (keyword)
5697     {
5698     case RID_WHILE:
5699       {
5700         tree condition;
5701
5702         /* Begin the while-statement.  */
5703         statement = begin_while_stmt ();
5704         /* Look for the `('.  */
5705         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5706         /* Parse the condition.  */
5707         condition = cp_parser_condition (parser);
5708         finish_while_stmt_cond (condition, statement);
5709         /* Look for the `)'.  */
5710         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5711         /* Parse the dependent statement.  */
5712         parser->in_iteration_statement_p = true;
5713         cp_parser_already_scoped_statement (parser);
5714         parser->in_iteration_statement_p = in_iteration_statement_p;
5715         /* We're done with the while-statement.  */
5716         finish_while_stmt (statement);
5717       }
5718       break;
5719
5720     case RID_DO:
5721       {
5722         tree expression;
5723
5724         /* Begin the do-statement.  */
5725         statement = begin_do_stmt ();
5726         /* Parse the body of the do-statement.  */
5727         parser->in_iteration_statement_p = true;
5728         cp_parser_implicitly_scoped_statement (parser);
5729         parser->in_iteration_statement_p = in_iteration_statement_p;
5730         finish_do_body (statement);
5731         /* Look for the `while' keyword.  */
5732         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5733         /* Look for the `('.  */
5734         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5735         /* Parse the expression.  */
5736         expression = cp_parser_expression (parser);
5737         /* We're done with the do-statement.  */
5738         finish_do_stmt (expression, statement);
5739         /* Look for the `)'.  */
5740         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5741         /* Look for the `;'.  */
5742         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5743       }
5744       break;
5745
5746     case RID_FOR:
5747       {
5748         tree condition = NULL_TREE;
5749         tree expression = NULL_TREE;
5750
5751         /* Begin the for-statement.  */
5752         statement = begin_for_stmt ();
5753         /* Look for the `('.  */
5754         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5755         /* Parse the initialization.  */
5756         cp_parser_for_init_statement (parser);
5757         finish_for_init_stmt (statement);
5758
5759         /* If there's a condition, process it.  */
5760         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5761           condition = cp_parser_condition (parser);
5762         finish_for_cond (condition, statement);
5763         /* Look for the `;'.  */
5764         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5765
5766         /* If there's an expression, process it.  */
5767         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5768           expression = cp_parser_expression (parser);
5769         finish_for_expr (expression, statement);
5770         /* Look for the `)'.  */
5771         cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5772
5773         /* Parse the body of the for-statement.  */
5774         parser->in_iteration_statement_p = true;
5775         cp_parser_already_scoped_statement (parser);
5776         parser->in_iteration_statement_p = in_iteration_statement_p;
5777
5778         /* We're done with the for-statement.  */
5779         finish_for_stmt (statement);
5780       }
5781       break;
5782
5783     default:
5784       cp_parser_error (parser, "expected iteration-statement");
5785       statement = error_mark_node;
5786       break;
5787     }
5788
5789   return statement;
5790 }
5791
5792 /* Parse a for-init-statement.
5793
5794    for-init-statement:
5795      expression-statement
5796      simple-declaration  */
5797
5798 static void
5799 cp_parser_for_init_statement (cp_parser* parser)
5800 {
5801   /* If the next token is a `;', then we have an empty
5802      expression-statement.  Grammatically, this is also a
5803      simple-declaration, but an invalid one, because it does not
5804      declare anything.  Therefore, if we did not handle this case
5805      specially, we would issue an error message about an invalid
5806      declaration.  */
5807   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5808     {
5809       /* We're going to speculatively look for a declaration, falling back
5810          to an expression, if necessary.  */
5811       cp_parser_parse_tentatively (parser);
5812       /* Parse the declaration.  */
5813       cp_parser_simple_declaration (parser,
5814                                     /*function_definition_allowed_p=*/false);
5815       /* If the tentative parse failed, then we shall need to look for an
5816          expression-statement.  */
5817       if (cp_parser_parse_definitely (parser))
5818         return;
5819     }
5820
5821   cp_parser_expression_statement (parser, false);
5822 }
5823
5824 /* Parse a jump-statement.
5825
5826    jump-statement:
5827      break ;
5828      continue ;
5829      return expression [opt] ;
5830      goto identifier ;  
5831
5832    GNU extension:
5833
5834    jump-statement:
5835      goto * expression ;
5836
5837    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5838    GOTO_STMT.  */
5839
5840 static tree
5841 cp_parser_jump_statement (cp_parser* parser)
5842 {
5843   tree statement = error_mark_node;
5844   cp_token *token;
5845   enum rid keyword;
5846
5847   /* Peek at the next token.  */
5848   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
5849   if (!token)
5850     return error_mark_node;
5851
5852   /* See what kind of keyword it is.  */
5853   keyword = token->keyword;
5854   switch (keyword)
5855     {
5856     case RID_BREAK:
5857       if (!parser->in_switch_statement_p
5858           && !parser->in_iteration_statement_p)
5859         {
5860           error ("break statement not within loop or switch");
5861           statement = error_mark_node;
5862         }
5863       else
5864         statement = finish_break_stmt ();
5865       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5866       break;
5867
5868     case RID_CONTINUE:
5869       if (!parser->in_iteration_statement_p)
5870         {
5871           error ("continue statement not within a loop");
5872           statement = error_mark_node;
5873         }
5874       else
5875         statement = finish_continue_stmt ();
5876       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5877       break;
5878
5879     case RID_RETURN:
5880       {
5881         tree expr;
5882
5883         /* If the next token is a `;', then there is no 
5884            expression.  */
5885         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5886           expr = cp_parser_expression (parser);
5887         else
5888           expr = NULL_TREE;
5889         /* Build the return-statement.  */
5890         statement = finish_return_stmt (expr);
5891         /* Look for the final `;'.  */
5892         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5893       }
5894       break;
5895
5896     case RID_GOTO:
5897       /* Create the goto-statement.  */
5898       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
5899         {
5900           /* Issue a warning about this use of a GNU extension.  */
5901           if (pedantic)
5902             pedwarn ("ISO C++ forbids computed gotos");
5903           /* Consume the '*' token.  */
5904           cp_lexer_consume_token (parser->lexer);
5905           /* Parse the dependent expression.  */
5906           finish_goto_stmt (cp_parser_expression (parser));
5907         }
5908       else
5909         finish_goto_stmt (cp_parser_identifier (parser));
5910       /* Look for the final `;'.  */
5911       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5912       break;
5913
5914     default:
5915       cp_parser_error (parser, "expected jump-statement");
5916       break;
5917     }
5918
5919   return statement;
5920 }
5921
5922 /* Parse a declaration-statement.
5923
5924    declaration-statement:
5925      block-declaration  */
5926
5927 static void
5928 cp_parser_declaration_statement (cp_parser* parser)
5929 {
5930   /* Parse the block-declaration.  */
5931   cp_parser_block_declaration (parser, /*statement_p=*/true);
5932
5933   /* Finish off the statement.  */
5934   finish_stmt ();
5935 }
5936
5937 /* Some dependent statements (like `if (cond) statement'), are
5938    implicitly in their own scope.  In other words, if the statement is
5939    a single statement (as opposed to a compound-statement), it is
5940    none-the-less treated as if it were enclosed in braces.  Any
5941    declarations appearing in the dependent statement are out of scope
5942    after control passes that point.  This function parses a statement,
5943    but ensures that is in its own scope, even if it is not a
5944    compound-statement.  
5945
5946    Returns the new statement.  */
5947
5948 static tree
5949 cp_parser_implicitly_scoped_statement (cp_parser* parser)
5950 {
5951   tree statement;
5952
5953   /* If the token is not a `{', then we must take special action.  */
5954   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
5955     {
5956       /* Create a compound-statement.  */
5957       statement = begin_compound_stmt (/*has_no_scope=*/false);
5958       /* Parse the dependent-statement.  */
5959       cp_parser_statement (parser, false);
5960       /* Finish the dummy compound-statement.  */
5961       finish_compound_stmt (statement);
5962     }
5963   /* Otherwise, we simply parse the statement directly.  */
5964   else
5965     statement = cp_parser_compound_statement (parser, false);
5966
5967   /* Return the statement.  */
5968   return statement;
5969 }
5970
5971 /* For some dependent statements (like `while (cond) statement'), we
5972    have already created a scope.  Therefore, even if the dependent
5973    statement is a compound-statement, we do not want to create another
5974    scope.  */
5975
5976 static void
5977 cp_parser_already_scoped_statement (cp_parser* parser)
5978 {
5979   /* If the token is not a `{', then we must take special action.  */
5980   if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
5981     {
5982       tree statement;
5983
5984       /* Create a compound-statement.  */
5985       statement = begin_compound_stmt (/*has_no_scope=*/true);
5986       /* Parse the dependent-statement.  */
5987       cp_parser_statement (parser, false);
5988       /* Finish the dummy compound-statement.  */
5989       finish_compound_stmt (statement);
5990     }
5991   /* Otherwise, we simply parse the statement directly.  */
5992   else
5993     cp_parser_statement (parser, false);
5994 }
5995
5996 /* Declarations [gram.dcl.dcl] */
5997
5998 /* Parse an optional declaration-sequence.
5999
6000    declaration-seq:
6001      declaration
6002      declaration-seq declaration  */
6003
6004 static void
6005 cp_parser_declaration_seq_opt (cp_parser* parser)
6006 {
6007   while (true)
6008     {
6009       cp_token *token;
6010
6011       token = cp_lexer_peek_token (parser->lexer);
6012
6013       if (token->type == CPP_CLOSE_BRACE
6014           || token->type == CPP_EOF)
6015         break;
6016
6017       if (token->type == CPP_SEMICOLON) 
6018         {
6019           /* A declaration consisting of a single semicolon is
6020              invalid.  Allow it unless we're being pedantic.  */
6021           if (pedantic)
6022             pedwarn ("extra `;'");
6023           cp_lexer_consume_token (parser->lexer);
6024           continue;
6025         }
6026
6027       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6028          parser to enter or exit implicit `extern "C"' blocks.  */
6029       while (pending_lang_change > 0)
6030         {
6031           push_lang_context (lang_name_c);
6032           --pending_lang_change;
6033         }
6034       while (pending_lang_change < 0)
6035         {
6036           pop_lang_context ();
6037           ++pending_lang_change;
6038         }
6039
6040       /* Parse the declaration itself.  */
6041       cp_parser_declaration (parser);
6042     }
6043 }
6044
6045 /* Parse a declaration.
6046
6047    declaration:
6048      block-declaration
6049      function-definition
6050      template-declaration
6051      explicit-instantiation
6052      explicit-specialization
6053      linkage-specification
6054      namespace-definition    
6055
6056    GNU extension:
6057
6058    declaration:
6059       __extension__ declaration */
6060
6061 static void
6062 cp_parser_declaration (cp_parser* parser)
6063 {
6064   cp_token token1;
6065   cp_token token2;
6066   int saved_pedantic;
6067
6068   /* Check for the `__extension__' keyword.  */
6069   if (cp_parser_extension_opt (parser, &saved_pedantic))
6070     {
6071       /* Parse the qualified declaration.  */
6072       cp_parser_declaration (parser);
6073       /* Restore the PEDANTIC flag.  */
6074       pedantic = saved_pedantic;
6075
6076       return;
6077     }
6078
6079   /* Try to figure out what kind of declaration is present.  */
6080   token1 = *cp_lexer_peek_token (parser->lexer);
6081   if (token1.type != CPP_EOF)
6082     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6083
6084   /* If the next token is `extern' and the following token is a string
6085      literal, then we have a linkage specification.  */
6086   if (token1.keyword == RID_EXTERN
6087       && cp_parser_is_string_literal (&token2))
6088     cp_parser_linkage_specification (parser);
6089   /* If the next token is `template', then we have either a template
6090      declaration, an explicit instantiation, or an explicit
6091      specialization.  */
6092   else if (token1.keyword == RID_TEMPLATE)
6093     {
6094       /* `template <>' indicates a template specialization.  */
6095       if (token2.type == CPP_LESS
6096           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6097         cp_parser_explicit_specialization (parser);
6098       /* `template <' indicates a template declaration.  */
6099       else if (token2.type == CPP_LESS)
6100         cp_parser_template_declaration (parser, /*member_p=*/false);
6101       /* Anything else must be an explicit instantiation.  */
6102       else
6103         cp_parser_explicit_instantiation (parser);
6104     }
6105   /* If the next token is `export', then we have a template
6106      declaration.  */
6107   else if (token1.keyword == RID_EXPORT)
6108     cp_parser_template_declaration (parser, /*member_p=*/false);
6109   /* If the next token is `extern', 'static' or 'inline' and the one
6110      after that is `template', we have a GNU extended explicit
6111      instantiation directive.  */
6112   else if (cp_parser_allow_gnu_extensions_p (parser)
6113            && (token1.keyword == RID_EXTERN
6114                || token1.keyword == RID_STATIC
6115                || token1.keyword == RID_INLINE)
6116            && token2.keyword == RID_TEMPLATE)
6117     cp_parser_explicit_instantiation (parser);
6118   /* If the next token is `namespace', check for a named or unnamed
6119      namespace definition.  */
6120   else if (token1.keyword == RID_NAMESPACE
6121            && (/* A named namespace definition.  */
6122                (token2.type == CPP_NAME
6123                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
6124                     == CPP_OPEN_BRACE))
6125                /* An unnamed namespace definition.  */
6126                || token2.type == CPP_OPEN_BRACE))
6127     cp_parser_namespace_definition (parser);
6128   /* We must have either a block declaration or a function
6129      definition.  */
6130   else
6131     /* Try to parse a block-declaration, or a function-definition.  */
6132     cp_parser_block_declaration (parser, /*statement_p=*/false);
6133 }
6134
6135 /* Parse a block-declaration.  
6136
6137    block-declaration:
6138      simple-declaration
6139      asm-definition
6140      namespace-alias-definition
6141      using-declaration
6142      using-directive  
6143
6144    GNU Extension:
6145
6146    block-declaration:
6147      __extension__ block-declaration 
6148      label-declaration
6149
6150    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6151    part of a declaration-statement.  */
6152
6153 static void
6154 cp_parser_block_declaration (cp_parser *parser, 
6155                              bool      statement_p)
6156 {
6157   cp_token *token1;
6158   int saved_pedantic;
6159
6160   /* Check for the `__extension__' keyword.  */
6161   if (cp_parser_extension_opt (parser, &saved_pedantic))
6162     {
6163       /* Parse the qualified declaration.  */
6164       cp_parser_block_declaration (parser, statement_p);
6165       /* Restore the PEDANTIC flag.  */
6166       pedantic = saved_pedantic;
6167
6168       return;
6169     }
6170
6171   /* Peek at the next token to figure out which kind of declaration is
6172      present.  */
6173   token1 = cp_lexer_peek_token (parser->lexer);
6174
6175   /* If the next keyword is `asm', we have an asm-definition.  */
6176   if (token1->keyword == RID_ASM)
6177     {
6178       if (statement_p)
6179         cp_parser_commit_to_tentative_parse (parser);
6180       cp_parser_asm_definition (parser);
6181     }
6182   /* If the next keyword is `namespace', we have a
6183      namespace-alias-definition.  */
6184   else if (token1->keyword == RID_NAMESPACE)
6185     cp_parser_namespace_alias_definition (parser);
6186   /* If the next keyword is `using', we have either a
6187      using-declaration or a using-directive.  */
6188   else if (token1->keyword == RID_USING)
6189     {
6190       cp_token *token2;
6191
6192       if (statement_p)
6193         cp_parser_commit_to_tentative_parse (parser);
6194       /* If the token after `using' is `namespace', then we have a
6195          using-directive.  */
6196       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6197       if (token2->keyword == RID_NAMESPACE)
6198         cp_parser_using_directive (parser);
6199       /* Otherwise, it's a using-declaration.  */
6200       else
6201         cp_parser_using_declaration (parser);
6202     }
6203   /* If the next keyword is `__label__' we have a label declaration.  */
6204   else if (token1->keyword == RID_LABEL)
6205     {
6206       if (statement_p)
6207         cp_parser_commit_to_tentative_parse (parser);
6208       cp_parser_label_declaration (parser);
6209     }
6210   /* Anything else must be a simple-declaration.  */
6211   else
6212     cp_parser_simple_declaration (parser, !statement_p);
6213 }
6214
6215 /* Parse a simple-declaration.
6216
6217    simple-declaration:
6218      decl-specifier-seq [opt] init-declarator-list [opt] ;  
6219
6220    init-declarator-list:
6221      init-declarator
6222      init-declarator-list , init-declarator 
6223
6224    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6225    function-definition as a simple-declaration.  */
6226
6227 static void
6228 cp_parser_simple_declaration (cp_parser* parser, 
6229                               bool function_definition_allowed_p)
6230 {
6231   tree decl_specifiers;
6232   tree attributes;
6233   int declares_class_or_enum;
6234   bool saw_declarator;
6235
6236   /* Defer access checks until we know what is being declared; the
6237      checks for names appearing in the decl-specifier-seq should be
6238      done as if we were in the scope of the thing being declared.  */
6239   push_deferring_access_checks (dk_deferred);
6240
6241   /* Parse the decl-specifier-seq.  We have to keep track of whether
6242      or not the decl-specifier-seq declares a named class or
6243      enumeration type, since that is the only case in which the
6244      init-declarator-list is allowed to be empty.  
6245
6246      [dcl.dcl]
6247
6248      In a simple-declaration, the optional init-declarator-list can be
6249      omitted only when declaring a class or enumeration, that is when
6250      the decl-specifier-seq contains either a class-specifier, an
6251      elaborated-type-specifier, or an enum-specifier.  */
6252   decl_specifiers
6253     = cp_parser_decl_specifier_seq (parser, 
6254                                     CP_PARSER_FLAGS_OPTIONAL,
6255                                     &attributes,
6256                                     &declares_class_or_enum);
6257   /* We no longer need to defer access checks.  */
6258   stop_deferring_access_checks ();
6259
6260   /* In a block scope, a valid declaration must always have a
6261      decl-specifier-seq.  By not trying to parse declarators, we can
6262      resolve the declaration/expression ambiguity more quickly.  */
6263   if (!function_definition_allowed_p && !decl_specifiers)
6264     {
6265       cp_parser_error (parser, "expected declaration");
6266       goto done;
6267     }
6268
6269   /* If the next two tokens are both identifiers, the code is
6270      erroneous. The usual cause of this situation is code like:
6271
6272        T t;
6273
6274      where "T" should name a type -- but does not.  */
6275   if (cp_parser_diagnose_invalid_type_name (parser))
6276     {
6277       /* If parsing tentatively, we should commit; we really are
6278          looking at a declaration.  */
6279       cp_parser_commit_to_tentative_parse (parser);
6280       /* Give up.  */
6281       goto done;
6282     }
6283
6284   /* Keep going until we hit the `;' at the end of the simple
6285      declaration.  */
6286   saw_declarator = false;
6287   while (cp_lexer_next_token_is_not (parser->lexer, 
6288                                      CPP_SEMICOLON))
6289     {
6290       cp_token *token;
6291       bool function_definition_p;
6292       tree decl;
6293
6294       saw_declarator = true;
6295       /* Parse the init-declarator.  */
6296       decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6297                                         function_definition_allowed_p,
6298                                         /*member_p=*/false,
6299                                         declares_class_or_enum,
6300                                         &function_definition_p);
6301       /* If an error occurred while parsing tentatively, exit quickly.
6302          (That usually happens when in the body of a function; each
6303          statement is treated as a declaration-statement until proven
6304          otherwise.)  */
6305       if (cp_parser_error_occurred (parser))
6306         goto done;
6307       /* Handle function definitions specially.  */
6308       if (function_definition_p)
6309         {
6310           /* If the next token is a `,', then we are probably
6311              processing something like:
6312
6313                void f() {}, *p;
6314
6315              which is erroneous.  */
6316           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6317             error ("mixing declarations and function-definitions is forbidden");
6318           /* Otherwise, we're done with the list of declarators.  */
6319           else
6320             {
6321               pop_deferring_access_checks ();
6322               return;
6323             }
6324         }
6325       /* The next token should be either a `,' or a `;'.  */
6326       token = cp_lexer_peek_token (parser->lexer);
6327       /* If it's a `,', there are more declarators to come.  */
6328       if (token->type == CPP_COMMA)
6329         cp_lexer_consume_token (parser->lexer);
6330       /* If it's a `;', we are done.  */
6331       else if (token->type == CPP_SEMICOLON)
6332         break;
6333       /* Anything else is an error.  */
6334       else
6335         {
6336           cp_parser_error (parser, "expected `,' or `;'");
6337           /* Skip tokens until we reach the end of the statement.  */
6338           cp_parser_skip_to_end_of_statement (parser);
6339           goto done;
6340         }
6341       /* After the first time around, a function-definition is not
6342          allowed -- even if it was OK at first.  For example:
6343
6344            int i, f() {}
6345
6346          is not valid.  */
6347       function_definition_allowed_p = false;
6348     }
6349
6350   /* Issue an error message if no declarators are present, and the
6351      decl-specifier-seq does not itself declare a class or
6352      enumeration.  */
6353   if (!saw_declarator)
6354     {
6355       if (cp_parser_declares_only_class_p (parser))
6356         shadow_tag (decl_specifiers);
6357       /* Perform any deferred access checks.  */
6358       perform_deferred_access_checks ();
6359     }
6360
6361   /* Consume the `;'.  */
6362   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6363
6364  done:
6365   pop_deferring_access_checks ();
6366 }
6367
6368 /* Parse a decl-specifier-seq.
6369
6370    decl-specifier-seq:
6371      decl-specifier-seq [opt] decl-specifier
6372
6373    decl-specifier:
6374      storage-class-specifier
6375      type-specifier
6376      function-specifier
6377      friend
6378      typedef  
6379
6380    GNU Extension:
6381
6382    decl-specifier-seq:
6383      decl-specifier-seq [opt] attributes
6384
6385    Returns a TREE_LIST, giving the decl-specifiers in the order they
6386    appear in the source code.  The TREE_VALUE of each node is the
6387    decl-specifier.  For a keyword (such as `auto' or `friend'), the
6388    TREE_VALUE is simply the corresponding TREE_IDENTIFIER.  For the
6389    representation of a type-specifier, see cp_parser_type_specifier.  
6390
6391    If there are attributes, they will be stored in *ATTRIBUTES,
6392    represented as described above cp_parser_attributes.  
6393
6394    If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6395    appears, and the entity that will be a friend is not going to be a
6396    class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
6397    even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6398    friendship is granted might not be a class.  
6399
6400    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6401    *flags:
6402
6403      1: one of the decl-specifiers is an elaborated-type-specifier
6404      2: one of the decl-specifiers is an enum-specifier or a
6405         class-specifier
6406
6407    */
6408
6409 static tree
6410 cp_parser_decl_specifier_seq (cp_parser* parser, 
6411                               cp_parser_flags flags, 
6412                               tree* attributes,
6413                               int* declares_class_or_enum)
6414 {
6415   tree decl_specs = NULL_TREE;
6416   bool friend_p = false;
6417   bool constructor_possible_p = !parser->in_declarator_p;
6418   
6419   /* Assume no class or enumeration type is declared.  */
6420   *declares_class_or_enum = 0;
6421
6422   /* Assume there are no attributes.  */
6423   *attributes = NULL_TREE;
6424
6425   /* Keep reading specifiers until there are no more to read.  */
6426   while (true)
6427     {
6428       tree decl_spec = NULL_TREE;
6429       bool constructor_p;
6430       cp_token *token;
6431
6432       /* Peek at the next token.  */
6433       token = cp_lexer_peek_token (parser->lexer);
6434       /* Handle attributes.  */
6435       if (token->keyword == RID_ATTRIBUTE)
6436         {
6437           /* Parse the attributes.  */
6438           decl_spec = cp_parser_attributes_opt (parser);
6439           /* Add them to the list.  */
6440           *attributes = chainon (*attributes, decl_spec);
6441           continue;
6442         }
6443       /* If the next token is an appropriate keyword, we can simply
6444          add it to the list.  */
6445       switch (token->keyword)
6446         {
6447         case RID_FRIEND:
6448           /* decl-specifier:
6449                friend  */
6450           if (friend_p)
6451             error ("duplicate `friend'");
6452           else
6453             friend_p = true;
6454           /* The representation of the specifier is simply the
6455              appropriate TREE_IDENTIFIER node.  */
6456           decl_spec = token->value;
6457           /* Consume the token.  */
6458           cp_lexer_consume_token (parser->lexer);
6459           break;
6460
6461           /* function-specifier:
6462                inline
6463                virtual
6464                explicit  */
6465         case RID_INLINE:
6466         case RID_VIRTUAL:
6467         case RID_EXPLICIT:
6468           decl_spec = cp_parser_function_specifier_opt (parser);
6469           break;
6470           
6471           /* decl-specifier:
6472                typedef  */
6473         case RID_TYPEDEF:
6474           /* The representation of the specifier is simply the
6475              appropriate TREE_IDENTIFIER node.  */
6476           decl_spec = token->value;
6477           /* Consume the token.  */
6478           cp_lexer_consume_token (parser->lexer);
6479           /* A constructor declarator cannot appear in a typedef.  */
6480           constructor_possible_p = false;
6481           /* The "typedef" keyword can only occur in a declaration; we
6482              may as well commit at this point.  */
6483           cp_parser_commit_to_tentative_parse (parser);
6484           break;
6485
6486           /* storage-class-specifier:
6487                auto
6488                register
6489                static
6490                extern
6491                mutable  
6492
6493              GNU Extension:
6494                thread  */
6495         case RID_AUTO:
6496         case RID_REGISTER:
6497         case RID_STATIC:
6498         case RID_EXTERN:
6499         case RID_MUTABLE:
6500         case RID_THREAD:
6501           decl_spec = cp_parser_storage_class_specifier_opt (parser);
6502           break;
6503           
6504         default:
6505           break;
6506         }
6507
6508       /* Constructors are a special case.  The `S' in `S()' is not a
6509          decl-specifier; it is the beginning of the declarator.  */
6510       constructor_p = (!decl_spec 
6511                        && constructor_possible_p
6512                        && cp_parser_constructor_declarator_p (parser,
6513                                                               friend_p));
6514
6515       /* If we don't have a DECL_SPEC yet, then we must be looking at
6516          a type-specifier.  */
6517       if (!decl_spec && !constructor_p)
6518         {
6519           int decl_spec_declares_class_or_enum;
6520           bool is_cv_qualifier;
6521
6522           decl_spec
6523             = cp_parser_type_specifier (parser, flags,
6524                                         friend_p,
6525                                         /*is_declaration=*/true,
6526                                         &decl_spec_declares_class_or_enum,
6527                                         &is_cv_qualifier);
6528
6529           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6530
6531           /* If this type-specifier referenced a user-defined type
6532              (a typedef, class-name, etc.), then we can't allow any
6533              more such type-specifiers henceforth.
6534
6535              [dcl.spec]
6536
6537              The longest sequence of decl-specifiers that could
6538              possibly be a type name is taken as the
6539              decl-specifier-seq of a declaration.  The sequence shall
6540              be self-consistent as described below.
6541
6542              [dcl.type]
6543
6544              As a general rule, at most one type-specifier is allowed
6545              in the complete decl-specifier-seq of a declaration.  The
6546              only exceptions are the following:
6547
6548              -- const or volatile can be combined with any other
6549                 type-specifier. 
6550
6551              -- signed or unsigned can be combined with char, long,
6552                 short, or int.
6553
6554              -- ..
6555
6556              Example:
6557
6558                typedef char* Pc;
6559                void g (const int Pc);
6560
6561              Here, Pc is *not* part of the decl-specifier seq; it's
6562              the declarator.  Therefore, once we see a type-specifier
6563              (other than a cv-qualifier), we forbid any additional
6564              user-defined types.  We *do* still allow things like `int
6565              int' to be considered a decl-specifier-seq, and issue the
6566              error message later.  */
6567           if (decl_spec && !is_cv_qualifier)
6568             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6569           /* A constructor declarator cannot follow a type-specifier.  */
6570           if (decl_spec)
6571             constructor_possible_p = false;
6572         }
6573
6574       /* If we still do not have a DECL_SPEC, then there are no more
6575          decl-specifiers.  */
6576       if (!decl_spec)
6577         {
6578           /* Issue an error message, unless the entire construct was
6579              optional.  */
6580           if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6581             {
6582               cp_parser_error (parser, "expected decl specifier");
6583               return error_mark_node;
6584             }
6585
6586           break;
6587         }
6588
6589       /* Add the DECL_SPEC to the list of specifiers.  */
6590       decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6591
6592       /* After we see one decl-specifier, further decl-specifiers are
6593          always optional.  */
6594       flags |= CP_PARSER_FLAGS_OPTIONAL;
6595     }
6596
6597   /* We have built up the DECL_SPECS in reverse order.  Return them in
6598      the correct order.  */
6599   return nreverse (decl_specs);
6600 }
6601
6602 /* Parse an (optional) storage-class-specifier. 
6603
6604    storage-class-specifier:
6605      auto
6606      register
6607      static
6608      extern
6609      mutable  
6610
6611    GNU Extension:
6612
6613    storage-class-specifier:
6614      thread
6615
6616    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6617    
6618 static tree
6619 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6620 {
6621   switch (cp_lexer_peek_token (parser->lexer)->keyword)
6622     {
6623     case RID_AUTO:
6624     case RID_REGISTER:
6625     case RID_STATIC:
6626     case RID_EXTERN:
6627     case RID_MUTABLE:
6628     case RID_THREAD:
6629       /* Consume the token.  */
6630       return cp_lexer_consume_token (parser->lexer)->value;
6631
6632     default:
6633       return NULL_TREE;
6634     }
6635 }
6636
6637 /* Parse an (optional) function-specifier. 
6638
6639    function-specifier:
6640      inline
6641      virtual
6642      explicit
6643
6644    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6645    
6646 static tree
6647 cp_parser_function_specifier_opt (cp_parser* parser)
6648 {
6649   switch (cp_lexer_peek_token (parser->lexer)->keyword)
6650     {
6651     case RID_INLINE:
6652     case RID_VIRTUAL:
6653     case RID_EXPLICIT:
6654       /* Consume the token.  */
6655       return cp_lexer_consume_token (parser->lexer)->value;
6656
6657     default:
6658       return NULL_TREE;
6659     }
6660 }
6661
6662 /* Parse a linkage-specification.
6663
6664    linkage-specification:
6665      extern string-literal { declaration-seq [opt] }
6666      extern string-literal declaration  */
6667
6668 static void
6669 cp_parser_linkage_specification (cp_parser* parser)
6670 {
6671   cp_token *token;
6672   tree linkage;
6673
6674   /* Look for the `extern' keyword.  */
6675   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6676
6677   /* Peek at the next token.  */
6678   token = cp_lexer_peek_token (parser->lexer);
6679   /* If it's not a string-literal, then there's a problem.  */
6680   if (!cp_parser_is_string_literal (token))
6681     {
6682       cp_parser_error (parser, "expected language-name");
6683       return;
6684     }
6685   /* Consume the token.  */
6686   cp_lexer_consume_token (parser->lexer);
6687
6688   /* Transform the literal into an identifier.  If the literal is a
6689      wide-character string, or contains embedded NULs, then we can't
6690      handle it as the user wants.  */
6691   if (token->type == CPP_WSTRING
6692       || (strlen (TREE_STRING_POINTER (token->value))
6693           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6694     {
6695       cp_parser_error (parser, "invalid linkage-specification");
6696       /* Assume C++ linkage.  */
6697       linkage = get_identifier ("c++");
6698     }
6699   /* If it's a simple string constant, things are easier.  */
6700   else
6701     linkage = get_identifier (TREE_STRING_POINTER (token->value));
6702
6703   /* We're now using the new linkage.  */
6704   push_lang_context (linkage);
6705
6706   /* If the next token is a `{', then we're using the first
6707      production.  */
6708   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6709     {
6710       /* Consume the `{' token.  */
6711       cp_lexer_consume_token (parser->lexer);
6712       /* Parse the declarations.  */
6713       cp_parser_declaration_seq_opt (parser);
6714       /* Look for the closing `}'.  */
6715       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6716     }
6717   /* Otherwise, there's just one declaration.  */
6718   else
6719     {
6720       bool saved_in_unbraced_linkage_specification_p;
6721
6722       saved_in_unbraced_linkage_specification_p 
6723         = parser->in_unbraced_linkage_specification_p;
6724       parser->in_unbraced_linkage_specification_p = true;
6725       have_extern_spec = true;
6726       cp_parser_declaration (parser);
6727       have_extern_spec = false;
6728       parser->in_unbraced_linkage_specification_p 
6729         = saved_in_unbraced_linkage_specification_p;
6730     }
6731
6732   /* We're done with the linkage-specification.  */
6733   pop_lang_context ();
6734 }
6735
6736 /* Special member functions [gram.special] */
6737
6738 /* Parse a conversion-function-id.
6739
6740    conversion-function-id:
6741      operator conversion-type-id  
6742
6743    Returns an IDENTIFIER_NODE representing the operator.  */
6744
6745 static tree 
6746 cp_parser_conversion_function_id (cp_parser* parser)
6747 {
6748   tree type;
6749   tree saved_scope;
6750   tree saved_qualifying_scope;
6751   tree saved_object_scope;
6752
6753   /* Look for the `operator' token.  */
6754   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6755     return error_mark_node;
6756   /* When we parse the conversion-type-id, the current scope will be
6757      reset.  However, we need that information in able to look up the
6758      conversion function later, so we save it here.  */
6759   saved_scope = parser->scope;
6760   saved_qualifying_scope = parser->qualifying_scope;
6761   saved_object_scope = parser->object_scope;
6762   /* We must enter the scope of the class so that the names of
6763      entities declared within the class are available in the
6764      conversion-type-id.  For example, consider:
6765
6766        struct S { 
6767          typedef int I;
6768          operator I();
6769        };
6770
6771        S::operator I() { ... }
6772
6773      In order to see that `I' is a type-name in the definition, we
6774      must be in the scope of `S'.  */
6775   if (saved_scope)
6776     push_scope (saved_scope);
6777   /* Parse the conversion-type-id.  */
6778   type = cp_parser_conversion_type_id (parser);
6779   /* Leave the scope of the class, if any.  */
6780   if (saved_scope)
6781     pop_scope (saved_scope);
6782   /* Restore the saved scope.  */
6783   parser->scope = saved_scope;
6784   parser->qualifying_scope = saved_qualifying_scope;
6785   parser->object_scope = saved_object_scope;
6786   /* If the TYPE is invalid, indicate failure.  */
6787   if (type == error_mark_node)
6788     return error_mark_node;
6789   return mangle_conv_op_name_for_type (type);
6790 }
6791
6792 /* Parse a conversion-type-id:
6793
6794    conversion-type-id:
6795      type-specifier-seq conversion-declarator [opt]
6796
6797    Returns the TYPE specified.  */
6798
6799 static tree
6800 cp_parser_conversion_type_id (cp_parser* parser)
6801 {
6802   tree attributes;
6803   tree type_specifiers;
6804   tree declarator;
6805
6806   /* Parse the attributes.  */
6807   attributes = cp_parser_attributes_opt (parser);
6808   /* Parse the type-specifiers.  */
6809   type_specifiers = cp_parser_type_specifier_seq (parser);
6810   /* If that didn't work, stop.  */
6811   if (type_specifiers == error_mark_node)
6812     return error_mark_node;
6813   /* Parse the conversion-declarator.  */
6814   declarator = cp_parser_conversion_declarator_opt (parser);
6815
6816   return grokdeclarator (declarator, type_specifiers, TYPENAME,
6817                          /*initialized=*/0, &attributes);
6818 }
6819
6820 /* Parse an (optional) conversion-declarator.
6821
6822    conversion-declarator:
6823      ptr-operator conversion-declarator [opt]  
6824
6825    Returns a representation of the declarator.  See
6826    cp_parser_declarator for details.  */
6827
6828 static tree
6829 cp_parser_conversion_declarator_opt (cp_parser* parser)
6830 {
6831   enum tree_code code;
6832   tree class_type;
6833   tree cv_qualifier_seq;
6834
6835   /* We don't know if there's a ptr-operator next, or not.  */
6836   cp_parser_parse_tentatively (parser);
6837   /* Try the ptr-operator.  */
6838   code = cp_parser_ptr_operator (parser, &class_type, 
6839                                  &cv_qualifier_seq);
6840   /* If it worked, look for more conversion-declarators.  */
6841   if (cp_parser_parse_definitely (parser))
6842     {
6843      tree declarator;
6844
6845      /* Parse another optional declarator.  */
6846      declarator = cp_parser_conversion_declarator_opt (parser);
6847
6848      /* Create the representation of the declarator.  */
6849      if (code == INDIRECT_REF)
6850        declarator = make_pointer_declarator (cv_qualifier_seq,
6851                                              declarator);
6852      else
6853        declarator =  make_reference_declarator (cv_qualifier_seq,
6854                                                 declarator);
6855
6856      /* Handle the pointer-to-member case.  */
6857      if (class_type)
6858        declarator = build_nt (SCOPE_REF, class_type, declarator);
6859
6860      return declarator;
6861    }
6862
6863   return NULL_TREE;
6864 }
6865
6866 /* Parse an (optional) ctor-initializer.
6867
6868    ctor-initializer:
6869      : mem-initializer-list  
6870
6871    Returns TRUE iff the ctor-initializer was actually present.  */
6872
6873 static bool
6874 cp_parser_ctor_initializer_opt (cp_parser* parser)
6875 {
6876   /* If the next token is not a `:', then there is no
6877      ctor-initializer.  */
6878   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
6879     {
6880       /* Do default initialization of any bases and members.  */
6881       if (DECL_CONSTRUCTOR_P (current_function_decl))
6882         finish_mem_initializers (NULL_TREE);
6883
6884       return false;
6885     }
6886
6887   /* Consume the `:' token.  */
6888   cp_lexer_consume_token (parser->lexer);
6889   /* And the mem-initializer-list.  */
6890   cp_parser_mem_initializer_list (parser);
6891
6892   return true;
6893 }
6894
6895 /* Parse a mem-initializer-list.
6896
6897    mem-initializer-list:
6898      mem-initializer
6899      mem-initializer , mem-initializer-list  */
6900
6901 static void
6902 cp_parser_mem_initializer_list (cp_parser* parser)
6903 {
6904   tree mem_initializer_list = NULL_TREE;
6905
6906   /* Let the semantic analysis code know that we are starting the
6907      mem-initializer-list.  */
6908   if (!DECL_CONSTRUCTOR_P (current_function_decl))
6909     error ("only constructors take base initializers");
6910
6911   /* Loop through the list.  */
6912   while (true)
6913     {
6914       tree mem_initializer;
6915
6916       /* Parse the mem-initializer.  */
6917       mem_initializer = cp_parser_mem_initializer (parser);
6918       /* Add it to the list, unless it was erroneous.  */
6919       if (mem_initializer)
6920         {
6921           TREE_CHAIN (mem_initializer) = mem_initializer_list;
6922           mem_initializer_list = mem_initializer;
6923         }
6924       /* If the next token is not a `,', we're done.  */
6925       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6926         break;
6927       /* Consume the `,' token.  */
6928       cp_lexer_consume_token (parser->lexer);
6929     }
6930
6931   /* Perform semantic analysis.  */
6932   if (DECL_CONSTRUCTOR_P (current_function_decl))
6933     finish_mem_initializers (mem_initializer_list);
6934 }
6935
6936 /* Parse a mem-initializer.
6937
6938    mem-initializer:
6939      mem-initializer-id ( expression-list [opt] )  
6940
6941    GNU extension:
6942   
6943    mem-initializer:
6944      ( expression-list [opt] )
6945
6946    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
6947    class) or FIELD_DECL (for a non-static data member) to initialize;
6948    the TREE_VALUE is the expression-list.  */
6949
6950 static tree
6951 cp_parser_mem_initializer (cp_parser* parser)
6952 {
6953   tree mem_initializer_id;
6954   tree expression_list;
6955   tree member;
6956   
6957   /* Find out what is being initialized.  */
6958   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6959     {
6960       pedwarn ("anachronistic old-style base class initializer");
6961       mem_initializer_id = NULL_TREE;
6962     }
6963   else
6964     mem_initializer_id = cp_parser_mem_initializer_id (parser);
6965   member = expand_member_init (mem_initializer_id);
6966   if (member && !DECL_P (member))
6967     in_base_initializer = 1;
6968
6969   expression_list 
6970     = cp_parser_parenthesized_expression_list (parser, false,
6971                                                /*non_constant_p=*/NULL);
6972   if (!expression_list)
6973     expression_list = void_type_node;
6974
6975   in_base_initializer = 0;
6976   
6977   return member ? build_tree_list (member, expression_list) : NULL_TREE;
6978 }
6979
6980 /* Parse a mem-initializer-id.
6981
6982    mem-initializer-id:
6983      :: [opt] nested-name-specifier [opt] class-name
6984      identifier  
6985
6986    Returns a TYPE indicating the class to be initializer for the first
6987    production.  Returns an IDENTIFIER_NODE indicating the data member
6988    to be initialized for the second production.  */
6989
6990 static tree
6991 cp_parser_mem_initializer_id (cp_parser* parser)
6992 {
6993   bool global_scope_p;
6994   bool nested_name_specifier_p;
6995   tree id;
6996
6997   /* Look for the optional `::' operator.  */
6998   global_scope_p 
6999     = (cp_parser_global_scope_opt (parser, 
7000                                    /*current_scope_valid_p=*/false) 
7001        != NULL_TREE);
7002   /* Look for the optional nested-name-specifier.  The simplest way to
7003      implement:
7004
7005        [temp.res]
7006
7007        The keyword `typename' is not permitted in a base-specifier or
7008        mem-initializer; in these contexts a qualified name that
7009        depends on a template-parameter is implicitly assumed to be a
7010        type name.
7011
7012      is to assume that we have seen the `typename' keyword at this
7013      point.  */
7014   nested_name_specifier_p 
7015     = (cp_parser_nested_name_specifier_opt (parser,
7016                                             /*typename_keyword_p=*/true,
7017                                             /*check_dependency_p=*/true,
7018                                             /*type_p=*/true,
7019                                             /*is_declaration=*/true)
7020        != NULL_TREE);
7021   /* If there is a `::' operator or a nested-name-specifier, then we
7022      are definitely looking for a class-name.  */
7023   if (global_scope_p || nested_name_specifier_p)
7024     return cp_parser_class_name (parser,
7025                                  /*typename_keyword_p=*/true,
7026                                  /*template_keyword_p=*/false,
7027                                  /*type_p=*/false,
7028                                  /*check_dependency_p=*/true,
7029                                  /*class_head_p=*/false,
7030                                  /*is_declaration=*/true);
7031   /* Otherwise, we could also be looking for an ordinary identifier.  */
7032   cp_parser_parse_tentatively (parser);
7033   /* Try a class-name.  */
7034   id = cp_parser_class_name (parser, 
7035                              /*typename_keyword_p=*/true,
7036                              /*template_keyword_p=*/false,
7037                              /*type_p=*/false,
7038                              /*check_dependency_p=*/true,
7039                              /*class_head_p=*/false,
7040                              /*is_declaration=*/true);
7041   /* If we found one, we're done.  */
7042   if (cp_parser_parse_definitely (parser))
7043     return id;
7044   /* Otherwise, look for an ordinary identifier.  */
7045   return cp_parser_identifier (parser);
7046 }
7047
7048 /* Overloading [gram.over] */
7049
7050 /* Parse an operator-function-id.
7051
7052    operator-function-id:
7053      operator operator  
7054
7055    Returns an IDENTIFIER_NODE for the operator which is a
7056    human-readable spelling of the identifier, e.g., `operator +'.  */
7057
7058 static tree 
7059 cp_parser_operator_function_id (cp_parser* parser)
7060 {
7061   /* Look for the `operator' keyword.  */
7062   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7063     return error_mark_node;
7064   /* And then the name of the operator itself.  */
7065   return cp_parser_operator (parser);
7066 }
7067
7068 /* Parse an operator.
7069
7070    operator:
7071      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7072      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7073      || ++ -- , ->* -> () []
7074
7075    GNU Extensions:
7076    
7077    operator:
7078      <? >? <?= >?=
7079
7080    Returns an IDENTIFIER_NODE for the operator which is a
7081    human-readable spelling of the identifier, e.g., `operator +'.  */
7082    
7083 static tree
7084 cp_parser_operator (cp_parser* parser)
7085 {
7086   tree id = NULL_TREE;
7087   cp_token *token;
7088
7089   /* Peek at the next token.  */
7090   token = cp_lexer_peek_token (parser->lexer);
7091   /* Figure out which operator we have.  */
7092   switch (token->type)
7093     {
7094     case CPP_KEYWORD:
7095       {
7096         enum tree_code op;
7097
7098         /* The keyword should be either `new' or `delete'.  */
7099         if (token->keyword == RID_NEW)
7100           op = NEW_EXPR;
7101         else if (token->keyword == RID_DELETE)
7102           op = DELETE_EXPR;
7103         else
7104           break;
7105
7106         /* Consume the `new' or `delete' token.  */
7107         cp_lexer_consume_token (parser->lexer);
7108
7109         /* Peek at the next token.  */
7110         token = cp_lexer_peek_token (parser->lexer);
7111         /* If it's a `[' token then this is the array variant of the
7112            operator.  */
7113         if (token->type == CPP_OPEN_SQUARE)
7114           {
7115             /* Consume the `[' token.  */
7116             cp_lexer_consume_token (parser->lexer);
7117             /* Look for the `]' token.  */
7118             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7119             id = ansi_opname (op == NEW_EXPR 
7120                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7121           }
7122         /* Otherwise, we have the non-array variant.  */
7123         else
7124           id = ansi_opname (op);
7125
7126         return id;
7127       }
7128
7129     case CPP_PLUS:
7130       id = ansi_opname (PLUS_EXPR);
7131       break;
7132
7133     case CPP_MINUS:
7134       id = ansi_opname (MINUS_EXPR);
7135       break;
7136
7137     case CPP_MULT:
7138       id = ansi_opname (MULT_EXPR);
7139       break;
7140
7141     case CPP_DIV:
7142       id = ansi_opname (TRUNC_DIV_EXPR);
7143       break;
7144
7145     case CPP_MOD:
7146       id = ansi_opname (TRUNC_MOD_EXPR);
7147       break;
7148
7149     case CPP_XOR:
7150       id = ansi_opname (BIT_XOR_EXPR);
7151       break;
7152
7153     case CPP_AND:
7154       id = ansi_opname (BIT_AND_EXPR);
7155       break;
7156
7157     case CPP_OR:
7158       id = ansi_opname (BIT_IOR_EXPR);
7159       break;
7160
7161     case CPP_COMPL:
7162       id = ansi_opname (BIT_NOT_EXPR);
7163       break;
7164       
7165     case CPP_NOT:
7166       id = ansi_opname (TRUTH_NOT_EXPR);
7167       break;
7168
7169     case CPP_EQ:
7170       id = ansi_assopname (NOP_EXPR);
7171       break;
7172
7173     case CPP_LESS:
7174       id = ansi_opname (LT_EXPR);
7175       break;
7176
7177     case CPP_GREATER:
7178       id = ansi_opname (GT_EXPR);
7179       break;
7180
7181     case CPP_PLUS_EQ:
7182       id = ansi_assopname (PLUS_EXPR);
7183       break;
7184
7185     case CPP_MINUS_EQ:
7186       id = ansi_assopname (MINUS_EXPR);
7187       break;
7188
7189     case CPP_MULT_EQ:
7190       id = ansi_assopname (MULT_EXPR);
7191       break;
7192
7193     case CPP_DIV_EQ:
7194       id = ansi_assopname (TRUNC_DIV_EXPR);
7195       break;
7196
7197     case CPP_MOD_EQ:
7198       id = ansi_assopname (TRUNC_MOD_EXPR);
7199       break;
7200
7201     case CPP_XOR_EQ:
7202       id = ansi_assopname (BIT_XOR_EXPR);
7203       break;
7204
7205     case CPP_AND_EQ:
7206       id = ansi_assopname (BIT_AND_EXPR);
7207       break;
7208
7209     case CPP_OR_EQ:
7210       id = ansi_assopname (BIT_IOR_EXPR);
7211       break;
7212
7213     case CPP_LSHIFT:
7214       id = ansi_opname (LSHIFT_EXPR);
7215       break;
7216
7217     case CPP_RSHIFT:
7218       id = ansi_opname (RSHIFT_EXPR);
7219       break;
7220
7221     case CPP_LSHIFT_EQ:
7222       id = ansi_assopname (LSHIFT_EXPR);
7223       break;
7224
7225     case CPP_RSHIFT_EQ:
7226       id = ansi_assopname (RSHIFT_EXPR);
7227       break;
7228
7229     case CPP_EQ_EQ:
7230       id = ansi_opname (EQ_EXPR);
7231       break;
7232
7233     case CPP_NOT_EQ:
7234       id = ansi_opname (NE_EXPR);
7235       break;
7236
7237     case CPP_LESS_EQ:
7238       id = ansi_opname (LE_EXPR);
7239       break;
7240
7241     case CPP_GREATER_EQ:
7242       id = ansi_opname (GE_EXPR);
7243       break;
7244
7245     case CPP_AND_AND:
7246       id = ansi_opname (TRUTH_ANDIF_EXPR);
7247       break;
7248
7249     case CPP_OR_OR:
7250       id = ansi_opname (TRUTH_ORIF_EXPR);
7251       break;
7252       
7253     case CPP_PLUS_PLUS:
7254       id = ansi_opname (POSTINCREMENT_EXPR);
7255       break;
7256
7257     case CPP_MINUS_MINUS:
7258       id = ansi_opname (PREDECREMENT_EXPR);
7259       break;
7260
7261     case CPP_COMMA:
7262       id = ansi_opname (COMPOUND_EXPR);
7263       break;
7264
7265     case CPP_DEREF_STAR:
7266       id = ansi_opname (MEMBER_REF);
7267       break;
7268
7269     case CPP_DEREF:
7270       id = ansi_opname (COMPONENT_REF);
7271       break;
7272
7273     case CPP_OPEN_PAREN:
7274       /* Consume the `('.  */
7275       cp_lexer_consume_token (parser->lexer);
7276       /* Look for the matching `)'.  */
7277       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7278       return ansi_opname (CALL_EXPR);
7279
7280     case CPP_OPEN_SQUARE:
7281       /* Consume the `['.  */
7282       cp_lexer_consume_token (parser->lexer);
7283       /* Look for the matching `]'.  */
7284       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7285       return ansi_opname (ARRAY_REF);
7286
7287       /* Extensions.  */
7288     case CPP_MIN:
7289       id = ansi_opname (MIN_EXPR);
7290       break;
7291
7292     case CPP_MAX:
7293       id = ansi_opname (MAX_EXPR);
7294       break;
7295
7296     case CPP_MIN_EQ:
7297       id = ansi_assopname (MIN_EXPR);
7298       break;
7299
7300     case CPP_MAX_EQ:
7301       id = ansi_assopname (MAX_EXPR);
7302       break;
7303
7304     default:
7305       /* Anything else is an error.  */
7306       break;
7307     }
7308
7309   /* If we have selected an identifier, we need to consume the
7310      operator token.  */
7311   if (id)
7312     cp_lexer_consume_token (parser->lexer);
7313   /* Otherwise, no valid operator name was present.  */
7314   else
7315     {
7316       cp_parser_error (parser, "expected operator");
7317       id = error_mark_node;
7318     }
7319
7320   return id;
7321 }
7322
7323 /* Parse a template-declaration.
7324
7325    template-declaration:
7326      export [opt] template < template-parameter-list > declaration  
7327
7328    If MEMBER_P is TRUE, this template-declaration occurs within a
7329    class-specifier.  
7330
7331    The grammar rule given by the standard isn't correct.  What
7332    is really meant is:
7333
7334    template-declaration:
7335      export [opt] template-parameter-list-seq 
7336        decl-specifier-seq [opt] init-declarator [opt] ;
7337      export [opt] template-parameter-list-seq 
7338        function-definition
7339
7340    template-parameter-list-seq:
7341      template-parameter-list-seq [opt]
7342      template < template-parameter-list >  */
7343
7344 static void
7345 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7346 {
7347   /* Check for `export'.  */
7348   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7349     {
7350       /* Consume the `export' token.  */
7351       cp_lexer_consume_token (parser->lexer);
7352       /* Warn that we do not support `export'.  */
7353       warning ("keyword `export' not implemented, and will be ignored");
7354     }
7355
7356   cp_parser_template_declaration_after_export (parser, member_p);
7357 }
7358
7359 /* Parse a template-parameter-list.
7360
7361    template-parameter-list:
7362      template-parameter
7363      template-parameter-list , template-parameter
7364
7365    Returns a TREE_LIST.  Each node represents a template parameter.
7366    The nodes are connected via their TREE_CHAINs.  */
7367
7368 static tree
7369 cp_parser_template_parameter_list (cp_parser* parser)
7370 {
7371   tree parameter_list = NULL_TREE;
7372
7373   while (true)
7374     {
7375       tree parameter;
7376       cp_token *token;
7377
7378       /* Parse the template-parameter.  */
7379       parameter = cp_parser_template_parameter (parser);
7380       /* Add it to the list.  */
7381       parameter_list = process_template_parm (parameter_list,
7382                                               parameter);
7383
7384       /* Peek at the next token.  */
7385       token = cp_lexer_peek_token (parser->lexer);
7386       /* If it's not a `,', we're done.  */
7387       if (token->type != CPP_COMMA)
7388         break;
7389       /* Otherwise, consume the `,' token.  */
7390       cp_lexer_consume_token (parser->lexer);
7391     }
7392
7393   return parameter_list;
7394 }
7395
7396 /* Parse a template-parameter.
7397
7398    template-parameter:
7399      type-parameter
7400      parameter-declaration
7401
7402    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
7403    TREE_PURPOSE is the default value, if any.  */
7404
7405 static tree
7406 cp_parser_template_parameter (cp_parser* parser)
7407 {
7408   cp_token *token;
7409
7410   /* Peek at the next token.  */
7411   token = cp_lexer_peek_token (parser->lexer);
7412   /* If it is `class' or `template', we have a type-parameter.  */
7413   if (token->keyword == RID_TEMPLATE)
7414     return cp_parser_type_parameter (parser);
7415   /* If it is `class' or `typename' we do not know yet whether it is a
7416      type parameter or a non-type parameter.  Consider:
7417
7418        template <typename T, typename T::X X> ...
7419
7420      or:
7421      
7422        template <class C, class D*> ...
7423
7424      Here, the first parameter is a type parameter, and the second is
7425      a non-type parameter.  We can tell by looking at the token after
7426      the identifier -- if it is a `,', `=', or `>' then we have a type
7427      parameter.  */
7428   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7429     {
7430       /* Peek at the token after `class' or `typename'.  */
7431       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7432       /* If it's an identifier, skip it.  */
7433       if (token->type == CPP_NAME)
7434         token = cp_lexer_peek_nth_token (parser->lexer, 3);
7435       /* Now, see if the token looks like the end of a template
7436          parameter.  */
7437       if (token->type == CPP_COMMA 
7438           || token->type == CPP_EQ
7439           || token->type == CPP_GREATER)
7440         return cp_parser_type_parameter (parser);
7441     }
7442
7443   /* Otherwise, it is a non-type parameter.  
7444
7445      [temp.param]
7446
7447      When parsing a default template-argument for a non-type
7448      template-parameter, the first non-nested `>' is taken as the end
7449      of the template parameter-list rather than a greater-than
7450      operator.  */
7451   return 
7452     cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7453 }
7454
7455 /* Parse a type-parameter.
7456
7457    type-parameter:
7458      class identifier [opt]
7459      class identifier [opt] = type-id
7460      typename identifier [opt]
7461      typename identifier [opt] = type-id
7462      template < template-parameter-list > class identifier [opt]
7463      template < template-parameter-list > class identifier [opt] 
7464        = id-expression  
7465
7466    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
7467    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
7468    the declaration of the parameter.  */
7469
7470 static tree
7471 cp_parser_type_parameter (cp_parser* parser)
7472 {
7473   cp_token *token;
7474   tree parameter;
7475
7476   /* Look for a keyword to tell us what kind of parameter this is.  */
7477   token = cp_parser_require (parser, CPP_KEYWORD, 
7478                              "`class', `typename', or `template'");
7479   if (!token)
7480     return error_mark_node;
7481
7482   switch (token->keyword)
7483     {
7484     case RID_CLASS:
7485     case RID_TYPENAME:
7486       {
7487         tree identifier;
7488         tree default_argument;
7489
7490         /* If the next token is an identifier, then it names the
7491            parameter.  */
7492         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7493           identifier = cp_parser_identifier (parser);
7494         else
7495           identifier = NULL_TREE;
7496
7497         /* Create the parameter.  */
7498         parameter = finish_template_type_parm (class_type_node, identifier);
7499
7500         /* If the next token is an `=', we have a default argument.  */
7501         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7502           {
7503             /* Consume the `=' token.  */
7504             cp_lexer_consume_token (parser->lexer);
7505             /* Parse the default-argument.  */
7506             default_argument = cp_parser_type_id (parser);
7507           }
7508         else
7509           default_argument = NULL_TREE;
7510
7511         /* Create the combined representation of the parameter and the
7512            default argument.  */
7513         parameter = build_tree_list (default_argument, parameter);
7514       }
7515       break;
7516
7517     case RID_TEMPLATE:
7518       {
7519         tree parameter_list;
7520         tree identifier;
7521         tree default_argument;
7522
7523         /* Look for the `<'.  */
7524         cp_parser_require (parser, CPP_LESS, "`<'");
7525         /* Parse the template-parameter-list.  */
7526         begin_template_parm_list ();
7527         parameter_list 
7528           = cp_parser_template_parameter_list (parser);
7529         parameter_list = end_template_parm_list (parameter_list);
7530         /* Look for the `>'.  */
7531         cp_parser_require (parser, CPP_GREATER, "`>'");
7532         /* Look for the `class' keyword.  */
7533         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7534         /* If the next token is an `=', then there is a
7535            default-argument.  If the next token is a `>', we are at
7536            the end of the parameter-list.  If the next token is a `,',
7537            then we are at the end of this parameter.  */
7538         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7539             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7540             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7541           identifier = cp_parser_identifier (parser);
7542         else
7543           identifier = NULL_TREE;
7544         /* Create the template parameter.  */
7545         parameter = finish_template_template_parm (class_type_node,
7546                                                    identifier);
7547                                                    
7548         /* If the next token is an `=', then there is a
7549            default-argument.  */
7550         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7551           {
7552             /* Consume the `='.  */
7553             cp_lexer_consume_token (parser->lexer);
7554             /* Parse the id-expression.  */
7555             default_argument 
7556               = cp_parser_id_expression (parser,
7557                                          /*template_keyword_p=*/false,
7558                                          /*check_dependency_p=*/true,
7559                                          /*template_p=*/NULL,
7560                                          /*declarator_p=*/false);
7561             /* Look up the name.  */
7562             default_argument 
7563               = cp_parser_lookup_name_simple (parser, default_argument);
7564             /* See if the default argument is valid.  */
7565             default_argument
7566               = check_template_template_default_arg (default_argument);
7567           }
7568         else
7569           default_argument = NULL_TREE;
7570
7571         /* Create the combined representation of the parameter and the
7572            default argument.  */
7573         parameter =  build_tree_list (default_argument, parameter);
7574       }
7575       break;
7576
7577     default:
7578       /* Anything else is an error.  */
7579       cp_parser_error (parser,
7580                        "expected `class', `typename', or `template'");
7581       parameter = error_mark_node;
7582     }
7583   
7584   return parameter;
7585 }
7586
7587 /* Parse a template-id.
7588
7589    template-id:
7590      template-name < template-argument-list [opt] >
7591
7592    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7593    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
7594    returned.  Otherwise, if the template-name names a function, or set
7595    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
7596    names a class, returns a TYPE_DECL for the specialization.  
7597
7598    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7599    uninstantiated templates.  */
7600
7601 static tree
7602 cp_parser_template_id (cp_parser *parser, 
7603                        bool template_keyword_p, 
7604                        bool check_dependency_p,
7605                        bool is_declaration)
7606 {
7607   tree template;
7608   tree arguments;
7609   tree template_id;
7610   ptrdiff_t start_of_id;
7611   tree access_check = NULL_TREE;
7612   cp_token *next_token;
7613   bool is_identifier;
7614
7615   /* If the next token corresponds to a template-id, there is no need
7616      to reparse it.  */
7617   next_token = cp_lexer_peek_token (parser->lexer);
7618   if (next_token->type == CPP_TEMPLATE_ID)
7619     {
7620       tree value;
7621       tree check;
7622
7623       /* Get the stored value.  */
7624       value = cp_lexer_consume_token (parser->lexer)->value;
7625       /* Perform any access checks that were deferred.  */
7626       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7627         perform_or_defer_access_check (TREE_PURPOSE (check),
7628                                        TREE_VALUE (check));
7629       /* Return the stored value.  */
7630       return TREE_VALUE (value);
7631     }
7632
7633   /* Avoid performing name lookup if there is no possibility of
7634      finding a template-id.  */
7635   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7636       || (next_token->type == CPP_NAME
7637           && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7638     {
7639       cp_parser_error (parser, "expected template-id");
7640       return error_mark_node;
7641     }
7642
7643   /* Remember where the template-id starts.  */
7644   if (cp_parser_parsing_tentatively (parser)
7645       && !cp_parser_committed_to_tentative_parse (parser))
7646     {
7647       next_token = cp_lexer_peek_token (parser->lexer);
7648       start_of_id = cp_lexer_token_difference (parser->lexer,
7649                                                parser->lexer->first_token,
7650                                                next_token);
7651     }
7652   else
7653     start_of_id = -1;
7654
7655   push_deferring_access_checks (dk_deferred);
7656
7657   /* Parse the template-name.  */
7658   is_identifier = false;
7659   template = cp_parser_template_name (parser, template_keyword_p,
7660                                       check_dependency_p,
7661                                       is_declaration,
7662                                       &is_identifier);
7663   if (template == error_mark_node || is_identifier)
7664     {
7665       pop_deferring_access_checks ();
7666       return template;
7667     }
7668
7669   /* Look for the `<' that starts the template-argument-list.  */
7670   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7671     {
7672       pop_deferring_access_checks ();
7673       return error_mark_node;
7674     }
7675
7676   /* Parse the arguments.  */
7677   arguments = cp_parser_enclosed_template_argument_list (parser);
7678
7679   /* Build a representation of the specialization.  */
7680   if (TREE_CODE (template) == IDENTIFIER_NODE)
7681     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7682   else if (DECL_CLASS_TEMPLATE_P (template)
7683            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7684     template_id 
7685       = finish_template_type (template, arguments, 
7686                               cp_lexer_next_token_is (parser->lexer, 
7687                                                       CPP_SCOPE));
7688   else
7689     {
7690       /* If it's not a class-template or a template-template, it should be
7691          a function-template.  */
7692       my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7693                            || TREE_CODE (template) == OVERLOAD
7694                            || BASELINK_P (template)),
7695                           20010716);
7696       
7697       template_id = lookup_template_function (template, arguments);
7698     }
7699   
7700   /* Retrieve any deferred checks.  Do not pop this access checks yet
7701      so the memory will not be reclaimed during token replacing below.  */
7702   access_check = get_deferred_access_checks ();
7703
7704   /* If parsing tentatively, replace the sequence of tokens that makes
7705      up the template-id with a CPP_TEMPLATE_ID token.  That way,
7706      should we re-parse the token stream, we will not have to repeat
7707      the effort required to do the parse, nor will we issue duplicate
7708      error messages about problems during instantiation of the
7709      template.  */
7710   if (start_of_id >= 0)
7711     {
7712       cp_token *token;
7713
7714       /* Find the token that corresponds to the start of the
7715          template-id.  */
7716       token = cp_lexer_advance_token (parser->lexer, 
7717                                       parser->lexer->first_token,
7718                                       start_of_id);
7719
7720       /* Reset the contents of the START_OF_ID token.  */
7721       token->type = CPP_TEMPLATE_ID;
7722       token->value = build_tree_list (access_check, template_id);
7723       token->keyword = RID_MAX;
7724       /* Purge all subsequent tokens.  */
7725       cp_lexer_purge_tokens_after (parser->lexer, token);
7726     }
7727
7728   pop_deferring_access_checks ();
7729   return template_id;
7730 }
7731
7732 /* Parse a template-name.
7733
7734    template-name:
7735      identifier
7736  
7737    The standard should actually say:
7738
7739    template-name:
7740      identifier
7741      operator-function-id
7742      conversion-function-id
7743
7744    A defect report has been filed about this issue.
7745
7746    If TEMPLATE_KEYWORD_P is true, then we have just seen the
7747    `template' keyword, in a construction like:
7748
7749      T::template f<3>()
7750
7751    In that case `f' is taken to be a template-name, even though there
7752    is no way of knowing for sure.
7753
7754    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7755    name refers to a set of overloaded functions, at least one of which
7756    is a template, or an IDENTIFIER_NODE with the name of the template,
7757    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
7758    names are looked up inside uninstantiated templates.  */
7759
7760 static tree
7761 cp_parser_template_name (cp_parser* parser, 
7762                          bool template_keyword_p, 
7763                          bool check_dependency_p,
7764                          bool is_declaration,
7765                          bool *is_identifier)
7766 {
7767   tree identifier;
7768   tree decl;
7769   tree fns;
7770
7771   /* If the next token is `operator', then we have either an
7772      operator-function-id or a conversion-function-id.  */
7773   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7774     {
7775       /* We don't know whether we're looking at an
7776          operator-function-id or a conversion-function-id.  */
7777       cp_parser_parse_tentatively (parser);
7778       /* Try an operator-function-id.  */
7779       identifier = cp_parser_operator_function_id (parser);
7780       /* If that didn't work, try a conversion-function-id.  */
7781       if (!cp_parser_parse_definitely (parser))
7782         identifier = cp_parser_conversion_function_id (parser);
7783     }
7784   /* Look for the identifier.  */
7785   else
7786     identifier = cp_parser_identifier (parser);
7787   
7788   /* If we didn't find an identifier, we don't have a template-id.  */
7789   if (identifier == error_mark_node)
7790     return error_mark_node;
7791
7792   /* If the name immediately followed the `template' keyword, then it
7793      is a template-name.  However, if the next token is not `<', then
7794      we do not treat it as a template-name, since it is not being used
7795      as part of a template-id.  This enables us to handle constructs
7796      like:
7797
7798        template <typename T> struct S { S(); };
7799        template <typename T> S<T>::S();
7800
7801      correctly.  We would treat `S' as a template -- if it were `S<T>'
7802      -- but we do not if there is no `<'.  */
7803
7804   if (processing_template_decl
7805       && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
7806     {
7807       /* In a declaration, in a dependent context, we pretend that the
7808          "template" keyword was present in order to improve error
7809          recovery.  For example, given:
7810          
7811            template <typename T> void f(T::X<int>);
7812          
7813          we want to treat "X<int>" as a template-id.  */
7814       if (is_declaration 
7815           && !template_keyword_p 
7816           && parser->scope && TYPE_P (parser->scope)
7817           && dependent_type_p (parser->scope))
7818         {
7819           ptrdiff_t start;
7820           cp_token* token;
7821           /* Explain what went wrong.  */
7822           error ("non-template `%D' used as template", identifier);
7823           error ("(use `%T::template %D' to indicate that it is a template)",
7824                  parser->scope, identifier);
7825           /* If parsing tentatively, find the location of the "<"
7826              token.  */
7827           if (cp_parser_parsing_tentatively (parser)
7828               && !cp_parser_committed_to_tentative_parse (parser))
7829             {
7830               cp_parser_simulate_error (parser);
7831               token = cp_lexer_peek_token (parser->lexer);
7832               token = cp_lexer_prev_token (parser->lexer, token);
7833               start = cp_lexer_token_difference (parser->lexer,
7834                                                  parser->lexer->first_token,
7835                                                  token);
7836             }
7837           else
7838             start = -1;
7839           /* Parse the template arguments so that we can issue error
7840              messages about them.  */
7841           cp_lexer_consume_token (parser->lexer);
7842           cp_parser_enclosed_template_argument_list (parser);
7843           /* Skip tokens until we find a good place from which to
7844              continue parsing.  */
7845           cp_parser_skip_to_closing_parenthesis (parser,
7846                                                  /*recovering=*/true,
7847                                                  /*or_comma=*/true,
7848                                                  /*consume_paren=*/false);
7849           /* If parsing tentatively, permanently remove the
7850              template argument list.  That will prevent duplicate
7851              error messages from being issued about the missing
7852              "template" keyword.  */
7853           if (start >= 0)
7854             {
7855               token = cp_lexer_advance_token (parser->lexer,
7856                                               parser->lexer->first_token,
7857                                               start);
7858               cp_lexer_purge_tokens_after (parser->lexer, token);
7859             }
7860           if (is_identifier)
7861             *is_identifier = true;
7862           return identifier;
7863         }
7864       if (template_keyword_p)
7865         return identifier;
7866     }
7867
7868   /* Look up the name.  */
7869   decl = cp_parser_lookup_name (parser, identifier,
7870                                 /*is_type=*/false,
7871                                 /*is_namespace=*/false,
7872                                 check_dependency_p);
7873   decl = maybe_get_template_decl_from_type_decl (decl);
7874
7875   /* If DECL is a template, then the name was a template-name.  */
7876   if (TREE_CODE (decl) == TEMPLATE_DECL)
7877     ;
7878   else 
7879     {
7880       /* The standard does not explicitly indicate whether a name that
7881          names a set of overloaded declarations, some of which are
7882          templates, is a template-name.  However, such a name should
7883          be a template-name; otherwise, there is no way to form a
7884          template-id for the overloaded templates.  */
7885       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
7886       if (TREE_CODE (fns) == OVERLOAD)
7887         {
7888           tree fn;
7889           
7890           for (fn = fns; fn; fn = OVL_NEXT (fn))
7891             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
7892               break;
7893         }
7894       else
7895         {
7896           /* Otherwise, the name does not name a template.  */
7897           cp_parser_error (parser, "expected template-name");
7898           return error_mark_node;
7899         }
7900     }
7901
7902   /* If DECL is dependent, and refers to a function, then just return
7903      its name; we will look it up again during template instantiation.  */
7904   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
7905     {
7906       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
7907       if (TYPE_P (scope) && dependent_type_p (scope))
7908         return identifier;
7909     }
7910
7911   return decl;
7912 }
7913
7914 /* Parse a template-argument-list.
7915
7916    template-argument-list:
7917      template-argument
7918      template-argument-list , template-argument
7919
7920    Returns a TREE_VEC containing the arguments.  */
7921
7922 static tree
7923 cp_parser_template_argument_list (cp_parser* parser)
7924 {
7925   tree fixed_args[10];
7926   unsigned n_args = 0;
7927   unsigned alloced = 10;
7928   tree *arg_ary = fixed_args;
7929   tree vec;
7930
7931   do
7932     {
7933       tree argument;
7934
7935       if (n_args)
7936         /* Consume the comma.  */
7937         cp_lexer_consume_token (parser->lexer);
7938       
7939       /* Parse the template-argument.  */
7940       argument = cp_parser_template_argument (parser);
7941       if (n_args == alloced)
7942         {
7943           alloced *= 2;
7944           
7945           if (arg_ary == fixed_args)
7946             {
7947               arg_ary = xmalloc (sizeof (tree) * alloced);
7948               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
7949             }
7950           else
7951             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
7952         }
7953       arg_ary[n_args++] = argument;
7954     }
7955   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
7956
7957   vec = make_tree_vec (n_args);
7958
7959   while (n_args--)
7960     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
7961   
7962   if (arg_ary != fixed_args)
7963     free (arg_ary);
7964   return vec;
7965 }
7966
7967 /* Parse a template-argument.
7968
7969    template-argument:
7970      assignment-expression
7971      type-id
7972      id-expression
7973
7974    The representation is that of an assignment-expression, type-id, or
7975    id-expression -- except that the qualified id-expression is
7976    evaluated, so that the value returned is either a DECL or an
7977    OVERLOAD.  
7978
7979    Although the standard says "assignment-expression", it forbids
7980    throw-expressions or assignments in the template argument.
7981    Therefore, we use "conditional-expression" instead.  */
7982
7983 static tree
7984 cp_parser_template_argument (cp_parser* parser)
7985 {
7986   tree argument;
7987   bool template_p;
7988   bool address_p;
7989   cp_token *token;
7990   cp_id_kind idk;
7991   tree qualifying_class;
7992
7993   /* There's really no way to know what we're looking at, so we just
7994      try each alternative in order.  
7995
7996        [temp.arg]
7997
7998        In a template-argument, an ambiguity between a type-id and an
7999        expression is resolved to a type-id, regardless of the form of
8000        the corresponding template-parameter.  
8001
8002      Therefore, we try a type-id first.  */
8003   cp_parser_parse_tentatively (parser);
8004   argument = cp_parser_type_id (parser);
8005   /* If the next token isn't a `,' or a `>', then this argument wasn't
8006      really finished.  */
8007   if (!cp_parser_next_token_ends_template_argument_p (parser))
8008     cp_parser_error (parser, "expected template-argument");
8009   /* If that worked, we're done.  */
8010   if (cp_parser_parse_definitely (parser))
8011     return argument;
8012   /* We're still not sure what the argument will be.  */
8013   cp_parser_parse_tentatively (parser);
8014   /* Try a template.  */
8015   argument = cp_parser_id_expression (parser, 
8016                                       /*template_keyword_p=*/false,
8017                                       /*check_dependency_p=*/true,
8018                                       &template_p,
8019                                       /*declarator_p=*/false);
8020   /* If the next token isn't a `,' or a `>', then this argument wasn't
8021      really finished.  */
8022   if (!cp_parser_next_token_ends_template_argument_p (parser))
8023     cp_parser_error (parser, "expected template-argument");
8024   if (!cp_parser_error_occurred (parser))
8025     {
8026       /* Figure out what is being referred to.  */
8027       argument = cp_parser_lookup_name_simple (parser, argument);
8028       if (template_p)
8029         argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8030                                                 TREE_OPERAND (argument, 1),
8031                                                 tf_error);
8032       else if (TREE_CODE (argument) != TEMPLATE_DECL)
8033         cp_parser_error (parser, "expected template-name");
8034     }
8035   if (cp_parser_parse_definitely (parser))
8036     return argument;
8037   /* It must be a non-type argument.  There permitted cases are given
8038      in [temp.arg.nontype]:
8039
8040      -- an integral constant-expression of integral or enumeration
8041         type; or
8042
8043      -- the name of a non-type template-parameter; or
8044
8045      -- the name of an object or function with external linkage...
8046
8047      -- the address of an object or function with external linkage...
8048
8049      -- a pointer to member...  */
8050   /* Look for a non-type template parameter.  */
8051   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8052     {
8053       cp_parser_parse_tentatively (parser);
8054       argument = cp_parser_primary_expression (parser,
8055                                                &idk,
8056                                                &qualifying_class);
8057       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8058           || !cp_parser_next_token_ends_template_argument_p (parser))
8059         cp_parser_simulate_error (parser);
8060       if (cp_parser_parse_definitely (parser))
8061         return argument;
8062     }
8063   /* If the next token is "&", the argument must be the address of an
8064      object or function with external linkage.  */
8065   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8066   if (address_p)
8067     cp_lexer_consume_token (parser->lexer);
8068   /* See if we might have an id-expression.  */
8069   token = cp_lexer_peek_token (parser->lexer);
8070   if (token->type == CPP_NAME
8071       || token->keyword == RID_OPERATOR
8072       || token->type == CPP_SCOPE
8073       || token->type == CPP_TEMPLATE_ID
8074       || token->type == CPP_NESTED_NAME_SPECIFIER)
8075     {
8076       cp_parser_parse_tentatively (parser);
8077       argument = cp_parser_primary_expression (parser,
8078                                                &idk,
8079                                                &qualifying_class);
8080       if (cp_parser_error_occurred (parser)
8081           || !cp_parser_next_token_ends_template_argument_p (parser))
8082         cp_parser_abort_tentative_parse (parser);
8083       else
8084         {
8085           if (qualifying_class)
8086             argument = finish_qualified_id_expr (qualifying_class,
8087                                                  argument,
8088                                                  /*done=*/true,
8089                                                  address_p);
8090           if (TREE_CODE (argument) == VAR_DECL)
8091             {
8092               /* A variable without external linkage might still be a
8093                  valid constant-expression, so no error is issued here
8094                  if the external-linkage check fails.  */
8095               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8096                 cp_parser_simulate_error (parser);
8097             }
8098           else if (is_overloaded_fn (argument))
8099             /* All overloaded functions are allowed; if the external
8100                linkage test does not pass, an error will be issued
8101                later.  */
8102             ;
8103           else if (address_p
8104                    && (TREE_CODE (argument) == OFFSET_REF 
8105                        || TREE_CODE (argument) == SCOPE_REF))
8106             /* A pointer-to-member.  */
8107             ;
8108           else
8109             cp_parser_simulate_error (parser);
8110
8111           if (cp_parser_parse_definitely (parser))
8112             {
8113               if (address_p)
8114                 argument = build_x_unary_op (ADDR_EXPR, argument);
8115               return argument;
8116             }
8117         }
8118     }
8119   /* If the argument started with "&", there are no other valid
8120      alternatives at this point.  */
8121   if (address_p)
8122     {
8123       cp_parser_error (parser, "invalid non-type template argument");
8124       return error_mark_node;
8125     }
8126   /* The argument must be a constant-expression.  */
8127   argument = cp_parser_constant_expression (parser, 
8128                                             /*allow_non_constant_p=*/false,
8129                                             /*non_constant_p=*/NULL);
8130   /* If it's non-dependent, simplify it.  */
8131   return cp_parser_fold_non_dependent_expr (argument);
8132 }
8133
8134 /* Parse an explicit-instantiation.
8135
8136    explicit-instantiation:
8137      template declaration  
8138
8139    Although the standard says `declaration', what it really means is:
8140
8141    explicit-instantiation:
8142      template decl-specifier-seq [opt] declarator [opt] ; 
8143
8144    Things like `template int S<int>::i = 5, int S<double>::j;' are not
8145    supposed to be allowed.  A defect report has been filed about this
8146    issue.  
8147
8148    GNU Extension:
8149   
8150    explicit-instantiation:
8151      storage-class-specifier template 
8152        decl-specifier-seq [opt] declarator [opt] ;
8153      function-specifier template 
8154        decl-specifier-seq [opt] declarator [opt] ;  */
8155
8156 static void
8157 cp_parser_explicit_instantiation (cp_parser* parser)
8158 {
8159   int declares_class_or_enum;
8160   tree decl_specifiers;
8161   tree attributes;
8162   tree extension_specifier = NULL_TREE;
8163
8164   /* Look for an (optional) storage-class-specifier or
8165      function-specifier.  */
8166   if (cp_parser_allow_gnu_extensions_p (parser))
8167     {
8168       extension_specifier 
8169         = cp_parser_storage_class_specifier_opt (parser);
8170       if (!extension_specifier)
8171         extension_specifier = cp_parser_function_specifier_opt (parser);
8172     }
8173
8174   /* Look for the `template' keyword.  */
8175   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8176   /* Let the front end know that we are processing an explicit
8177      instantiation.  */
8178   begin_explicit_instantiation ();
8179   /* [temp.explicit] says that we are supposed to ignore access
8180      control while processing explicit instantiation directives.  */
8181   push_deferring_access_checks (dk_no_check);
8182   /* Parse a decl-specifier-seq.  */
8183   decl_specifiers 
8184     = cp_parser_decl_specifier_seq (parser,
8185                                     CP_PARSER_FLAGS_OPTIONAL,
8186                                     &attributes,
8187                                     &declares_class_or_enum);
8188   /* If there was exactly one decl-specifier, and it declared a class,
8189      and there's no declarator, then we have an explicit type
8190      instantiation.  */
8191   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8192     {
8193       tree type;
8194
8195       type = check_tag_decl (decl_specifiers);
8196       /* Turn access control back on for names used during
8197          template instantiation.  */
8198       pop_deferring_access_checks ();
8199       if (type)
8200         do_type_instantiation (type, extension_specifier, /*complain=*/1);
8201     }
8202   else
8203     {
8204       tree declarator;
8205       tree decl;
8206
8207       /* Parse the declarator.  */
8208       declarator 
8209         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8210                                 /*ctor_dtor_or_conv_p=*/NULL);
8211       cp_parser_check_for_definition_in_return_type (declarator, 
8212                                                      declares_class_or_enum);
8213       decl = grokdeclarator (declarator, decl_specifiers, 
8214                              NORMAL, 0, NULL);
8215       /* Turn access control back on for names used during
8216          template instantiation.  */
8217       pop_deferring_access_checks ();
8218       /* Do the explicit instantiation.  */
8219       do_decl_instantiation (decl, extension_specifier);
8220     }
8221   /* We're done with the instantiation.  */
8222   end_explicit_instantiation ();
8223
8224   cp_parser_consume_semicolon_at_end_of_statement (parser);
8225 }
8226
8227 /* Parse an explicit-specialization.
8228
8229    explicit-specialization:
8230      template < > declaration  
8231
8232    Although the standard says `declaration', what it really means is:
8233
8234    explicit-specialization:
8235      template <> decl-specifier [opt] init-declarator [opt] ;
8236      template <> function-definition 
8237      template <> explicit-specialization
8238      template <> template-declaration  */
8239
8240 static void
8241 cp_parser_explicit_specialization (cp_parser* parser)
8242 {
8243   /* Look for the `template' keyword.  */
8244   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8245   /* Look for the `<'.  */
8246   cp_parser_require (parser, CPP_LESS, "`<'");
8247   /* Look for the `>'.  */
8248   cp_parser_require (parser, CPP_GREATER, "`>'");
8249   /* We have processed another parameter list.  */
8250   ++parser->num_template_parameter_lists;
8251   /* Let the front end know that we are beginning a specialization.  */
8252   begin_specialization ();
8253
8254   /* If the next keyword is `template', we need to figure out whether
8255      or not we're looking a template-declaration.  */
8256   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8257     {
8258       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8259           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8260         cp_parser_template_declaration_after_export (parser,
8261                                                      /*member_p=*/false);
8262       else
8263         cp_parser_explicit_specialization (parser);
8264     }
8265   else
8266     /* Parse the dependent declaration.  */
8267     cp_parser_single_declaration (parser, 
8268                                   /*member_p=*/false,
8269                                   /*friend_p=*/NULL);
8270
8271   /* We're done with the specialization.  */
8272   end_specialization ();
8273   /* We're done with this parameter list.  */
8274   --parser->num_template_parameter_lists;
8275 }
8276
8277 /* Parse a type-specifier.
8278
8279    type-specifier:
8280      simple-type-specifier
8281      class-specifier
8282      enum-specifier
8283      elaborated-type-specifier
8284      cv-qualifier
8285
8286    GNU Extension:
8287
8288    type-specifier:
8289      __complex__
8290
8291    Returns a representation of the type-specifier.  If the
8292    type-specifier is a keyword (like `int' or `const', or
8293    `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8294    For a class-specifier, enum-specifier, or elaborated-type-specifier
8295    a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8296
8297    If IS_FRIEND is TRUE then this type-specifier is being declared a
8298    `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
8299    appearing in a decl-specifier-seq.
8300
8301    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8302    class-specifier, enum-specifier, or elaborated-type-specifier, then
8303    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
8304    if a type is declared; 2 if it is defined.  Otherwise, it is set to
8305    zero.
8306
8307    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8308    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
8309    is set to FALSE.  */
8310
8311 static tree
8312 cp_parser_type_specifier (cp_parser* parser, 
8313                           cp_parser_flags flags, 
8314                           bool is_friend,
8315                           bool is_declaration,
8316                           int* declares_class_or_enum,
8317                           bool* is_cv_qualifier)
8318 {
8319   tree type_spec = NULL_TREE;
8320   cp_token *token;
8321   enum rid keyword;
8322
8323   /* Assume this type-specifier does not declare a new type.  */
8324   if (declares_class_or_enum)
8325     *declares_class_or_enum = false;
8326   /* And that it does not specify a cv-qualifier.  */
8327   if (is_cv_qualifier)
8328     *is_cv_qualifier = false;
8329   /* Peek at the next token.  */
8330   token = cp_lexer_peek_token (parser->lexer);
8331
8332   /* If we're looking at a keyword, we can use that to guide the
8333      production we choose.  */
8334   keyword = token->keyword;
8335   switch (keyword)
8336     {
8337       /* Any of these indicate either a class-specifier, or an
8338          elaborated-type-specifier.  */
8339     case RID_CLASS:
8340     case RID_STRUCT:
8341     case RID_UNION:
8342     case RID_ENUM:
8343       /* Parse tentatively so that we can back up if we don't find a
8344          class-specifier or enum-specifier.  */
8345       cp_parser_parse_tentatively (parser);
8346       /* Look for the class-specifier or enum-specifier.  */
8347       if (keyword == RID_ENUM)
8348         type_spec = cp_parser_enum_specifier (parser);
8349       else
8350         type_spec = cp_parser_class_specifier (parser);
8351
8352       /* If that worked, we're done.  */
8353       if (cp_parser_parse_definitely (parser))
8354         {
8355           if (declares_class_or_enum)
8356             *declares_class_or_enum = 2;
8357           return type_spec;
8358         }
8359
8360       /* Fall through.  */
8361
8362     case RID_TYPENAME:
8363       /* Look for an elaborated-type-specifier.  */
8364       type_spec = cp_parser_elaborated_type_specifier (parser,
8365                                                        is_friend,
8366                                                        is_declaration);
8367       /* We're declaring a class or enum -- unless we're using
8368          `typename'.  */
8369       if (declares_class_or_enum && keyword != RID_TYPENAME)
8370         *declares_class_or_enum = 1;
8371       return type_spec;
8372
8373     case RID_CONST:
8374     case RID_VOLATILE:
8375     case RID_RESTRICT:
8376       type_spec = cp_parser_cv_qualifier_opt (parser);
8377       /* Even though we call a routine that looks for an optional
8378          qualifier, we know that there should be one.  */
8379       my_friendly_assert (type_spec != NULL, 20000328);
8380       /* This type-specifier was a cv-qualified.  */
8381       if (is_cv_qualifier)
8382         *is_cv_qualifier = true;
8383
8384       return type_spec;
8385
8386     case RID_COMPLEX:
8387       /* The `__complex__' keyword is a GNU extension.  */
8388       return cp_lexer_consume_token (parser->lexer)->value;
8389
8390     default:
8391       break;
8392     }
8393
8394   /* If we do not already have a type-specifier, assume we are looking
8395      at a simple-type-specifier.  */
8396   type_spec = cp_parser_simple_type_specifier (parser, flags, 
8397                                                /*identifier_p=*/true);
8398
8399   /* If we didn't find a type-specifier, and a type-specifier was not
8400      optional in this context, issue an error message.  */
8401   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8402     {
8403       cp_parser_error (parser, "expected type specifier");
8404       return error_mark_node;
8405     }
8406
8407   return type_spec;
8408 }
8409
8410 /* Parse a simple-type-specifier.
8411
8412    simple-type-specifier:
8413      :: [opt] nested-name-specifier [opt] type-name
8414      :: [opt] nested-name-specifier template template-id
8415      char
8416      wchar_t
8417      bool
8418      short
8419      int
8420      long
8421      signed
8422      unsigned
8423      float
8424      double
8425      void  
8426
8427    GNU Extension:
8428
8429    simple-type-specifier:
8430      __typeof__ unary-expression
8431      __typeof__ ( type-id )
8432
8433    For the various keywords, the value returned is simply the
8434    TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8435    For the first two productions, and if IDENTIFIER_P is false, the
8436    value returned is the indicated TYPE_DECL.  */
8437
8438 static tree
8439 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8440                                  bool identifier_p)
8441 {
8442   tree type = NULL_TREE;
8443   cp_token *token;
8444
8445   /* Peek at the next token.  */
8446   token = cp_lexer_peek_token (parser->lexer);
8447
8448   /* If we're looking at a keyword, things are easy.  */
8449   switch (token->keyword)
8450     {
8451     case RID_CHAR:
8452       type = char_type_node;
8453       break;
8454     case RID_WCHAR:
8455       type = wchar_type_node;
8456       break;
8457     case RID_BOOL:
8458       type = boolean_type_node;
8459       break;
8460     case RID_SHORT:
8461       type = short_integer_type_node;
8462       break;
8463     case RID_INT:
8464       type = integer_type_node;
8465       break;
8466     case RID_LONG:
8467       type = long_integer_type_node;
8468       break;
8469     case RID_SIGNED:
8470       type = integer_type_node;
8471       break;
8472     case RID_UNSIGNED:
8473       type = unsigned_type_node;
8474       break;
8475     case RID_FLOAT:
8476       type = float_type_node;
8477       break;
8478     case RID_DOUBLE:
8479       type = double_type_node;
8480       break;
8481     case RID_VOID:
8482       type = void_type_node;
8483       break;
8484
8485     case RID_TYPEOF:
8486       {
8487         tree operand;
8488
8489         /* Consume the `typeof' token.  */
8490         cp_lexer_consume_token (parser->lexer);
8491         /* Parse the operand to `typeof'.  */
8492         operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8493         /* If it is not already a TYPE, take its type.  */
8494         if (!TYPE_P (operand))
8495           operand = finish_typeof (operand);
8496
8497         return operand;
8498       }
8499
8500     default:
8501       break;
8502     }
8503
8504   /* If the type-specifier was for a built-in type, we're done.  */
8505   if (type)
8506     {
8507       tree id;
8508
8509       /* Consume the token.  */
8510       id = cp_lexer_consume_token (parser->lexer)->value;
8511       return identifier_p ? id : TYPE_NAME (type);
8512     }
8513
8514   /* The type-specifier must be a user-defined type.  */
8515   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) 
8516     {
8517       /* Don't gobble tokens or issue error messages if this is an
8518          optional type-specifier.  */
8519       if (flags & CP_PARSER_FLAGS_OPTIONAL)
8520         cp_parser_parse_tentatively (parser);
8521
8522       /* Look for the optional `::' operator.  */
8523       cp_parser_global_scope_opt (parser,
8524                                   /*current_scope_valid_p=*/false);
8525       /* Look for the nested-name specifier.  */
8526       cp_parser_nested_name_specifier_opt (parser,
8527                                            /*typename_keyword_p=*/false,
8528                                            /*check_dependency_p=*/true,
8529                                            /*type_p=*/false,
8530                                            /*is_declaration=*/false);
8531       /* If we have seen a nested-name-specifier, and the next token
8532          is `template', then we are using the template-id production.  */
8533       if (parser->scope 
8534           && cp_parser_optional_template_keyword (parser))
8535         {
8536           /* Look for the template-id.  */
8537           type = cp_parser_template_id (parser, 
8538                                         /*template_keyword_p=*/true,
8539                                         /*check_dependency_p=*/true,
8540                                         /*is_declaration=*/false);
8541           /* If the template-id did not name a type, we are out of
8542              luck.  */
8543           if (TREE_CODE (type) != TYPE_DECL)
8544             {
8545               cp_parser_error (parser, "expected template-id for type");
8546               type = NULL_TREE;
8547             }
8548         }
8549       /* Otherwise, look for a type-name.  */
8550       else
8551         {
8552           type = cp_parser_type_name (parser);
8553           if (type == error_mark_node)
8554             type = NULL_TREE;
8555         }
8556
8557       /* If it didn't work out, we don't have a TYPE.  */
8558       if ((flags & CP_PARSER_FLAGS_OPTIONAL) 
8559           && !cp_parser_parse_definitely (parser))
8560         type = NULL_TREE;
8561     }
8562
8563   /* If we didn't get a type-name, issue an error message.  */
8564   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8565     {
8566       cp_parser_error (parser, "expected type-name");
8567       return error_mark_node;
8568     }
8569
8570   /* There is no valid C++ program where a non-template type is
8571      followed by a "<".  That usually indicates that the user thought
8572      that the type was a template.  */
8573   if (type)
8574     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8575
8576   return type;
8577 }
8578
8579 /* Parse a type-name.
8580
8581    type-name:
8582      class-name
8583      enum-name
8584      typedef-name  
8585
8586    enum-name:
8587      identifier
8588
8589    typedef-name:
8590      identifier 
8591
8592    Returns a TYPE_DECL for the the type.  */
8593
8594 static tree
8595 cp_parser_type_name (cp_parser* parser)
8596 {
8597   tree type_decl;
8598   tree identifier;
8599
8600   /* We can't know yet whether it is a class-name or not.  */
8601   cp_parser_parse_tentatively (parser);
8602   /* Try a class-name.  */
8603   type_decl = cp_parser_class_name (parser, 
8604                                     /*typename_keyword_p=*/false,
8605                                     /*template_keyword_p=*/false,
8606                                     /*type_p=*/false,
8607                                     /*check_dependency_p=*/true,
8608                                     /*class_head_p=*/false,
8609                                     /*is_declaration=*/false);
8610   /* If it's not a class-name, keep looking.  */
8611   if (!cp_parser_parse_definitely (parser))
8612     {
8613       /* It must be a typedef-name or an enum-name.  */
8614       identifier = cp_parser_identifier (parser);
8615       if (identifier == error_mark_node)
8616         return error_mark_node;
8617       
8618       /* Look up the type-name.  */
8619       type_decl = cp_parser_lookup_name_simple (parser, identifier);
8620       /* Issue an error if we did not find a type-name.  */
8621       if (TREE_CODE (type_decl) != TYPE_DECL)
8622         {
8623           cp_parser_error (parser, "expected type-name");
8624           type_decl = error_mark_node;
8625         }
8626       /* Remember that the name was used in the definition of the
8627          current class so that we can check later to see if the
8628          meaning would have been different after the class was
8629          entirely defined.  */
8630       else if (type_decl != error_mark_node
8631                && !parser->scope)
8632         maybe_note_name_used_in_class (identifier, type_decl);
8633     }
8634   
8635   return type_decl;
8636 }
8637
8638
8639 /* Parse an elaborated-type-specifier.  Note that the grammar given
8640    here incorporates the resolution to DR68.
8641
8642    elaborated-type-specifier:
8643      class-key :: [opt] nested-name-specifier [opt] identifier
8644      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8645      enum :: [opt] nested-name-specifier [opt] identifier
8646      typename :: [opt] nested-name-specifier identifier
8647      typename :: [opt] nested-name-specifier template [opt] 
8648        template-id 
8649
8650    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8651    declared `friend'.  If IS_DECLARATION is TRUE, then this
8652    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8653    something is being declared.
8654
8655    Returns the TYPE specified.  */
8656
8657 static tree
8658 cp_parser_elaborated_type_specifier (cp_parser* parser, 
8659                                      bool is_friend, 
8660                                      bool is_declaration)
8661 {
8662   enum tag_types tag_type;
8663   tree identifier;
8664   tree type = NULL_TREE;
8665
8666   /* See if we're looking at the `enum' keyword.  */
8667   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8668     {
8669       /* Consume the `enum' token.  */
8670       cp_lexer_consume_token (parser->lexer);
8671       /* Remember that it's an enumeration type.  */
8672       tag_type = enum_type;
8673     }
8674   /* Or, it might be `typename'.  */
8675   else if (cp_lexer_next_token_is_keyword (parser->lexer,
8676                                            RID_TYPENAME))
8677     {
8678       /* Consume the `typename' token.  */
8679       cp_lexer_consume_token (parser->lexer);
8680       /* Remember that it's a `typename' type.  */
8681       tag_type = typename_type;
8682       /* The `typename' keyword is only allowed in templates.  */
8683       if (!processing_template_decl)
8684         pedwarn ("using `typename' outside of template");
8685     }
8686   /* Otherwise it must be a class-key.  */
8687   else
8688     {
8689       tag_type = cp_parser_class_key (parser);
8690       if (tag_type == none_type)
8691         return error_mark_node;
8692     }
8693
8694   /* Look for the `::' operator.  */
8695   cp_parser_global_scope_opt (parser, 
8696                               /*current_scope_valid_p=*/false);
8697   /* Look for the nested-name-specifier.  */
8698   if (tag_type == typename_type)
8699     {
8700       if (cp_parser_nested_name_specifier (parser,
8701                                            /*typename_keyword_p=*/true,
8702                                            /*check_dependency_p=*/true,
8703                                            /*type_p=*/true,
8704                                            is_declaration) 
8705           == error_mark_node)
8706         return error_mark_node;
8707     }
8708   else
8709     /* Even though `typename' is not present, the proposed resolution
8710        to Core Issue 180 says that in `class A<T>::B', `B' should be
8711        considered a type-name, even if `A<T>' is dependent.  */
8712     cp_parser_nested_name_specifier_opt (parser,
8713                                          /*typename_keyword_p=*/true,
8714                                          /*check_dependency_p=*/true,
8715                                          /*type_p=*/true,
8716                                          is_declaration);
8717   /* For everything but enumeration types, consider a template-id.  */
8718   if (tag_type != enum_type)
8719     {
8720       bool template_p = false;
8721       tree decl;
8722
8723       /* Allow the `template' keyword.  */
8724       template_p = cp_parser_optional_template_keyword (parser);
8725       /* If we didn't see `template', we don't know if there's a
8726          template-id or not.  */
8727       if (!template_p)
8728         cp_parser_parse_tentatively (parser);
8729       /* Parse the template-id.  */
8730       decl = cp_parser_template_id (parser, template_p,
8731                                     /*check_dependency_p=*/true,
8732                                     is_declaration);
8733       /* If we didn't find a template-id, look for an ordinary
8734          identifier.  */
8735       if (!template_p && !cp_parser_parse_definitely (parser))
8736         ;
8737       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8738          in effect, then we must assume that, upon instantiation, the
8739          template will correspond to a class.  */
8740       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8741                && tag_type == typename_type)
8742         type = make_typename_type (parser->scope, decl,
8743                                    /*complain=*/1);
8744       else 
8745         type = TREE_TYPE (decl);
8746     }
8747
8748   /* For an enumeration type, consider only a plain identifier.  */
8749   if (!type)
8750     {
8751       identifier = cp_parser_identifier (parser);
8752
8753       if (identifier == error_mark_node)
8754         {
8755           parser->scope = NULL_TREE;
8756           return error_mark_node;
8757         }
8758
8759       /* For a `typename', we needn't call xref_tag.  */
8760       if (tag_type == typename_type)
8761         return make_typename_type (parser->scope, identifier, 
8762                                    /*complain=*/1);
8763       /* Look up a qualified name in the usual way.  */
8764       if (parser->scope)
8765         {
8766           tree decl;
8767
8768           /* In an elaborated-type-specifier, names are assumed to name
8769              types, so we set IS_TYPE to TRUE when calling
8770              cp_parser_lookup_name.  */
8771           decl = cp_parser_lookup_name (parser, identifier, 
8772                                         /*is_type=*/true,
8773                                         /*is_namespace=*/false,
8774                                         /*check_dependency=*/true);
8775
8776           /* If we are parsing friend declaration, DECL may be a
8777              TEMPLATE_DECL tree node here.  However, we need to check
8778              whether this TEMPLATE_DECL results in valid code.  Consider
8779              the following example:
8780
8781                namespace N {
8782                  template <class T> class C {};
8783                }
8784                class X {
8785                  template <class T> friend class N::C; // #1, valid code
8786                };
8787                template <class T> class Y {
8788                  friend class N::C;                    // #2, invalid code
8789                };
8790
8791              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8792              name lookup of `N::C'.  We see that friend declaration must
8793              be template for the code to be valid.  Note that
8794              processing_template_decl does not work here since it is
8795              always 1 for the above two cases.  */
8796
8797           decl = (cp_parser_maybe_treat_template_as_class 
8798                   (decl, /*tag_name_p=*/is_friend
8799                          && parser->num_template_parameter_lists));
8800
8801           if (TREE_CODE (decl) != TYPE_DECL)
8802             {
8803               error ("expected type-name");
8804               return error_mark_node;
8805             }
8806
8807           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
8808             check_elaborated_type_specifier 
8809               (tag_type, decl,
8810                (parser->num_template_parameter_lists
8811                 || DECL_SELF_REFERENCE_P (decl)));
8812
8813           type = TREE_TYPE (decl);
8814         }
8815       else 
8816         {
8817           /* An elaborated-type-specifier sometimes introduces a new type and
8818              sometimes names an existing type.  Normally, the rule is that it
8819              introduces a new type only if there is not an existing type of
8820              the same name already in scope.  For example, given:
8821
8822                struct S {};
8823                void f() { struct S s; }
8824
8825              the `struct S' in the body of `f' is the same `struct S' as in
8826              the global scope; the existing definition is used.  However, if
8827              there were no global declaration, this would introduce a new 
8828              local class named `S'.
8829
8830              An exception to this rule applies to the following code:
8831
8832                namespace N { struct S; }
8833
8834              Here, the elaborated-type-specifier names a new type
8835              unconditionally; even if there is already an `S' in the
8836              containing scope this declaration names a new type.
8837              This exception only applies if the elaborated-type-specifier
8838              forms the complete declaration:
8839
8840                [class.name] 
8841
8842                A declaration consisting solely of `class-key identifier ;' is
8843                either a redeclaration of the name in the current scope or a
8844                forward declaration of the identifier as a class name.  It
8845                introduces the name into the current scope.
8846
8847              We are in this situation precisely when the next token is a `;'.
8848
8849              An exception to the exception is that a `friend' declaration does
8850              *not* name a new type; i.e., given:
8851
8852                struct S { friend struct T; };
8853
8854              `T' is not a new type in the scope of `S'.  
8855
8856              Also, `new struct S' or `sizeof (struct S)' never results in the
8857              definition of a new type; a new type can only be declared in a
8858              declaration context.  */
8859
8860           type = xref_tag (tag_type, identifier, 
8861                            /*attributes=*/NULL_TREE,
8862                            (is_friend 
8863                             || !is_declaration
8864                             || cp_lexer_next_token_is_not (parser->lexer, 
8865                                                            CPP_SEMICOLON)),
8866                            parser->num_template_parameter_lists);
8867         }
8868     }
8869   if (tag_type != enum_type)
8870     cp_parser_check_class_key (tag_type, type);
8871
8872   /* A "<" cannot follow an elaborated type specifier.  If that
8873      happens, the user was probably trying to form a template-id.  */
8874   cp_parser_check_for_invalid_template_id (parser, type);
8875
8876   return type;
8877 }
8878
8879 /* Parse an enum-specifier.
8880
8881    enum-specifier:
8882      enum identifier [opt] { enumerator-list [opt] }
8883
8884    Returns an ENUM_TYPE representing the enumeration.  */
8885
8886 static tree
8887 cp_parser_enum_specifier (cp_parser* parser)
8888 {
8889   cp_token *token;
8890   tree identifier = NULL_TREE;
8891   tree type;
8892
8893   /* Look for the `enum' keyword.  */
8894   if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
8895     return error_mark_node;
8896   /* Peek at the next token.  */
8897   token = cp_lexer_peek_token (parser->lexer);
8898
8899   /* See if it is an identifier.  */
8900   if (token->type == CPP_NAME)
8901     identifier = cp_parser_identifier (parser);
8902
8903   /* Look for the `{'.  */
8904   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8905     return error_mark_node;
8906
8907   /* At this point, we're going ahead with the enum-specifier, even
8908      if some other problem occurs.  */
8909   cp_parser_commit_to_tentative_parse (parser);
8910
8911   /* Issue an error message if type-definitions are forbidden here.  */
8912   cp_parser_check_type_definition (parser);
8913
8914   /* Create the new type.  */
8915   type = start_enum (identifier ? identifier : make_anon_name ());
8916
8917   /* Peek at the next token.  */
8918   token = cp_lexer_peek_token (parser->lexer);
8919   /* If it's not a `}', then there are some enumerators.  */
8920   if (token->type != CPP_CLOSE_BRACE)
8921     cp_parser_enumerator_list (parser, type);
8922   /* Look for the `}'.  */
8923   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8924
8925   /* Finish up the enumeration.  */
8926   finish_enum (type);
8927
8928   return type;
8929 }
8930
8931 /* Parse an enumerator-list.  The enumerators all have the indicated
8932    TYPE.  
8933
8934    enumerator-list:
8935      enumerator-definition
8936      enumerator-list , enumerator-definition  */
8937
8938 static void
8939 cp_parser_enumerator_list (cp_parser* parser, tree type)
8940 {
8941   while (true)
8942     {
8943       cp_token *token;
8944
8945       /* Parse an enumerator-definition.  */
8946       cp_parser_enumerator_definition (parser, type);
8947       /* Peek at the next token.  */
8948       token = cp_lexer_peek_token (parser->lexer);
8949       /* If it's not a `,', then we've reached the end of the 
8950          list.  */
8951       if (token->type != CPP_COMMA)
8952         break;
8953       /* Otherwise, consume the `,' and keep going.  */
8954       cp_lexer_consume_token (parser->lexer);
8955       /* If the next token is a `}', there is a trailing comma.  */
8956       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8957         {
8958           if (pedantic && !in_system_header)
8959             pedwarn ("comma at end of enumerator list");
8960           break;
8961         }
8962     }
8963 }
8964
8965 /* Parse an enumerator-definition.  The enumerator has the indicated
8966    TYPE.
8967
8968    enumerator-definition:
8969      enumerator
8970      enumerator = constant-expression
8971     
8972    enumerator:
8973      identifier  */
8974
8975 static void
8976 cp_parser_enumerator_definition (cp_parser* parser, tree type)
8977 {
8978   cp_token *token;
8979   tree identifier;
8980   tree value;
8981
8982   /* Look for the identifier.  */
8983   identifier = cp_parser_identifier (parser);
8984   if (identifier == error_mark_node)
8985     return;
8986   
8987   /* Peek at the next token.  */
8988   token = cp_lexer_peek_token (parser->lexer);
8989   /* If it's an `=', then there's an explicit value.  */
8990   if (token->type == CPP_EQ)
8991     {
8992       /* Consume the `=' token.  */
8993       cp_lexer_consume_token (parser->lexer);
8994       /* Parse the value.  */
8995       value = cp_parser_constant_expression (parser, 
8996                                              /*allow_non_constant_p=*/false,
8997                                              NULL);
8998     }
8999   else
9000     value = NULL_TREE;
9001
9002   /* Create the enumerator.  */
9003   build_enumerator (identifier, value, type);
9004 }
9005
9006 /* Parse a namespace-name.
9007
9008    namespace-name:
9009      original-namespace-name
9010      namespace-alias
9011
9012    Returns the NAMESPACE_DECL for the namespace.  */
9013
9014 static tree
9015 cp_parser_namespace_name (cp_parser* parser)
9016 {
9017   tree identifier;
9018   tree namespace_decl;
9019
9020   /* Get the name of the namespace.  */
9021   identifier = cp_parser_identifier (parser);
9022   if (identifier == error_mark_node)
9023     return error_mark_node;
9024
9025   /* Look up the identifier in the currently active scope.  Look only
9026      for namespaces, due to:
9027
9028        [basic.lookup.udir]
9029
9030        When looking up a namespace-name in a using-directive or alias
9031        definition, only namespace names are considered.  
9032
9033      And:
9034
9035        [basic.lookup.qual]
9036
9037        During the lookup of a name preceding the :: scope resolution
9038        operator, object, function, and enumerator names are ignored.  
9039
9040      (Note that cp_parser_class_or_namespace_name only calls this
9041      function if the token after the name is the scope resolution
9042      operator.)  */
9043   namespace_decl = cp_parser_lookup_name (parser, identifier,
9044                                           /*is_type=*/false,
9045                                           /*is_namespace=*/true,
9046                                           /*check_dependency=*/true);
9047   /* If it's not a namespace, issue an error.  */
9048   if (namespace_decl == error_mark_node
9049       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9050     {
9051       cp_parser_error (parser, "expected namespace-name");
9052       namespace_decl = error_mark_node;
9053     }
9054   
9055   return namespace_decl;
9056 }
9057
9058 /* Parse a namespace-definition.
9059
9060    namespace-definition:
9061      named-namespace-definition
9062      unnamed-namespace-definition  
9063
9064    named-namespace-definition:
9065      original-namespace-definition
9066      extension-namespace-definition
9067
9068    original-namespace-definition:
9069      namespace identifier { namespace-body }
9070    
9071    extension-namespace-definition:
9072      namespace original-namespace-name { namespace-body }
9073  
9074    unnamed-namespace-definition:
9075      namespace { namespace-body } */
9076
9077 static void
9078 cp_parser_namespace_definition (cp_parser* parser)
9079 {
9080   tree identifier;
9081
9082   /* Look for the `namespace' keyword.  */
9083   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9084
9085   /* Get the name of the namespace.  We do not attempt to distinguish
9086      between an original-namespace-definition and an
9087      extension-namespace-definition at this point.  The semantic
9088      analysis routines are responsible for that.  */
9089   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9090     identifier = cp_parser_identifier (parser);
9091   else
9092     identifier = NULL_TREE;
9093
9094   /* Look for the `{' to start the namespace.  */
9095   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9096   /* Start the namespace.  */
9097   push_namespace (identifier);
9098   /* Parse the body of the namespace.  */
9099   cp_parser_namespace_body (parser);
9100   /* Finish the namespace.  */
9101   pop_namespace ();
9102   /* Look for the final `}'.  */
9103   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9104 }
9105
9106 /* Parse a namespace-body.
9107
9108    namespace-body:
9109      declaration-seq [opt]  */
9110
9111 static void
9112 cp_parser_namespace_body (cp_parser* parser)
9113 {
9114   cp_parser_declaration_seq_opt (parser);
9115 }
9116
9117 /* Parse a namespace-alias-definition.
9118
9119    namespace-alias-definition:
9120      namespace identifier = qualified-namespace-specifier ;  */
9121
9122 static void
9123 cp_parser_namespace_alias_definition (cp_parser* parser)
9124 {
9125   tree identifier;
9126   tree namespace_specifier;
9127
9128   /* Look for the `namespace' keyword.  */
9129   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9130   /* Look for the identifier.  */
9131   identifier = cp_parser_identifier (parser);
9132   if (identifier == error_mark_node)
9133     return;
9134   /* Look for the `=' token.  */
9135   cp_parser_require (parser, CPP_EQ, "`='");
9136   /* Look for the qualified-namespace-specifier.  */
9137   namespace_specifier 
9138     = cp_parser_qualified_namespace_specifier (parser);
9139   /* Look for the `;' token.  */
9140   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9141
9142   /* Register the alias in the symbol table.  */
9143   do_namespace_alias (identifier, namespace_specifier);
9144 }
9145
9146 /* Parse a qualified-namespace-specifier.
9147
9148    qualified-namespace-specifier:
9149      :: [opt] nested-name-specifier [opt] namespace-name
9150
9151    Returns a NAMESPACE_DECL corresponding to the specified
9152    namespace.  */
9153
9154 static tree
9155 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9156 {
9157   /* Look for the optional `::'.  */
9158   cp_parser_global_scope_opt (parser, 
9159                               /*current_scope_valid_p=*/false);
9160
9161   /* Look for the optional nested-name-specifier.  */
9162   cp_parser_nested_name_specifier_opt (parser,
9163                                        /*typename_keyword_p=*/false,
9164                                        /*check_dependency_p=*/true,
9165                                        /*type_p=*/false,
9166                                        /*is_declaration=*/true);
9167
9168   return cp_parser_namespace_name (parser);
9169 }
9170
9171 /* Parse a using-declaration.
9172
9173    using-declaration:
9174      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9175      using :: unqualified-id ;  */
9176
9177 static void
9178 cp_parser_using_declaration (cp_parser* parser)
9179 {
9180   cp_token *token;
9181   bool typename_p = false;
9182   bool global_scope_p;
9183   tree decl;
9184   tree identifier;
9185   tree scope;
9186
9187   /* Look for the `using' keyword.  */
9188   cp_parser_require_keyword (parser, RID_USING, "`using'");
9189   
9190   /* Peek at the next token.  */
9191   token = cp_lexer_peek_token (parser->lexer);
9192   /* See if it's `typename'.  */
9193   if (token->keyword == RID_TYPENAME)
9194     {
9195       /* Remember that we've seen it.  */
9196       typename_p = true;
9197       /* Consume the `typename' token.  */
9198       cp_lexer_consume_token (parser->lexer);
9199     }
9200
9201   /* Look for the optional global scope qualification.  */
9202   global_scope_p 
9203     = (cp_parser_global_scope_opt (parser,
9204                                    /*current_scope_valid_p=*/false) 
9205        != NULL_TREE);
9206
9207   /* If we saw `typename', or didn't see `::', then there must be a
9208      nested-name-specifier present.  */
9209   if (typename_p || !global_scope_p)
9210     cp_parser_nested_name_specifier (parser, typename_p, 
9211                                      /*check_dependency_p=*/true,
9212                                      /*type_p=*/false,
9213                                      /*is_declaration=*/true);
9214   /* Otherwise, we could be in either of the two productions.  In that
9215      case, treat the nested-name-specifier as optional.  */
9216   else
9217     cp_parser_nested_name_specifier_opt (parser,
9218                                          /*typename_keyword_p=*/false,
9219                                          /*check_dependency_p=*/true,
9220                                          /*type_p=*/false,
9221                                          /*is_declaration=*/true);
9222
9223   /* Parse the unqualified-id.  */
9224   identifier = cp_parser_unqualified_id (parser, 
9225                                          /*template_keyword_p=*/false,
9226                                          /*check_dependency_p=*/true,
9227                                          /*declarator_p=*/true);
9228
9229   /* The function we call to handle a using-declaration is different
9230      depending on what scope we are in.  */
9231   if (identifier == error_mark_node)
9232     ;
9233   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9234            && TREE_CODE (identifier) != BIT_NOT_EXPR)
9235     /* [namespace.udecl]
9236
9237        A using declaration shall not name a template-id.  */
9238     error ("a template-id may not appear in a using-declaration");
9239   else
9240     {
9241       scope = current_scope ();
9242       if (scope && TYPE_P (scope))
9243         {
9244           /* Create the USING_DECL.  */
9245           decl = do_class_using_decl (build_nt (SCOPE_REF,
9246                                                 parser->scope,
9247                                                 identifier));
9248           /* Add it to the list of members in this class.  */
9249           finish_member_declaration (decl);
9250         }
9251       else
9252         {
9253           decl = cp_parser_lookup_name_simple (parser, identifier);
9254           if (decl == error_mark_node)
9255             {
9256               if (parser->scope && parser->scope != global_namespace)
9257                 error ("`%D::%D' has not been declared", 
9258                        parser->scope, identifier);
9259               else
9260                 error ("`::%D' has not been declared", identifier);
9261             }
9262           else if (scope)
9263             do_local_using_decl (decl);
9264           else
9265             do_toplevel_using_decl (decl);
9266         }
9267     }
9268
9269   /* Look for the final `;'.  */
9270   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9271 }
9272
9273 /* Parse a using-directive.  
9274  
9275    using-directive:
9276      using namespace :: [opt] nested-name-specifier [opt]
9277        namespace-name ;  */
9278
9279 static void
9280 cp_parser_using_directive (cp_parser* parser)
9281 {
9282   tree namespace_decl;
9283   tree attribs;
9284
9285   /* Look for the `using' keyword.  */
9286   cp_parser_require_keyword (parser, RID_USING, "`using'");
9287   /* And the `namespace' keyword.  */
9288   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9289   /* Look for the optional `::' operator.  */
9290   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9291   /* And the optional nested-name-specifier.  */
9292   cp_parser_nested_name_specifier_opt (parser,
9293                                        /*typename_keyword_p=*/false,
9294                                        /*check_dependency_p=*/true,
9295                                        /*type_p=*/false,
9296                                        /*is_declaration=*/true);
9297   /* Get the namespace being used.  */
9298   namespace_decl = cp_parser_namespace_name (parser);
9299   /* And any specified attributes.  */
9300   attribs = cp_parser_attributes_opt (parser);
9301   /* Update the symbol table.  */
9302   parse_using_directive (namespace_decl, attribs);
9303   /* Look for the final `;'.  */
9304   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9305 }
9306
9307 /* Parse an asm-definition.
9308
9309    asm-definition:
9310      asm ( string-literal ) ;  
9311
9312    GNU Extension:
9313
9314    asm-definition:
9315      asm volatile [opt] ( string-literal ) ;
9316      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9317      asm volatile [opt] ( string-literal : asm-operand-list [opt]
9318                           : asm-operand-list [opt] ) ;
9319      asm volatile [opt] ( string-literal : asm-operand-list [opt] 
9320                           : asm-operand-list [opt] 
9321                           : asm-operand-list [opt] ) ;  */
9322
9323 static void
9324 cp_parser_asm_definition (cp_parser* parser)
9325 {
9326   cp_token *token;
9327   tree string;
9328   tree outputs = NULL_TREE;
9329   tree inputs = NULL_TREE;
9330   tree clobbers = NULL_TREE;
9331   tree asm_stmt;
9332   bool volatile_p = false;
9333   bool extended_p = false;
9334
9335   /* Look for the `asm' keyword.  */
9336   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9337   /* See if the next token is `volatile'.  */
9338   if (cp_parser_allow_gnu_extensions_p (parser)
9339       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9340     {
9341       /* Remember that we saw the `volatile' keyword.  */
9342       volatile_p = true;
9343       /* Consume the token.  */
9344       cp_lexer_consume_token (parser->lexer);
9345     }
9346   /* Look for the opening `('.  */
9347   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9348   /* Look for the string.  */
9349   token = cp_parser_require (parser, CPP_STRING, "asm body");
9350   if (!token)
9351     return;
9352   string = token->value;
9353   /* If we're allowing GNU extensions, check for the extended assembly
9354      syntax.  Unfortunately, the `:' tokens need not be separated by 
9355      a space in C, and so, for compatibility, we tolerate that here
9356      too.  Doing that means that we have to treat the `::' operator as
9357      two `:' tokens.  */
9358   if (cp_parser_allow_gnu_extensions_p (parser)
9359       && at_function_scope_p ()
9360       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9361           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9362     {
9363       bool inputs_p = false;
9364       bool clobbers_p = false;
9365
9366       /* The extended syntax was used.  */
9367       extended_p = true;
9368
9369       /* Look for outputs.  */
9370       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9371         {
9372           /* Consume the `:'.  */
9373           cp_lexer_consume_token (parser->lexer);
9374           /* Parse the output-operands.  */
9375           if (cp_lexer_next_token_is_not (parser->lexer, 
9376                                           CPP_COLON)
9377               && cp_lexer_next_token_is_not (parser->lexer,
9378                                              CPP_SCOPE)
9379               && cp_lexer_next_token_is_not (parser->lexer,
9380                                              CPP_CLOSE_PAREN))
9381             outputs = cp_parser_asm_operand_list (parser);
9382         }
9383       /* If the next token is `::', there are no outputs, and the
9384          next token is the beginning of the inputs.  */
9385       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9386         {
9387           /* Consume the `::' token.  */
9388           cp_lexer_consume_token (parser->lexer);
9389           /* The inputs are coming next.  */
9390           inputs_p = true;
9391         }
9392
9393       /* Look for inputs.  */
9394       if (inputs_p
9395           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9396         {
9397           if (!inputs_p)
9398             /* Consume the `:'.  */
9399             cp_lexer_consume_token (parser->lexer);
9400           /* Parse the output-operands.  */
9401           if (cp_lexer_next_token_is_not (parser->lexer, 
9402                                           CPP_COLON)
9403               && cp_lexer_next_token_is_not (parser->lexer,
9404                                              CPP_SCOPE)
9405               && cp_lexer_next_token_is_not (parser->lexer,
9406                                              CPP_CLOSE_PAREN))
9407             inputs = cp_parser_asm_operand_list (parser);
9408         }
9409       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9410         /* The clobbers are coming next.  */
9411         clobbers_p = true;
9412
9413       /* Look for clobbers.  */
9414       if (clobbers_p 
9415           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9416         {
9417           if (!clobbers_p)
9418             /* Consume the `:'.  */
9419             cp_lexer_consume_token (parser->lexer);
9420           /* Parse the clobbers.  */
9421           if (cp_lexer_next_token_is_not (parser->lexer,
9422                                           CPP_CLOSE_PAREN))
9423             clobbers = cp_parser_asm_clobber_list (parser);
9424         }
9425     }
9426   /* Look for the closing `)'.  */
9427   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9428     cp_parser_skip_to_closing_parenthesis (parser, true, false,
9429                                            /*consume_paren=*/true);
9430   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9431
9432   /* Create the ASM_STMT.  */
9433   if (at_function_scope_p ())
9434     {
9435       asm_stmt = 
9436         finish_asm_stmt (volatile_p 
9437                          ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9438                          string, outputs, inputs, clobbers);
9439       /* If the extended syntax was not used, mark the ASM_STMT.  */
9440       if (!extended_p)
9441         ASM_INPUT_P (asm_stmt) = 1;
9442     }
9443   else
9444     assemble_asm (string);
9445 }
9446
9447 /* Declarators [gram.dcl.decl] */
9448
9449 /* Parse an init-declarator.
9450
9451    init-declarator:
9452      declarator initializer [opt]
9453
9454    GNU Extension:
9455
9456    init-declarator:
9457      declarator asm-specification [opt] attributes [opt] initializer [opt]
9458
9459    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9460    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
9461    then this declarator appears in a class scope.  The new DECL created
9462    by this declarator is returned.
9463
9464    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9465    for a function-definition here as well.  If the declarator is a
9466    declarator for a function-definition, *FUNCTION_DEFINITION_P will
9467    be TRUE upon return.  By that point, the function-definition will
9468    have been completely parsed.
9469
9470    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9471    is FALSE.  */
9472
9473 static tree
9474 cp_parser_init_declarator (cp_parser* parser, 
9475                            tree decl_specifiers, 
9476                            tree prefix_attributes,
9477                            bool function_definition_allowed_p,
9478                            bool member_p,
9479                            int declares_class_or_enum,
9480                            bool* function_definition_p)
9481 {
9482   cp_token *token;
9483   tree declarator;
9484   tree attributes;
9485   tree asm_specification;
9486   tree initializer;
9487   tree decl = NULL_TREE;
9488   tree scope;
9489   bool is_initialized;
9490   bool is_parenthesized_init;
9491   bool is_non_constant_init;
9492   int ctor_dtor_or_conv_p;
9493   bool friend_p;
9494
9495   /* Assume that this is not the declarator for a function
9496      definition.  */
9497   if (function_definition_p)
9498     *function_definition_p = false;
9499
9500   /* Defer access checks while parsing the declarator; we cannot know
9501      what names are accessible until we know what is being 
9502      declared.  */
9503   resume_deferring_access_checks ();
9504
9505   /* Parse the declarator.  */
9506   declarator 
9507     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9508                             &ctor_dtor_or_conv_p);
9509   /* Gather up the deferred checks.  */
9510   stop_deferring_access_checks ();
9511
9512   /* If the DECLARATOR was erroneous, there's no need to go
9513      further.  */
9514   if (declarator == error_mark_node)
9515     return error_mark_node;
9516
9517   cp_parser_check_for_definition_in_return_type (declarator,
9518                                                  declares_class_or_enum);
9519
9520   /* Figure out what scope the entity declared by the DECLARATOR is
9521      located in.  `grokdeclarator' sometimes changes the scope, so
9522      we compute it now.  */
9523   scope = get_scope_of_declarator (declarator);
9524
9525   /* If we're allowing GNU extensions, look for an asm-specification
9526      and attributes.  */
9527   if (cp_parser_allow_gnu_extensions_p (parser))
9528     {
9529       /* Look for an asm-specification.  */
9530       asm_specification = cp_parser_asm_specification_opt (parser);
9531       /* And attributes.  */
9532       attributes = cp_parser_attributes_opt (parser);
9533     }
9534   else
9535     {
9536       asm_specification = NULL_TREE;
9537       attributes = NULL_TREE;
9538     }
9539
9540   /* Peek at the next token.  */
9541   token = cp_lexer_peek_token (parser->lexer);
9542   /* Check to see if the token indicates the start of a
9543      function-definition.  */
9544   if (cp_parser_token_starts_function_definition_p (token))
9545     {
9546       if (!function_definition_allowed_p)
9547         {
9548           /* If a function-definition should not appear here, issue an
9549              error message.  */
9550           cp_parser_error (parser,
9551                            "a function-definition is not allowed here");
9552           return error_mark_node;
9553         }
9554       else
9555         {
9556           /* Neither attributes nor an asm-specification are allowed
9557              on a function-definition.  */
9558           if (asm_specification)
9559             error ("an asm-specification is not allowed on a function-definition");
9560           if (attributes)
9561             error ("attributes are not allowed on a function-definition");
9562           /* This is a function-definition.  */
9563           *function_definition_p = true;
9564
9565           /* Parse the function definition.  */
9566           decl = (cp_parser_function_definition_from_specifiers_and_declarator
9567                   (parser, decl_specifiers, prefix_attributes, declarator));
9568
9569           return decl;
9570         }
9571     }
9572
9573   /* [dcl.dcl]
9574
9575      Only in function declarations for constructors, destructors, and
9576      type conversions can the decl-specifier-seq be omitted.  
9577
9578      We explicitly postpone this check past the point where we handle
9579      function-definitions because we tolerate function-definitions
9580      that are missing their return types in some modes.  */
9581   if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9582     {
9583       cp_parser_error (parser, 
9584                        "expected constructor, destructor, or type conversion");
9585       return error_mark_node;
9586     }
9587
9588   /* An `=' or an `(' indicates an initializer.  */
9589   is_initialized = (token->type == CPP_EQ 
9590                      || token->type == CPP_OPEN_PAREN);
9591   /* If the init-declarator isn't initialized and isn't followed by a
9592      `,' or `;', it's not a valid init-declarator.  */
9593   if (!is_initialized 
9594       && token->type != CPP_COMMA
9595       && token->type != CPP_SEMICOLON)
9596     {
9597       cp_parser_error (parser, "expected init-declarator");
9598       return error_mark_node;
9599     }
9600
9601   /* Because start_decl has side-effects, we should only call it if we
9602      know we're going ahead.  By this point, we know that we cannot
9603      possibly be looking at any other construct.  */
9604   cp_parser_commit_to_tentative_parse (parser);
9605
9606   /* Check to see whether or not this declaration is a friend.  */
9607   friend_p = cp_parser_friend_p (decl_specifiers);
9608
9609   /* Check that the number of template-parameter-lists is OK.  */
9610   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
9611     return error_mark_node;
9612
9613   /* Enter the newly declared entry in the symbol table.  If we're
9614      processing a declaration in a class-specifier, we wait until
9615      after processing the initializer.  */
9616   if (!member_p)
9617     {
9618       if (parser->in_unbraced_linkage_specification_p)
9619         {
9620           decl_specifiers = tree_cons (error_mark_node,
9621                                        get_identifier ("extern"),
9622                                        decl_specifiers);
9623           have_extern_spec = false;
9624         }
9625       decl = start_decl (declarator, decl_specifiers,
9626                          is_initialized, attributes, prefix_attributes);
9627     }
9628
9629   /* Enter the SCOPE.  That way unqualified names appearing in the
9630      initializer will be looked up in SCOPE.  */
9631   if (scope)
9632     push_scope (scope);
9633
9634   /* Perform deferred access control checks, now that we know in which
9635      SCOPE the declared entity resides.  */
9636   if (!member_p && decl) 
9637     {
9638       tree saved_current_function_decl = NULL_TREE;
9639
9640       /* If the entity being declared is a function, pretend that we
9641          are in its scope.  If it is a `friend', it may have access to
9642          things that would not otherwise be accessible.  */
9643       if (TREE_CODE (decl) == FUNCTION_DECL)
9644         {
9645           saved_current_function_decl = current_function_decl;
9646           current_function_decl = decl;
9647         }
9648         
9649       /* Perform the access control checks for the declarator and the
9650          the decl-specifiers.  */
9651       perform_deferred_access_checks ();
9652
9653       /* Restore the saved value.  */
9654       if (TREE_CODE (decl) == FUNCTION_DECL)
9655         current_function_decl = saved_current_function_decl;
9656     }
9657
9658   /* Parse the initializer.  */
9659   if (is_initialized)
9660     initializer = cp_parser_initializer (parser, 
9661                                          &is_parenthesized_init,
9662                                          &is_non_constant_init);
9663   else
9664     {
9665       initializer = NULL_TREE;
9666       is_parenthesized_init = false;
9667       is_non_constant_init = true;
9668     }
9669
9670   /* The old parser allows attributes to appear after a parenthesized
9671      initializer.  Mark Mitchell proposed removing this functionality
9672      on the GCC mailing lists on 2002-08-13.  This parser accepts the
9673      attributes -- but ignores them.  */
9674   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9675     if (cp_parser_attributes_opt (parser))
9676       warning ("attributes after parenthesized initializer ignored");
9677
9678   /* Leave the SCOPE, now that we have processed the initializer.  It
9679      is important to do this before calling cp_finish_decl because it
9680      makes decisions about whether to create DECL_STMTs or not based
9681      on the current scope.  */
9682   if (scope)
9683     pop_scope (scope);
9684
9685   /* For an in-class declaration, use `grokfield' to create the
9686      declaration.  */
9687   if (member_p)
9688     {
9689       decl = grokfield (declarator, decl_specifiers,
9690                         initializer, /*asmspec=*/NULL_TREE,
9691                         /*attributes=*/NULL_TREE);
9692       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9693         cp_parser_save_default_args (parser, decl);
9694     }
9695   
9696   /* Finish processing the declaration.  But, skip friend
9697      declarations.  */
9698   if (!friend_p && decl)
9699     cp_finish_decl (decl, 
9700                     initializer, 
9701                     asm_specification,
9702                     /* If the initializer is in parentheses, then this is
9703                        a direct-initialization, which means that an
9704                        `explicit' constructor is OK.  Otherwise, an
9705                        `explicit' constructor cannot be used.  */
9706                     ((is_parenthesized_init || !is_initialized)
9707                      ? 0 : LOOKUP_ONLYCONVERTING));
9708
9709   /* Remember whether or not variables were initialized by
9710      constant-expressions.  */
9711   if (decl && TREE_CODE (decl) == VAR_DECL 
9712       && is_initialized && !is_non_constant_init)
9713     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9714
9715   return decl;
9716 }
9717
9718 /* Parse a declarator.
9719    
9720    declarator:
9721      direct-declarator
9722      ptr-operator declarator  
9723
9724    abstract-declarator:
9725      ptr-operator abstract-declarator [opt]
9726      direct-abstract-declarator
9727
9728    GNU Extensions:
9729
9730    declarator:
9731      attributes [opt] direct-declarator
9732      attributes [opt] ptr-operator declarator  
9733
9734    abstract-declarator:
9735      attributes [opt] ptr-operator abstract-declarator [opt]
9736      attributes [opt] direct-abstract-declarator
9737      
9738    Returns a representation of the declarator.  If the declarator has
9739    the form `* declarator', then an INDIRECT_REF is returned, whose
9740    only operand is the sub-declarator.  Analogously, `& declarator' is
9741    represented as an ADDR_EXPR.  For `X::* declarator', a SCOPE_REF is
9742    used.  The first operand is the TYPE for `X'.  The second operand
9743    is an INDIRECT_REF whose operand is the sub-declarator.
9744
9745    Otherwise, the representation is as for a direct-declarator.
9746
9747    (It would be better to define a structure type to represent
9748    declarators, rather than abusing `tree' nodes to represent
9749    declarators.  That would be much clearer and save some memory.
9750    There is no reason for declarators to be garbage-collected, for
9751    example; they are created during parser and no longer needed after
9752    `grokdeclarator' has been called.)
9753
9754    For a ptr-operator that has the optional cv-qualifier-seq,
9755    cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9756    node.
9757
9758    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9759    detect constructor, destructor or conversion operators. It is set
9760    to -1 if the declarator is a name, and +1 if it is a
9761    function. Otherwise it is set to zero. Usually you just want to
9762    test for >0, but internally the negative value is used.
9763    
9764    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9765    a decl-specifier-seq unless it declares a constructor, destructor,
9766    or conversion.  It might seem that we could check this condition in
9767    semantic analysis, rather than parsing, but that makes it difficult
9768    to handle something like `f()'.  We want to notice that there are
9769    no decl-specifiers, and therefore realize that this is an
9770    expression, not a declaration.)  */
9771
9772 static tree
9773 cp_parser_declarator (cp_parser* parser, 
9774                       cp_parser_declarator_kind dcl_kind, 
9775                       int* ctor_dtor_or_conv_p)
9776 {
9777   cp_token *token;
9778   tree declarator;
9779   enum tree_code code;
9780   tree cv_qualifier_seq;
9781   tree class_type;
9782   tree attributes = NULL_TREE;
9783
9784   /* Assume this is not a constructor, destructor, or type-conversion
9785      operator.  */
9786   if (ctor_dtor_or_conv_p)
9787     *ctor_dtor_or_conv_p = 0;
9788
9789   if (cp_parser_allow_gnu_extensions_p (parser))
9790     attributes = cp_parser_attributes_opt (parser);
9791   
9792   /* Peek at the next token.  */
9793   token = cp_lexer_peek_token (parser->lexer);
9794   
9795   /* Check for the ptr-operator production.  */
9796   cp_parser_parse_tentatively (parser);
9797   /* Parse the ptr-operator.  */
9798   code = cp_parser_ptr_operator (parser, 
9799                                  &class_type, 
9800                                  &cv_qualifier_seq);
9801   /* If that worked, then we have a ptr-operator.  */
9802   if (cp_parser_parse_definitely (parser))
9803     {
9804       /* The dependent declarator is optional if we are parsing an
9805          abstract-declarator.  */
9806       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9807         cp_parser_parse_tentatively (parser);
9808
9809       /* Parse the dependent declarator.  */
9810       declarator = cp_parser_declarator (parser, dcl_kind,
9811                                          /*ctor_dtor_or_conv_p=*/NULL);
9812
9813       /* If we are parsing an abstract-declarator, we must handle the
9814          case where the dependent declarator is absent.  */
9815       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9816           && !cp_parser_parse_definitely (parser))
9817         declarator = NULL_TREE;
9818         
9819       /* Build the representation of the ptr-operator.  */
9820       if (code == INDIRECT_REF)
9821         declarator = make_pointer_declarator (cv_qualifier_seq, 
9822                                               declarator);
9823       else
9824         declarator = make_reference_declarator (cv_qualifier_seq,
9825                                                 declarator);
9826       /* Handle the pointer-to-member case.  */
9827       if (class_type)
9828         declarator = build_nt (SCOPE_REF, class_type, declarator);
9829     }
9830   /* Everything else is a direct-declarator.  */
9831   else
9832     declarator = cp_parser_direct_declarator (parser, dcl_kind,
9833                                               ctor_dtor_or_conv_p);
9834
9835   if (attributes && declarator != error_mark_node)
9836     declarator = tree_cons (attributes, declarator, NULL_TREE);
9837   
9838   return declarator;
9839 }
9840
9841 /* Parse a direct-declarator or direct-abstract-declarator.
9842
9843    direct-declarator:
9844      declarator-id
9845      direct-declarator ( parameter-declaration-clause )
9846        cv-qualifier-seq [opt] 
9847        exception-specification [opt]
9848      direct-declarator [ constant-expression [opt] ]
9849      ( declarator )  
9850
9851    direct-abstract-declarator:
9852      direct-abstract-declarator [opt]
9853        ( parameter-declaration-clause ) 
9854        cv-qualifier-seq [opt]
9855        exception-specification [opt]
9856      direct-abstract-declarator [opt] [ constant-expression [opt] ]
9857      ( abstract-declarator )
9858
9859    Returns a representation of the declarator.  DCL_KIND is
9860    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9861    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
9862    we are parsing a direct-declarator.  It is
9863    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9864    of ambiguity we prefer an abstract declarator, as per
9865    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
9866    cp_parser_declarator.
9867
9868    For the declarator-id production, the representation is as for an
9869    id-expression, except that a qualified name is represented as a
9870    SCOPE_REF.  A function-declarator is represented as a CALL_EXPR;
9871    see the documentation of the FUNCTION_DECLARATOR_* macros for
9872    information about how to find the various declarator components.
9873    An array-declarator is represented as an ARRAY_REF.  The
9874    direct-declarator is the first operand; the constant-expression
9875    indicating the size of the array is the second operand.  */
9876
9877 static tree
9878 cp_parser_direct_declarator (cp_parser* parser,
9879                              cp_parser_declarator_kind dcl_kind,
9880                              int* ctor_dtor_or_conv_p)
9881 {
9882   cp_token *token;
9883   tree declarator = NULL_TREE;
9884   tree scope = NULL_TREE;
9885   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9886   bool saved_in_declarator_p = parser->in_declarator_p;
9887   bool first = true;
9888   
9889   while (true)
9890     {
9891       /* Peek at the next token.  */
9892       token = cp_lexer_peek_token (parser->lexer);
9893       if (token->type == CPP_OPEN_PAREN)
9894         {
9895           /* This is either a parameter-declaration-clause, or a
9896              parenthesized declarator. When we know we are parsing a
9897              named declarator, it must be a parenthesized declarator
9898              if FIRST is true. For instance, `(int)' is a
9899              parameter-declaration-clause, with an omitted
9900              direct-abstract-declarator. But `((*))', is a
9901              parenthesized abstract declarator. Finally, when T is a
9902              template parameter `(T)' is a
9903              parameter-declaration-clause, and not a parenthesized
9904              named declarator.
9905              
9906              We first try and parse a parameter-declaration-clause,
9907              and then try a nested declarator (if FIRST is true).
9908
9909              It is not an error for it not to be a
9910              parameter-declaration-clause, even when FIRST is
9911              false. Consider,
9912
9913                int i (int);
9914                int i (3);
9915
9916              The first is the declaration of a function while the
9917              second is a the definition of a variable, including its
9918              initializer.
9919
9920              Having seen only the parenthesis, we cannot know which of
9921              these two alternatives should be selected.  Even more
9922              complex are examples like:
9923
9924                int i (int (a));
9925                int i (int (3));
9926
9927              The former is a function-declaration; the latter is a
9928              variable initialization.  
9929
9930              Thus again, we try a parameter-declaration-clause, and if
9931              that fails, we back out and return.  */
9932
9933           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9934             {
9935               tree params;
9936               unsigned saved_num_template_parameter_lists;
9937               
9938               cp_parser_parse_tentatively (parser);
9939
9940               /* Consume the `('.  */
9941               cp_lexer_consume_token (parser->lexer);
9942               if (first)
9943                 {
9944                   /* If this is going to be an abstract declarator, we're
9945                      in a declarator and we can't have default args.  */
9946                   parser->default_arg_ok_p = false;
9947                   parser->in_declarator_p = true;
9948                 }
9949           
9950               /* Inside the function parameter list, surrounding
9951                  template-parameter-lists do not apply.  */
9952               saved_num_template_parameter_lists
9953                 = parser->num_template_parameter_lists;
9954               parser->num_template_parameter_lists = 0;
9955
9956               /* Parse the parameter-declaration-clause.  */
9957               params = cp_parser_parameter_declaration_clause (parser);
9958
9959               parser->num_template_parameter_lists
9960                 = saved_num_template_parameter_lists;
9961
9962               /* If all went well, parse the cv-qualifier-seq and the
9963                  exception-specification.  */
9964               if (cp_parser_parse_definitely (parser))
9965                 {
9966                   tree cv_qualifiers;
9967                   tree exception_specification;
9968
9969                   if (ctor_dtor_or_conv_p)
9970                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
9971                   first = false;
9972                   /* Consume the `)'.  */
9973                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9974
9975                   /* Parse the cv-qualifier-seq.  */
9976                   cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
9977                   /* And the exception-specification.  */
9978                   exception_specification 
9979                     = cp_parser_exception_specification_opt (parser);
9980
9981                   /* Create the function-declarator.  */
9982                   declarator = make_call_declarator (declarator,
9983                                                      params,
9984                                                      cv_qualifiers,
9985                                                      exception_specification);
9986                   /* Any subsequent parameter lists are to do with
9987                      return type, so are not those of the declared
9988                      function.  */
9989                   parser->default_arg_ok_p = false;
9990                   
9991                   /* Repeat the main loop.  */
9992                   continue;
9993                 }
9994             }
9995           
9996           /* If this is the first, we can try a parenthesized
9997              declarator.  */
9998           if (first)
9999             {
10000               parser->default_arg_ok_p = saved_default_arg_ok_p;
10001               parser->in_declarator_p = saved_in_declarator_p;
10002               
10003               /* Consume the `('.  */
10004               cp_lexer_consume_token (parser->lexer);
10005               /* Parse the nested declarator.  */
10006               declarator 
10007                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
10008               first = false;
10009               /* Expect a `)'.  */
10010               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10011                 declarator = error_mark_node;
10012               if (declarator == error_mark_node)
10013                 break;
10014               
10015               goto handle_declarator;
10016             }
10017           /* Otherwise, we must be done.  */
10018           else
10019             break;
10020         }
10021       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10022                && token->type == CPP_OPEN_SQUARE)
10023         {
10024           /* Parse an array-declarator.  */
10025           tree bounds;
10026
10027           if (ctor_dtor_or_conv_p)
10028             *ctor_dtor_or_conv_p = 0;
10029           
10030           first = false;
10031           parser->default_arg_ok_p = false;
10032           parser->in_declarator_p = true;
10033           /* Consume the `['.  */
10034           cp_lexer_consume_token (parser->lexer);
10035           /* Peek at the next token.  */
10036           token = cp_lexer_peek_token (parser->lexer);
10037           /* If the next token is `]', then there is no
10038              constant-expression.  */
10039           if (token->type != CPP_CLOSE_SQUARE)
10040             {
10041               bool non_constant_p;
10042
10043               bounds 
10044                 = cp_parser_constant_expression (parser,
10045                                                  /*allow_non_constant=*/true,
10046                                                  &non_constant_p);
10047               if (!non_constant_p)
10048                 bounds = cp_parser_fold_non_dependent_expr (bounds);
10049             }
10050           else
10051             bounds = NULL_TREE;
10052           /* Look for the closing `]'.  */
10053           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10054             {
10055               declarator = error_mark_node;
10056               break;
10057             }
10058
10059           declarator = build_nt (ARRAY_REF, declarator, bounds);
10060         }
10061       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10062         {
10063           /* Parse a declarator-id */
10064           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10065             cp_parser_parse_tentatively (parser);
10066           declarator = cp_parser_declarator_id (parser);
10067           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10068             {
10069               if (!cp_parser_parse_definitely (parser))
10070                 declarator = error_mark_node;
10071               else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10072                 {
10073                   cp_parser_error (parser, "expected unqualified-id");
10074                   declarator = error_mark_node;
10075                 }
10076             }
10077           
10078           if (declarator == error_mark_node)
10079             break;
10080           
10081           if (TREE_CODE (declarator) == SCOPE_REF
10082               && !current_scope ())
10083             {
10084               tree scope = TREE_OPERAND (declarator, 0);
10085
10086               /* In the declaration of a member of a template class
10087                  outside of the class itself, the SCOPE will sometimes
10088                  be a TYPENAME_TYPE.  For example, given:
10089                   
10090                  template <typename T>
10091                  int S<T>::R::i = 3;
10092                   
10093                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
10094                  this context, we must resolve S<T>::R to an ordinary
10095                  type, rather than a typename type.
10096                   
10097                  The reason we normally avoid resolving TYPENAME_TYPEs
10098                  is that a specialization of `S' might render
10099                  `S<T>::R' not a type.  However, if `S' is
10100                  specialized, then this `i' will not be used, so there
10101                  is no harm in resolving the types here.  */
10102               if (TREE_CODE (scope) == TYPENAME_TYPE)
10103                 {
10104                   tree type;
10105
10106                   /* Resolve the TYPENAME_TYPE.  */
10107                   type = resolve_typename_type (scope,
10108                                                  /*only_current_p=*/false);
10109                   /* If that failed, the declarator is invalid.  */
10110                   if (type != error_mark_node)
10111                     scope = type;
10112                   /* Build a new DECLARATOR.  */
10113                   declarator = build_nt (SCOPE_REF, 
10114                                          scope,
10115                                          TREE_OPERAND (declarator, 1));
10116                 }
10117             }
10118       
10119           /* Check to see whether the declarator-id names a constructor, 
10120              destructor, or conversion.  */
10121           if (declarator && ctor_dtor_or_conv_p 
10122               && ((TREE_CODE (declarator) == SCOPE_REF 
10123                    && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10124                   || (TREE_CODE (declarator) != SCOPE_REF
10125                       && at_class_scope_p ())))
10126             {
10127               tree unqualified_name;
10128               tree class_type;
10129
10130               /* Get the unqualified part of the name.  */
10131               if (TREE_CODE (declarator) == SCOPE_REF)
10132                 {
10133                   class_type = TREE_OPERAND (declarator, 0);
10134                   unqualified_name = TREE_OPERAND (declarator, 1);
10135                 }
10136               else
10137                 {
10138                   class_type = current_class_type;
10139                   unqualified_name = declarator;
10140                 }
10141
10142               /* See if it names ctor, dtor or conv.  */
10143               if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10144                   || IDENTIFIER_TYPENAME_P (unqualified_name)
10145                   || constructor_name_p (unqualified_name, class_type))
10146                 *ctor_dtor_or_conv_p = -1;
10147             }
10148
10149         handle_declarator:;
10150           scope = get_scope_of_declarator (declarator);
10151           if (scope)
10152             /* Any names that appear after the declarator-id for a member
10153                are looked up in the containing scope.  */
10154             push_scope (scope);
10155           parser->in_declarator_p = true;
10156           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10157               || (declarator
10158                   && (TREE_CODE (declarator) == SCOPE_REF
10159                       || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10160             /* Default args are only allowed on function
10161                declarations.  */
10162             parser->default_arg_ok_p = saved_default_arg_ok_p;
10163           else
10164             parser->default_arg_ok_p = false;
10165
10166           first = false;
10167         }
10168       /* We're done.  */
10169       else
10170         break;
10171     }
10172
10173   /* For an abstract declarator, we might wind up with nothing at this
10174      point.  That's an error; the declarator is not optional.  */
10175   if (!declarator)
10176     cp_parser_error (parser, "expected declarator");
10177
10178   /* If we entered a scope, we must exit it now.  */
10179   if (scope)
10180     pop_scope (scope);
10181
10182   parser->default_arg_ok_p = saved_default_arg_ok_p;
10183   parser->in_declarator_p = saved_in_declarator_p;
10184   
10185   return declarator;
10186 }
10187
10188 /* Parse a ptr-operator.  
10189
10190    ptr-operator:
10191      * cv-qualifier-seq [opt]
10192      &
10193      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10194
10195    GNU Extension:
10196
10197    ptr-operator:
10198      & cv-qualifier-seq [opt]
10199
10200    Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10201    used.  Returns ADDR_EXPR if a reference was used.  In the
10202    case of a pointer-to-member, *TYPE is filled in with the 
10203    TYPE containing the member.  *CV_QUALIFIER_SEQ is filled in
10204    with the cv-qualifier-seq, or NULL_TREE, if there are no
10205    cv-qualifiers.  Returns ERROR_MARK if an error occurred.  */
10206    
10207 static enum tree_code
10208 cp_parser_ptr_operator (cp_parser* parser, 
10209                         tree* type, 
10210                         tree* cv_qualifier_seq)
10211 {
10212   enum tree_code code = ERROR_MARK;
10213   cp_token *token;
10214
10215   /* Assume that it's not a pointer-to-member.  */
10216   *type = NULL_TREE;
10217   /* And that there are no cv-qualifiers.  */
10218   *cv_qualifier_seq = NULL_TREE;
10219
10220   /* Peek at the next token.  */
10221   token = cp_lexer_peek_token (parser->lexer);
10222   /* If it's a `*' or `&' we have a pointer or reference.  */
10223   if (token->type == CPP_MULT || token->type == CPP_AND)
10224     {
10225       /* Remember which ptr-operator we were processing.  */
10226       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10227
10228       /* Consume the `*' or `&'.  */
10229       cp_lexer_consume_token (parser->lexer);
10230
10231       /* A `*' can be followed by a cv-qualifier-seq, and so can a
10232          `&', if we are allowing GNU extensions.  (The only qualifier
10233          that can legally appear after `&' is `restrict', but that is
10234          enforced during semantic analysis.  */
10235       if (code == INDIRECT_REF 
10236           || cp_parser_allow_gnu_extensions_p (parser))
10237         *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10238     }
10239   else
10240     {
10241       /* Try the pointer-to-member case.  */
10242       cp_parser_parse_tentatively (parser);
10243       /* Look for the optional `::' operator.  */
10244       cp_parser_global_scope_opt (parser,
10245                                   /*current_scope_valid_p=*/false);
10246       /* Look for the nested-name specifier.  */
10247       cp_parser_nested_name_specifier (parser,
10248                                        /*typename_keyword_p=*/false,
10249                                        /*check_dependency_p=*/true,
10250                                        /*type_p=*/false,
10251                                        /*is_declaration=*/false);
10252       /* If we found it, and the next token is a `*', then we are
10253          indeed looking at a pointer-to-member operator.  */
10254       if (!cp_parser_error_occurred (parser)
10255           && cp_parser_require (parser, CPP_MULT, "`*'"))
10256         {
10257           /* The type of which the member is a member is given by the
10258              current SCOPE.  */
10259           *type = parser->scope;
10260           /* The next name will not be qualified.  */
10261           parser->scope = NULL_TREE;
10262           parser->qualifying_scope = NULL_TREE;
10263           parser->object_scope = NULL_TREE;
10264           /* Indicate that the `*' operator was used.  */
10265           code = INDIRECT_REF;
10266           /* Look for the optional cv-qualifier-seq.  */
10267           *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10268         }
10269       /* If that didn't work we don't have a ptr-operator.  */
10270       if (!cp_parser_parse_definitely (parser))
10271         cp_parser_error (parser, "expected ptr-operator");
10272     }
10273
10274   return code;
10275 }
10276
10277 /* Parse an (optional) cv-qualifier-seq.
10278
10279    cv-qualifier-seq:
10280      cv-qualifier cv-qualifier-seq [opt]  
10281
10282    Returns a TREE_LIST.  The TREE_VALUE of each node is the
10283    representation of a cv-qualifier.  */
10284
10285 static tree
10286 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10287 {
10288   tree cv_qualifiers = NULL_TREE;
10289   
10290   while (true)
10291     {
10292       tree cv_qualifier;
10293
10294       /* Look for the next cv-qualifier.  */
10295       cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10296       /* If we didn't find one, we're done.  */
10297       if (!cv_qualifier)
10298         break;
10299
10300       /* Add this cv-qualifier to the list.  */
10301       cv_qualifiers 
10302         = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10303     }
10304
10305   /* We built up the list in reverse order.  */
10306   return nreverse (cv_qualifiers);
10307 }
10308
10309 /* Parse an (optional) cv-qualifier.
10310
10311    cv-qualifier:
10312      const
10313      volatile  
10314
10315    GNU Extension:
10316
10317    cv-qualifier:
10318      __restrict__ */
10319
10320 static tree
10321 cp_parser_cv_qualifier_opt (cp_parser* parser)
10322 {
10323   cp_token *token;
10324   tree cv_qualifier = NULL_TREE;
10325
10326   /* Peek at the next token.  */
10327   token = cp_lexer_peek_token (parser->lexer);
10328   /* See if it's a cv-qualifier.  */
10329   switch (token->keyword)
10330     {
10331     case RID_CONST:
10332     case RID_VOLATILE:
10333     case RID_RESTRICT:
10334       /* Save the value of the token.  */
10335       cv_qualifier = token->value;
10336       /* Consume the token.  */
10337       cp_lexer_consume_token (parser->lexer);
10338       break;
10339
10340     default:
10341       break;
10342     }
10343
10344   return cv_qualifier;
10345 }
10346
10347 /* Parse a declarator-id.
10348
10349    declarator-id:
10350      id-expression
10351      :: [opt] nested-name-specifier [opt] type-name  
10352
10353    In the `id-expression' case, the value returned is as for
10354    cp_parser_id_expression if the id-expression was an unqualified-id.
10355    If the id-expression was a qualified-id, then a SCOPE_REF is
10356    returned.  The first operand is the scope (either a NAMESPACE_DECL
10357    or TREE_TYPE), but the second is still just a representation of an
10358    unqualified-id.  */
10359
10360 static tree
10361 cp_parser_declarator_id (cp_parser* parser)
10362 {
10363   tree id_expression;
10364
10365   /* The expression must be an id-expression.  Assume that qualified
10366      names are the names of types so that:
10367
10368        template <class T>
10369        int S<T>::R::i = 3;
10370
10371      will work; we must treat `S<T>::R' as the name of a type.
10372      Similarly, assume that qualified names are templates, where
10373      required, so that:
10374
10375        template <class T>
10376        int S<T>::R<T>::i = 3;
10377
10378      will work, too.  */
10379   id_expression = cp_parser_id_expression (parser,
10380                                            /*template_keyword_p=*/false,
10381                                            /*check_dependency_p=*/false,
10382                                            /*template_p=*/NULL,
10383                                            /*declarator_p=*/true);
10384   /* If the name was qualified, create a SCOPE_REF to represent 
10385      that.  */
10386   if (parser->scope)
10387     {
10388       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10389       parser->scope = NULL_TREE;
10390     }
10391
10392   return id_expression;
10393 }
10394
10395 /* Parse a type-id.
10396
10397    type-id:
10398      type-specifier-seq abstract-declarator [opt]
10399
10400    Returns the TYPE specified.  */
10401
10402 static tree
10403 cp_parser_type_id (cp_parser* parser)
10404 {
10405   tree type_specifier_seq;
10406   tree abstract_declarator;
10407
10408   /* Parse the type-specifier-seq.  */
10409   type_specifier_seq 
10410     = cp_parser_type_specifier_seq (parser);
10411   if (type_specifier_seq == error_mark_node)
10412     return error_mark_node;
10413
10414   /* There might or might not be an abstract declarator.  */
10415   cp_parser_parse_tentatively (parser);
10416   /* Look for the declarator.  */
10417   abstract_declarator 
10418     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10419   /* Check to see if there really was a declarator.  */
10420   if (!cp_parser_parse_definitely (parser))
10421     abstract_declarator = NULL_TREE;
10422
10423   return groktypename (build_tree_list (type_specifier_seq,
10424                                         abstract_declarator));
10425 }
10426
10427 /* Parse a type-specifier-seq.
10428
10429    type-specifier-seq:
10430      type-specifier type-specifier-seq [opt]
10431
10432    GNU extension:
10433
10434    type-specifier-seq:
10435      attributes type-specifier-seq [opt]
10436
10437    Returns a TREE_LIST.  Either the TREE_VALUE of each node is a
10438    type-specifier, or the TREE_PURPOSE is a list of attributes.  */
10439
10440 static tree
10441 cp_parser_type_specifier_seq (cp_parser* parser)
10442 {
10443   bool seen_type_specifier = false;
10444   tree type_specifier_seq = NULL_TREE;
10445
10446   /* Parse the type-specifiers and attributes.  */
10447   while (true)
10448     {
10449       tree type_specifier;
10450
10451       /* Check for attributes first.  */
10452       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10453         {
10454           type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10455                                           NULL_TREE,
10456                                           type_specifier_seq);
10457           continue;
10458         }
10459
10460       /* After the first type-specifier, others are optional.  */
10461       if (seen_type_specifier)
10462         cp_parser_parse_tentatively (parser);
10463       /* Look for the type-specifier.  */
10464       type_specifier = cp_parser_type_specifier (parser, 
10465                                                  CP_PARSER_FLAGS_NONE,
10466                                                  /*is_friend=*/false,
10467                                                  /*is_declaration=*/false,
10468                                                  NULL,
10469                                                  NULL);
10470       /* If the first type-specifier could not be found, this is not a
10471          type-specifier-seq at all.  */
10472       if (!seen_type_specifier && type_specifier == error_mark_node)
10473         return error_mark_node;
10474       /* If subsequent type-specifiers could not be found, the
10475          type-specifier-seq is complete.  */
10476       else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10477         break;
10478
10479       /* Add the new type-specifier to the list.  */
10480       type_specifier_seq 
10481         = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10482       seen_type_specifier = true;
10483     }
10484
10485   /* We built up the list in reverse order.  */
10486   return nreverse (type_specifier_seq);
10487 }
10488
10489 /* Parse a parameter-declaration-clause.
10490
10491    parameter-declaration-clause:
10492      parameter-declaration-list [opt] ... [opt]
10493      parameter-declaration-list , ...
10494
10495    Returns a representation for the parameter declarations.  Each node
10496    is a TREE_LIST.  (See cp_parser_parameter_declaration for the exact
10497    representation.)  If the parameter-declaration-clause ends with an
10498    ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10499    list.  A return value of NULL_TREE indicates a
10500    parameter-declaration-clause consisting only of an ellipsis.  */
10501
10502 static tree
10503 cp_parser_parameter_declaration_clause (cp_parser* parser)
10504 {
10505   tree parameters;
10506   cp_token *token;
10507   bool ellipsis_p;
10508
10509   /* Peek at the next token.  */
10510   token = cp_lexer_peek_token (parser->lexer);
10511   /* Check for trivial parameter-declaration-clauses.  */
10512   if (token->type == CPP_ELLIPSIS)
10513     {
10514       /* Consume the `...' token.  */
10515       cp_lexer_consume_token (parser->lexer);
10516       return NULL_TREE;
10517     }
10518   else if (token->type == CPP_CLOSE_PAREN)
10519     /* There are no parameters.  */
10520     {
10521 #ifndef NO_IMPLICIT_EXTERN_C
10522       if (in_system_header && current_class_type == NULL
10523           && current_lang_name == lang_name_c)
10524         return NULL_TREE;
10525       else
10526 #endif
10527         return void_list_node;
10528     }
10529   /* Check for `(void)', too, which is a special case.  */
10530   else if (token->keyword == RID_VOID
10531            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
10532                == CPP_CLOSE_PAREN))
10533     {
10534       /* Consume the `void' token.  */
10535       cp_lexer_consume_token (parser->lexer);
10536       /* There are no parameters.  */
10537       return void_list_node;
10538     }
10539   
10540   /* Parse the parameter-declaration-list.  */
10541   parameters = cp_parser_parameter_declaration_list (parser);
10542   /* If a parse error occurred while parsing the
10543      parameter-declaration-list, then the entire
10544      parameter-declaration-clause is erroneous.  */
10545   if (parameters == error_mark_node)
10546     return error_mark_node;
10547
10548   /* Peek at the next token.  */
10549   token = cp_lexer_peek_token (parser->lexer);
10550   /* If it's a `,', the clause should terminate with an ellipsis.  */
10551   if (token->type == CPP_COMMA)
10552     {
10553       /* Consume the `,'.  */
10554       cp_lexer_consume_token (parser->lexer);
10555       /* Expect an ellipsis.  */
10556       ellipsis_p 
10557         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10558     }
10559   /* It might also be `...' if the optional trailing `,' was 
10560      omitted.  */
10561   else if (token->type == CPP_ELLIPSIS)
10562     {
10563       /* Consume the `...' token.  */
10564       cp_lexer_consume_token (parser->lexer);
10565       /* And remember that we saw it.  */
10566       ellipsis_p = true;
10567     }
10568   else
10569     ellipsis_p = false;
10570
10571   /* Finish the parameter list.  */
10572   return finish_parmlist (parameters, ellipsis_p);
10573 }
10574
10575 /* Parse a parameter-declaration-list.
10576
10577    parameter-declaration-list:
10578      parameter-declaration
10579      parameter-declaration-list , parameter-declaration
10580
10581    Returns a representation of the parameter-declaration-list, as for
10582    cp_parser_parameter_declaration_clause.  However, the
10583    `void_list_node' is never appended to the list.  */
10584
10585 static tree
10586 cp_parser_parameter_declaration_list (cp_parser* parser)
10587 {
10588   tree parameters = NULL_TREE;
10589
10590   /* Look for more parameters.  */
10591   while (true)
10592     {
10593       tree parameter;
10594       /* Parse the parameter.  */
10595       parameter 
10596         = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10597
10598       /* If a parse error occurred parsing the parameter declaration,
10599          then the entire parameter-declaration-list is erroneous.  */
10600       if (parameter == error_mark_node)
10601         {
10602           parameters = error_mark_node;
10603           break;
10604         }
10605       /* Add the new parameter to the list.  */
10606       TREE_CHAIN (parameter) = parameters;
10607       parameters = parameter;
10608
10609       /* Peek at the next token.  */
10610       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10611           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10612         /* The parameter-declaration-list is complete.  */
10613         break;
10614       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10615         {
10616           cp_token *token;
10617
10618           /* Peek at the next token.  */
10619           token = cp_lexer_peek_nth_token (parser->lexer, 2);
10620           /* If it's an ellipsis, then the list is complete.  */
10621           if (token->type == CPP_ELLIPSIS)
10622             break;
10623           /* Otherwise, there must be more parameters.  Consume the
10624              `,'.  */
10625           cp_lexer_consume_token (parser->lexer);
10626         }
10627       else
10628         {
10629           cp_parser_error (parser, "expected `,' or `...'");
10630           break;
10631         }
10632     }
10633
10634   /* We built up the list in reverse order; straighten it out now.  */
10635   return nreverse (parameters);
10636 }
10637
10638 /* Parse a parameter declaration.
10639
10640    parameter-declaration:
10641      decl-specifier-seq declarator
10642      decl-specifier-seq declarator = assignment-expression
10643      decl-specifier-seq abstract-declarator [opt]
10644      decl-specifier-seq abstract-declarator [opt] = assignment-expression
10645
10646    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10647    declares a template parameter.  (In that case, a non-nested `>'
10648    token encountered during the parsing of the assignment-expression
10649    is not interpreted as a greater-than operator.)
10650
10651    Returns a TREE_LIST representing the parameter-declaration.  The
10652    TREE_VALUE is a representation of the decl-specifier-seq and
10653    declarator.  In particular, the TREE_VALUE will be a TREE_LIST
10654    whose TREE_PURPOSE represents the decl-specifier-seq and whose
10655    TREE_VALUE represents the declarator.  */
10656
10657 static tree
10658 cp_parser_parameter_declaration (cp_parser *parser, 
10659                                  bool template_parm_p)
10660 {
10661   int declares_class_or_enum;
10662   bool greater_than_is_operator_p;
10663   tree decl_specifiers;
10664   tree attributes;
10665   tree declarator;
10666   tree default_argument;
10667   tree parameter;
10668   cp_token *token;
10669   const char *saved_message;
10670
10671   /* In a template parameter, `>' is not an operator.
10672
10673      [temp.param]
10674
10675      When parsing a default template-argument for a non-type
10676      template-parameter, the first non-nested `>' is taken as the end
10677      of the template parameter-list rather than a greater-than
10678      operator.  */
10679   greater_than_is_operator_p = !template_parm_p;
10680
10681   /* Type definitions may not appear in parameter types.  */
10682   saved_message = parser->type_definition_forbidden_message;
10683   parser->type_definition_forbidden_message 
10684     = "types may not be defined in parameter types";
10685
10686   /* Parse the declaration-specifiers.  */
10687   decl_specifiers 
10688     = cp_parser_decl_specifier_seq (parser,
10689                                     CP_PARSER_FLAGS_NONE,
10690                                     &attributes,
10691                                     &declares_class_or_enum);
10692   /* If an error occurred, there's no reason to attempt to parse the
10693      rest of the declaration.  */
10694   if (cp_parser_error_occurred (parser))
10695     {
10696       parser->type_definition_forbidden_message = saved_message;
10697       return error_mark_node;
10698     }
10699
10700   /* Peek at the next token.  */
10701   token = cp_lexer_peek_token (parser->lexer);
10702   /* If the next token is a `)', `,', `=', `>', or `...', then there
10703      is no declarator.  */
10704   if (token->type == CPP_CLOSE_PAREN 
10705       || token->type == CPP_COMMA
10706       || token->type == CPP_EQ
10707       || token->type == CPP_ELLIPSIS
10708       || token->type == CPP_GREATER)
10709     declarator = NULL_TREE;
10710   /* Otherwise, there should be a declarator.  */
10711   else
10712     {
10713       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10714       parser->default_arg_ok_p = false;
10715   
10716       declarator = cp_parser_declarator (parser,
10717                                          CP_PARSER_DECLARATOR_EITHER,
10718                                          /*ctor_dtor_or_conv_p=*/NULL);
10719       parser->default_arg_ok_p = saved_default_arg_ok_p;
10720       /* After the declarator, allow more attributes.  */
10721       attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10722     }
10723
10724   /* The restriction on defining new types applies only to the type
10725      of the parameter, not to the default argument.  */
10726   parser->type_definition_forbidden_message = saved_message;
10727
10728   /* If the next token is `=', then process a default argument.  */
10729   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10730     {
10731       bool saved_greater_than_is_operator_p;
10732       /* Consume the `='.  */
10733       cp_lexer_consume_token (parser->lexer);
10734
10735       /* If we are defining a class, then the tokens that make up the
10736          default argument must be saved and processed later.  */
10737       if (!template_parm_p && at_class_scope_p () 
10738           && TYPE_BEING_DEFINED (current_class_type))
10739         {
10740           unsigned depth = 0;
10741
10742           /* Create a DEFAULT_ARG to represented the unparsed default
10743              argument.  */
10744           default_argument = make_node (DEFAULT_ARG);
10745           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10746
10747           /* Add tokens until we have processed the entire default
10748              argument.  */
10749           while (true)
10750             {
10751               bool done = false;
10752               cp_token *token;
10753
10754               /* Peek at the next token.  */
10755               token = cp_lexer_peek_token (parser->lexer);
10756               /* What we do depends on what token we have.  */
10757               switch (token->type)
10758                 {
10759                   /* In valid code, a default argument must be
10760                      immediately followed by a `,' `)', or `...'.  */
10761                 case CPP_COMMA:
10762                 case CPP_CLOSE_PAREN:
10763                 case CPP_ELLIPSIS:
10764                   /* If we run into a non-nested `;', `}', or `]',
10765                      then the code is invalid -- but the default
10766                      argument is certainly over.  */
10767                 case CPP_SEMICOLON:
10768                 case CPP_CLOSE_BRACE:
10769                 case CPP_CLOSE_SQUARE:
10770                   if (depth == 0)
10771                     done = true;
10772                   /* Update DEPTH, if necessary.  */
10773                   else if (token->type == CPP_CLOSE_PAREN
10774                            || token->type == CPP_CLOSE_BRACE
10775                            || token->type == CPP_CLOSE_SQUARE)
10776                     --depth;
10777                   break;
10778
10779                 case CPP_OPEN_PAREN:
10780                 case CPP_OPEN_SQUARE:
10781                 case CPP_OPEN_BRACE:
10782                   ++depth;
10783                   break;
10784
10785                 case CPP_GREATER:
10786                   /* If we see a non-nested `>', and `>' is not an
10787                      operator, then it marks the end of the default
10788                      argument.  */
10789                   if (!depth && !greater_than_is_operator_p)
10790                     done = true;
10791                   break;
10792
10793                   /* If we run out of tokens, issue an error message.  */
10794                 case CPP_EOF:
10795                   error ("file ends in default argument");
10796                   done = true;
10797                   break;
10798
10799                 case CPP_NAME:
10800                 case CPP_SCOPE:
10801                   /* In these cases, we should look for template-ids.
10802                      For example, if the default argument is 
10803                      `X<int, double>()', we need to do name lookup to
10804                      figure out whether or not `X' is a template; if
10805                      so, the `,' does not end the default argument.
10806
10807                      That is not yet done.  */
10808                   break;
10809
10810                 default:
10811                   break;
10812                 }
10813
10814               /* If we've reached the end, stop.  */
10815               if (done)
10816                 break;
10817               
10818               /* Add the token to the token block.  */
10819               token = cp_lexer_consume_token (parser->lexer);
10820               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10821                                          token);
10822             }
10823         }
10824       /* Outside of a class definition, we can just parse the
10825          assignment-expression.  */
10826       else
10827         {
10828           bool saved_local_variables_forbidden_p;
10829
10830           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10831              set correctly.  */
10832           saved_greater_than_is_operator_p 
10833             = parser->greater_than_is_operator_p;
10834           parser->greater_than_is_operator_p = greater_than_is_operator_p;
10835           /* Local variable names (and the `this' keyword) may not
10836              appear in a default argument.  */
10837           saved_local_variables_forbidden_p 
10838             = parser->local_variables_forbidden_p;
10839           parser->local_variables_forbidden_p = true;
10840           /* Parse the assignment-expression.  */
10841           default_argument = cp_parser_assignment_expression (parser);
10842           /* Restore saved state.  */
10843           parser->greater_than_is_operator_p 
10844             = saved_greater_than_is_operator_p;
10845           parser->local_variables_forbidden_p 
10846             = saved_local_variables_forbidden_p; 
10847         }
10848       if (!parser->default_arg_ok_p)
10849         {
10850           if (!flag_pedantic_errors)
10851             warning ("deprecated use of default argument for parameter of non-function");
10852           else
10853             {
10854               error ("default arguments are only permitted for function parameters");
10855               default_argument = NULL_TREE;
10856             }
10857         }
10858     }
10859   else
10860     default_argument = NULL_TREE;
10861   
10862   /* Create the representation of the parameter.  */
10863   if (attributes)
10864     decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10865   parameter = build_tree_list (default_argument, 
10866                                build_tree_list (decl_specifiers,
10867                                                 declarator));
10868
10869   return parameter;
10870 }
10871
10872 /* Parse a function-definition.  
10873
10874    function-definition:
10875      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10876        function-body 
10877      decl-specifier-seq [opt] declarator function-try-block  
10878
10879    GNU Extension:
10880
10881    function-definition:
10882      __extension__ function-definition 
10883
10884    Returns the FUNCTION_DECL for the function.  If FRIEND_P is
10885    non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10886    be a `friend'.  */
10887
10888 static tree
10889 cp_parser_function_definition (cp_parser* parser, bool* friend_p)
10890 {
10891   tree decl_specifiers;
10892   tree attributes;
10893   tree declarator;
10894   tree fn;
10895   cp_token *token;
10896   int declares_class_or_enum;
10897   bool member_p;
10898   /* The saved value of the PEDANTIC flag.  */
10899   int saved_pedantic;
10900
10901   /* Any pending qualification must be cleared by our caller.  It is
10902      more robust to force the callers to clear PARSER->SCOPE than to
10903      do it here since if the qualification is in effect here, it might
10904      also end up in effect elsewhere that it is not intended.  */
10905   my_friendly_assert (!parser->scope, 20010821);
10906
10907   /* Handle `__extension__'.  */
10908   if (cp_parser_extension_opt (parser, &saved_pedantic))
10909     {
10910       /* Parse the function-definition.  */
10911       fn = cp_parser_function_definition (parser, friend_p);
10912       /* Restore the PEDANTIC flag.  */
10913       pedantic = saved_pedantic;
10914
10915       return fn;
10916     }
10917
10918   /* Check to see if this definition appears in a class-specifier.  */
10919   member_p = (at_class_scope_p () 
10920               && TYPE_BEING_DEFINED (current_class_type));
10921   /* Defer access checks in the decl-specifier-seq until we know what
10922      function is being defined.  There is no need to do this for the
10923      definition of member functions; we cannot be defining a member
10924      from another class.  */
10925   push_deferring_access_checks (member_p ? dk_no_check: dk_deferred);
10926
10927   /* Parse the decl-specifier-seq.  */
10928   decl_specifiers 
10929     = cp_parser_decl_specifier_seq (parser,
10930                                     CP_PARSER_FLAGS_OPTIONAL,
10931                                     &attributes,
10932                                     &declares_class_or_enum);
10933   /* Figure out whether this declaration is a `friend'.  */
10934   if (friend_p)
10935     *friend_p = cp_parser_friend_p (decl_specifiers);
10936
10937   /* Parse the declarator.  */
10938   declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10939                                      /*ctor_dtor_or_conv_p=*/NULL);
10940
10941   /* Gather up any access checks that occurred.  */
10942   stop_deferring_access_checks ();
10943
10944   /* If something has already gone wrong, we may as well stop now.  */
10945   if (declarator == error_mark_node)
10946     {
10947       /* Skip to the end of the function, or if this wasn't anything
10948          like a function-definition, to a `;' in the hopes of finding
10949          a sensible place from which to continue parsing.  */
10950       cp_parser_skip_to_end_of_block_or_statement (parser);
10951       pop_deferring_access_checks ();
10952       return error_mark_node;
10953     }
10954
10955   /* The next character should be a `{' (for a simple function
10956      definition), a `:' (for a ctor-initializer), or `try' (for a
10957      function-try block).  */
10958   token = cp_lexer_peek_token (parser->lexer);
10959   if (!cp_parser_token_starts_function_definition_p (token))
10960     {
10961       /* Issue the error-message.  */
10962       cp_parser_error (parser, "expected function-definition");
10963       /* Skip to the next `;'.  */
10964       cp_parser_skip_to_end_of_block_or_statement (parser);
10965
10966       pop_deferring_access_checks ();
10967       return error_mark_node;
10968     }
10969
10970   cp_parser_check_for_definition_in_return_type (declarator,
10971                                                  declares_class_or_enum);
10972
10973   /* If we are in a class scope, then we must handle
10974      function-definitions specially.  In particular, we save away the
10975      tokens that make up the function body, and parse them again
10976      later, in order to handle code like:
10977
10978        struct S {
10979          int f () { return i; }
10980          int i;
10981        }; 
10982  
10983      Here, we cannot parse the body of `f' until after we have seen
10984      the declaration of `i'.  */
10985   if (member_p)
10986     {
10987       cp_token_cache *cache;
10988
10989       /* Create the function-declaration.  */
10990       fn = start_method (decl_specifiers, declarator, attributes);
10991       /* If something went badly wrong, bail out now.  */
10992       if (fn == error_mark_node)
10993         {
10994           /* If there's a function-body, skip it.  */
10995           if (cp_parser_token_starts_function_definition_p 
10996               (cp_lexer_peek_token (parser->lexer)))
10997             cp_parser_skip_to_end_of_block_or_statement (parser);
10998           pop_deferring_access_checks ();
10999           return error_mark_node;
11000         }
11001
11002       /* Remember it, if there default args to post process.  */
11003       cp_parser_save_default_args (parser, fn);
11004       
11005       /* Create a token cache.  */
11006       cache = cp_token_cache_new ();
11007       /* Save away the tokens that make up the body of the 
11008          function.  */
11009       cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11010       /* Handle function try blocks.  */
11011       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11012         cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11013
11014       /* Save away the inline definition; we will process it when the
11015          class is complete.  */
11016       DECL_PENDING_INLINE_INFO (fn) = cache;
11017       DECL_PENDING_INLINE_P (fn) = 1;
11018
11019       /* We need to know that this was defined in the class, so that
11020          friend templates are handled correctly.  */
11021       DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
11022
11023       /* We're done with the inline definition.  */
11024       finish_method (fn);
11025
11026       /* Add FN to the queue of functions to be parsed later.  */
11027       TREE_VALUE (parser->unparsed_functions_queues)
11028         = tree_cons (NULL_TREE, fn, 
11029                      TREE_VALUE (parser->unparsed_functions_queues));
11030
11031       pop_deferring_access_checks ();
11032       return fn;
11033     }
11034
11035   /* Check that the number of template-parameter-lists is OK.  */
11036   if (!cp_parser_check_declarator_template_parameters (parser, 
11037                                                        declarator))
11038     {
11039       cp_parser_skip_to_end_of_block_or_statement (parser);
11040       pop_deferring_access_checks ();
11041       return error_mark_node;
11042     }
11043
11044   fn = cp_parser_function_definition_from_specifiers_and_declarator
11045           (parser, decl_specifiers, attributes, declarator);
11046   pop_deferring_access_checks ();
11047   return fn;
11048 }
11049
11050 /* Parse a function-body.
11051
11052    function-body:
11053      compound_statement  */
11054
11055 static void
11056 cp_parser_function_body (cp_parser *parser)
11057 {
11058   cp_parser_compound_statement (parser, false);
11059 }
11060
11061 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11062    true if a ctor-initializer was present.  */
11063
11064 static bool
11065 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11066 {
11067   tree body;
11068   bool ctor_initializer_p;
11069
11070   /* Begin the function body.  */
11071   body = begin_function_body ();
11072   /* Parse the optional ctor-initializer.  */
11073   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11074   /* Parse the function-body.  */
11075   cp_parser_function_body (parser);
11076   /* Finish the function body.  */
11077   finish_function_body (body);
11078
11079   return ctor_initializer_p;
11080 }
11081
11082 /* Parse an initializer.
11083
11084    initializer:
11085      = initializer-clause
11086      ( expression-list )  
11087
11088    Returns a expression representing the initializer.  If no
11089    initializer is present, NULL_TREE is returned.  
11090
11091    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11092    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11093    set to FALSE if there is no initializer present.  If there is an
11094    initializer, and it is not a constant-expression, *NON_CONSTANT_P
11095    is set to true; otherwise it is set to false.  */
11096
11097 static tree
11098 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11099                        bool* non_constant_p)
11100 {
11101   cp_token *token;
11102   tree init;
11103
11104   /* Peek at the next token.  */
11105   token = cp_lexer_peek_token (parser->lexer);
11106
11107   /* Let our caller know whether or not this initializer was
11108      parenthesized.  */
11109   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11110   /* Assume that the initializer is constant.  */
11111   *non_constant_p = false;
11112
11113   if (token->type == CPP_EQ)
11114     {
11115       /* Consume the `='.  */
11116       cp_lexer_consume_token (parser->lexer);
11117       /* Parse the initializer-clause.  */
11118       init = cp_parser_initializer_clause (parser, non_constant_p);
11119     }
11120   else if (token->type == CPP_OPEN_PAREN)
11121     init = cp_parser_parenthesized_expression_list (parser, false,
11122                                                     non_constant_p);
11123   else
11124     {
11125       /* Anything else is an error.  */
11126       cp_parser_error (parser, "expected initializer");
11127       init = error_mark_node;
11128     }
11129
11130   return init;
11131 }
11132
11133 /* Parse an initializer-clause.  
11134
11135    initializer-clause:
11136      assignment-expression
11137      { initializer-list , [opt] }
11138      { }
11139
11140    Returns an expression representing the initializer.  
11141
11142    If the `assignment-expression' production is used the value
11143    returned is simply a representation for the expression.  
11144
11145    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11146    the elements of the initializer-list (or NULL_TREE, if the last
11147    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11148    NULL_TREE.  There is no way to detect whether or not the optional
11149    trailing `,' was provided.  NON_CONSTANT_P is as for
11150    cp_parser_initializer.  */
11151
11152 static tree
11153 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11154 {
11155   tree initializer;
11156
11157   /* If it is not a `{', then we are looking at an
11158      assignment-expression.  */
11159   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11160     initializer 
11161       = cp_parser_constant_expression (parser,
11162                                        /*allow_non_constant_p=*/true,
11163                                        non_constant_p);
11164   else
11165     {
11166       /* Consume the `{' token.  */
11167       cp_lexer_consume_token (parser->lexer);
11168       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11169       initializer = make_node (CONSTRUCTOR);
11170       /* Mark it with TREE_HAS_CONSTRUCTOR.  This should not be
11171          necessary, but check_initializer depends upon it, for 
11172          now.  */
11173       TREE_HAS_CONSTRUCTOR (initializer) = 1;
11174       /* If it's not a `}', then there is a non-trivial initializer.  */
11175       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11176         {
11177           /* Parse the initializer list.  */
11178           CONSTRUCTOR_ELTS (initializer)
11179             = cp_parser_initializer_list (parser, non_constant_p);
11180           /* A trailing `,' token is allowed.  */
11181           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11182             cp_lexer_consume_token (parser->lexer);
11183         }
11184       /* Now, there should be a trailing `}'.  */
11185       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11186     }
11187
11188   return initializer;
11189 }
11190
11191 /* Parse an initializer-list.
11192
11193    initializer-list:
11194      initializer-clause
11195      initializer-list , initializer-clause
11196
11197    GNU Extension:
11198    
11199    initializer-list:
11200      identifier : initializer-clause
11201      initializer-list, identifier : initializer-clause
11202
11203    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
11204    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
11205    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
11206    as for cp_parser_initializer.  */
11207
11208 static tree
11209 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11210 {
11211   tree initializers = NULL_TREE;
11212
11213   /* Assume all of the expressions are constant.  */
11214   *non_constant_p = false;
11215
11216   /* Parse the rest of the list.  */
11217   while (true)
11218     {
11219       cp_token *token;
11220       tree identifier;
11221       tree initializer;
11222       bool clause_non_constant_p;
11223
11224       /* If the next token is an identifier and the following one is a
11225          colon, we are looking at the GNU designated-initializer
11226          syntax.  */
11227       if (cp_parser_allow_gnu_extensions_p (parser)
11228           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11229           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11230         {
11231           /* Consume the identifier.  */
11232           identifier = cp_lexer_consume_token (parser->lexer)->value;
11233           /* Consume the `:'.  */
11234           cp_lexer_consume_token (parser->lexer);
11235         }
11236       else
11237         identifier = NULL_TREE;
11238
11239       /* Parse the initializer.  */
11240       initializer = cp_parser_initializer_clause (parser, 
11241                                                   &clause_non_constant_p);
11242       /* If any clause is non-constant, so is the entire initializer.  */
11243       if (clause_non_constant_p)
11244         *non_constant_p = true;
11245       /* Add it to the list.  */
11246       initializers = tree_cons (identifier, initializer, initializers);
11247
11248       /* If the next token is not a comma, we have reached the end of
11249          the list.  */
11250       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11251         break;
11252
11253       /* Peek at the next token.  */
11254       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11255       /* If the next token is a `}', then we're still done.  An
11256          initializer-clause can have a trailing `,' after the
11257          initializer-list and before the closing `}'.  */
11258       if (token->type == CPP_CLOSE_BRACE)
11259         break;
11260
11261       /* Consume the `,' token.  */
11262       cp_lexer_consume_token (parser->lexer);
11263     }
11264
11265   /* The initializers were built up in reverse order, so we need to
11266      reverse them now.  */
11267   return nreverse (initializers);
11268 }
11269
11270 /* Classes [gram.class] */
11271
11272 /* Parse a class-name.
11273
11274    class-name:
11275      identifier
11276      template-id
11277
11278    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11279    to indicate that names looked up in dependent types should be
11280    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
11281    keyword has been used to indicate that the name that appears next
11282    is a template.  TYPE_P is true iff the next name should be treated
11283    as class-name, even if it is declared to be some other kind of name
11284    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11285    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
11286    being defined in a class-head.
11287
11288    Returns the TYPE_DECL representing the class.  */
11289
11290 static tree
11291 cp_parser_class_name (cp_parser *parser, 
11292                       bool typename_keyword_p, 
11293                       bool template_keyword_p, 
11294                       bool type_p,
11295                       bool check_dependency_p,
11296                       bool class_head_p,
11297                       bool is_declaration)
11298 {
11299   tree decl;
11300   tree scope;
11301   bool typename_p;
11302   cp_token *token;
11303
11304   /* All class-names start with an identifier.  */
11305   token = cp_lexer_peek_token (parser->lexer);
11306   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11307     {
11308       cp_parser_error (parser, "expected class-name");
11309       return error_mark_node;
11310     }
11311     
11312   /* PARSER->SCOPE can be cleared when parsing the template-arguments
11313      to a template-id, so we save it here.  */
11314   scope = parser->scope;
11315   if (scope == error_mark_node)
11316     return error_mark_node;
11317   
11318   /* Any name names a type if we're following the `typename' keyword
11319      in a qualified name where the enclosing scope is type-dependent.  */
11320   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11321                 && dependent_type_p (scope));
11322   /* Handle the common case (an identifier, but not a template-id)
11323      efficiently.  */
11324   if (token->type == CPP_NAME 
11325       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11326     {
11327       tree identifier;
11328
11329       /* Look for the identifier.  */
11330       identifier = cp_parser_identifier (parser);
11331       /* If the next token isn't an identifier, we are certainly not
11332          looking at a class-name.  */
11333       if (identifier == error_mark_node)
11334         decl = error_mark_node;
11335       /* If we know this is a type-name, there's no need to look it
11336          up.  */
11337       else if (typename_p)
11338         decl = identifier;
11339       else
11340         {
11341           /* If the next token is a `::', then the name must be a type
11342              name.
11343
11344              [basic.lookup.qual]
11345
11346              During the lookup for a name preceding the :: scope
11347              resolution operator, object, function, and enumerator
11348              names are ignored.  */
11349           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11350             type_p = true;
11351           /* Look up the name.  */
11352           decl = cp_parser_lookup_name (parser, identifier, 
11353                                         type_p,
11354                                         /*is_namespace=*/false,
11355                                         check_dependency_p);
11356         }
11357     }
11358   else
11359     {
11360       /* Try a template-id.  */
11361       decl = cp_parser_template_id (parser, template_keyword_p,
11362                                     check_dependency_p,
11363                                     is_declaration);
11364       if (decl == error_mark_node)
11365         return error_mark_node;
11366     }
11367
11368   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11369
11370   /* If this is a typename, create a TYPENAME_TYPE.  */
11371   if (typename_p && decl != error_mark_node)
11372     decl = TYPE_NAME (make_typename_type (scope, decl,
11373                                           /*complain=*/1));
11374
11375   /* Check to see that it is really the name of a class.  */
11376   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 
11377       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11378       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11379     /* Situations like this:
11380
11381          template <typename T> struct A {
11382            typename T::template X<int>::I i; 
11383          };
11384
11385        are problematic.  Is `T::template X<int>' a class-name?  The
11386        standard does not seem to be definitive, but there is no other
11387        valid interpretation of the following `::'.  Therefore, those
11388        names are considered class-names.  */
11389     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11390   else if (decl == error_mark_node
11391            || TREE_CODE (decl) != TYPE_DECL
11392            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11393     {
11394       cp_parser_error (parser, "expected class-name");
11395       return error_mark_node;
11396     }
11397
11398   return decl;
11399 }
11400
11401 /* Parse a class-specifier.
11402
11403    class-specifier:
11404      class-head { member-specification [opt] }
11405
11406    Returns the TREE_TYPE representing the class.  */
11407
11408 static tree
11409 cp_parser_class_specifier (cp_parser* parser)
11410 {
11411   cp_token *token;
11412   tree type;
11413   tree attributes = NULL_TREE;
11414   int has_trailing_semicolon;
11415   bool nested_name_specifier_p;
11416   unsigned saved_num_template_parameter_lists;
11417
11418   push_deferring_access_checks (dk_no_deferred);
11419
11420   /* Parse the class-head.  */
11421   type = cp_parser_class_head (parser,
11422                                &nested_name_specifier_p);
11423   /* If the class-head was a semantic disaster, skip the entire body
11424      of the class.  */
11425   if (!type)
11426     {
11427       cp_parser_skip_to_end_of_block_or_statement (parser);
11428       pop_deferring_access_checks ();
11429       return error_mark_node;
11430     }
11431
11432   /* Look for the `{'.  */
11433   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11434     {
11435       pop_deferring_access_checks ();
11436       return error_mark_node;
11437     }
11438
11439   /* Issue an error message if type-definitions are forbidden here.  */
11440   cp_parser_check_type_definition (parser);
11441   /* Remember that we are defining one more class.  */
11442   ++parser->num_classes_being_defined;
11443   /* Inside the class, surrounding template-parameter-lists do not
11444      apply.  */
11445   saved_num_template_parameter_lists 
11446     = parser->num_template_parameter_lists; 
11447   parser->num_template_parameter_lists = 0;
11448
11449   /* Start the class.  */
11450   type = begin_class_definition (type);
11451   if (type == error_mark_node)
11452     /* If the type is erroneous, skip the entire body of the class.  */
11453     cp_parser_skip_to_closing_brace (parser);
11454   else
11455     /* Parse the member-specification.  */
11456     cp_parser_member_specification_opt (parser);
11457   /* Look for the trailing `}'.  */
11458   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11459   /* We get better error messages by noticing a common problem: a
11460      missing trailing `;'.  */
11461   token = cp_lexer_peek_token (parser->lexer);
11462   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11463   /* Look for attributes to apply to this class.  */
11464   if (cp_parser_allow_gnu_extensions_p (parser))
11465     attributes = cp_parser_attributes_opt (parser);
11466   /* If we got any attributes in class_head, xref_tag will stick them in
11467      TREE_TYPE of the type.  Grab them now.  */
11468   if (type != error_mark_node)
11469     {
11470       attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11471       TYPE_ATTRIBUTES (type) = NULL_TREE;
11472       type = finish_struct (type, attributes);
11473     }
11474   if (nested_name_specifier_p)
11475     pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11476   /* If this class is not itself within the scope of another class,
11477      then we need to parse the bodies of all of the queued function
11478      definitions.  Note that the queued functions defined in a class
11479      are not always processed immediately following the
11480      class-specifier for that class.  Consider:
11481
11482        struct A {
11483          struct B { void f() { sizeof (A); } };
11484        };
11485
11486      If `f' were processed before the processing of `A' were
11487      completed, there would be no way to compute the size of `A'.
11488      Note that the nesting we are interested in here is lexical --
11489      not the semantic nesting given by TYPE_CONTEXT.  In particular,
11490      for:
11491
11492        struct A { struct B; };
11493        struct A::B { void f() { } };
11494
11495      there is no need to delay the parsing of `A::B::f'.  */
11496   if (--parser->num_classes_being_defined == 0) 
11497     {
11498       tree queue_entry;
11499       tree fn;
11500
11501       /* In a first pass, parse default arguments to the functions.
11502          Then, in a second pass, parse the bodies of the functions.
11503          This two-phased approach handles cases like:
11504          
11505             struct S { 
11506               void f() { g(); } 
11507               void g(int i = 3);
11508             };
11509
11510          */
11511       for (TREE_PURPOSE (parser->unparsed_functions_queues)
11512              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11513            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11514            TREE_PURPOSE (parser->unparsed_functions_queues)
11515              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11516         {
11517           fn = TREE_VALUE (queue_entry);
11518           /* Make sure that any template parameters are in scope.  */
11519           maybe_begin_member_template_processing (fn);
11520           /* If there are default arguments that have not yet been processed,
11521              take care of them now.  */
11522           cp_parser_late_parsing_default_args (parser, fn);
11523           /* Remove any template parameters from the symbol table.  */
11524           maybe_end_member_template_processing ();
11525         }
11526       /* Now parse the body of the functions.  */
11527       for (TREE_VALUE (parser->unparsed_functions_queues)
11528              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11529            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11530            TREE_VALUE (parser->unparsed_functions_queues)
11531              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11532         {
11533           /* Figure out which function we need to process.  */
11534           fn = TREE_VALUE (queue_entry);
11535
11536           /* Parse the function.  */
11537           cp_parser_late_parsing_for_member (parser, fn);
11538         }
11539
11540     }
11541
11542   /* Put back any saved access checks.  */
11543   pop_deferring_access_checks ();
11544
11545   /* Restore the count of active template-parameter-lists.  */
11546   parser->num_template_parameter_lists
11547     = saved_num_template_parameter_lists;
11548
11549   return type;
11550 }
11551
11552 /* Parse a class-head.
11553
11554    class-head:
11555      class-key identifier [opt] base-clause [opt]
11556      class-key nested-name-specifier identifier base-clause [opt]
11557      class-key nested-name-specifier [opt] template-id 
11558        base-clause [opt]  
11559
11560    GNU Extensions:
11561      class-key attributes identifier [opt] base-clause [opt]
11562      class-key attributes nested-name-specifier identifier base-clause [opt]
11563      class-key attributes nested-name-specifier [opt] template-id 
11564        base-clause [opt]  
11565
11566    Returns the TYPE of the indicated class.  Sets
11567    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11568    involving a nested-name-specifier was used, and FALSE otherwise.
11569
11570    Returns NULL_TREE if the class-head is syntactically valid, but
11571    semantically invalid in a way that means we should skip the entire
11572    body of the class.  */
11573
11574 static tree
11575 cp_parser_class_head (cp_parser* parser, 
11576                       bool* nested_name_specifier_p)
11577 {
11578   cp_token *token;
11579   tree nested_name_specifier;
11580   enum tag_types class_key;
11581   tree id = NULL_TREE;
11582   tree type = NULL_TREE;
11583   tree attributes;
11584   bool template_id_p = false;
11585   bool qualified_p = false;
11586   bool invalid_nested_name_p = false;
11587   unsigned num_templates;
11588
11589   /* Assume no nested-name-specifier will be present.  */
11590   *nested_name_specifier_p = false;
11591   /* Assume no template parameter lists will be used in defining the
11592      type.  */
11593   num_templates = 0;
11594
11595   /* Look for the class-key.  */
11596   class_key = cp_parser_class_key (parser);
11597   if (class_key == none_type)
11598     return error_mark_node;
11599
11600   /* Parse the attributes.  */
11601   attributes = cp_parser_attributes_opt (parser);
11602
11603   /* If the next token is `::', that is invalid -- but sometimes
11604      people do try to write:
11605
11606        struct ::S {};  
11607
11608      Handle this gracefully by accepting the extra qualifier, and then
11609      issuing an error about it later if this really is a
11610      class-head.  If it turns out just to be an elaborated type
11611      specifier, remain silent.  */
11612   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11613     qualified_p = true;
11614
11615   push_deferring_access_checks (dk_no_check);
11616
11617   /* Determine the name of the class.  Begin by looking for an
11618      optional nested-name-specifier.  */
11619   nested_name_specifier 
11620     = cp_parser_nested_name_specifier_opt (parser,
11621                                            /*typename_keyword_p=*/false,
11622                                            /*check_dependency_p=*/false,
11623                                            /*type_p=*/false,
11624                                            /*is_declaration=*/false);
11625   /* If there was a nested-name-specifier, then there *must* be an
11626      identifier.  */
11627   if (nested_name_specifier)
11628     {
11629       /* Although the grammar says `identifier', it really means
11630          `class-name' or `template-name'.  You are only allowed to
11631          define a class that has already been declared with this
11632          syntax.  
11633
11634          The proposed resolution for Core Issue 180 says that whever
11635          you see `class T::X' you should treat `X' as a type-name.
11636          
11637          It is OK to define an inaccessible class; for example:
11638          
11639            class A { class B; };
11640            class A::B {};
11641          
11642          We do not know if we will see a class-name, or a
11643          template-name.  We look for a class-name first, in case the
11644          class-name is a template-id; if we looked for the
11645          template-name first we would stop after the template-name.  */
11646       cp_parser_parse_tentatively (parser);
11647       type = cp_parser_class_name (parser,
11648                                    /*typename_keyword_p=*/false,
11649                                    /*template_keyword_p=*/false,
11650                                    /*type_p=*/true,
11651                                    /*check_dependency_p=*/false,
11652                                    /*class_head_p=*/true,
11653                                    /*is_declaration=*/false);
11654       /* If that didn't work, ignore the nested-name-specifier.  */
11655       if (!cp_parser_parse_definitely (parser))
11656         {
11657           invalid_nested_name_p = true;
11658           id = cp_parser_identifier (parser);
11659           if (id == error_mark_node)
11660             id = NULL_TREE;
11661         }
11662       /* If we could not find a corresponding TYPE, treat this
11663          declaration like an unqualified declaration.  */
11664       if (type == error_mark_node)
11665         nested_name_specifier = NULL_TREE;
11666       /* Otherwise, count the number of templates used in TYPE and its
11667          containing scopes.  */
11668       else 
11669         {
11670           tree scope;
11671
11672           for (scope = TREE_TYPE (type); 
11673                scope && TREE_CODE (scope) != NAMESPACE_DECL;
11674                scope = (TYPE_P (scope) 
11675                         ? TYPE_CONTEXT (scope)
11676                         : DECL_CONTEXT (scope))) 
11677             if (TYPE_P (scope) 
11678                 && CLASS_TYPE_P (scope)
11679                 && CLASSTYPE_TEMPLATE_INFO (scope)
11680                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11681                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11682               ++num_templates;
11683         }
11684     }
11685   /* Otherwise, the identifier is optional.  */
11686   else
11687     {
11688       /* We don't know whether what comes next is a template-id,
11689          an identifier, or nothing at all.  */
11690       cp_parser_parse_tentatively (parser);
11691       /* Check for a template-id.  */
11692       id = cp_parser_template_id (parser, 
11693                                   /*template_keyword_p=*/false,
11694                                   /*check_dependency_p=*/true,
11695                                   /*is_declaration=*/true);
11696       /* If that didn't work, it could still be an identifier.  */
11697       if (!cp_parser_parse_definitely (parser))
11698         {
11699           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11700             id = cp_parser_identifier (parser);
11701           else
11702             id = NULL_TREE;
11703         }
11704       else
11705         {
11706           template_id_p = true;
11707           ++num_templates;
11708         }
11709     }
11710
11711   pop_deferring_access_checks ();
11712
11713   cp_parser_check_for_invalid_template_id (parser, id);
11714
11715   /* If it's not a `:' or a `{' then we can't really be looking at a
11716      class-head, since a class-head only appears as part of a
11717      class-specifier.  We have to detect this situation before calling
11718      xref_tag, since that has irreversible side-effects.  */
11719   if (!cp_parser_next_token_starts_class_definition_p (parser))
11720     {
11721       cp_parser_error (parser, "expected `{' or `:'");
11722       return error_mark_node;
11723     }
11724
11725   /* At this point, we're going ahead with the class-specifier, even
11726      if some other problem occurs.  */
11727   cp_parser_commit_to_tentative_parse (parser);
11728   /* Issue the error about the overly-qualified name now.  */
11729   if (qualified_p)
11730     cp_parser_error (parser,
11731                      "global qualification of class name is invalid");
11732   else if (invalid_nested_name_p)
11733     cp_parser_error (parser,
11734                      "qualified name does not name a class");
11735   /* Make sure that the right number of template parameters were
11736      present.  */
11737   if (!cp_parser_check_template_parameters (parser, num_templates))
11738     /* If something went wrong, there is no point in even trying to
11739        process the class-definition.  */
11740     return NULL_TREE;
11741
11742   /* Look up the type.  */
11743   if (template_id_p)
11744     {
11745       type = TREE_TYPE (id);
11746       maybe_process_partial_specialization (type);
11747     }
11748   else if (!nested_name_specifier)
11749     {
11750       /* If the class was unnamed, create a dummy name.  */
11751       if (!id)
11752         id = make_anon_name ();
11753       type = xref_tag (class_key, id, attributes, /*globalize=*/false,
11754                        parser->num_template_parameter_lists);
11755     }
11756   else
11757     {
11758       tree class_type;
11759       tree scope;
11760
11761       /* Given:
11762
11763             template <typename T> struct S { struct T };
11764             template <typename T> struct S<T>::T { };
11765
11766          we will get a TYPENAME_TYPE when processing the definition of
11767          `S::T'.  We need to resolve it to the actual type before we
11768          try to define it.  */
11769       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11770         {
11771           class_type = resolve_typename_type (TREE_TYPE (type),
11772                                               /*only_current_p=*/false);
11773           if (class_type != error_mark_node)
11774             type = TYPE_NAME (class_type);
11775           else
11776             {
11777               cp_parser_error (parser, "could not resolve typename type");
11778               type = error_mark_node;
11779             }
11780         }
11781
11782       /* Figure out in what scope the declaration is being placed.  */
11783       scope = current_scope ();
11784       if (!scope)
11785         scope = current_namespace;
11786       /* If that scope does not contain the scope in which the
11787          class was originally declared, the program is invalid.  */
11788       if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11789         {
11790           error ("declaration of `%D' in `%D' which does not "
11791                  "enclose `%D'", type, scope, nested_name_specifier);
11792           return NULL_TREE;
11793         }
11794       /* [dcl.meaning]
11795
11796          A declarator-id shall not be qualified exception of the
11797          definition of a ... nested class outside of its class
11798          ... [or] a the definition or explicit instantiation of a
11799          class member of a namespace outside of its namespace.  */
11800       if (scope == CP_DECL_CONTEXT (type))
11801         {
11802           pedwarn ("extra qualification ignored");
11803           nested_name_specifier = NULL_TREE;
11804         }
11805
11806       maybe_process_partial_specialization (TREE_TYPE (type));
11807       class_type = current_class_type;
11808       /* Enter the scope indicated by the nested-name-specifier.  */
11809       if (nested_name_specifier)
11810         push_scope (nested_name_specifier);
11811       /* Get the canonical version of this type.  */
11812       type = TYPE_MAIN_DECL (TREE_TYPE (type));
11813       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
11814           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
11815         type = push_template_decl (type);
11816       type = TREE_TYPE (type);
11817       if (nested_name_specifier)
11818         *nested_name_specifier_p = true;
11819     }
11820   /* Indicate whether this class was declared as a `class' or as a
11821      `struct'.  */
11822   if (TREE_CODE (type) == RECORD_TYPE)
11823     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11824   cp_parser_check_class_key (class_key, type);
11825
11826   /* Enter the scope containing the class; the names of base classes
11827      should be looked up in that context.  For example, given:
11828
11829        struct A { struct B {}; struct C; };
11830        struct A::C : B {};
11831
11832      is valid.  */
11833   if (nested_name_specifier)
11834     push_scope (nested_name_specifier);
11835   /* Now, look for the base-clause.  */
11836   token = cp_lexer_peek_token (parser->lexer);
11837   if (token->type == CPP_COLON)
11838     {
11839       tree bases;
11840
11841       /* Get the list of base-classes.  */
11842       bases = cp_parser_base_clause (parser);
11843       /* Process them.  */
11844       xref_basetypes (type, bases);
11845     }
11846   /* Leave the scope given by the nested-name-specifier.  We will
11847      enter the class scope itself while processing the members.  */
11848   if (nested_name_specifier)
11849     pop_scope (nested_name_specifier);
11850
11851   return type;
11852 }
11853
11854 /* Parse a class-key.
11855
11856    class-key:
11857      class
11858      struct
11859      union
11860
11861    Returns the kind of class-key specified, or none_type to indicate
11862    error.  */
11863
11864 static enum tag_types
11865 cp_parser_class_key (cp_parser* parser)
11866 {
11867   cp_token *token;
11868   enum tag_types tag_type;
11869
11870   /* Look for the class-key.  */
11871   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11872   if (!token)
11873     return none_type;
11874
11875   /* Check to see if the TOKEN is a class-key.  */
11876   tag_type = cp_parser_token_is_class_key (token);
11877   if (!tag_type)
11878     cp_parser_error (parser, "expected class-key");
11879   return tag_type;
11880 }
11881
11882 /* Parse an (optional) member-specification.
11883
11884    member-specification:
11885      member-declaration member-specification [opt]
11886      access-specifier : member-specification [opt]  */
11887
11888 static void
11889 cp_parser_member_specification_opt (cp_parser* parser)
11890 {
11891   while (true)
11892     {
11893       cp_token *token;
11894       enum rid keyword;
11895
11896       /* Peek at the next token.  */
11897       token = cp_lexer_peek_token (parser->lexer);
11898       /* If it's a `}', or EOF then we've seen all the members.  */
11899       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11900         break;
11901
11902       /* See if this token is a keyword.  */
11903       keyword = token->keyword;
11904       switch (keyword)
11905         {
11906         case RID_PUBLIC:
11907         case RID_PROTECTED:
11908         case RID_PRIVATE:
11909           /* Consume the access-specifier.  */
11910           cp_lexer_consume_token (parser->lexer);
11911           /* Remember which access-specifier is active.  */
11912           current_access_specifier = token->value;
11913           /* Look for the `:'.  */
11914           cp_parser_require (parser, CPP_COLON, "`:'");
11915           break;
11916
11917         default:
11918           /* Otherwise, the next construction must be a
11919              member-declaration.  */
11920           cp_parser_member_declaration (parser);
11921         }
11922     }
11923 }
11924
11925 /* Parse a member-declaration.  
11926
11927    member-declaration:
11928      decl-specifier-seq [opt] member-declarator-list [opt] ;
11929      function-definition ; [opt]
11930      :: [opt] nested-name-specifier template [opt] unqualified-id ;
11931      using-declaration
11932      template-declaration 
11933
11934    member-declarator-list:
11935      member-declarator
11936      member-declarator-list , member-declarator
11937
11938    member-declarator:
11939      declarator pure-specifier [opt] 
11940      declarator constant-initializer [opt]
11941      identifier [opt] : constant-expression 
11942
11943    GNU Extensions:
11944
11945    member-declaration:
11946      __extension__ member-declaration
11947
11948    member-declarator:
11949      declarator attributes [opt] pure-specifier [opt]
11950      declarator attributes [opt] constant-initializer [opt]
11951      identifier [opt] attributes [opt] : constant-expression  */
11952
11953 static void
11954 cp_parser_member_declaration (cp_parser* parser)
11955 {
11956   tree decl_specifiers;
11957   tree prefix_attributes;
11958   tree decl;
11959   int declares_class_or_enum;
11960   bool friend_p;
11961   cp_token *token;
11962   int saved_pedantic;
11963
11964   /* Check for the `__extension__' keyword.  */
11965   if (cp_parser_extension_opt (parser, &saved_pedantic))
11966     {
11967       /* Recurse.  */
11968       cp_parser_member_declaration (parser);
11969       /* Restore the old value of the PEDANTIC flag.  */
11970       pedantic = saved_pedantic;
11971
11972       return;
11973     }
11974
11975   /* Check for a template-declaration.  */
11976   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11977     {
11978       /* Parse the template-declaration.  */
11979       cp_parser_template_declaration (parser, /*member_p=*/true);
11980
11981       return;
11982     }
11983
11984   /* Check for a using-declaration.  */
11985   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11986     {
11987       /* Parse the using-declaration.  */
11988       cp_parser_using_declaration (parser);
11989
11990       return;
11991     }
11992   
11993   /* We can't tell whether we're looking at a declaration or a
11994      function-definition.  */
11995   cp_parser_parse_tentatively (parser);
11996
11997   /* Parse the decl-specifier-seq.  */
11998   decl_specifiers 
11999     = cp_parser_decl_specifier_seq (parser,
12000                                     CP_PARSER_FLAGS_OPTIONAL,
12001                                     &prefix_attributes,
12002                                     &declares_class_or_enum);
12003   /* Check for an invalid type-name.  */
12004   if (cp_parser_diagnose_invalid_type_name (parser))
12005     return;
12006   /* If there is no declarator, then the decl-specifier-seq should
12007      specify a type.  */
12008   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12009     {
12010       /* If there was no decl-specifier-seq, and the next token is a
12011          `;', then we have something like:
12012
12013            struct S { ; };
12014
12015          [class.mem]
12016
12017          Each member-declaration shall declare at least one member
12018          name of the class.  */
12019       if (!decl_specifiers)
12020         {
12021           if (pedantic)
12022             pedwarn ("extra semicolon");
12023         }
12024       else 
12025         {
12026           tree type;
12027           
12028           /* See if this declaration is a friend.  */
12029           friend_p = cp_parser_friend_p (decl_specifiers);
12030           /* If there were decl-specifiers, check to see if there was
12031              a class-declaration.  */
12032           type = check_tag_decl (decl_specifiers);
12033           /* Nested classes have already been added to the class, but
12034              a `friend' needs to be explicitly registered.  */
12035           if (friend_p)
12036             {
12037               /* If the `friend' keyword was present, the friend must
12038                  be introduced with a class-key.  */
12039                if (!declares_class_or_enum)
12040                  error ("a class-key must be used when declaring a friend");
12041                /* In this case:
12042
12043                     template <typename T> struct A { 
12044                       friend struct A<T>::B; 
12045                     };
12046  
12047                   A<T>::B will be represented by a TYPENAME_TYPE, and
12048                   therefore not recognized by check_tag_decl.  */
12049                if (!type)
12050                  {
12051                    tree specifier;
12052
12053                    for (specifier = decl_specifiers; 
12054                         specifier;
12055                         specifier = TREE_CHAIN (specifier))
12056                      {
12057                        tree s = TREE_VALUE (specifier);
12058
12059                        if (TREE_CODE (s) == IDENTIFIER_NODE)
12060                          get_global_value_if_present (s, &type);
12061                        if (TREE_CODE (s) == TYPE_DECL)
12062                          s = TREE_TYPE (s);
12063                        if (TYPE_P (s))
12064                          {
12065                            type = s;
12066                            break;
12067                          }
12068                      }
12069                  }
12070                if (!type)
12071                  error ("friend declaration does not name a class or "
12072                         "function");
12073                else
12074                  make_friend_class (current_class_type, type,
12075                                     /*complain=*/true);
12076             }
12077           /* If there is no TYPE, an error message will already have
12078              been issued.  */
12079           else if (!type)
12080             ;
12081           /* An anonymous aggregate has to be handled specially; such
12082              a declaration really declares a data member (with a
12083              particular type), as opposed to a nested class.  */
12084           else if (ANON_AGGR_TYPE_P (type))
12085             {
12086               /* Remove constructors and such from TYPE, now that we
12087                  know it is an anonymous aggregate.  */
12088               fixup_anonymous_aggr (type);
12089               /* And make the corresponding data member.  */
12090               decl = build_decl (FIELD_DECL, NULL_TREE, type);
12091               /* Add it to the class.  */
12092               finish_member_declaration (decl);
12093             }
12094           else
12095             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12096         }
12097     }
12098   else
12099     {
12100       /* See if these declarations will be friends.  */
12101       friend_p = cp_parser_friend_p (decl_specifiers);
12102
12103       /* Keep going until we hit the `;' at the end of the 
12104          declaration.  */
12105       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12106         {
12107           tree attributes = NULL_TREE;
12108           tree first_attribute;
12109
12110           /* Peek at the next token.  */
12111           token = cp_lexer_peek_token (parser->lexer);
12112
12113           /* Check for a bitfield declaration.  */
12114           if (token->type == CPP_COLON
12115               || (token->type == CPP_NAME
12116                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type 
12117                   == CPP_COLON))
12118             {
12119               tree identifier;
12120               tree width;
12121
12122               /* Get the name of the bitfield.  Note that we cannot just
12123                  check TOKEN here because it may have been invalidated by
12124                  the call to cp_lexer_peek_nth_token above.  */
12125               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12126                 identifier = cp_parser_identifier (parser);
12127               else
12128                 identifier = NULL_TREE;
12129
12130               /* Consume the `:' token.  */
12131               cp_lexer_consume_token (parser->lexer);
12132               /* Get the width of the bitfield.  */
12133               width 
12134                 = cp_parser_constant_expression (parser,
12135                                                  /*allow_non_constant=*/false,
12136                                                  NULL);
12137
12138               /* Look for attributes that apply to the bitfield.  */
12139               attributes = cp_parser_attributes_opt (parser);
12140               /* Remember which attributes are prefix attributes and
12141                  which are not.  */
12142               first_attribute = attributes;
12143               /* Combine the attributes.  */
12144               attributes = chainon (prefix_attributes, attributes);
12145
12146               /* Create the bitfield declaration.  */
12147               decl = grokbitfield (identifier, 
12148                                    decl_specifiers,
12149                                    width);
12150               /* Apply the attributes.  */
12151               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12152             }
12153           else
12154             {
12155               tree declarator;
12156               tree initializer;
12157               tree asm_specification;
12158               int ctor_dtor_or_conv_p;
12159
12160               /* Parse the declarator.  */
12161               declarator 
12162                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12163                                         &ctor_dtor_or_conv_p);
12164
12165               /* If something went wrong parsing the declarator, make sure
12166                  that we at least consume some tokens.  */
12167               if (declarator == error_mark_node)
12168                 {
12169                   /* Skip to the end of the statement.  */
12170                   cp_parser_skip_to_end_of_statement (parser);
12171                   break;
12172                 }
12173
12174               cp_parser_check_for_definition_in_return_type 
12175                 (declarator, declares_class_or_enum);
12176
12177               /* Look for an asm-specification.  */
12178               asm_specification = cp_parser_asm_specification_opt (parser);
12179               /* Look for attributes that apply to the declaration.  */
12180               attributes = cp_parser_attributes_opt (parser);
12181               /* Remember which attributes are prefix attributes and
12182                  which are not.  */
12183               first_attribute = attributes;
12184               /* Combine the attributes.  */
12185               attributes = chainon (prefix_attributes, attributes);
12186
12187               /* If it's an `=', then we have a constant-initializer or a
12188                  pure-specifier.  It is not correct to parse the
12189                  initializer before registering the member declaration
12190                  since the member declaration should be in scope while
12191                  its initializer is processed.  However, the rest of the
12192                  front end does not yet provide an interface that allows
12193                  us to handle this correctly.  */
12194               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12195                 {
12196                   /* In [class.mem]:
12197
12198                      A pure-specifier shall be used only in the declaration of
12199                      a virtual function.  
12200
12201                      A member-declarator can contain a constant-initializer
12202                      only if it declares a static member of integral or
12203                      enumeration type.  
12204
12205                      Therefore, if the DECLARATOR is for a function, we look
12206                      for a pure-specifier; otherwise, we look for a
12207                      constant-initializer.  When we call `grokfield', it will
12208                      perform more stringent semantics checks.  */
12209                   if (TREE_CODE (declarator) == CALL_EXPR)
12210                     initializer = cp_parser_pure_specifier (parser);
12211                   else
12212                     {
12213                       /* This declaration cannot be a function
12214                          definition.  */
12215                       cp_parser_commit_to_tentative_parse (parser);
12216                       /* Parse the initializer.  */
12217                       initializer = cp_parser_constant_initializer (parser);
12218                     }
12219                 }
12220               /* Otherwise, there is no initializer.  */
12221               else
12222                 initializer = NULL_TREE;
12223
12224               /* See if we are probably looking at a function
12225                  definition.  We are certainly not looking at at a
12226                  member-declarator.  Calling `grokfield' has
12227                  side-effects, so we must not do it unless we are sure
12228                  that we are looking at a member-declarator.  */
12229               if (cp_parser_token_starts_function_definition_p 
12230                   (cp_lexer_peek_token (parser->lexer)))
12231                 decl = error_mark_node;
12232               else
12233                 {
12234                   /* Create the declaration.  */
12235                   decl = grokfield (declarator, decl_specifiers, 
12236                                     initializer, asm_specification,
12237                                     attributes);
12238                   /* Any initialization must have been from a
12239                      constant-expression.  */
12240                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12241                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12242                 }
12243             }
12244
12245           /* Reset PREFIX_ATTRIBUTES.  */
12246           while (attributes && TREE_CHAIN (attributes) != first_attribute)
12247             attributes = TREE_CHAIN (attributes);
12248           if (attributes)
12249             TREE_CHAIN (attributes) = NULL_TREE;
12250
12251           /* If there is any qualification still in effect, clear it
12252              now; we will be starting fresh with the next declarator.  */
12253           parser->scope = NULL_TREE;
12254           parser->qualifying_scope = NULL_TREE;
12255           parser->object_scope = NULL_TREE;
12256           /* If it's a `,', then there are more declarators.  */
12257           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12258             cp_lexer_consume_token (parser->lexer);
12259           /* If the next token isn't a `;', then we have a parse error.  */
12260           else if (cp_lexer_next_token_is_not (parser->lexer,
12261                                                CPP_SEMICOLON))
12262             {
12263               cp_parser_error (parser, "expected `;'");
12264               /* Skip tokens until we find a `;'.  */
12265               cp_parser_skip_to_end_of_statement (parser);
12266
12267               break;
12268             }
12269
12270           if (decl)
12271             {
12272               /* Add DECL to the list of members.  */
12273               if (!friend_p)
12274                 finish_member_declaration (decl);
12275
12276               if (TREE_CODE (decl) == FUNCTION_DECL)
12277                 cp_parser_save_default_args (parser, decl);
12278             }
12279         }
12280     }
12281
12282   /* If everything went well, look for the `;'.  */
12283   if (cp_parser_parse_definitely (parser))
12284     {
12285       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12286       return;
12287     }
12288
12289   /* Parse the function-definition.  */
12290   decl = cp_parser_function_definition (parser, &friend_p);
12291   /* If the member was not a friend, declare it here.  */
12292   if (!friend_p)
12293     finish_member_declaration (decl);
12294   /* Peek at the next token.  */
12295   token = cp_lexer_peek_token (parser->lexer);
12296   /* If the next token is a semicolon, consume it.  */
12297   if (token->type == CPP_SEMICOLON)
12298     cp_lexer_consume_token (parser->lexer);
12299 }
12300
12301 /* Parse a pure-specifier.
12302
12303    pure-specifier:
12304      = 0
12305
12306    Returns INTEGER_ZERO_NODE if a pure specifier is found.
12307    Otherwise, ERROR_MARK_NODE is returned.  */
12308
12309 static tree
12310 cp_parser_pure_specifier (cp_parser* parser)
12311 {
12312   cp_token *token;
12313
12314   /* Look for the `=' token.  */
12315   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12316     return error_mark_node;
12317   /* Look for the `0' token.  */
12318   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12319   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
12320      to get information from the lexer about how the number was
12321      spelled in order to fix this problem.  */
12322   if (!token || !integer_zerop (token->value))
12323     return error_mark_node;
12324
12325   return integer_zero_node;
12326 }
12327
12328 /* Parse a constant-initializer.
12329
12330    constant-initializer:
12331      = constant-expression
12332
12333    Returns a representation of the constant-expression.  */
12334
12335 static tree
12336 cp_parser_constant_initializer (cp_parser* parser)
12337 {
12338   /* Look for the `=' token.  */
12339   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12340     return error_mark_node;
12341
12342   /* It is invalid to write:
12343
12344        struct S { static const int i = { 7 }; };
12345
12346      */
12347   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12348     {
12349       cp_parser_error (parser,
12350                        "a brace-enclosed initializer is not allowed here");
12351       /* Consume the opening brace.  */
12352       cp_lexer_consume_token (parser->lexer);
12353       /* Skip the initializer.  */
12354       cp_parser_skip_to_closing_brace (parser);
12355       /* Look for the trailing `}'.  */
12356       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12357       
12358       return error_mark_node;
12359     }
12360
12361   return cp_parser_constant_expression (parser, 
12362                                         /*allow_non_constant=*/false,
12363                                         NULL);
12364 }
12365
12366 /* Derived classes [gram.class.derived] */
12367
12368 /* Parse a base-clause.
12369
12370    base-clause:
12371      : base-specifier-list  
12372
12373    base-specifier-list:
12374      base-specifier
12375      base-specifier-list , base-specifier
12376
12377    Returns a TREE_LIST representing the base-classes, in the order in
12378    which they were declared.  The representation of each node is as
12379    described by cp_parser_base_specifier.  
12380
12381    In the case that no bases are specified, this function will return
12382    NULL_TREE, not ERROR_MARK_NODE.  */
12383
12384 static tree
12385 cp_parser_base_clause (cp_parser* parser)
12386 {
12387   tree bases = NULL_TREE;
12388
12389   /* Look for the `:' that begins the list.  */
12390   cp_parser_require (parser, CPP_COLON, "`:'");
12391
12392   /* Scan the base-specifier-list.  */
12393   while (true)
12394     {
12395       cp_token *token;
12396       tree base;
12397
12398       /* Look for the base-specifier.  */
12399       base = cp_parser_base_specifier (parser);
12400       /* Add BASE to the front of the list.  */
12401       if (base != error_mark_node)
12402         {
12403           TREE_CHAIN (base) = bases;
12404           bases = base;
12405         }
12406       /* Peek at the next token.  */
12407       token = cp_lexer_peek_token (parser->lexer);
12408       /* If it's not a comma, then the list is complete.  */
12409       if (token->type != CPP_COMMA)
12410         break;
12411       /* Consume the `,'.  */
12412       cp_lexer_consume_token (parser->lexer);
12413     }
12414
12415   /* PARSER->SCOPE may still be non-NULL at this point, if the last
12416      base class had a qualified name.  However, the next name that
12417      appears is certainly not qualified.  */
12418   parser->scope = NULL_TREE;
12419   parser->qualifying_scope = NULL_TREE;
12420   parser->object_scope = NULL_TREE;
12421
12422   return nreverse (bases);
12423 }
12424
12425 /* Parse a base-specifier.
12426
12427    base-specifier:
12428      :: [opt] nested-name-specifier [opt] class-name
12429      virtual access-specifier [opt] :: [opt] nested-name-specifier
12430        [opt] class-name
12431      access-specifier virtual [opt] :: [opt] nested-name-specifier
12432        [opt] class-name
12433
12434    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
12435    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12436    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
12437    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
12438        
12439 static tree
12440 cp_parser_base_specifier (cp_parser* parser)
12441 {
12442   cp_token *token;
12443   bool done = false;
12444   bool virtual_p = false;
12445   bool duplicate_virtual_error_issued_p = false;
12446   bool duplicate_access_error_issued_p = false;
12447   bool class_scope_p, template_p;
12448   tree access = access_default_node;
12449   tree type;
12450
12451   /* Process the optional `virtual' and `access-specifier'.  */
12452   while (!done)
12453     {
12454       /* Peek at the next token.  */
12455       token = cp_lexer_peek_token (parser->lexer);
12456       /* Process `virtual'.  */
12457       switch (token->keyword)
12458         {
12459         case RID_VIRTUAL:
12460           /* If `virtual' appears more than once, issue an error.  */
12461           if (virtual_p && !duplicate_virtual_error_issued_p)
12462             {
12463               cp_parser_error (parser,
12464                                "`virtual' specified more than once in base-specified");
12465               duplicate_virtual_error_issued_p = true;
12466             }
12467
12468           virtual_p = true;
12469
12470           /* Consume the `virtual' token.  */
12471           cp_lexer_consume_token (parser->lexer);
12472
12473           break;
12474
12475         case RID_PUBLIC:
12476         case RID_PROTECTED:
12477         case RID_PRIVATE:
12478           /* If more than one access specifier appears, issue an
12479              error.  */
12480           if (access != access_default_node
12481               && !duplicate_access_error_issued_p)
12482             {
12483               cp_parser_error (parser,
12484                                "more than one access specifier in base-specified");
12485               duplicate_access_error_issued_p = true;
12486             }
12487
12488           access = ridpointers[(int) token->keyword];
12489
12490           /* Consume the access-specifier.  */
12491           cp_lexer_consume_token (parser->lexer);
12492
12493           break;
12494
12495         default:
12496           done = true;
12497           break;
12498         }
12499     }
12500
12501   /* Look for the optional `::' operator.  */
12502   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12503   /* Look for the nested-name-specifier.  The simplest way to
12504      implement:
12505
12506        [temp.res]
12507
12508        The keyword `typename' is not permitted in a base-specifier or
12509        mem-initializer; in these contexts a qualified name that
12510        depends on a template-parameter is implicitly assumed to be a
12511        type name.
12512
12513      is to pretend that we have seen the `typename' keyword at this
12514      point.  */ 
12515   cp_parser_nested_name_specifier_opt (parser,
12516                                        /*typename_keyword_p=*/true,
12517                                        /*check_dependency_p=*/true,
12518                                        /*type_p=*/true,
12519                                        /*is_declaration=*/true);
12520   /* If the base class is given by a qualified name, assume that names
12521      we see are type names or templates, as appropriate.  */
12522   class_scope_p = (parser->scope && TYPE_P (parser->scope));
12523   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12524   
12525   /* Finally, look for the class-name.  */
12526   type = cp_parser_class_name (parser, 
12527                                class_scope_p,
12528                                template_p,
12529                                /*type_p=*/true,
12530                                /*check_dependency_p=*/true,
12531                                /*class_head_p=*/false,
12532                                /*is_declaration=*/true);
12533
12534   if (type == error_mark_node)
12535     return error_mark_node;
12536
12537   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12538 }
12539
12540 /* Exception handling [gram.exception] */
12541
12542 /* Parse an (optional) exception-specification.
12543
12544    exception-specification:
12545      throw ( type-id-list [opt] )
12546
12547    Returns a TREE_LIST representing the exception-specification.  The
12548    TREE_VALUE of each node is a type.  */
12549
12550 static tree
12551 cp_parser_exception_specification_opt (cp_parser* parser)
12552 {
12553   cp_token *token;
12554   tree type_id_list;
12555
12556   /* Peek at the next token.  */
12557   token = cp_lexer_peek_token (parser->lexer);
12558   /* If it's not `throw', then there's no exception-specification.  */
12559   if (!cp_parser_is_keyword (token, RID_THROW))
12560     return NULL_TREE;
12561
12562   /* Consume the `throw'.  */
12563   cp_lexer_consume_token (parser->lexer);
12564
12565   /* Look for the `('.  */
12566   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12567
12568   /* Peek at the next token.  */
12569   token = cp_lexer_peek_token (parser->lexer);
12570   /* If it's not a `)', then there is a type-id-list.  */
12571   if (token->type != CPP_CLOSE_PAREN)
12572     {
12573       const char *saved_message;
12574
12575       /* Types may not be defined in an exception-specification.  */
12576       saved_message = parser->type_definition_forbidden_message;
12577       parser->type_definition_forbidden_message
12578         = "types may not be defined in an exception-specification";
12579       /* Parse the type-id-list.  */
12580       type_id_list = cp_parser_type_id_list (parser);
12581       /* Restore the saved message.  */
12582       parser->type_definition_forbidden_message = saved_message;
12583     }
12584   else
12585     type_id_list = empty_except_spec;
12586
12587   /* Look for the `)'.  */
12588   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12589
12590   return type_id_list;
12591 }
12592
12593 /* Parse an (optional) type-id-list.
12594
12595    type-id-list:
12596      type-id
12597      type-id-list , type-id
12598
12599    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
12600    in the order that the types were presented.  */
12601
12602 static tree
12603 cp_parser_type_id_list (cp_parser* parser)
12604 {
12605   tree types = NULL_TREE;
12606
12607   while (true)
12608     {
12609       cp_token *token;
12610       tree type;
12611
12612       /* Get the next type-id.  */
12613       type = cp_parser_type_id (parser);
12614       /* Add it to the list.  */
12615       types = add_exception_specifier (types, type, /*complain=*/1);
12616       /* Peek at the next token.  */
12617       token = cp_lexer_peek_token (parser->lexer);
12618       /* If it is not a `,', we are done.  */
12619       if (token->type != CPP_COMMA)
12620         break;
12621       /* Consume the `,'.  */
12622       cp_lexer_consume_token (parser->lexer);
12623     }
12624
12625   return nreverse (types);
12626 }
12627
12628 /* Parse a try-block.
12629
12630    try-block:
12631      try compound-statement handler-seq  */
12632
12633 static tree
12634 cp_parser_try_block (cp_parser* parser)
12635 {
12636   tree try_block;
12637
12638   cp_parser_require_keyword (parser, RID_TRY, "`try'");
12639   try_block = begin_try_block ();
12640   cp_parser_compound_statement (parser, false);
12641   finish_try_block (try_block);
12642   cp_parser_handler_seq (parser);
12643   finish_handler_sequence (try_block);
12644
12645   return try_block;
12646 }
12647
12648 /* Parse a function-try-block.
12649
12650    function-try-block:
12651      try ctor-initializer [opt] function-body handler-seq  */
12652
12653 static bool
12654 cp_parser_function_try_block (cp_parser* parser)
12655 {
12656   tree try_block;
12657   bool ctor_initializer_p;
12658
12659   /* Look for the `try' keyword.  */
12660   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12661     return false;
12662   /* Let the rest of the front-end know where we are.  */
12663   try_block = begin_function_try_block ();
12664   /* Parse the function-body.  */
12665   ctor_initializer_p 
12666     = cp_parser_ctor_initializer_opt_and_function_body (parser);
12667   /* We're done with the `try' part.  */
12668   finish_function_try_block (try_block);
12669   /* Parse the handlers.  */
12670   cp_parser_handler_seq (parser);
12671   /* We're done with the handlers.  */
12672   finish_function_handler_sequence (try_block);
12673
12674   return ctor_initializer_p;
12675 }
12676
12677 /* Parse a handler-seq.
12678
12679    handler-seq:
12680      handler handler-seq [opt]  */
12681
12682 static void
12683 cp_parser_handler_seq (cp_parser* parser)
12684 {
12685   while (true)
12686     {
12687       cp_token *token;
12688
12689       /* Parse the handler.  */
12690       cp_parser_handler (parser);
12691       /* Peek at the next token.  */
12692       token = cp_lexer_peek_token (parser->lexer);
12693       /* If it's not `catch' then there are no more handlers.  */
12694       if (!cp_parser_is_keyword (token, RID_CATCH))
12695         break;
12696     }
12697 }
12698
12699 /* Parse a handler.
12700
12701    handler:
12702      catch ( exception-declaration ) compound-statement  */
12703
12704 static void
12705 cp_parser_handler (cp_parser* parser)
12706 {
12707   tree handler;
12708   tree declaration;
12709
12710   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12711   handler = begin_handler ();
12712   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12713   declaration = cp_parser_exception_declaration (parser);
12714   finish_handler_parms (declaration, handler);
12715   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12716   cp_parser_compound_statement (parser, false);
12717   finish_handler (handler);
12718 }
12719
12720 /* Parse an exception-declaration.
12721
12722    exception-declaration:
12723      type-specifier-seq declarator
12724      type-specifier-seq abstract-declarator
12725      type-specifier-seq
12726      ...  
12727
12728    Returns a VAR_DECL for the declaration, or NULL_TREE if the
12729    ellipsis variant is used.  */
12730
12731 static tree
12732 cp_parser_exception_declaration (cp_parser* parser)
12733 {
12734   tree type_specifiers;
12735   tree declarator;
12736   const char *saved_message;
12737
12738   /* If it's an ellipsis, it's easy to handle.  */
12739   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12740     {
12741       /* Consume the `...' token.  */
12742       cp_lexer_consume_token (parser->lexer);
12743       return NULL_TREE;
12744     }
12745
12746   /* Types may not be defined in exception-declarations.  */
12747   saved_message = parser->type_definition_forbidden_message;
12748   parser->type_definition_forbidden_message
12749     = "types may not be defined in exception-declarations";
12750
12751   /* Parse the type-specifier-seq.  */
12752   type_specifiers = cp_parser_type_specifier_seq (parser);
12753   /* If it's a `)', then there is no declarator.  */
12754   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12755     declarator = NULL_TREE;
12756   else
12757     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12758                                        /*ctor_dtor_or_conv_p=*/NULL);
12759
12760   /* Restore the saved message.  */
12761   parser->type_definition_forbidden_message = saved_message;
12762
12763   return start_handler_parms (type_specifiers, declarator);
12764 }
12765
12766 /* Parse a throw-expression. 
12767
12768    throw-expression:
12769      throw assignment-expression [opt]
12770
12771    Returns a THROW_EXPR representing the throw-expression.  */
12772
12773 static tree
12774 cp_parser_throw_expression (cp_parser* parser)
12775 {
12776   tree expression;
12777
12778   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12779   /* We can't be sure if there is an assignment-expression or not.  */
12780   cp_parser_parse_tentatively (parser);
12781   /* Try it.  */
12782   expression = cp_parser_assignment_expression (parser);
12783   /* If it didn't work, this is just a rethrow.  */
12784   if (!cp_parser_parse_definitely (parser))
12785     expression = NULL_TREE;
12786
12787   return build_throw (expression);
12788 }
12789
12790 /* GNU Extensions */
12791
12792 /* Parse an (optional) asm-specification.
12793
12794    asm-specification:
12795      asm ( string-literal )
12796
12797    If the asm-specification is present, returns a STRING_CST
12798    corresponding to the string-literal.  Otherwise, returns
12799    NULL_TREE.  */
12800
12801 static tree
12802 cp_parser_asm_specification_opt (cp_parser* parser)
12803 {
12804   cp_token *token;
12805   tree asm_specification;
12806
12807   /* Peek at the next token.  */
12808   token = cp_lexer_peek_token (parser->lexer);
12809   /* If the next token isn't the `asm' keyword, then there's no 
12810      asm-specification.  */
12811   if (!cp_parser_is_keyword (token, RID_ASM))
12812     return NULL_TREE;
12813
12814   /* Consume the `asm' token.  */
12815   cp_lexer_consume_token (parser->lexer);
12816   /* Look for the `('.  */
12817   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12818
12819   /* Look for the string-literal.  */
12820   token = cp_parser_require (parser, CPP_STRING, "string-literal");
12821   if (token)
12822     asm_specification = token->value;
12823   else
12824     asm_specification = NULL_TREE;
12825
12826   /* Look for the `)'.  */
12827   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12828
12829   return asm_specification;
12830 }
12831
12832 /* Parse an asm-operand-list.  
12833
12834    asm-operand-list:
12835      asm-operand
12836      asm-operand-list , asm-operand
12837      
12838    asm-operand:
12839      string-literal ( expression )  
12840      [ string-literal ] string-literal ( expression )
12841
12842    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
12843    each node is the expression.  The TREE_PURPOSE is itself a
12844    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12845    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12846    is a STRING_CST for the string literal before the parenthesis.  */
12847
12848 static tree
12849 cp_parser_asm_operand_list (cp_parser* parser)
12850 {
12851   tree asm_operands = NULL_TREE;
12852
12853   while (true)
12854     {
12855       tree string_literal;
12856       tree expression;
12857       tree name;
12858       cp_token *token;
12859       
12860       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 
12861         {
12862           /* Consume the `[' token.  */
12863           cp_lexer_consume_token (parser->lexer);
12864           /* Read the operand name.  */
12865           name = cp_parser_identifier (parser);
12866           if (name != error_mark_node) 
12867             name = build_string (IDENTIFIER_LENGTH (name),
12868                                  IDENTIFIER_POINTER (name));
12869           /* Look for the closing `]'.  */
12870           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12871         }
12872       else
12873         name = NULL_TREE;
12874       /* Look for the string-literal.  */
12875       token = cp_parser_require (parser, CPP_STRING, "string-literal");
12876       string_literal = token ? token->value : error_mark_node;
12877       /* Look for the `('.  */
12878       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12879       /* Parse the expression.  */
12880       expression = cp_parser_expression (parser);
12881       /* Look for the `)'.  */
12882       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12883       /* Add this operand to the list.  */
12884       asm_operands = tree_cons (build_tree_list (name, string_literal),
12885                                 expression, 
12886                                 asm_operands);
12887       /* If the next token is not a `,', there are no more 
12888          operands.  */
12889       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12890         break;
12891       /* Consume the `,'.  */
12892       cp_lexer_consume_token (parser->lexer);
12893     }
12894
12895   return nreverse (asm_operands);
12896 }
12897
12898 /* Parse an asm-clobber-list.  
12899
12900    asm-clobber-list:
12901      string-literal
12902      asm-clobber-list , string-literal  
12903
12904    Returns a TREE_LIST, indicating the clobbers in the order that they
12905    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
12906
12907 static tree
12908 cp_parser_asm_clobber_list (cp_parser* parser)
12909 {
12910   tree clobbers = NULL_TREE;
12911
12912   while (true)
12913     {
12914       cp_token *token;
12915       tree string_literal;
12916
12917       /* Look for the string literal.  */
12918       token = cp_parser_require (parser, CPP_STRING, "string-literal");
12919       string_literal = token ? token->value : error_mark_node;
12920       /* Add it to the list.  */
12921       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12922       /* If the next token is not a `,', then the list is 
12923          complete.  */
12924       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12925         break;
12926       /* Consume the `,' token.  */
12927       cp_lexer_consume_token (parser->lexer);
12928     }
12929
12930   return clobbers;
12931 }
12932
12933 /* Parse an (optional) series of attributes.
12934
12935    attributes:
12936      attributes attribute
12937
12938    attribute:
12939      __attribute__ (( attribute-list [opt] ))  
12940
12941    The return value is as for cp_parser_attribute_list.  */
12942      
12943 static tree
12944 cp_parser_attributes_opt (cp_parser* parser)
12945 {
12946   tree attributes = NULL_TREE;
12947
12948   while (true)
12949     {
12950       cp_token *token;
12951       tree attribute_list;
12952
12953       /* Peek at the next token.  */
12954       token = cp_lexer_peek_token (parser->lexer);
12955       /* If it's not `__attribute__', then we're done.  */
12956       if (token->keyword != RID_ATTRIBUTE)
12957         break;
12958
12959       /* Consume the `__attribute__' keyword.  */
12960       cp_lexer_consume_token (parser->lexer);
12961       /* Look for the two `(' tokens.  */
12962       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12963       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12964
12965       /* Peek at the next token.  */
12966       token = cp_lexer_peek_token (parser->lexer);
12967       if (token->type != CPP_CLOSE_PAREN)
12968         /* Parse the attribute-list.  */
12969         attribute_list = cp_parser_attribute_list (parser);
12970       else
12971         /* If the next token is a `)', then there is no attribute
12972            list.  */
12973         attribute_list = NULL;
12974
12975       /* Look for the two `)' tokens.  */
12976       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12977       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12978
12979       /* Add these new attributes to the list.  */
12980       attributes = chainon (attributes, attribute_list);
12981     }
12982
12983   return attributes;
12984 }
12985
12986 /* Parse an attribute-list.  
12987
12988    attribute-list:  
12989      attribute 
12990      attribute-list , attribute
12991
12992    attribute:
12993      identifier     
12994      identifier ( identifier )
12995      identifier ( identifier , expression-list )
12996      identifier ( expression-list ) 
12997
12998    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
12999    TREE_PURPOSE of each node is the identifier indicating which
13000    attribute is in use.  The TREE_VALUE represents the arguments, if
13001    any.  */
13002
13003 static tree
13004 cp_parser_attribute_list (cp_parser* parser)
13005 {
13006   tree attribute_list = NULL_TREE;
13007
13008   while (true)
13009     {
13010       cp_token *token;
13011       tree identifier;
13012       tree attribute;
13013
13014       /* Look for the identifier.  We also allow keywords here; for
13015          example `__attribute__ ((const))' is legal.  */
13016       token = cp_lexer_peek_token (parser->lexer);
13017       if (token->type != CPP_NAME 
13018           && token->type != CPP_KEYWORD)
13019         return error_mark_node;
13020       /* Consume the token.  */
13021       token = cp_lexer_consume_token (parser->lexer);
13022       
13023       /* Save away the identifier that indicates which attribute this is.  */
13024       identifier = token->value;
13025       attribute = build_tree_list (identifier, NULL_TREE);
13026
13027       /* Peek at the next token.  */
13028       token = cp_lexer_peek_token (parser->lexer);
13029       /* If it's an `(', then parse the attribute arguments.  */
13030       if (token->type == CPP_OPEN_PAREN)
13031         {
13032           tree arguments;
13033
13034           arguments = (cp_parser_parenthesized_expression_list 
13035                        (parser, true, /*non_constant_p=*/NULL));
13036           /* Save the identifier and arguments away.  */
13037           TREE_VALUE (attribute) = arguments;
13038         }
13039
13040       /* Add this attribute to the list.  */
13041       TREE_CHAIN (attribute) = attribute_list;
13042       attribute_list = attribute;
13043
13044       /* Now, look for more attributes.  */
13045       token = cp_lexer_peek_token (parser->lexer);
13046       /* If the next token isn't a `,', we're done.  */
13047       if (token->type != CPP_COMMA)
13048         break;
13049
13050       /* Consume the comma and keep going.  */
13051       cp_lexer_consume_token (parser->lexer);
13052     }
13053
13054   /* We built up the list in reverse order.  */
13055   return nreverse (attribute_list);
13056 }
13057
13058 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13059    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13060    current value of the PEDANTIC flag, regardless of whether or not
13061    the `__extension__' keyword is present.  The caller is responsible
13062    for restoring the value of the PEDANTIC flag.  */
13063
13064 static bool
13065 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13066 {
13067   /* Save the old value of the PEDANTIC flag.  */
13068   *saved_pedantic = pedantic;
13069
13070   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13071     {
13072       /* Consume the `__extension__' token.  */
13073       cp_lexer_consume_token (parser->lexer);
13074       /* We're not being pedantic while the `__extension__' keyword is
13075          in effect.  */
13076       pedantic = 0;
13077
13078       return true;
13079     }
13080
13081   return false;
13082 }
13083
13084 /* Parse a label declaration.
13085
13086    label-declaration:
13087      __label__ label-declarator-seq ;
13088
13089    label-declarator-seq:
13090      identifier , label-declarator-seq
13091      identifier  */
13092
13093 static void
13094 cp_parser_label_declaration (cp_parser* parser)
13095 {
13096   /* Look for the `__label__' keyword.  */
13097   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13098
13099   while (true)
13100     {
13101       tree identifier;
13102
13103       /* Look for an identifier.  */
13104       identifier = cp_parser_identifier (parser);
13105       /* Declare it as a lobel.  */
13106       finish_label_decl (identifier);
13107       /* If the next token is a `;', stop.  */
13108       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13109         break;
13110       /* Look for the `,' separating the label declarations.  */
13111       cp_parser_require (parser, CPP_COMMA, "`,'");
13112     }
13113
13114   /* Look for the final `;'.  */
13115   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13116 }
13117
13118 /* Support Functions */
13119
13120 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13121    NAME should have one of the representations used for an
13122    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13123    is returned.  If PARSER->SCOPE is a dependent type, then a
13124    SCOPE_REF is returned.
13125
13126    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13127    returned; the name was already resolved when the TEMPLATE_ID_EXPR
13128    was formed.  Abstractly, such entities should not be passed to this
13129    function, because they do not need to be looked up, but it is
13130    simpler to check for this special case here, rather than at the
13131    call-sites.
13132
13133    In cases not explicitly covered above, this function returns a
13134    DECL, OVERLOAD, or baselink representing the result of the lookup.
13135    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13136    is returned.
13137
13138    If IS_TYPE is TRUE, bindings that do not refer to types are
13139    ignored.
13140
13141    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13142    are ignored.
13143
13144    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13145    types.  */
13146
13147 static tree
13148 cp_parser_lookup_name (cp_parser *parser, tree name, 
13149                        bool is_type, bool is_namespace, bool check_dependency)
13150 {
13151   tree decl;
13152   tree object_type = parser->context->object_type;
13153
13154   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13155      no longer valid.  Note that if we are parsing tentatively, and
13156      the parse fails, OBJECT_TYPE will be automatically restored.  */
13157   parser->context->object_type = NULL_TREE;
13158
13159   if (name == error_mark_node)
13160     return error_mark_node;
13161
13162   /* A template-id has already been resolved; there is no lookup to
13163      do.  */
13164   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13165     return name;
13166   if (BASELINK_P (name))
13167     {
13168       my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13169                            == TEMPLATE_ID_EXPR),
13170                           20020909);
13171       return name;
13172     }
13173
13174   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
13175      it should already have been checked to make sure that the name
13176      used matches the type being destroyed.  */
13177   if (TREE_CODE (name) == BIT_NOT_EXPR)
13178     {
13179       tree type;
13180
13181       /* Figure out to which type this destructor applies.  */
13182       if (parser->scope)
13183         type = parser->scope;
13184       else if (object_type)
13185         type = object_type;
13186       else
13187         type = current_class_type;
13188       /* If that's not a class type, there is no destructor.  */
13189       if (!type || !CLASS_TYPE_P (type))
13190         return error_mark_node;
13191       /* If it was a class type, return the destructor.  */
13192       return CLASSTYPE_DESTRUCTORS (type);
13193     }
13194
13195   /* By this point, the NAME should be an ordinary identifier.  If
13196      the id-expression was a qualified name, the qualifying scope is
13197      stored in PARSER->SCOPE at this point.  */
13198   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13199                       20000619);
13200   
13201   /* Perform the lookup.  */
13202   if (parser->scope)
13203     { 
13204       bool dependent_p;
13205
13206       if (parser->scope == error_mark_node)
13207         return error_mark_node;
13208
13209       /* If the SCOPE is dependent, the lookup must be deferred until
13210          the template is instantiated -- unless we are explicitly
13211          looking up names in uninstantiated templates.  Even then, we
13212          cannot look up the name if the scope is not a class type; it
13213          might, for example, be a template type parameter.  */
13214       dependent_p = (TYPE_P (parser->scope)
13215                      && !(parser->in_declarator_p
13216                           && currently_open_class (parser->scope))
13217                      && dependent_type_p (parser->scope));
13218       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13219            && dependent_p)
13220         {
13221           if (!is_type)
13222             decl = build_nt (SCOPE_REF, parser->scope, name);
13223           else
13224             /* The resolution to Core Issue 180 says that `struct A::B'
13225                should be considered a type-name, even if `A' is
13226                dependent.  */
13227             decl = TYPE_NAME (make_typename_type (parser->scope,
13228                                                   name,
13229                                                   /*complain=*/1));
13230         }
13231       else
13232         {
13233           /* If PARSER->SCOPE is a dependent type, then it must be a
13234              class type, and we must not be checking dependencies;
13235              otherwise, we would have processed this lookup above.  So
13236              that PARSER->SCOPE is not considered a dependent base by
13237              lookup_member, we must enter the scope here.  */
13238           if (dependent_p)
13239             push_scope (parser->scope);
13240           /* If the PARSER->SCOPE is a a template specialization, it
13241              may be instantiated during name lookup.  In that case,
13242              errors may be issued.  Even if we rollback the current
13243              tentative parse, those errors are valid.  */
13244           decl = lookup_qualified_name (parser->scope, name, is_type,
13245                                         /*complain=*/true);
13246           if (dependent_p)
13247             pop_scope (parser->scope);
13248         }
13249       parser->qualifying_scope = parser->scope;
13250       parser->object_scope = NULL_TREE;
13251     }
13252   else if (object_type)
13253     {
13254       tree object_decl = NULL_TREE;
13255       /* Look up the name in the scope of the OBJECT_TYPE, unless the
13256          OBJECT_TYPE is not a class.  */
13257       if (CLASS_TYPE_P (object_type))
13258         /* If the OBJECT_TYPE is a template specialization, it may
13259            be instantiated during name lookup.  In that case, errors
13260            may be issued.  Even if we rollback the current tentative
13261            parse, those errors are valid.  */
13262         object_decl = lookup_member (object_type,
13263                                      name,
13264                                      /*protect=*/0, is_type);
13265       /* Look it up in the enclosing context, too.  */
13266       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13267                                is_namespace,
13268                                /*flags=*/0);
13269       parser->object_scope = object_type;
13270       parser->qualifying_scope = NULL_TREE;
13271       if (object_decl)
13272         decl = object_decl;
13273     }
13274   else
13275     {
13276       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13277                                is_namespace,
13278                                /*flags=*/0);
13279       parser->qualifying_scope = NULL_TREE;
13280       parser->object_scope = NULL_TREE;
13281     }
13282
13283   /* If the lookup failed, let our caller know.  */
13284   if (!decl 
13285       || decl == error_mark_node
13286       || (TREE_CODE (decl) == FUNCTION_DECL 
13287           && DECL_ANTICIPATED (decl)))
13288     return error_mark_node;
13289
13290   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
13291   if (TREE_CODE (decl) == TREE_LIST)
13292     {
13293       /* The error message we have to print is too complicated for
13294          cp_parser_error, so we incorporate its actions directly.  */
13295       if (!cp_parser_simulate_error (parser))
13296         {
13297           error ("reference to `%D' is ambiguous", name);
13298           print_candidates (decl);
13299         }
13300       return error_mark_node;
13301     }
13302
13303   my_friendly_assert (DECL_P (decl) 
13304                       || TREE_CODE (decl) == OVERLOAD
13305                       || TREE_CODE (decl) == SCOPE_REF
13306                       || BASELINK_P (decl),
13307                       20000619);
13308
13309   /* If we have resolved the name of a member declaration, check to
13310      see if the declaration is accessible.  When the name resolves to
13311      set of overloaded functions, accessibility is checked when
13312      overload resolution is done.  
13313
13314      During an explicit instantiation, access is not checked at all,
13315      as per [temp.explicit].  */
13316   if (DECL_P (decl))
13317     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13318
13319   return decl;
13320 }
13321
13322 /* Like cp_parser_lookup_name, but for use in the typical case where
13323    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13324    TRUE.  */
13325
13326 static tree
13327 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13328 {
13329   return cp_parser_lookup_name (parser, name, 
13330                                 /*is_type=*/false,
13331                                 /*is_namespace=*/false,
13332                                 /*check_dependency=*/true);
13333 }
13334
13335 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13336    the current context, return the TYPE_DECL.  If TAG_NAME_P is
13337    true, the DECL indicates the class being defined in a class-head,
13338    or declared in an elaborated-type-specifier.
13339
13340    Otherwise, return DECL.  */
13341
13342 static tree
13343 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13344 {
13345   /* If the TEMPLATE_DECL is being declared as part of a class-head,
13346      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13347
13348        struct A { 
13349          template <typename T> struct B;
13350        };
13351
13352        template <typename T> struct A::B {}; 
13353    
13354      Similarly, in a elaborated-type-specifier:
13355
13356        namespace N { struct X{}; }
13357
13358        struct A {
13359          template <typename T> friend struct N::X;
13360        };
13361
13362      However, if the DECL refers to a class type, and we are in
13363      the scope of the class, then the name lookup automatically
13364      finds the TYPE_DECL created by build_self_reference rather
13365      than a TEMPLATE_DECL.  For example, in:
13366
13367        template <class T> struct S {
13368          S s;
13369        };
13370
13371      there is no need to handle such case.  */
13372
13373   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13374     return DECL_TEMPLATE_RESULT (decl);
13375
13376   return decl;
13377 }
13378
13379 /* If too many, or too few, template-parameter lists apply to the
13380    declarator, issue an error message.  Returns TRUE if all went well,
13381    and FALSE otherwise.  */
13382
13383 static bool
13384 cp_parser_check_declarator_template_parameters (cp_parser* parser, 
13385                                                 tree declarator)
13386 {
13387   unsigned num_templates;
13388
13389   /* We haven't seen any classes that involve template parameters yet.  */
13390   num_templates = 0;
13391
13392   switch (TREE_CODE (declarator))
13393     {
13394     case CALL_EXPR:
13395     case ARRAY_REF:
13396     case INDIRECT_REF:
13397     case ADDR_EXPR:
13398       {
13399         tree main_declarator = TREE_OPERAND (declarator, 0);
13400         return
13401           cp_parser_check_declarator_template_parameters (parser, 
13402                                                           main_declarator);
13403       }
13404
13405     case SCOPE_REF:
13406       {
13407         tree scope;
13408         tree member;
13409
13410         scope = TREE_OPERAND (declarator, 0);
13411         member = TREE_OPERAND (declarator, 1);
13412
13413         /* If this is a pointer-to-member, then we are not interested
13414            in the SCOPE, because it does not qualify the thing that is
13415            being declared.  */
13416         if (TREE_CODE (member) == INDIRECT_REF)
13417           return (cp_parser_check_declarator_template_parameters
13418                   (parser, member));
13419
13420         while (scope && CLASS_TYPE_P (scope))
13421           {
13422             /* You're supposed to have one `template <...>'
13423                for every template class, but you don't need one
13424                for a full specialization.  For example:
13425                
13426                template <class T> struct S{};
13427                template <> struct S<int> { void f(); };
13428                void S<int>::f () {}
13429                
13430                is correct; there shouldn't be a `template <>' for
13431                the definition of `S<int>::f'.  */
13432             if (CLASSTYPE_TEMPLATE_INFO (scope)
13433                 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13434                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13435                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13436               ++num_templates;
13437
13438             scope = TYPE_CONTEXT (scope);
13439           }
13440       }
13441
13442       /* Fall through.  */
13443
13444     default:
13445       /* If the DECLARATOR has the form `X<y>' then it uses one
13446          additional level of template parameters.  */
13447       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13448         ++num_templates;
13449
13450       return cp_parser_check_template_parameters (parser, 
13451                                                   num_templates);
13452     }
13453 }
13454
13455 /* NUM_TEMPLATES were used in the current declaration.  If that is
13456    invalid, return FALSE and issue an error messages.  Otherwise,
13457    return TRUE.  */
13458
13459 static bool
13460 cp_parser_check_template_parameters (cp_parser* parser,
13461                                      unsigned num_templates)
13462 {
13463   /* If there are more template classes than parameter lists, we have
13464      something like:
13465      
13466        template <class T> void S<T>::R<T>::f ();  */
13467   if (parser->num_template_parameter_lists < num_templates)
13468     {
13469       error ("too few template-parameter-lists");
13470       return false;
13471     }
13472   /* If there are the same number of template classes and parameter
13473      lists, that's OK.  */
13474   if (parser->num_template_parameter_lists == num_templates)
13475     return true;
13476   /* If there are more, but only one more, then we are referring to a
13477      member template.  That's OK too.  */
13478   if (parser->num_template_parameter_lists == num_templates + 1)
13479       return true;
13480   /* Otherwise, there are too many template parameter lists.  We have
13481      something like:
13482
13483      template <class T> template <class U> void S::f();  */
13484   error ("too many template-parameter-lists");
13485   return false;
13486 }
13487
13488 /* Parse a binary-expression of the general form:
13489
13490    binary-expression:
13491      <expr>
13492      binary-expression <token> <expr>
13493
13494    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
13495    to parser the <expr>s.  If the first production is used, then the
13496    value returned by FN is returned directly.  Otherwise, a node with
13497    the indicated EXPR_TYPE is returned, with operands corresponding to
13498    the two sub-expressions.  */
13499
13500 static tree
13501 cp_parser_binary_expression (cp_parser* parser, 
13502                              const cp_parser_token_tree_map token_tree_map, 
13503                              cp_parser_expression_fn fn)
13504 {
13505   tree lhs;
13506
13507   /* Parse the first expression.  */
13508   lhs = (*fn) (parser);
13509   /* Now, look for more expressions.  */
13510   while (true)
13511     {
13512       cp_token *token;
13513       const cp_parser_token_tree_map_node *map_node;
13514       tree rhs;
13515
13516       /* Peek at the next token.  */
13517       token = cp_lexer_peek_token (parser->lexer);
13518       /* If the token is `>', and that's not an operator at the
13519          moment, then we're done.  */
13520       if (token->type == CPP_GREATER
13521           && !parser->greater_than_is_operator_p)
13522         break;
13523       /* If we find one of the tokens we want, build the corresponding
13524          tree representation.  */
13525       for (map_node = token_tree_map; 
13526            map_node->token_type != CPP_EOF;
13527            ++map_node)
13528         if (map_node->token_type == token->type)
13529           {
13530             /* Consume the operator token.  */
13531             cp_lexer_consume_token (parser->lexer);
13532             /* Parse the right-hand side of the expression.  */
13533             rhs = (*fn) (parser);
13534             /* Build the binary tree node.  */
13535             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13536             break;
13537           }
13538
13539       /* If the token wasn't one of the ones we want, we're done.  */
13540       if (map_node->token_type == CPP_EOF)
13541         break;
13542     }
13543
13544   return lhs;
13545 }
13546
13547 /* Parse an optional `::' token indicating that the following name is
13548    from the global namespace.  If so, PARSER->SCOPE is set to the
13549    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13550    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13551    Returns the new value of PARSER->SCOPE, if the `::' token is
13552    present, and NULL_TREE otherwise.  */
13553
13554 static tree
13555 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13556 {
13557   cp_token *token;
13558
13559   /* Peek at the next token.  */
13560   token = cp_lexer_peek_token (parser->lexer);
13561   /* If we're looking at a `::' token then we're starting from the
13562      global namespace, not our current location.  */
13563   if (token->type == CPP_SCOPE)
13564     {
13565       /* Consume the `::' token.  */
13566       cp_lexer_consume_token (parser->lexer);
13567       /* Set the SCOPE so that we know where to start the lookup.  */
13568       parser->scope = global_namespace;
13569       parser->qualifying_scope = global_namespace;
13570       parser->object_scope = NULL_TREE;
13571
13572       return parser->scope;
13573     }
13574   else if (!current_scope_valid_p)
13575     {
13576       parser->scope = NULL_TREE;
13577       parser->qualifying_scope = NULL_TREE;
13578       parser->object_scope = NULL_TREE;
13579     }
13580
13581   return NULL_TREE;
13582 }
13583
13584 /* Returns TRUE if the upcoming token sequence is the start of a
13585    constructor declarator.  If FRIEND_P is true, the declarator is
13586    preceded by the `friend' specifier.  */
13587
13588 static bool
13589 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13590 {
13591   bool constructor_p;
13592   tree type_decl = NULL_TREE;
13593   bool nested_name_p;
13594   cp_token *next_token;
13595
13596   /* The common case is that this is not a constructor declarator, so
13597      try to avoid doing lots of work if at all possible.  It's not
13598      valid declare a constructor at function scope.  */
13599   if (at_function_scope_p ())
13600     return false;
13601   /* And only certain tokens can begin a constructor declarator.  */
13602   next_token = cp_lexer_peek_token (parser->lexer);
13603   if (next_token->type != CPP_NAME
13604       && next_token->type != CPP_SCOPE
13605       && next_token->type != CPP_NESTED_NAME_SPECIFIER
13606       && next_token->type != CPP_TEMPLATE_ID)
13607     return false;
13608
13609   /* Parse tentatively; we are going to roll back all of the tokens
13610      consumed here.  */
13611   cp_parser_parse_tentatively (parser);
13612   /* Assume that we are looking at a constructor declarator.  */
13613   constructor_p = true;
13614
13615   /* Look for the optional `::' operator.  */
13616   cp_parser_global_scope_opt (parser,
13617                               /*current_scope_valid_p=*/false);
13618   /* Look for the nested-name-specifier.  */
13619   nested_name_p 
13620     = (cp_parser_nested_name_specifier_opt (parser,
13621                                             /*typename_keyword_p=*/false,
13622                                             /*check_dependency_p=*/false,
13623                                             /*type_p=*/false,
13624                                             /*is_declaration=*/false)
13625        != NULL_TREE);
13626   /* Outside of a class-specifier, there must be a
13627      nested-name-specifier.  */
13628   if (!nested_name_p && 
13629       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13630        || friend_p))
13631     constructor_p = false;
13632   /* If we still think that this might be a constructor-declarator,
13633      look for a class-name.  */
13634   if (constructor_p)
13635     {
13636       /* If we have:
13637
13638            template <typename T> struct S { S(); };
13639            template <typename T> S<T>::S ();
13640
13641          we must recognize that the nested `S' names a class.
13642          Similarly, for:
13643
13644            template <typename T> S<T>::S<T> ();
13645
13646          we must recognize that the nested `S' names a template.  */
13647       type_decl = cp_parser_class_name (parser,
13648                                         /*typename_keyword_p=*/false,
13649                                         /*template_keyword_p=*/false,
13650                                         /*type_p=*/false,
13651                                         /*check_dependency_p=*/false,
13652                                         /*class_head_p=*/false,
13653                                         /*is_declaration=*/false);
13654       /* If there was no class-name, then this is not a constructor.  */
13655       constructor_p = !cp_parser_error_occurred (parser);
13656     }
13657
13658   /* If we're still considering a constructor, we have to see a `(',
13659      to begin the parameter-declaration-clause, followed by either a
13660      `)', an `...', or a decl-specifier.  We need to check for a
13661      type-specifier to avoid being fooled into thinking that:
13662
13663        S::S (f) (int);
13664
13665      is a constructor.  (It is actually a function named `f' that
13666      takes one parameter (of type `int') and returns a value of type
13667      `S::S'.  */
13668   if (constructor_p 
13669       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13670     {
13671       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13672           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13673           && !cp_parser_storage_class_specifier_opt (parser))
13674         {
13675           tree type;
13676           unsigned saved_num_template_parameter_lists;
13677
13678           /* Names appearing in the type-specifier should be looked up
13679              in the scope of the class.  */
13680           if (current_class_type)
13681             type = NULL_TREE;
13682           else
13683             {
13684               type = TREE_TYPE (type_decl);
13685               if (TREE_CODE (type) == TYPENAME_TYPE)
13686                 {
13687                   type = resolve_typename_type (type, 
13688                                                 /*only_current_p=*/false);
13689                   if (type == error_mark_node)
13690                     {
13691                       cp_parser_abort_tentative_parse (parser);
13692                       return false;
13693                     }
13694                 }
13695               push_scope (type);
13696             }
13697
13698           /* Inside the constructor parameter list, surrounding
13699              template-parameter-lists do not apply.  */
13700           saved_num_template_parameter_lists
13701             = parser->num_template_parameter_lists;
13702           parser->num_template_parameter_lists = 0;
13703
13704           /* Look for the type-specifier.  */
13705           cp_parser_type_specifier (parser,
13706                                     CP_PARSER_FLAGS_NONE,
13707                                     /*is_friend=*/false,
13708                                     /*is_declarator=*/true,
13709                                     /*declares_class_or_enum=*/NULL,
13710                                     /*is_cv_qualifier=*/NULL);
13711
13712           parser->num_template_parameter_lists
13713             = saved_num_template_parameter_lists;
13714
13715           /* Leave the scope of the class.  */
13716           if (type)
13717             pop_scope (type);
13718
13719           constructor_p = !cp_parser_error_occurred (parser);
13720         }
13721     }
13722   else
13723     constructor_p = false;
13724   /* We did not really want to consume any tokens.  */
13725   cp_parser_abort_tentative_parse (parser);
13726
13727   return constructor_p;
13728 }
13729
13730 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13731    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
13732    they must be performed once we are in the scope of the function.
13733
13734    Returns the function defined.  */
13735
13736 static tree
13737 cp_parser_function_definition_from_specifiers_and_declarator
13738   (cp_parser* parser,
13739    tree decl_specifiers,
13740    tree attributes,
13741    tree declarator)
13742 {
13743   tree fn;
13744   bool success_p;
13745
13746   /* Begin the function-definition.  */
13747   success_p = begin_function_definition (decl_specifiers, 
13748                                          attributes, 
13749                                          declarator);
13750
13751   /* If there were names looked up in the decl-specifier-seq that we
13752      did not check, check them now.  We must wait until we are in the
13753      scope of the function to perform the checks, since the function
13754      might be a friend.  */
13755   perform_deferred_access_checks ();
13756
13757   if (!success_p)
13758     {
13759       /* If begin_function_definition didn't like the definition, skip
13760          the entire function.  */
13761       error ("invalid function declaration");
13762       cp_parser_skip_to_end_of_block_or_statement (parser);
13763       fn = error_mark_node;
13764     }
13765   else
13766     fn = cp_parser_function_definition_after_declarator (parser,
13767                                                          /*inline_p=*/false);
13768
13769   return fn;
13770 }
13771
13772 /* Parse the part of a function-definition that follows the
13773    declarator.  INLINE_P is TRUE iff this function is an inline
13774    function defined with a class-specifier.
13775
13776    Returns the function defined.  */
13777
13778 static tree 
13779 cp_parser_function_definition_after_declarator (cp_parser* parser, 
13780                                                 bool inline_p)
13781 {
13782   tree fn;
13783   bool ctor_initializer_p = false;
13784   bool saved_in_unbraced_linkage_specification_p;
13785   unsigned saved_num_template_parameter_lists;
13786
13787   /* If the next token is `return', then the code may be trying to
13788      make use of the "named return value" extension that G++ used to
13789      support.  */
13790   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13791     {
13792       /* Consume the `return' keyword.  */
13793       cp_lexer_consume_token (parser->lexer);
13794       /* Look for the identifier that indicates what value is to be
13795          returned.  */
13796       cp_parser_identifier (parser);
13797       /* Issue an error message.  */
13798       error ("named return values are no longer supported");
13799       /* Skip tokens until we reach the start of the function body.  */
13800       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13801         cp_lexer_consume_token (parser->lexer);
13802     }
13803   /* The `extern' in `extern "C" void f () { ... }' does not apply to
13804      anything declared inside `f'.  */
13805   saved_in_unbraced_linkage_specification_p 
13806     = parser->in_unbraced_linkage_specification_p;
13807   parser->in_unbraced_linkage_specification_p = false;
13808   /* Inside the function, surrounding template-parameter-lists do not
13809      apply.  */
13810   saved_num_template_parameter_lists 
13811     = parser->num_template_parameter_lists; 
13812   parser->num_template_parameter_lists = 0;
13813   /* If the next token is `try', then we are looking at a
13814      function-try-block.  */
13815   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13816     ctor_initializer_p = cp_parser_function_try_block (parser);
13817   /* A function-try-block includes the function-body, so we only do
13818      this next part if we're not processing a function-try-block.  */
13819   else
13820     ctor_initializer_p 
13821       = cp_parser_ctor_initializer_opt_and_function_body (parser);
13822
13823   /* Finish the function.  */
13824   fn = finish_function ((ctor_initializer_p ? 1 : 0) | 
13825                         (inline_p ? 2 : 0));
13826   /* Generate code for it, if necessary.  */
13827   expand_or_defer_fn (fn);
13828   /* Restore the saved values.  */
13829   parser->in_unbraced_linkage_specification_p 
13830     = saved_in_unbraced_linkage_specification_p;
13831   parser->num_template_parameter_lists 
13832     = saved_num_template_parameter_lists;
13833
13834   return fn;
13835 }
13836
13837 /* Parse a template-declaration, assuming that the `export' (and
13838    `extern') keywords, if present, has already been scanned.  MEMBER_P
13839    is as for cp_parser_template_declaration.  */
13840
13841 static void
13842 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
13843 {
13844   tree decl = NULL_TREE;
13845   tree parameter_list;
13846   bool friend_p = false;
13847
13848   /* Look for the `template' keyword.  */
13849   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13850     return;
13851       
13852   /* And the `<'.  */
13853   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13854     return;
13855       
13856   /* Parse the template parameters.  */
13857   begin_template_parm_list ();
13858   /* If the next token is `>', then we have an invalid
13859      specialization.  Rather than complain about an invalid template
13860      parameter, issue an error message here.  */
13861   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13862     {
13863       cp_parser_error (parser, "invalid explicit specialization");
13864       parameter_list = NULL_TREE;
13865     }
13866   else
13867     parameter_list = cp_parser_template_parameter_list (parser);
13868   parameter_list = end_template_parm_list (parameter_list);
13869   /* Look for the `>'.  */
13870   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13871   /* We just processed one more parameter list.  */
13872   ++parser->num_template_parameter_lists;
13873   /* If the next token is `template', there are more template
13874      parameters.  */
13875   if (cp_lexer_next_token_is_keyword (parser->lexer, 
13876                                       RID_TEMPLATE))
13877     cp_parser_template_declaration_after_export (parser, member_p);
13878   else
13879     {
13880       decl = cp_parser_single_declaration (parser,
13881                                            member_p,
13882                                            &friend_p);
13883
13884       /* If this is a member template declaration, let the front
13885          end know.  */
13886       if (member_p && !friend_p && decl)
13887         {
13888           if (TREE_CODE (decl) == TYPE_DECL)
13889             cp_parser_check_access_in_redeclaration (decl);
13890
13891           decl = finish_member_template_decl (decl);
13892         }
13893       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
13894         make_friend_class (current_class_type, TREE_TYPE (decl),
13895                            /*complain=*/true);
13896     }
13897   /* We are done with the current parameter list.  */
13898   --parser->num_template_parameter_lists;
13899
13900   /* Finish up.  */
13901   finish_template_decl (parameter_list);
13902
13903   /* Register member declarations.  */
13904   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13905     finish_member_declaration (decl);
13906
13907   /* If DECL is a function template, we must return to parse it later.
13908      (Even though there is no definition, there might be default
13909      arguments that need handling.)  */
13910   if (member_p && decl 
13911       && (TREE_CODE (decl) == FUNCTION_DECL
13912           || DECL_FUNCTION_TEMPLATE_P (decl)))
13913     TREE_VALUE (parser->unparsed_functions_queues)
13914       = tree_cons (NULL_TREE, decl, 
13915                    TREE_VALUE (parser->unparsed_functions_queues));
13916 }
13917
13918 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13919    `function-definition' sequence.  MEMBER_P is true, this declaration
13920    appears in a class scope.
13921
13922    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
13923    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
13924
13925 static tree
13926 cp_parser_single_declaration (cp_parser* parser, 
13927                               bool member_p,
13928                               bool* friend_p)
13929 {
13930   int declares_class_or_enum;
13931   tree decl = NULL_TREE;
13932   tree decl_specifiers;
13933   tree attributes;
13934
13935   /* Parse the dependent declaration.  We don't know yet
13936      whether it will be a function-definition.  */
13937   cp_parser_parse_tentatively (parser);
13938   /* Defer access checks until we know what is being declared.  */
13939   push_deferring_access_checks (dk_deferred);
13940
13941   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13942      alternative.  */
13943   decl_specifiers 
13944     = cp_parser_decl_specifier_seq (parser,
13945                                     CP_PARSER_FLAGS_OPTIONAL,
13946                                     &attributes,
13947                                     &declares_class_or_enum);
13948   /* Gather up the access checks that occurred the
13949      decl-specifier-seq.  */
13950   stop_deferring_access_checks ();
13951
13952   /* Check for the declaration of a template class.  */
13953   if (declares_class_or_enum)
13954     {
13955       if (cp_parser_declares_only_class_p (parser))
13956         {
13957           decl = shadow_tag (decl_specifiers);
13958           if (decl)
13959             decl = TYPE_NAME (decl);
13960           else
13961             decl = error_mark_node;
13962         }
13963     }
13964   else
13965     decl = NULL_TREE;
13966   /* If it's not a template class, try for a template function.  If
13967      the next token is a `;', then this declaration does not declare
13968      anything.  But, if there were errors in the decl-specifiers, then
13969      the error might well have come from an attempted class-specifier.
13970      In that case, there's no need to warn about a missing declarator.  */
13971   if (!decl
13972       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13973           || !value_member (error_mark_node, decl_specifiers)))
13974     decl = cp_parser_init_declarator (parser, 
13975                                       decl_specifiers,
13976                                       attributes,
13977                                       /*function_definition_allowed_p=*/false,
13978                                       member_p,
13979                                       declares_class_or_enum,
13980                                       /*function_definition_p=*/NULL);
13981
13982   pop_deferring_access_checks ();
13983
13984   /* Clear any current qualification; whatever comes next is the start
13985      of something new.  */
13986   parser->scope = NULL_TREE;
13987   parser->qualifying_scope = NULL_TREE;
13988   parser->object_scope = NULL_TREE;
13989   /* Look for a trailing `;' after the declaration.  */
13990   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")
13991       && cp_parser_committed_to_tentative_parse (parser))
13992     cp_parser_skip_to_end_of_block_or_statement (parser);
13993   /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS.  */
13994   if (cp_parser_parse_definitely (parser))
13995     {
13996       if (friend_p)
13997         *friend_p = cp_parser_friend_p (decl_specifiers);
13998     }
13999   /* Otherwise, try a function-definition.  */
14000   else
14001     decl = cp_parser_function_definition (parser, friend_p);
14002
14003   return decl;
14004 }
14005
14006 /* Parse a cast-expression that is not the operand of a unary "&".  */
14007
14008 static tree
14009 cp_parser_simple_cast_expression (cp_parser *parser)
14010 {
14011   return cp_parser_cast_expression (parser, /*address_p=*/false);
14012 }
14013
14014 /* Parse a functional cast to TYPE.  Returns an expression
14015    representing the cast.  */
14016
14017 static tree
14018 cp_parser_functional_cast (cp_parser* parser, tree type)
14019 {
14020   tree expression_list;
14021
14022   expression_list 
14023     = cp_parser_parenthesized_expression_list (parser, false,
14024                                                /*non_constant_p=*/NULL);
14025
14026   return build_functional_cast (type, expression_list);
14027 }
14028
14029 /* Parse a template-argument-list, as well as the trailing ">" (but
14030    not the opening ">").  See cp_parser_template_argument_list for the
14031    return value.  */
14032
14033 static tree
14034 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14035 {
14036   tree arguments;
14037   tree saved_scope;
14038   tree saved_qualifying_scope;
14039   tree saved_object_scope;
14040   bool saved_greater_than_is_operator_p;
14041
14042   /* [temp.names]
14043
14044      When parsing a template-id, the first non-nested `>' is taken as
14045      the end of the template-argument-list rather than a greater-than
14046      operator.  */
14047   saved_greater_than_is_operator_p 
14048     = parser->greater_than_is_operator_p;
14049   parser->greater_than_is_operator_p = false;
14050   /* Parsing the argument list may modify SCOPE, so we save it
14051      here.  */
14052   saved_scope = parser->scope;
14053   saved_qualifying_scope = parser->qualifying_scope;
14054   saved_object_scope = parser->object_scope;
14055   /* Parse the template-argument-list itself.  */
14056   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14057     arguments = NULL_TREE;
14058   else
14059     arguments = cp_parser_template_argument_list (parser);
14060   /* Look for the `>' that ends the template-argument-list.  */
14061   cp_parser_require (parser, CPP_GREATER, "`>'");
14062   /* The `>' token might be a greater-than operator again now.  */
14063   parser->greater_than_is_operator_p 
14064     = saved_greater_than_is_operator_p;
14065   /* Restore the SAVED_SCOPE.  */
14066   parser->scope = saved_scope;
14067   parser->qualifying_scope = saved_qualifying_scope;
14068   parser->object_scope = saved_object_scope;
14069
14070   return arguments;
14071 }
14072
14073
14074 /* MEMBER_FUNCTION is a member function, or a friend.  If default
14075    arguments, or the body of the function have not yet been parsed,
14076    parse them now.  */
14077
14078 static void
14079 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14080 {
14081   cp_lexer *saved_lexer;
14082
14083   /* If this member is a template, get the underlying
14084      FUNCTION_DECL.  */
14085   if (DECL_FUNCTION_TEMPLATE_P (member_function))
14086     member_function = DECL_TEMPLATE_RESULT (member_function);
14087
14088   /* There should not be any class definitions in progress at this
14089      point; the bodies of members are only parsed outside of all class
14090      definitions.  */
14091   my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14092   /* While we're parsing the member functions we might encounter more
14093      classes.  We want to handle them right away, but we don't want
14094      them getting mixed up with functions that are currently in the
14095      queue.  */
14096   parser->unparsed_functions_queues
14097     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14098
14099   /* Make sure that any template parameters are in scope.  */
14100   maybe_begin_member_template_processing (member_function);
14101
14102   /* If the body of the function has not yet been parsed, parse it
14103      now.  */
14104   if (DECL_PENDING_INLINE_P (member_function))
14105     {
14106       tree function_scope;
14107       cp_token_cache *tokens;
14108
14109       /* The function is no longer pending; we are processing it.  */
14110       tokens = DECL_PENDING_INLINE_INFO (member_function);
14111       DECL_PENDING_INLINE_INFO (member_function) = NULL;
14112       DECL_PENDING_INLINE_P (member_function) = 0;
14113       /* If this was an inline function in a local class, enter the scope
14114          of the containing function.  */
14115       function_scope = decl_function_context (member_function);
14116       if (function_scope)
14117         push_function_context_to (function_scope);
14118       
14119       /* Save away the current lexer.  */
14120       saved_lexer = parser->lexer;
14121       /* Make a new lexer to feed us the tokens saved for this function.  */
14122       parser->lexer = cp_lexer_new_from_tokens (tokens);
14123       parser->lexer->next = saved_lexer;
14124       
14125       /* Set the current source position to be the location of the first
14126          token in the saved inline body.  */
14127       cp_lexer_peek_token (parser->lexer);
14128       
14129       /* Let the front end know that we going to be defining this
14130          function.  */
14131       start_function (NULL_TREE, member_function, NULL_TREE,
14132                       SF_PRE_PARSED | SF_INCLASS_INLINE);
14133       
14134       /* Now, parse the body of the function.  */
14135       cp_parser_function_definition_after_declarator (parser,
14136                                                       /*inline_p=*/true);
14137       
14138       /* Leave the scope of the containing function.  */
14139       if (function_scope)
14140         pop_function_context_from (function_scope);
14141       /* Restore the lexer.  */
14142       parser->lexer = saved_lexer;
14143     }
14144
14145   /* Remove any template parameters from the symbol table.  */
14146   maybe_end_member_template_processing ();
14147
14148   /* Restore the queue.  */
14149   parser->unparsed_functions_queues 
14150     = TREE_CHAIN (parser->unparsed_functions_queues);
14151 }
14152
14153 /* If DECL contains any default args, remember it on the unparsed
14154    functions queue.  */
14155
14156 static void
14157 cp_parser_save_default_args (cp_parser* parser, tree decl)
14158 {
14159   tree probe;
14160
14161   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14162        probe;
14163        probe = TREE_CHAIN (probe))
14164     if (TREE_PURPOSE (probe))
14165       {
14166         TREE_PURPOSE (parser->unparsed_functions_queues)
14167           = tree_cons (NULL_TREE, decl, 
14168                        TREE_PURPOSE (parser->unparsed_functions_queues));
14169         break;
14170       }
14171   return;
14172 }
14173
14174 /* FN is a FUNCTION_DECL which may contains a parameter with an
14175    unparsed DEFAULT_ARG.  Parse the default args now.  */
14176
14177 static void
14178 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14179 {
14180   cp_lexer *saved_lexer;
14181   cp_token_cache *tokens;
14182   bool saved_local_variables_forbidden_p;
14183   tree parameters;
14184
14185   /* While we're parsing the default args, we might (due to the
14186      statement expression extension) encounter more classes.  We want
14187      to handle them right away, but we don't want them getting mixed
14188      up with default args that are currently in the queue.  */
14189   parser->unparsed_functions_queues
14190     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14191
14192   for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14193        parameters;
14194        parameters = TREE_CHAIN (parameters))
14195     {
14196       if (!TREE_PURPOSE (parameters)
14197           || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14198         continue;
14199   
14200        /* Save away the current lexer.  */
14201       saved_lexer = parser->lexer;
14202        /* Create a new one, using the tokens we have saved.  */
14203       tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
14204       parser->lexer = cp_lexer_new_from_tokens (tokens);
14205
14206        /* Set the current source position to be the location of the
14207           first token in the default argument.  */
14208       cp_lexer_peek_token (parser->lexer);
14209
14210        /* Local variable names (and the `this' keyword) may not appear
14211           in a default argument.  */
14212       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14213       parser->local_variables_forbidden_p = true;
14214        /* Parse the assignment-expression.  */
14215       if (DECL_CONTEXT (fn))
14216         push_nested_class (DECL_CONTEXT (fn));
14217       TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14218       if (DECL_CONTEXT (fn))
14219         pop_nested_class ();
14220
14221        /* Restore saved state.  */
14222       parser->lexer = saved_lexer;
14223       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14224     }
14225
14226   /* Restore the queue.  */
14227   parser->unparsed_functions_queues 
14228     = TREE_CHAIN (parser->unparsed_functions_queues);
14229 }
14230
14231 /* Parse the operand of `sizeof' (or a similar operator).  Returns
14232    either a TYPE or an expression, depending on the form of the
14233    input.  The KEYWORD indicates which kind of expression we have
14234    encountered.  */
14235
14236 static tree
14237 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14238 {
14239   static const char *format;
14240   tree expr = NULL_TREE;
14241   const char *saved_message;
14242   bool saved_constant_expression_p;
14243
14244   /* Initialize FORMAT the first time we get here.  */
14245   if (!format)
14246     format = "types may not be defined in `%s' expressions";
14247
14248   /* Types cannot be defined in a `sizeof' expression.  Save away the
14249      old message.  */
14250   saved_message = parser->type_definition_forbidden_message;
14251   /* And create the new one.  */
14252   parser->type_definition_forbidden_message 
14253     = xmalloc (strlen (format) 
14254                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14255                + 1 /* `\0' */);
14256   sprintf ((char *) parser->type_definition_forbidden_message,
14257            format, IDENTIFIER_POINTER (ridpointers[keyword]));
14258
14259   /* The restrictions on constant-expressions do not apply inside
14260      sizeof expressions.  */
14261   saved_constant_expression_p = parser->constant_expression_p;
14262   parser->constant_expression_p = false;
14263
14264   /* Do not actually evaluate the expression.  */
14265   ++skip_evaluation;
14266   /* If it's a `(', then we might be looking at the type-id
14267      construction.  */
14268   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14269     {
14270       tree type;
14271
14272       /* We can't be sure yet whether we're looking at a type-id or an
14273          expression.  */
14274       cp_parser_parse_tentatively (parser);
14275       /* Consume the `('.  */
14276       cp_lexer_consume_token (parser->lexer);
14277       /* Parse the type-id.  */
14278       type = cp_parser_type_id (parser);
14279       /* Now, look for the trailing `)'.  */
14280       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14281       /* If all went well, then we're done.  */
14282       if (cp_parser_parse_definitely (parser))
14283         {
14284           /* Build a list of decl-specifiers; right now, we have only
14285              a single type-specifier.  */
14286           type = build_tree_list (NULL_TREE,
14287                                   type);
14288
14289           /* Call grokdeclarator to figure out what type this is.  */
14290           expr = grokdeclarator (NULL_TREE,
14291                                  type,
14292                                  TYPENAME,
14293                                  /*initialized=*/0,
14294                                  /*attrlist=*/NULL);
14295         }
14296     }
14297
14298   /* If the type-id production did not work out, then we must be
14299      looking at the unary-expression production.  */
14300   if (!expr)
14301     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14302   /* Go back to evaluating expressions.  */
14303   --skip_evaluation;
14304
14305   /* Free the message we created.  */
14306   free ((char *) parser->type_definition_forbidden_message);
14307   /* And restore the old one.  */
14308   parser->type_definition_forbidden_message = saved_message;
14309   parser->constant_expression_p = saved_constant_expression_p;
14310
14311   return expr;
14312 }
14313
14314 /* If the current declaration has no declarator, return true.  */
14315
14316 static bool
14317 cp_parser_declares_only_class_p (cp_parser *parser)
14318 {
14319   /* If the next token is a `;' or a `,' then there is no 
14320      declarator.  */
14321   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14322           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14323 }
14324
14325 /* Simplify EXPR if it is a non-dependent expression.  Returns the
14326    (possibly simplified) expression.  */
14327
14328 static tree
14329 cp_parser_fold_non_dependent_expr (tree expr)
14330 {
14331   /* If we're in a template, but EXPR isn't value dependent, simplify
14332      it.  We're supposed to treat:
14333      
14334        template <typename T> void f(T[1 + 1]);
14335        template <typename T> void f(T[2]);
14336                    
14337      as two declarations of the same function, for example.  */
14338   if (processing_template_decl
14339       && !type_dependent_expression_p (expr)
14340       && !value_dependent_expression_p (expr))
14341     {
14342       HOST_WIDE_INT saved_processing_template_decl;
14343
14344       saved_processing_template_decl = processing_template_decl;
14345       processing_template_decl = 0;
14346       expr = tsubst_copy_and_build (expr,
14347                                     /*args=*/NULL_TREE,
14348                                     tf_error,
14349                                     /*in_decl=*/NULL_TREE,
14350                                     /*function_p=*/false);
14351       processing_template_decl = saved_processing_template_decl;
14352     }
14353   return expr;
14354 }
14355
14356 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14357    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
14358
14359 static bool
14360 cp_parser_friend_p (tree decl_specifiers)
14361 {
14362   while (decl_specifiers)
14363     {
14364       /* See if this decl-specifier is `friend'.  */
14365       if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14366           && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14367         return true;
14368
14369       /* Go on to the next decl-specifier.  */
14370       decl_specifiers = TREE_CHAIN (decl_specifiers);
14371     }
14372
14373   return false;
14374 }
14375
14376 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
14377    issue an error message indicating that TOKEN_DESC was expected.
14378    
14379    Returns the token consumed, if the token had the appropriate type.
14380    Otherwise, returns NULL.  */
14381
14382 static cp_token *
14383 cp_parser_require (cp_parser* parser,
14384                    enum cpp_ttype type,
14385                    const char* token_desc)
14386 {
14387   if (cp_lexer_next_token_is (parser->lexer, type))
14388     return cp_lexer_consume_token (parser->lexer);
14389   else
14390     {
14391       /* Output the MESSAGE -- unless we're parsing tentatively.  */
14392       if (!cp_parser_simulate_error (parser))
14393         error ("expected %s", token_desc);
14394       return NULL;
14395     }
14396 }
14397
14398 /* Like cp_parser_require, except that tokens will be skipped until
14399    the desired token is found.  An error message is still produced if
14400    the next token is not as expected.  */
14401
14402 static void
14403 cp_parser_skip_until_found (cp_parser* parser, 
14404                             enum cpp_ttype type, 
14405                             const char* token_desc)
14406 {
14407   cp_token *token;
14408   unsigned nesting_depth = 0;
14409
14410   if (cp_parser_require (parser, type, token_desc))
14411     return;
14412
14413   /* Skip tokens until the desired token is found.  */
14414   while (true)
14415     {
14416       /* Peek at the next token.  */
14417       token = cp_lexer_peek_token (parser->lexer);
14418       /* If we've reached the token we want, consume it and 
14419          stop.  */
14420       if (token->type == type && !nesting_depth)
14421         {
14422           cp_lexer_consume_token (parser->lexer);
14423           return;
14424         }
14425       /* If we've run out of tokens, stop.  */
14426       if (token->type == CPP_EOF)
14427         return;
14428       if (token->type == CPP_OPEN_BRACE 
14429           || token->type == CPP_OPEN_PAREN
14430           || token->type == CPP_OPEN_SQUARE)
14431         ++nesting_depth;
14432       else if (token->type == CPP_CLOSE_BRACE 
14433                || token->type == CPP_CLOSE_PAREN
14434                || token->type == CPP_CLOSE_SQUARE)
14435         {
14436           if (nesting_depth-- == 0)
14437             return;
14438         }
14439       /* Consume this token.  */
14440       cp_lexer_consume_token (parser->lexer);
14441     }
14442 }
14443
14444 /* If the next token is the indicated keyword, consume it.  Otherwise,
14445    issue an error message indicating that TOKEN_DESC was expected.
14446    
14447    Returns the token consumed, if the token had the appropriate type.
14448    Otherwise, returns NULL.  */
14449
14450 static cp_token *
14451 cp_parser_require_keyword (cp_parser* parser,
14452                            enum rid keyword,
14453                            const char* token_desc)
14454 {
14455   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14456
14457   if (token && token->keyword != keyword)
14458     {
14459       dyn_string_t error_msg;
14460
14461       /* Format the error message.  */
14462       error_msg = dyn_string_new (0);
14463       dyn_string_append_cstr (error_msg, "expected ");
14464       dyn_string_append_cstr (error_msg, token_desc);
14465       cp_parser_error (parser, error_msg->s);
14466       dyn_string_delete (error_msg);
14467       return NULL;
14468     }
14469
14470   return token;
14471 }
14472
14473 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14474    function-definition.  */
14475
14476 static bool 
14477 cp_parser_token_starts_function_definition_p (cp_token* token)
14478 {
14479   return (/* An ordinary function-body begins with an `{'.  */
14480           token->type == CPP_OPEN_BRACE
14481           /* A ctor-initializer begins with a `:'.  */
14482           || token->type == CPP_COLON
14483           /* A function-try-block begins with `try'.  */
14484           || token->keyword == RID_TRY
14485           /* The named return value extension begins with `return'.  */
14486           || token->keyword == RID_RETURN);
14487 }
14488
14489 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14490    definition.  */
14491
14492 static bool
14493 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14494 {
14495   cp_token *token;
14496
14497   token = cp_lexer_peek_token (parser->lexer);
14498   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14499 }
14500
14501 /* Returns TRUE iff the next token is the "," or ">" ending a
14502    template-argument.  */
14503
14504 static bool
14505 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14506 {
14507   cp_token *token;
14508
14509   token = cp_lexer_peek_token (parser->lexer);
14510   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
14511 }
14512  
14513 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14514    or none_type otherwise.  */
14515
14516 static enum tag_types
14517 cp_parser_token_is_class_key (cp_token* token)
14518 {
14519   switch (token->keyword)
14520     {
14521     case RID_CLASS:
14522       return class_type;
14523     case RID_STRUCT:
14524       return record_type;
14525     case RID_UNION:
14526       return union_type;
14527       
14528     default:
14529       return none_type;
14530     }
14531 }
14532
14533 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
14534
14535 static void
14536 cp_parser_check_class_key (enum tag_types class_key, tree type)
14537 {
14538   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14539     pedwarn ("`%s' tag used in naming `%#T'",
14540             class_key == union_type ? "union"
14541              : class_key == record_type ? "struct" : "class", 
14542              type);
14543 }
14544                            
14545 /* Issue an error message if DECL is redeclared with different
14546    access than its original declaration [class.access.spec/3].
14547    This applies to nested classes and nested class templates.
14548    [class.mem/1].  */
14549
14550 static void cp_parser_check_access_in_redeclaration (tree decl)
14551 {
14552   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14553     return;
14554
14555   if ((TREE_PRIVATE (decl)
14556        != (current_access_specifier == access_private_node))
14557       || (TREE_PROTECTED (decl)
14558           != (current_access_specifier == access_protected_node)))
14559     error ("%D redeclared with different access", decl);
14560 }
14561
14562 /* Look for the `template' keyword, as a syntactic disambiguator.
14563    Return TRUE iff it is present, in which case it will be 
14564    consumed.  */
14565
14566 static bool
14567 cp_parser_optional_template_keyword (cp_parser *parser)
14568 {
14569   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14570     {
14571       /* The `template' keyword can only be used within templates;
14572          outside templates the parser can always figure out what is a
14573          template and what is not.  */
14574       if (!processing_template_decl)
14575         {
14576           error ("`template' (as a disambiguator) is only allowed "
14577                  "within templates");
14578           /* If this part of the token stream is rescanned, the same
14579              error message would be generated.  So, we purge the token
14580              from the stream.  */
14581           cp_lexer_purge_token (parser->lexer);
14582           return false;
14583         }
14584       else
14585         {
14586           /* Consume the `template' keyword.  */
14587           cp_lexer_consume_token (parser->lexer);
14588           return true;
14589         }
14590     }
14591
14592   return false;
14593 }
14594
14595 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
14596    set PARSER->SCOPE, and perform other related actions.  */
14597
14598 static void
14599 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14600 {
14601   tree value;
14602   tree check;
14603
14604   /* Get the stored value.  */
14605   value = cp_lexer_consume_token (parser->lexer)->value;
14606   /* Perform any access checks that were deferred.  */
14607   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14608     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
14609   /* Set the scope from the stored value.  */
14610   parser->scope = TREE_VALUE (value);
14611   parser->qualifying_scope = TREE_TYPE (value);
14612   parser->object_scope = NULL_TREE;
14613 }
14614
14615 /* Add tokens to CACHE until an non-nested END token appears.  */
14616
14617 static void
14618 cp_parser_cache_group (cp_parser *parser, 
14619                        cp_token_cache *cache,
14620                        enum cpp_ttype end,
14621                        unsigned depth)
14622 {
14623   while (true)
14624     {
14625       cp_token *token;
14626
14627       /* Abort a parenthesized expression if we encounter a brace.  */
14628       if ((end == CPP_CLOSE_PAREN || depth == 0)
14629           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14630         return;
14631       /* Consume the next token.  */
14632       token = cp_lexer_consume_token (parser->lexer);
14633       /* If we've reached the end of the file, stop.  */
14634       if (token->type == CPP_EOF)
14635         return;
14636       /* Add this token to the tokens we are saving.  */
14637       cp_token_cache_push_token (cache, token);
14638       /* See if it starts a new group.  */
14639       if (token->type == CPP_OPEN_BRACE)
14640         {
14641           cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14642           if (depth == 0)
14643             return;
14644         }
14645       else if (token->type == CPP_OPEN_PAREN)
14646         cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14647       else if (token->type == end)
14648         return;
14649     }
14650 }
14651
14652 /* Begin parsing tentatively.  We always save tokens while parsing
14653    tentatively so that if the tentative parsing fails we can restore the
14654    tokens.  */
14655
14656 static void
14657 cp_parser_parse_tentatively (cp_parser* parser)
14658 {
14659   /* Enter a new parsing context.  */
14660   parser->context = cp_parser_context_new (parser->context);
14661   /* Begin saving tokens.  */
14662   cp_lexer_save_tokens (parser->lexer);
14663   /* In order to avoid repetitive access control error messages,
14664      access checks are queued up until we are no longer parsing
14665      tentatively.  */
14666   push_deferring_access_checks (dk_deferred);
14667 }
14668
14669 /* Commit to the currently active tentative parse.  */
14670
14671 static void
14672 cp_parser_commit_to_tentative_parse (cp_parser* parser)
14673 {
14674   cp_parser_context *context;
14675   cp_lexer *lexer;
14676
14677   /* Mark all of the levels as committed.  */
14678   lexer = parser->lexer;
14679   for (context = parser->context; context->next; context = context->next)
14680     {
14681       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14682         break;
14683       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14684       while (!cp_lexer_saving_tokens (lexer))
14685         lexer = lexer->next;
14686       cp_lexer_commit_tokens (lexer);
14687     }
14688 }
14689
14690 /* Abort the currently active tentative parse.  All consumed tokens
14691    will be rolled back, and no diagnostics will be issued.  */
14692
14693 static void
14694 cp_parser_abort_tentative_parse (cp_parser* parser)
14695 {
14696   cp_parser_simulate_error (parser);
14697   /* Now, pretend that we want to see if the construct was
14698      successfully parsed.  */
14699   cp_parser_parse_definitely (parser);
14700 }
14701
14702 /* Stop parsing tentatively.  If a parse error has occurred, restore the
14703    token stream.  Otherwise, commit to the tokens we have consumed.
14704    Returns true if no error occurred; false otherwise.  */
14705
14706 static bool
14707 cp_parser_parse_definitely (cp_parser* parser)
14708 {
14709   bool error_occurred;
14710   cp_parser_context *context;
14711
14712   /* Remember whether or not an error occurred, since we are about to
14713      destroy that information.  */
14714   error_occurred = cp_parser_error_occurred (parser);
14715   /* Remove the topmost context from the stack.  */
14716   context = parser->context;
14717   parser->context = context->next;
14718   /* If no parse errors occurred, commit to the tentative parse.  */
14719   if (!error_occurred)
14720     {
14721       /* Commit to the tokens read tentatively, unless that was
14722          already done.  */
14723       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14724         cp_lexer_commit_tokens (parser->lexer);
14725
14726       pop_to_parent_deferring_access_checks ();
14727     }
14728   /* Otherwise, if errors occurred, roll back our state so that things
14729      are just as they were before we began the tentative parse.  */
14730   else
14731     {
14732       cp_lexer_rollback_tokens (parser->lexer);
14733       pop_deferring_access_checks ();
14734     }
14735   /* Add the context to the front of the free list.  */
14736   context->next = cp_parser_context_free_list;
14737   cp_parser_context_free_list = context;
14738
14739   return !error_occurred;
14740 }
14741
14742 /* Returns true if we are parsing tentatively -- but have decided that
14743    we will stick with this tentative parse, even if errors occur.  */
14744
14745 static bool
14746 cp_parser_committed_to_tentative_parse (cp_parser* parser)
14747 {
14748   return (cp_parser_parsing_tentatively (parser)
14749           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14750 }
14751
14752 /* Returns nonzero iff an error has occurred during the most recent
14753    tentative parse.  */
14754    
14755 static bool
14756 cp_parser_error_occurred (cp_parser* parser)
14757 {
14758   return (cp_parser_parsing_tentatively (parser)
14759           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14760 }
14761
14762 /* Returns nonzero if GNU extensions are allowed.  */
14763
14764 static bool
14765 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
14766 {
14767   return parser->allow_gnu_extensions_p;
14768 }
14769
14770 \f
14771
14772 /* The parser.  */
14773
14774 static GTY (()) cp_parser *the_parser;
14775
14776 /* External interface.  */
14777
14778 /* Parse one entire translation unit.  */
14779
14780 void
14781 c_parse_file (void)
14782 {
14783   bool error_occurred;
14784
14785   the_parser = cp_parser_new ();
14786   push_deferring_access_checks (flag_access_control
14787                                 ? dk_no_deferred : dk_no_check);
14788   error_occurred = cp_parser_translation_unit (the_parser);
14789   the_parser = NULL;
14790 }
14791
14792 /* Clean up after parsing the entire translation unit.  */
14793
14794 void
14795 free_parser_stacks (void)
14796 {
14797   /* Nothing to do.  */
14798 }
14799
14800 /* This variable must be provided by every front end.  */
14801
14802 int yydebug;
14803
14804 #include "gt-cp-parser.h"