OSDN Git Service

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