Merge from vendor branch BSDTAR:
[dragonfly.git] / contrib / gcc-3.4 / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>.
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GCC is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37
38 \f
39 /* The lexer.  */
40
41 /* Overview
42    --------
43
44    A cp_lexer represents a stream of cp_tokens.  It allows arbitrary
45    look-ahead.
46
47    Methodology
48    -----------
49
50    We use a circular buffer to store incoming tokens.
51
52    Some artifacts of the C++ language (such as the
53    expression/declaration ambiguity) require arbitrary look-ahead.
54    The strategy we adopt for dealing with these problems is to attempt
55    to parse one construct (e.g., the declaration) and fall back to the
56    other (e.g., the expression) if that attempt does not succeed.
57    Therefore, we must sometimes store an arbitrary number of tokens.
58
59    The parser routinely peeks at the next token, and then consumes it
60    later.  That also requires a buffer in which to store the tokens.
61      
62    In order to easily permit adding tokens to the end of the buffer,
63    while removing them from the beginning of the buffer, we use a
64    circular buffer.  */
65
66 /* A C++ token.  */
67
68 typedef struct cp_token GTY (())
69 {
70   /* The kind of token.  */
71   ENUM_BITFIELD (cpp_ttype) type : 8;
72   /* If this token is a keyword, this value indicates which keyword.
73      Otherwise, this value is RID_MAX.  */
74   ENUM_BITFIELD (rid) keyword : 8;
75   /* Token flags.  */
76   unsigned char flags;
77   /* The value associated with this token, if any.  */
78   tree value;
79   /* The location at which this token was found.  */
80   location_t location;
81 } cp_token;
82
83 /* The number of tokens in a single token block.
84    Computed so that cp_token_block fits in a 512B allocation unit.  */
85
86 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
87
88 /* A group of tokens.  These groups are chained together to store
89    large numbers of tokens.  (For example, a token block is created
90    when the body of an inline member function is first encountered;
91    the tokens are processed later after the class definition is
92    complete.)  
93
94    This somewhat ungainly data structure (as opposed to, say, a
95    variable-length array), is used due to constraints imposed by the
96    current garbage-collection methodology.  If it is made more
97    flexible, we could perhaps simplify the data structures involved.  */
98
99 typedef struct cp_token_block GTY (())
100 {
101   /* The tokens.  */
102   cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
103   /* The number of tokens in this block.  */
104   size_t num_tokens;
105   /* The next token block in the chain.  */
106   struct cp_token_block *next;
107   /* The previous block in the chain.  */
108   struct cp_token_block *prev;
109 } cp_token_block;
110
111 typedef struct cp_token_cache GTY (())
112 {
113   /* The first block in the cache.  NULL if there are no tokens in the
114      cache.  */
115   cp_token_block *first;
116   /* The last block in the cache.  NULL If there are no tokens in the
117      cache.  */
118   cp_token_block *last;
119 } cp_token_cache;
120
121 /* Prototypes.  */
122
123 static cp_token_cache *cp_token_cache_new 
124   (void);
125 static void cp_token_cache_push_token
126   (cp_token_cache *, cp_token *);
127
128 /* Create a new cp_token_cache.  */
129
130 static cp_token_cache *
131 cp_token_cache_new (void)
132 {
133   return ggc_alloc_cleared (sizeof (cp_token_cache));
134 }
135
136 /* Add *TOKEN to *CACHE.  */
137
138 static void
139 cp_token_cache_push_token (cp_token_cache *cache,
140                            cp_token *token)
141 {
142   cp_token_block *b = cache->last;
143
144   /* See if we need to allocate a new token block.  */
145   if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
146     {
147       b = ggc_alloc_cleared (sizeof (cp_token_block));
148       b->prev = cache->last;
149       if (cache->last)
150         {
151           cache->last->next = b;
152           cache->last = b;
153         }
154       else
155         cache->first = cache->last = b;
156     }
157   /* Add this token to the current token block.  */
158   b->tokens[b->num_tokens++] = *token;
159 }
160
161 /* The cp_lexer structure represents the C++ lexer.  It is responsible
162    for managing the token stream from the preprocessor and supplying
163    it to the parser.  */
164
165 typedef struct cp_lexer GTY (())
166 {
167   /* The memory allocated for the buffer.  Never NULL.  */
168   cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
169   /* A pointer just past the end of the memory allocated for the buffer.  */
170   cp_token * GTY ((skip (""))) buffer_end;
171   /* The first valid token in the buffer, or NULL if none.  */
172   cp_token * GTY ((skip (""))) first_token;
173   /* The next available token.  If NEXT_TOKEN is NULL, then there are
174      no more available tokens.  */
175   cp_token * GTY ((skip (""))) next_token;
176   /* A pointer just past the last available token.  If FIRST_TOKEN is
177      NULL, however, there are no available tokens, and then this
178      location is simply the place in which the next token read will be
179      placed.  If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180      When the LAST_TOKEN == BUFFER, then the last token is at the
181      highest memory address in the BUFFER.  */
182   cp_token * GTY ((skip (""))) last_token;
183
184   /* A stack indicating positions at which cp_lexer_save_tokens was
185      called.  The top entry is the most recent position at which we
186      began saving tokens.  The entries are differences in token
187      position between FIRST_TOKEN and the first saved token.
188
189      If the stack is non-empty, we are saving tokens.  When a token is
190      consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191      pointer will not.  The token stream will be preserved so that it
192      can be reexamined later.
193
194      If the stack is empty, then we are not saving tokens.  Whenever a
195      token is consumed, the FIRST_TOKEN pointer will be moved, and the
196      consumed token will be gone forever.  */
197   varray_type saved_tokens;
198
199   /* The STRING_CST tokens encountered while processing the current
200      string literal.  */
201   varray_type string_tokens;
202
203   /* True if we should obtain more tokens from the preprocessor; false
204      if we are processing a saved token cache.  */
205   bool main_lexer_p;
206
207   /* True if we should output debugging information.  */
208   bool debugging_p;
209
210   /* The next lexer in a linked list of lexers.  */
211   struct cp_lexer *next;
212 } cp_lexer;
213
214 /* Prototypes.  */
215
216 static cp_lexer *cp_lexer_new_main
217   (void);
218 static cp_lexer *cp_lexer_new_from_tokens
219   (struct cp_token_cache *);
220 static int cp_lexer_saving_tokens
221   (const cp_lexer *);
222 static cp_token *cp_lexer_next_token
223   (cp_lexer *, cp_token *);
224 static cp_token *cp_lexer_prev_token
225   (cp_lexer *, cp_token *);
226 static ptrdiff_t cp_lexer_token_difference 
227   (cp_lexer *, cp_token *, cp_token *);
228 static cp_token *cp_lexer_read_token
229   (cp_lexer *);
230 static void cp_lexer_maybe_grow_buffer
231   (cp_lexer *);
232 static void cp_lexer_get_preprocessor_token
233   (cp_lexer *, cp_token *);
234 static cp_token *cp_lexer_peek_token
235   (cp_lexer *);
236 static cp_token *cp_lexer_peek_nth_token
237   (cp_lexer *, size_t);
238 static inline bool cp_lexer_next_token_is
239   (cp_lexer *, enum cpp_ttype);
240 static bool cp_lexer_next_token_is_not
241   (cp_lexer *, enum cpp_ttype);
242 static bool cp_lexer_next_token_is_keyword
243   (cp_lexer *, enum rid);
244 static cp_token *cp_lexer_consume_token 
245   (cp_lexer *);
246 static void cp_lexer_purge_token
247   (cp_lexer *);
248 static void cp_lexer_purge_tokens_after
249   (cp_lexer *, cp_token *);
250 static void cp_lexer_save_tokens
251   (cp_lexer *);
252 static void cp_lexer_commit_tokens
253   (cp_lexer *);
254 static void cp_lexer_rollback_tokens
255   (cp_lexer *);
256 static inline void cp_lexer_set_source_position_from_token 
257   (cp_lexer *, const cp_token *);
258 static void cp_lexer_print_token
259   (FILE *, cp_token *);
260 static inline bool cp_lexer_debugging_p 
261   (cp_lexer *);
262 static void cp_lexer_start_debugging
263   (cp_lexer *) ATTRIBUTE_UNUSED;
264 static void cp_lexer_stop_debugging
265   (cp_lexer *) ATTRIBUTE_UNUSED;
266
267 /* Manifest constants.  */
268
269 #define CP_TOKEN_BUFFER_SIZE 5
270 #define CP_SAVED_TOKENS_SIZE 5
271
272 /* A token type for keywords, as opposed to ordinary identifiers.  */
273 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
274
275 /* A token type for template-ids.  If a template-id is processed while
276    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
277    the value of the CPP_TEMPLATE_ID is whatever was returned by
278    cp_parser_template_id.  */
279 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
280
281 /* A token type for nested-name-specifiers.  If a
282    nested-name-specifier is processed while parsing tentatively, it is
283    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
284    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
285    cp_parser_nested_name_specifier_opt.  */
286 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
287
288 /* A token type for tokens that are not tokens at all; these are used
289    to mark the end of a token block.  */
290 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
291
292 /* Variables.  */
293
294 /* The stream to which debugging output should be written.  */
295 static FILE *cp_lexer_debug_stream;
296
297 /* Create a new main C++ lexer, the lexer that gets tokens from the
298    preprocessor.  */
299
300 static cp_lexer *
301 cp_lexer_new_main (void)
302 {
303   cp_lexer *lexer;
304   cp_token first_token;
305
306   /* It's possible that lexing the first token will load a PCH file,
307      which is a GC collection point.  So we have to grab the first
308      token before allocating any memory.  */
309   cp_lexer_get_preprocessor_token (NULL, &first_token);
310   c_common_no_more_pch ();
311
312   /* Allocate the memory.  */
313   lexer = ggc_alloc_cleared (sizeof (cp_lexer));
314
315   /* Create the circular buffer.  */
316   lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
317   lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
318
319   /* There is one token in the buffer.  */
320   lexer->last_token = lexer->buffer + 1;
321   lexer->first_token = lexer->buffer;
322   lexer->next_token = lexer->buffer;
323   memcpy (lexer->buffer, &first_token, sizeof (cp_token));
324
325   /* This lexer obtains more tokens by calling c_lex.  */
326   lexer->main_lexer_p = true;
327
328   /* Create the SAVED_TOKENS stack.  */
329   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
330   
331   /* Create the STRINGS array.  */
332   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
333
334   /* Assume we are not debugging.  */
335   lexer->debugging_p = false;
336
337   return lexer;
338 }
339
340 /* Create a new lexer whose token stream is primed with the TOKENS.
341    When these tokens are exhausted, no new tokens will be read.  */
342
343 static cp_lexer *
344 cp_lexer_new_from_tokens (cp_token_cache *tokens)
345 {
346   cp_lexer *lexer;
347   cp_token *token;
348   cp_token_block *block;
349   ptrdiff_t num_tokens;
350
351   /* Allocate the memory.  */
352   lexer = ggc_alloc_cleared (sizeof (cp_lexer));
353
354   /* Create a new buffer, appropriately sized.  */
355   num_tokens = 0;
356   for (block = tokens->first; block != NULL; block = block->next)
357     num_tokens += block->num_tokens;
358   lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
359   lexer->buffer_end = lexer->buffer + num_tokens;
360   
361   /* Install the tokens.  */
362   token = lexer->buffer;
363   for (block = tokens->first; block != NULL; block = block->next)
364     {
365       memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
366       token += block->num_tokens;
367     }
368
369   /* The FIRST_TOKEN is the beginning of the buffer.  */
370   lexer->first_token = lexer->buffer;
371   /* The next available token is also at the beginning of the buffer.  */
372   lexer->next_token = lexer->buffer;
373   /* The buffer is full.  */
374   lexer->last_token = lexer->first_token;
375
376   /* This lexer doesn't obtain more tokens.  */
377   lexer->main_lexer_p = false;
378
379   /* Create the SAVED_TOKENS stack.  */
380   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
381   
382   /* Create the STRINGS array.  */
383   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
384
385   /* Assume we are not debugging.  */
386   lexer->debugging_p = false;
387
388   return lexer;
389 }
390
391 /* Returns nonzero if debugging information should be output.  */
392
393 static inline bool
394 cp_lexer_debugging_p (cp_lexer *lexer)
395 {
396   return lexer->debugging_p;
397 }
398
399 /* Set the current source position from the information stored in
400    TOKEN.  */
401
402 static inline void
403 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
404                                          const cp_token *token)
405 {
406   /* Ideally, the source position information would not be a global
407      variable, but it is.  */
408
409   /* Update the line number.  */
410   if (token->type != CPP_EOF)
411     input_location = token->location;
412 }
413
414 /* TOKEN points into the circular token buffer.  Return a pointer to
415    the next token in the buffer.  */
416
417 static inline cp_token *
418 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
419 {
420   token++;
421   if (token == lexer->buffer_end)
422     token = lexer->buffer;
423   return token;
424 }
425
426 /* TOKEN points into the circular token buffer.  Return a pointer to
427    the previous token in the buffer.  */
428
429 static inline cp_token *
430 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
431 {
432   if (token == lexer->buffer)
433     token = lexer->buffer_end;
434   return token - 1;
435 }
436
437 /* nonzero if we are presently saving tokens.  */
438
439 static int
440 cp_lexer_saving_tokens (const cp_lexer* lexer)
441 {
442   return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
443 }
444
445 /* Return a pointer to the token that is N tokens beyond TOKEN in the
446    buffer.  */
447
448 static cp_token *
449 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
450 {
451   token += n;
452   if (token >= lexer->buffer_end)
453     token = lexer->buffer + (token - lexer->buffer_end);
454   return token;
455 }
456
457 /* Returns the number of times that START would have to be incremented
458    to reach FINISH.  If START and FINISH are the same, returns zero.  */
459
460 static ptrdiff_t
461 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
462 {
463   if (finish >= start)
464     return finish - start;
465   else
466     return ((lexer->buffer_end - lexer->buffer)
467             - (start - finish));
468 }
469
470 /* Obtain another token from the C preprocessor and add it to the
471    token buffer.  Returns the newly read token.  */
472
473 static cp_token *
474 cp_lexer_read_token (cp_lexer* lexer)
475 {
476   cp_token *token;
477
478   /* Make sure there is room in the buffer.  */
479   cp_lexer_maybe_grow_buffer (lexer);
480
481   /* If there weren't any tokens, then this one will be the first.  */
482   if (!lexer->first_token)
483     lexer->first_token = lexer->last_token;
484   /* Similarly, if there were no available tokens, there is one now.  */
485   if (!lexer->next_token)
486     lexer->next_token = lexer->last_token;
487
488   /* Figure out where we're going to store the new token.  */
489   token = lexer->last_token;
490
491   /* Get a new token from the preprocessor.  */
492   cp_lexer_get_preprocessor_token (lexer, token);
493
494   /* Increment LAST_TOKEN.  */
495   lexer->last_token = cp_lexer_next_token (lexer, token);
496
497   /* Strings should have type `const char []'.  Right now, we will
498      have an ARRAY_TYPE that is constant rather than an array of
499      constant elements.
500      FIXME: Make fix_string_type get this right in the first place.  */
501   if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
502       && flag_const_strings)
503     {
504       tree type;
505
506       /* Get the current type.  It will be an ARRAY_TYPE.  */
507       type = TREE_TYPE (token->value);
508       /* Use build_cplus_array_type to rebuild the array, thereby
509          getting the right type.  */
510       type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
511       /* Reset the type of the token.  */
512       TREE_TYPE (token->value) = type;
513     }
514
515   return token;
516 }
517
518 /* If the circular buffer is full, make it bigger.  */
519
520 static void
521 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
522 {
523   /* If the buffer is full, enlarge it.  */
524   if (lexer->last_token == lexer->first_token)
525     {
526       cp_token *new_buffer;
527       cp_token *old_buffer;
528       cp_token *new_first_token;
529       ptrdiff_t buffer_length;
530       size_t num_tokens_to_copy;
531
532       /* Remember the current buffer pointer.  It will become invalid,
533          but we will need to do pointer arithmetic involving this
534          value.  */
535       old_buffer = lexer->buffer;
536       /* Compute the current buffer size.  */
537       buffer_length = lexer->buffer_end - lexer->buffer;
538       /* Allocate a buffer twice as big.  */
539       new_buffer = ggc_realloc (lexer->buffer, 
540                                 2 * buffer_length * sizeof (cp_token));
541       
542       /* Because the buffer is circular, logically consecutive tokens
543          are not necessarily placed consecutively in memory.
544          Therefore, we must keep move the tokens that were before
545          FIRST_TOKEN to the second half of the newly allocated
546          buffer.  */
547       num_tokens_to_copy = (lexer->first_token - old_buffer);
548       memcpy (new_buffer + buffer_length,
549               new_buffer,
550               num_tokens_to_copy * sizeof (cp_token));
551       /* Clear the rest of the buffer.  We never look at this storage,
552          but the garbage collector may.  */
553       memset (new_buffer + buffer_length + num_tokens_to_copy, 0, 
554               (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
555
556       /* Now recompute all of the buffer pointers.  */
557       new_first_token 
558         = new_buffer + (lexer->first_token - old_buffer);
559       if (lexer->next_token != NULL)
560         {
561           ptrdiff_t next_token_delta;
562
563           if (lexer->next_token > lexer->first_token)
564             next_token_delta = lexer->next_token - lexer->first_token;
565           else
566             next_token_delta = 
567               buffer_length - (lexer->first_token - lexer->next_token);
568           lexer->next_token = new_first_token + next_token_delta;
569         }
570       lexer->last_token = new_first_token + buffer_length;
571       lexer->buffer = new_buffer;
572       lexer->buffer_end = new_buffer + buffer_length * 2;
573       lexer->first_token = new_first_token;
574     }
575 }
576
577 /* Store the next token from the preprocessor in *TOKEN.  */
578
579 static void 
580 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
581                                  cp_token *token)
582 {
583   bool done;
584
585   /* If this not the main lexer, return a terminating CPP_EOF token.  */
586   if (lexer != NULL && !lexer->main_lexer_p)
587     {
588       token->type = CPP_EOF;
589       token->location.line = 0;
590       token->location.file = NULL;
591       token->value = NULL_TREE;
592       token->keyword = RID_MAX;
593
594       return;
595     }
596
597   done = false;
598   /* Keep going until we get a token we like.  */
599   while (!done)
600     {
601       /* Get a new token from the preprocessor.  */
602       token->type = c_lex_with_flags (&token->value, &token->flags);
603       /* Issue messages about tokens we cannot process.  */
604       switch (token->type)
605         {
606         case CPP_ATSIGN:
607         case CPP_HASH:
608         case CPP_PASTE:
609           error ("invalid token");
610           break;
611
612         default:
613           /* This is a good token, so we exit the loop.  */
614           done = true;
615           break;
616         }
617     }
618   /* Now we've got our token.  */
619   token->location = input_location;
620
621   /* Check to see if this token is a keyword.  */
622   if (token->type == CPP_NAME 
623       && C_IS_RESERVED_WORD (token->value))
624     {
625       /* Mark this token as a keyword.  */
626       token->type = CPP_KEYWORD;
627       /* Record which keyword.  */
628       token->keyword = C_RID_CODE (token->value);
629       /* Update the value.  Some keywords are mapped to particular
630          entities, rather than simply having the value of the
631          corresponding IDENTIFIER_NODE.  For example, `__const' is
632          mapped to `const'.  */
633       token->value = ridpointers[token->keyword];
634     }
635   else
636     token->keyword = RID_MAX;
637 }
638
639 /* Return a pointer to the next token in the token stream, but do not
640    consume it.  */
641
642 static cp_token *
643 cp_lexer_peek_token (cp_lexer* lexer)
644 {
645   cp_token *token;
646
647   /* If there are no tokens, read one now.  */
648   if (!lexer->next_token)
649     cp_lexer_read_token (lexer);
650
651   /* Provide debugging output.  */
652   if (cp_lexer_debugging_p (lexer))
653     {
654       fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
655       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
656       fprintf (cp_lexer_debug_stream, "\n");
657     }
658
659   token = lexer->next_token;
660   cp_lexer_set_source_position_from_token (lexer, token);
661   return token;
662 }
663
664 /* Return true if the next token has the indicated TYPE.  */
665
666 static bool
667 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
668 {
669   cp_token *token;
670
671   /* Peek at the next token.  */
672   token = cp_lexer_peek_token (lexer);
673   /* Check to see if it has the indicated TYPE.  */
674   return token->type == type;
675 }
676
677 /* Return true if the next token does not have the indicated TYPE.  */
678
679 static bool
680 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
681 {
682   return !cp_lexer_next_token_is (lexer, type);
683 }
684
685 /* Return true if the next token is the indicated KEYWORD.  */
686
687 static bool
688 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
689 {
690   cp_token *token;
691
692   /* Peek at the next token.  */
693   token = cp_lexer_peek_token (lexer);
694   /* Check to see if it is the indicated keyword.  */
695   return token->keyword == keyword;
696 }
697
698 /* Return a pointer to the Nth token in the token stream.  If N is 1,
699    then this is precisely equivalent to cp_lexer_peek_token.  */
700
701 static cp_token *
702 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
703 {
704   cp_token *token;
705
706   /* N is 1-based, not zero-based.  */
707   my_friendly_assert (n > 0, 20000224);
708
709   /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary.  */
710   token = lexer->next_token;
711   /* If there are no tokens in the buffer, get one now.  */
712   if (!token)
713     {
714       cp_lexer_read_token (lexer);
715       token = lexer->next_token;
716     }
717
718   /* Now, read tokens until we have enough.  */
719   while (--n > 0)
720     {
721       /* Advance to the next token.  */
722       token = cp_lexer_next_token (lexer, token);
723       /* If that's all the tokens we have, read a new one.  */
724       if (token == lexer->last_token)
725         token = cp_lexer_read_token (lexer);
726     }
727
728   return token;
729 }
730
731 /* Consume the next token.  The pointer returned is valid only until
732    another token is read.  Callers should preserve copy the token
733    explicitly if they will need its value for a longer period of
734    time.  */
735
736 static cp_token *
737 cp_lexer_consume_token (cp_lexer* lexer)
738 {
739   cp_token *token;
740
741   /* If there are no tokens, read one now.  */
742   if (!lexer->next_token)
743     cp_lexer_read_token (lexer);
744
745   /* Remember the token we'll be returning.  */
746   token = lexer->next_token;
747
748   /* Increment NEXT_TOKEN.  */
749   lexer->next_token = cp_lexer_next_token (lexer, 
750                                            lexer->next_token);
751   /* Check to see if we're all out of tokens.  */
752   if (lexer->next_token == lexer->last_token)
753     lexer->next_token = NULL;
754
755   /* If we're not saving tokens, then move FIRST_TOKEN too.  */
756   if (!cp_lexer_saving_tokens (lexer))
757     {
758       /* If there are no tokens available, set FIRST_TOKEN to NULL.  */
759       if (!lexer->next_token)
760         lexer->first_token = NULL;
761       else
762         lexer->first_token = lexer->next_token;
763     }
764
765   /* Provide debugging output.  */
766   if (cp_lexer_debugging_p (lexer))
767     {
768       fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
769       cp_lexer_print_token (cp_lexer_debug_stream, token);
770       fprintf (cp_lexer_debug_stream, "\n");
771     }
772
773   return token;
774 }
775
776 /* Permanently remove the next token from the token stream.  There
777    must be a valid next token already; this token never reads
778    additional tokens from the preprocessor.  */
779
780 static void
781 cp_lexer_purge_token (cp_lexer *lexer)
782 {
783   cp_token *token;
784   cp_token *next_token;
785
786   token = lexer->next_token;
787   while (true) 
788     {
789       next_token = cp_lexer_next_token (lexer, token);
790       if (next_token == lexer->last_token)
791         break;
792       *token = *next_token;
793       token = next_token;
794     }
795
796   lexer->last_token = token;
797   /* The token purged may have been the only token remaining; if so,
798      clear NEXT_TOKEN.  */
799   if (lexer->next_token == token)
800     lexer->next_token = NULL;
801 }
802
803 /* Permanently remove all tokens after TOKEN, up to, but not
804    including, the token that will be returned next by
805    cp_lexer_peek_token.  */
806
807 static void
808 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
809 {
810   cp_token *peek;
811   cp_token *t1;
812   cp_token *t2;
813
814   if (lexer->next_token)
815     {
816       /* Copy the tokens that have not yet been read to the location
817          immediately following TOKEN.  */
818       t1 = cp_lexer_next_token (lexer, token);
819       t2 = peek = cp_lexer_peek_token (lexer);
820       /* Move tokens into the vacant area between TOKEN and PEEK.  */
821       while (t2 != lexer->last_token)
822         {
823           *t1 = *t2;
824           t1 = cp_lexer_next_token (lexer, t1);
825           t2 = cp_lexer_next_token (lexer, t2);
826         }
827       /* Now, the next available token is right after TOKEN.  */
828       lexer->next_token = cp_lexer_next_token (lexer, token);
829       /* And the last token is wherever we ended up.  */
830       lexer->last_token = t1;
831     }
832   else
833     {
834       /* There are no tokens in the buffer, so there is nothing to
835          copy.  The last token in the buffer is TOKEN itself.  */
836       lexer->last_token = cp_lexer_next_token (lexer, token);
837     }
838 }
839
840 /* Begin saving tokens.  All tokens consumed after this point will be
841    preserved.  */
842
843 static void
844 cp_lexer_save_tokens (cp_lexer* lexer)
845 {
846   /* Provide debugging output.  */
847   if (cp_lexer_debugging_p (lexer))
848     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
849
850   /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
851      restore the tokens if required.  */
852   if (!lexer->next_token)
853     cp_lexer_read_token (lexer);
854
855   VARRAY_PUSH_INT (lexer->saved_tokens,
856                    cp_lexer_token_difference (lexer,
857                                               lexer->first_token,
858                                               lexer->next_token));
859 }
860
861 /* Commit to the portion of the token stream most recently saved.  */
862
863 static void
864 cp_lexer_commit_tokens (cp_lexer* lexer)
865 {
866   /* Provide debugging output.  */
867   if (cp_lexer_debugging_p (lexer))
868     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
869
870   VARRAY_POP (lexer->saved_tokens);
871 }
872
873 /* Return all tokens saved since the last call to cp_lexer_save_tokens
874    to the token stream.  Stop saving tokens.  */
875
876 static void
877 cp_lexer_rollback_tokens (cp_lexer* lexer)
878 {
879   size_t delta;
880
881   /* Provide debugging output.  */
882   if (cp_lexer_debugging_p (lexer))
883     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
884
885   /* Find the token that was the NEXT_TOKEN when we started saving
886      tokens.  */
887   delta = VARRAY_TOP_INT(lexer->saved_tokens);
888   /* Make it the next token again now.  */
889   lexer->next_token = cp_lexer_advance_token (lexer,
890                                               lexer->first_token, 
891                                               delta);
892   /* It might be the case that there were no tokens when we started
893      saving tokens, but that there are some tokens now.  */
894   if (!lexer->next_token && lexer->first_token)
895     lexer->next_token = lexer->first_token;
896
897   /* Stop saving tokens.  */
898   VARRAY_POP (lexer->saved_tokens);
899 }
900
901 /* Print a representation of the TOKEN on the STREAM.  */
902
903 static void
904 cp_lexer_print_token (FILE * stream, cp_token* token)
905 {
906   const char *token_type = NULL;
907
908   /* Figure out what kind of token this is.  */
909   switch (token->type)
910     {
911     case CPP_EQ:
912       token_type = "EQ";
913       break;
914
915     case CPP_COMMA:
916       token_type = "COMMA";
917       break;
918
919     case CPP_OPEN_PAREN:
920       token_type = "OPEN_PAREN";
921       break;
922
923     case CPP_CLOSE_PAREN:
924       token_type = "CLOSE_PAREN";
925       break;
926
927     case CPP_OPEN_BRACE:
928       token_type = "OPEN_BRACE";
929       break;
930
931     case CPP_CLOSE_BRACE:
932       token_type = "CLOSE_BRACE";
933       break;
934
935     case CPP_SEMICOLON:
936       token_type = "SEMICOLON";
937       break;
938
939     case CPP_NAME:
940       token_type = "NAME";
941       break;
942
943     case CPP_EOF:
944       token_type = "EOF";
945       break;
946
947     case CPP_KEYWORD:
948       token_type = "keyword";
949       break;
950
951       /* This is not a token that we know how to handle yet.  */
952     default:
953       break;
954     }
955
956   /* If we have a name for the token, print it out.  Otherwise, we
957      simply give the numeric code.  */
958   if (token_type)
959     fprintf (stream, "%s", token_type);
960   else
961     fprintf (stream, "%d", token->type);
962   /* And, for an identifier, print the identifier name.  */
963   if (token->type == CPP_NAME 
964       /* Some keywords have a value that is not an IDENTIFIER_NODE.
965          For example, `struct' is mapped to an INTEGER_CST.  */
966       || (token->type == CPP_KEYWORD 
967           && TREE_CODE (token->value) == IDENTIFIER_NODE))
968     fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
969 }
970
971 /* Start emitting debugging information.  */
972
973 static void
974 cp_lexer_start_debugging (cp_lexer* lexer)
975 {
976   ++lexer->debugging_p;
977 }
978   
979 /* Stop emitting debugging information.  */
980
981 static void
982 cp_lexer_stop_debugging (cp_lexer* lexer)
983 {
984   --lexer->debugging_p;
985 }
986
987 \f
988 /* The parser.  */
989
990 /* Overview
991    --------
992
993    A cp_parser parses the token stream as specified by the C++
994    grammar.  Its job is purely parsing, not semantic analysis.  For
995    example, the parser breaks the token stream into declarators,
996    expressions, statements, and other similar syntactic constructs.
997    It does not check that the types of the expressions on either side
998    of an assignment-statement are compatible, or that a function is
999    not declared with a parameter of type `void'.
1000
1001    The parser invokes routines elsewhere in the compiler to perform
1002    semantic analysis and to build up the abstract syntax tree for the
1003    code processed.  
1004
1005    The parser (and the template instantiation code, which is, in a
1006    way, a close relative of parsing) are the only parts of the
1007    compiler that should be calling push_scope and pop_scope, or
1008    related functions.  The parser (and template instantiation code)
1009    keeps track of what scope is presently active; everything else
1010    should simply honor that.  (The code that generates static
1011    initializers may also need to set the scope, in order to check
1012    access control correctly when emitting the initializers.)
1013
1014    Methodology
1015    -----------
1016    
1017    The parser is of the standard recursive-descent variety.  Upcoming
1018    tokens in the token stream are examined in order to determine which
1019    production to use when parsing a non-terminal.  Some C++ constructs
1020    require arbitrary look ahead to disambiguate.  For example, it is
1021    impossible, in the general case, to tell whether a statement is an
1022    expression or declaration without scanning the entire statement.
1023    Therefore, the parser is capable of "parsing tentatively."  When the
1024    parser is not sure what construct comes next, it enters this mode.
1025    Then, while we attempt to parse the construct, the parser queues up
1026    error messages, rather than issuing them immediately, and saves the
1027    tokens it consumes.  If the construct is parsed successfully, the
1028    parser "commits", i.e., it issues any queued error messages and
1029    the tokens that were being preserved are permanently discarded.
1030    If, however, the construct is not parsed successfully, the parser
1031    rolls back its state completely so that it can resume parsing using
1032    a different alternative.
1033
1034    Future Improvements
1035    -------------------
1036    
1037    The performance of the parser could probably be improved
1038    substantially.  Some possible improvements include:
1039
1040      - The expression parser recurses through the various levels of
1041        precedence as specified in the grammar, rather than using an
1042        operator-precedence technique.  Therefore, parsing a simple
1043        identifier requires multiple recursive calls.
1044
1045      - We could often eliminate the need to parse tentatively by
1046        looking ahead a little bit.  In some places, this approach
1047        might not entirely eliminate the need to parse tentatively, but
1048        it might still speed up the average case.  */
1049
1050 /* Flags that are passed to some parsing functions.  These values can
1051    be bitwise-ored together.  */
1052
1053 typedef enum cp_parser_flags
1054 {
1055   /* No flags.  */
1056   CP_PARSER_FLAGS_NONE = 0x0,
1057   /* The construct is optional.  If it is not present, then no error
1058      should be issued.  */
1059   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1060   /* When parsing a type-specifier, do not allow user-defined types.  */
1061   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1062 } cp_parser_flags;
1063
1064 /* The different kinds of declarators we want to parse.  */
1065
1066 typedef enum cp_parser_declarator_kind
1067 {
1068   /* We want an abstract declartor.  */
1069   CP_PARSER_DECLARATOR_ABSTRACT,
1070   /* We want a named declarator.  */
1071   CP_PARSER_DECLARATOR_NAMED,
1072   /* We don't mind, but the name must be an unqualified-id.  */
1073   CP_PARSER_DECLARATOR_EITHER
1074 } cp_parser_declarator_kind;
1075
1076 /* A mapping from a token type to a corresponding tree node type.  */
1077
1078 typedef struct cp_parser_token_tree_map_node
1079 {
1080   /* The token type.  */
1081   ENUM_BITFIELD (cpp_ttype) token_type : 8;
1082   /* The corresponding tree code.  */
1083   ENUM_BITFIELD (tree_code) tree_type : 8;
1084 } cp_parser_token_tree_map_node;
1085
1086 /* A complete map consists of several ordinary entries, followed by a
1087    terminator.  The terminating entry has a token_type of CPP_EOF.  */
1088
1089 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1090
1091 /* The status of a tentative parse.  */
1092
1093 typedef enum cp_parser_status_kind
1094 {
1095   /* No errors have occurred.  */
1096   CP_PARSER_STATUS_KIND_NO_ERROR,
1097   /* An error has occurred.  */
1098   CP_PARSER_STATUS_KIND_ERROR,
1099   /* We are committed to this tentative parse, whether or not an error
1100      has occurred.  */
1101   CP_PARSER_STATUS_KIND_COMMITTED
1102 } cp_parser_status_kind;
1103
1104 /* Context that is saved and restored when parsing tentatively.  */
1105
1106 typedef struct cp_parser_context GTY (())
1107 {
1108   /* If this is a tentative parsing context, the status of the
1109      tentative parse.  */
1110   enum cp_parser_status_kind status;
1111   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1112      that are looked up in this context must be looked up both in the
1113      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1114      the context of the containing expression.  */
1115   tree object_type;
1116   /* The next parsing context in the stack.  */
1117   struct cp_parser_context *next;
1118 } cp_parser_context;
1119
1120 /* Prototypes.  */
1121
1122 /* Constructors and destructors.  */
1123
1124 static cp_parser_context *cp_parser_context_new
1125   (cp_parser_context *);
1126
1127 /* Class variables.  */
1128
1129 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1130
1131 /* Constructors and destructors.  */
1132
1133 /* Construct a new context.  The context below this one on the stack
1134    is given by NEXT.  */
1135
1136 static cp_parser_context *
1137 cp_parser_context_new (cp_parser_context* next)
1138 {
1139   cp_parser_context *context;
1140
1141   /* Allocate the storage.  */
1142   if (cp_parser_context_free_list != NULL)
1143     {
1144       /* Pull the first entry from the free list.  */
1145       context = cp_parser_context_free_list;
1146       cp_parser_context_free_list = context->next;
1147       memset (context, 0, sizeof (*context));
1148     }
1149   else
1150     context = ggc_alloc_cleared (sizeof (cp_parser_context));
1151   /* No errors have occurred yet in this context.  */
1152   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1153   /* If this is not the bottomost context, copy information that we
1154      need from the previous context.  */
1155   if (next)
1156     {
1157       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1158          expression, then we are parsing one in this context, too.  */
1159       context->object_type = next->object_type;
1160       /* Thread the stack.  */
1161       context->next = next;
1162     }
1163
1164   return context;
1165 }
1166
1167 /* The cp_parser structure represents the C++ parser.  */
1168
1169 typedef struct cp_parser GTY(())
1170 {
1171   /* The lexer from which we are obtaining tokens.  */
1172   cp_lexer *lexer;
1173
1174   /* The scope in which names should be looked up.  If NULL_TREE, then
1175      we look up names in the scope that is currently open in the
1176      source program.  If non-NULL, this is either a TYPE or
1177      NAMESPACE_DECL for the scope in which we should look.  
1178
1179      This value is not cleared automatically after a name is looked
1180      up, so we must be careful to clear it before starting a new look
1181      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1182      will look up `Z' in the scope of `X', rather than the current
1183      scope.)  Unfortunately, it is difficult to tell when name lookup
1184      is complete, because we sometimes peek at a token, look it up,
1185      and then decide not to consume it.  */
1186   tree scope;
1187
1188   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1189      last lookup took place.  OBJECT_SCOPE is used if an expression
1190      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1191      respectively.  QUALIFYING_SCOPE is used for an expression of the 
1192      form "X::Y"; it refers to X.  */
1193   tree object_scope;
1194   tree qualifying_scope;
1195
1196   /* A stack of parsing contexts.  All but the bottom entry on the
1197      stack will be tentative contexts.
1198
1199      We parse tentatively in order to determine which construct is in
1200      use in some situations.  For example, in order to determine
1201      whether a statement is an expression-statement or a
1202      declaration-statement we parse it tentatively as a
1203      declaration-statement.  If that fails, we then reparse the same
1204      token stream as an expression-statement.  */
1205   cp_parser_context *context;
1206
1207   /* True if we are parsing GNU C++.  If this flag is not set, then
1208      GNU extensions are not recognized.  */
1209   bool allow_gnu_extensions_p;
1210
1211   /* TRUE if the `>' token should be interpreted as the greater-than
1212      operator.  FALSE if it is the end of a template-id or
1213      template-parameter-list.  */
1214   bool greater_than_is_operator_p;
1215
1216   /* TRUE if default arguments are allowed within a parameter list
1217      that starts at this point. FALSE if only a gnu extension makes
1218      them permissible.  */
1219   bool default_arg_ok_p;
1220   
1221   /* TRUE if we are parsing an integral constant-expression.  See
1222      [expr.const] for a precise definition.  */
1223   bool integral_constant_expression_p;
1224
1225   /* TRUE if we are parsing an integral constant-expression -- but a
1226      non-constant expression should be permitted as well.  This flag
1227      is used when parsing an array bound so that GNU variable-length
1228      arrays are tolerated.  */
1229   bool allow_non_integral_constant_expression_p;
1230
1231   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1232      been seen that makes the expression non-constant.  */
1233   bool non_integral_constant_expression_p;
1234
1235   /* TRUE if we are parsing the argument to "__offsetof__".  */
1236   bool in_offsetof_p;
1237
1238   /* TRUE if local variable names and `this' are forbidden in the
1239      current context.  */
1240   bool local_variables_forbidden_p;
1241
1242   /* TRUE if the declaration we are parsing is part of a
1243      linkage-specification of the form `extern string-literal
1244      declaration'.  */
1245   bool in_unbraced_linkage_specification_p;
1246
1247   /* TRUE if we are presently parsing a declarator, after the
1248      direct-declarator.  */
1249   bool in_declarator_p;
1250
1251   /* TRUE if we are presently parsing a template-argument-list.  */
1252   bool in_template_argument_list_p;
1253
1254   /* TRUE if we are presently parsing the body of an
1255      iteration-statement.  */
1256   bool in_iteration_statement_p;
1257
1258   /* TRUE if we are presently parsing the body of a switch
1259      statement.  */
1260   bool in_switch_statement_p;
1261
1262   /* TRUE if we are parsing a type-id in an expression context.  In
1263      such a situation, both "type (expr)" and "type (type)" are valid
1264      alternatives.  */
1265   bool in_type_id_in_expr_p;
1266
1267   /* If non-NULL, then we are parsing a construct where new type
1268      definitions are not permitted.  The string stored here will be
1269      issued as an error message if a type is defined.  */
1270   const char *type_definition_forbidden_message;
1271
1272   /* A list of lists. The outer list is a stack, used for member
1273      functions of local classes. At each level there are two sub-list,
1274      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1275      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1276      TREE_VALUE's. The functions are chained in reverse declaration
1277      order.
1278
1279      The TREE_PURPOSE sublist contains those functions with default
1280      arguments that need post processing, and the TREE_VALUE sublist
1281      contains those functions with definitions that need post
1282      processing.
1283
1284      These lists can only be processed once the outermost class being
1285      defined is complete.  */
1286   tree unparsed_functions_queues;
1287
1288   /* The number of classes whose definitions are currently in
1289      progress.  */
1290   unsigned num_classes_being_defined;
1291
1292   /* The number of template parameter lists that apply directly to the
1293      current declaration.  */
1294   unsigned num_template_parameter_lists;
1295 } cp_parser;
1296
1297 /* The type of a function that parses some kind of expression.  */
1298 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1299
1300 /* Prototypes.  */
1301
1302 /* Constructors and destructors.  */
1303
1304 static cp_parser *cp_parser_new
1305   (void);
1306
1307 /* Routines to parse various constructs.  
1308
1309    Those that return `tree' will return the error_mark_node (rather
1310    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1311    Sometimes, they will return an ordinary node if error-recovery was
1312    attempted, even though a parse error occurred.  So, to check
1313    whether or not a parse error occurred, you should always use
1314    cp_parser_error_occurred.  If the construct is optional (indicated
1315    either by an `_opt' in the name of the function that does the
1316    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1317    the construct is not present.  */
1318
1319 /* Lexical conventions [gram.lex]  */
1320
1321 static tree cp_parser_identifier
1322   (cp_parser *);
1323
1324 /* Basic concepts [gram.basic]  */
1325
1326 static bool cp_parser_translation_unit
1327   (cp_parser *);
1328
1329 /* Expressions [gram.expr]  */
1330
1331 static tree cp_parser_primary_expression
1332   (cp_parser *, cp_id_kind *, tree *);
1333 static tree cp_parser_id_expression
1334   (cp_parser *, bool, bool, bool *, bool);
1335 static tree cp_parser_unqualified_id
1336   (cp_parser *, bool, bool, bool);
1337 static tree cp_parser_nested_name_specifier_opt
1338   (cp_parser *, bool, bool, bool, bool);
1339 static tree cp_parser_nested_name_specifier
1340   (cp_parser *, bool, bool, bool, bool);
1341 static tree cp_parser_class_or_namespace_name
1342   (cp_parser *, bool, bool, bool, bool, bool);
1343 static tree cp_parser_postfix_expression
1344   (cp_parser *, bool);
1345 static tree cp_parser_parenthesized_expression_list
1346   (cp_parser *, bool, bool *);
1347 static void cp_parser_pseudo_destructor_name
1348   (cp_parser *, tree *, tree *);
1349 static tree cp_parser_unary_expression
1350   (cp_parser *, bool);
1351 static enum tree_code cp_parser_unary_operator
1352   (cp_token *);
1353 static tree cp_parser_new_expression
1354   (cp_parser *);
1355 static tree cp_parser_new_placement
1356   (cp_parser *);
1357 static tree cp_parser_new_type_id
1358   (cp_parser *);
1359 static tree cp_parser_new_declarator_opt
1360   (cp_parser *);
1361 static tree cp_parser_direct_new_declarator
1362   (cp_parser *);
1363 static tree cp_parser_new_initializer
1364   (cp_parser *);
1365 static tree cp_parser_delete_expression
1366   (cp_parser *);
1367 static tree cp_parser_cast_expression 
1368   (cp_parser *, bool);
1369 static tree cp_parser_pm_expression
1370   (cp_parser *);
1371 static tree cp_parser_multiplicative_expression
1372   (cp_parser *);
1373 static tree cp_parser_additive_expression
1374   (cp_parser *);
1375 static tree cp_parser_shift_expression
1376   (cp_parser *);
1377 static tree cp_parser_relational_expression
1378   (cp_parser *);
1379 static tree cp_parser_equality_expression
1380   (cp_parser *);
1381 static tree cp_parser_and_expression
1382   (cp_parser *);
1383 static tree cp_parser_exclusive_or_expression
1384   (cp_parser *);
1385 static tree cp_parser_inclusive_or_expression
1386   (cp_parser *);
1387 static tree cp_parser_logical_and_expression
1388   (cp_parser *);
1389 static tree cp_parser_logical_or_expression 
1390   (cp_parser *);
1391 static tree cp_parser_question_colon_clause
1392   (cp_parser *, tree);
1393 static tree cp_parser_assignment_expression
1394   (cp_parser *);
1395 static enum tree_code cp_parser_assignment_operator_opt
1396   (cp_parser *);
1397 static tree cp_parser_expression
1398   (cp_parser *);
1399 static tree cp_parser_constant_expression
1400   (cp_parser *, bool, bool *);
1401
1402 /* Statements [gram.stmt.stmt]  */
1403
1404 static void cp_parser_statement
1405   (cp_parser *, bool);
1406 static tree cp_parser_labeled_statement
1407   (cp_parser *, bool);
1408 static tree cp_parser_expression_statement
1409   (cp_parser *, bool);
1410 static tree cp_parser_compound_statement
1411   (cp_parser *, bool);
1412 static void cp_parser_statement_seq_opt
1413   (cp_parser *, bool);
1414 static tree cp_parser_selection_statement
1415   (cp_parser *);
1416 static tree cp_parser_condition
1417   (cp_parser *);
1418 static tree cp_parser_iteration_statement
1419   (cp_parser *);
1420 static void cp_parser_for_init_statement
1421   (cp_parser *);
1422 static tree cp_parser_jump_statement
1423   (cp_parser *);
1424 static void cp_parser_declaration_statement
1425   (cp_parser *);
1426
1427 static tree cp_parser_implicitly_scoped_statement
1428   (cp_parser *);
1429 static void cp_parser_already_scoped_statement
1430   (cp_parser *);
1431
1432 /* Declarations [gram.dcl.dcl] */
1433
1434 static void cp_parser_declaration_seq_opt
1435   (cp_parser *);
1436 static void cp_parser_declaration
1437   (cp_parser *);
1438 static void cp_parser_block_declaration
1439   (cp_parser *, bool);
1440 static void cp_parser_simple_declaration
1441   (cp_parser *, bool);
1442 static tree cp_parser_decl_specifier_seq 
1443   (cp_parser *, cp_parser_flags, tree *, int *);
1444 static tree cp_parser_storage_class_specifier_opt
1445   (cp_parser *);
1446 static tree cp_parser_function_specifier_opt
1447   (cp_parser *);
1448 static tree cp_parser_type_specifier
1449   (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1450 static tree cp_parser_simple_type_specifier
1451   (cp_parser *, cp_parser_flags, bool);
1452 static tree cp_parser_type_name
1453   (cp_parser *);
1454 static tree cp_parser_elaborated_type_specifier
1455   (cp_parser *, bool, bool);
1456 static tree cp_parser_enum_specifier
1457   (cp_parser *);
1458 static void cp_parser_enumerator_list
1459   (cp_parser *, tree);
1460 static void cp_parser_enumerator_definition 
1461   (cp_parser *, tree);
1462 static tree cp_parser_namespace_name
1463   (cp_parser *);
1464 static void cp_parser_namespace_definition
1465   (cp_parser *);
1466 static void cp_parser_namespace_body
1467   (cp_parser *);
1468 static tree cp_parser_qualified_namespace_specifier
1469   (cp_parser *);
1470 static void cp_parser_namespace_alias_definition
1471   (cp_parser *);
1472 static void cp_parser_using_declaration
1473   (cp_parser *);
1474 static void cp_parser_using_directive
1475   (cp_parser *);
1476 static void cp_parser_asm_definition
1477   (cp_parser *);
1478 static void cp_parser_linkage_specification
1479   (cp_parser *);
1480
1481 /* Declarators [gram.dcl.decl] */
1482
1483 static tree cp_parser_init_declarator
1484   (cp_parser *, tree, tree, bool, bool, int, bool *);
1485 static tree cp_parser_declarator
1486   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1487 static tree cp_parser_direct_declarator
1488   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1489 static enum tree_code cp_parser_ptr_operator
1490   (cp_parser *, tree *, tree *);
1491 static tree cp_parser_cv_qualifier_seq_opt
1492   (cp_parser *);
1493 static tree cp_parser_cv_qualifier_opt
1494   (cp_parser *);
1495 static tree cp_parser_declarator_id
1496   (cp_parser *);
1497 static tree cp_parser_type_id
1498   (cp_parser *);
1499 static tree cp_parser_type_specifier_seq
1500   (cp_parser *);
1501 static tree cp_parser_parameter_declaration_clause
1502   (cp_parser *);
1503 static tree cp_parser_parameter_declaration_list
1504   (cp_parser *);
1505 static tree cp_parser_parameter_declaration
1506   (cp_parser *, bool, bool *);
1507 static void cp_parser_function_body
1508   (cp_parser *);
1509 static tree cp_parser_initializer
1510   (cp_parser *, bool *, bool *);
1511 static tree cp_parser_initializer_clause
1512   (cp_parser *, bool *);
1513 static tree cp_parser_initializer_list
1514   (cp_parser *, bool *);
1515
1516 static bool cp_parser_ctor_initializer_opt_and_function_body
1517   (cp_parser *);
1518
1519 /* Classes [gram.class] */
1520
1521 static tree cp_parser_class_name
1522   (cp_parser *, bool, bool, bool, bool, bool, bool);
1523 static tree cp_parser_class_specifier
1524   (cp_parser *);
1525 static tree cp_parser_class_head
1526   (cp_parser *, bool *, tree *);
1527 static enum tag_types cp_parser_class_key
1528   (cp_parser *);
1529 static void cp_parser_member_specification_opt
1530   (cp_parser *);
1531 static void cp_parser_member_declaration
1532   (cp_parser *);
1533 static tree cp_parser_pure_specifier
1534   (cp_parser *);
1535 static tree cp_parser_constant_initializer
1536   (cp_parser *);
1537
1538 /* Derived classes [gram.class.derived] */
1539
1540 static tree cp_parser_base_clause
1541   (cp_parser *);
1542 static tree cp_parser_base_specifier
1543   (cp_parser *);
1544
1545 /* Special member functions [gram.special] */
1546
1547 static tree cp_parser_conversion_function_id
1548   (cp_parser *);
1549 static tree cp_parser_conversion_type_id
1550   (cp_parser *);
1551 static tree cp_parser_conversion_declarator_opt
1552   (cp_parser *);
1553 static bool cp_parser_ctor_initializer_opt
1554   (cp_parser *);
1555 static void cp_parser_mem_initializer_list
1556   (cp_parser *);
1557 static tree cp_parser_mem_initializer
1558   (cp_parser *);
1559 static tree cp_parser_mem_initializer_id
1560   (cp_parser *);
1561
1562 /* Overloading [gram.over] */
1563
1564 static tree cp_parser_operator_function_id
1565   (cp_parser *);
1566 static tree cp_parser_operator
1567   (cp_parser *);
1568
1569 /* Templates [gram.temp] */
1570
1571 static void cp_parser_template_declaration
1572   (cp_parser *, bool);
1573 static tree cp_parser_template_parameter_list
1574   (cp_parser *);
1575 static tree cp_parser_template_parameter
1576   (cp_parser *);
1577 static tree cp_parser_type_parameter
1578   (cp_parser *);
1579 static tree cp_parser_template_id
1580   (cp_parser *, bool, bool, bool);
1581 static tree cp_parser_template_name
1582   (cp_parser *, bool, bool, bool, bool *);
1583 static tree cp_parser_template_argument_list
1584   (cp_parser *);
1585 static tree cp_parser_template_argument
1586   (cp_parser *);
1587 static void cp_parser_explicit_instantiation
1588   (cp_parser *);
1589 static void cp_parser_explicit_specialization
1590   (cp_parser *);
1591
1592 /* Exception handling [gram.exception] */
1593
1594 static tree cp_parser_try_block 
1595   (cp_parser *);
1596 static bool cp_parser_function_try_block
1597   (cp_parser *);
1598 static void cp_parser_handler_seq
1599   (cp_parser *);
1600 static void cp_parser_handler
1601   (cp_parser *);
1602 static tree cp_parser_exception_declaration
1603   (cp_parser *);
1604 static tree cp_parser_throw_expression
1605   (cp_parser *);
1606 static tree cp_parser_exception_specification_opt
1607   (cp_parser *);
1608 static tree cp_parser_type_id_list
1609   (cp_parser *);
1610
1611 /* GNU Extensions */
1612
1613 static tree cp_parser_asm_specification_opt
1614   (cp_parser *);
1615 static tree cp_parser_asm_operand_list
1616   (cp_parser *);
1617 static tree cp_parser_asm_clobber_list
1618   (cp_parser *);
1619 static tree cp_parser_attributes_opt
1620   (cp_parser *);
1621 static tree cp_parser_attribute_list
1622   (cp_parser *);
1623 static bool cp_parser_extension_opt
1624   (cp_parser *, int *);
1625 static void cp_parser_label_declaration
1626   (cp_parser *);
1627
1628 /* Utility Routines */
1629
1630 static tree cp_parser_lookup_name
1631   (cp_parser *, tree, bool, bool, bool, bool);
1632 static tree cp_parser_lookup_name_simple
1633   (cp_parser *, tree);
1634 static tree cp_parser_maybe_treat_template_as_class
1635   (tree, bool);
1636 static bool cp_parser_check_declarator_template_parameters
1637   (cp_parser *, tree);
1638 static bool cp_parser_check_template_parameters
1639   (cp_parser *, unsigned);
1640 static tree cp_parser_simple_cast_expression
1641   (cp_parser *);
1642 static tree cp_parser_binary_expression
1643   (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1644 static tree cp_parser_global_scope_opt
1645   (cp_parser *, bool);
1646 static bool cp_parser_constructor_declarator_p
1647   (cp_parser *, bool);
1648 static tree cp_parser_function_definition_from_specifiers_and_declarator
1649   (cp_parser *, tree, tree, tree);
1650 static tree cp_parser_function_definition_after_declarator
1651   (cp_parser *, bool);
1652 static void cp_parser_template_declaration_after_export
1653   (cp_parser *, bool);
1654 static tree cp_parser_single_declaration
1655   (cp_parser *, bool, bool *);
1656 static tree cp_parser_functional_cast
1657   (cp_parser *, tree);
1658 static tree cp_parser_save_member_function_body
1659   (cp_parser *, tree, tree, tree);
1660 static tree cp_parser_enclosed_template_argument_list
1661   (cp_parser *);
1662 static void cp_parser_save_default_args
1663   (cp_parser *, tree);
1664 static void cp_parser_late_parsing_for_member
1665   (cp_parser *, tree);
1666 static void cp_parser_late_parsing_default_args
1667   (cp_parser *, tree);
1668 static tree cp_parser_sizeof_operand
1669   (cp_parser *, enum rid);
1670 static bool cp_parser_declares_only_class_p
1671   (cp_parser *);
1672 static bool cp_parser_friend_p
1673   (tree);
1674 static cp_token *cp_parser_require
1675   (cp_parser *, enum cpp_ttype, const char *);
1676 static cp_token *cp_parser_require_keyword
1677   (cp_parser *, enum rid, const char *);
1678 static bool cp_parser_token_starts_function_definition_p 
1679   (cp_token *);
1680 static bool cp_parser_next_token_starts_class_definition_p
1681   (cp_parser *);
1682 static bool cp_parser_next_token_ends_template_argument_p
1683   (cp_parser *);
1684 static bool cp_parser_nth_token_starts_template_argument_list_p
1685   (cp_parser *, size_t);
1686 static enum tag_types cp_parser_token_is_class_key
1687   (cp_token *);
1688 static void cp_parser_check_class_key
1689   (enum tag_types, tree type);
1690 static void cp_parser_check_access_in_redeclaration
1691   (tree type);
1692 static bool cp_parser_optional_template_keyword
1693   (cp_parser *);
1694 static void cp_parser_pre_parsed_nested_name_specifier 
1695   (cp_parser *);
1696 static void cp_parser_cache_group
1697   (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1698 static void cp_parser_parse_tentatively 
1699   (cp_parser *);
1700 static void cp_parser_commit_to_tentative_parse
1701   (cp_parser *);
1702 static void cp_parser_abort_tentative_parse
1703   (cp_parser *);
1704 static bool cp_parser_parse_definitely
1705   (cp_parser *);
1706 static inline bool cp_parser_parsing_tentatively
1707   (cp_parser *);
1708 static bool cp_parser_committed_to_tentative_parse
1709   (cp_parser *);
1710 static void cp_parser_error
1711   (cp_parser *, const char *);
1712 static void cp_parser_name_lookup_error
1713   (cp_parser *, tree, tree, const char *);
1714 static bool cp_parser_simulate_error
1715   (cp_parser *);
1716 static void cp_parser_check_type_definition
1717   (cp_parser *);
1718 static void cp_parser_check_for_definition_in_return_type
1719   (tree, int);
1720 static void cp_parser_check_for_invalid_template_id
1721   (cp_parser *, tree);
1722 static bool cp_parser_non_integral_constant_expression
1723   (cp_parser *, const char *);
1724 static bool cp_parser_diagnose_invalid_type_name
1725   (cp_parser *);
1726 static int cp_parser_skip_to_closing_parenthesis
1727   (cp_parser *, bool, bool, bool);
1728 static void cp_parser_skip_to_end_of_statement
1729   (cp_parser *);
1730 static void cp_parser_consume_semicolon_at_end_of_statement
1731   (cp_parser *);
1732 static void cp_parser_skip_to_end_of_block_or_statement
1733   (cp_parser *);
1734 static void cp_parser_skip_to_closing_brace
1735   (cp_parser *);
1736 static void cp_parser_skip_until_found
1737   (cp_parser *, enum cpp_ttype, const char *);
1738 static bool cp_parser_error_occurred
1739   (cp_parser *);
1740 static bool cp_parser_allow_gnu_extensions_p
1741   (cp_parser *);
1742 static bool cp_parser_is_string_literal
1743   (cp_token *);
1744 static bool cp_parser_is_keyword 
1745   (cp_token *, enum rid);
1746
1747 /* Returns nonzero if we are parsing tentatively.  */
1748
1749 static inline bool
1750 cp_parser_parsing_tentatively (cp_parser* parser)
1751 {
1752   return parser->context->next != NULL;
1753 }
1754
1755 /* Returns nonzero if TOKEN is a string literal.  */
1756
1757 static bool
1758 cp_parser_is_string_literal (cp_token* token)
1759 {
1760   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1761 }
1762
1763 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1764
1765 static bool
1766 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1767 {
1768   return token->keyword == keyword;
1769 }
1770
1771 /* Issue the indicated error MESSAGE.  */
1772
1773 static void
1774 cp_parser_error (cp_parser* parser, const char* message)
1775 {
1776   /* Output the MESSAGE -- unless we're parsing tentatively.  */
1777   if (!cp_parser_simulate_error (parser))
1778     {
1779       cp_token *token;
1780       token = cp_lexer_peek_token (parser->lexer);
1781       c_parse_error (message, 
1782                      /* Because c_parser_error does not understand
1783                         CPP_KEYWORD, keywords are treated like
1784                         identifiers.  */
1785                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type), 
1786                      token->value);
1787     }
1788 }
1789
1790 /* Issue an error about name-lookup failing.  NAME is the
1791    IDENTIFIER_NODE DECL is the result of
1792    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1793    the thing that we hoped to find.  */
1794
1795 static void
1796 cp_parser_name_lookup_error (cp_parser* parser,
1797                              tree name,
1798                              tree decl,
1799                              const char* desired)
1800 {
1801   /* If name lookup completely failed, tell the user that NAME was not
1802      declared.  */
1803   if (decl == error_mark_node)
1804     {
1805       if (parser->scope && parser->scope != global_namespace)
1806         error ("`%D::%D' has not been declared", 
1807                parser->scope, name);
1808       else if (parser->scope == global_namespace)
1809         error ("`::%D' has not been declared", name);
1810       else
1811         error ("`%D' has not been declared", name);
1812     }
1813   else if (parser->scope && parser->scope != global_namespace)
1814     error ("`%D::%D' %s", parser->scope, name, desired);
1815   else if (parser->scope == global_namespace)
1816     error ("`::%D' %s", name, desired);
1817   else
1818     error ("`%D' %s", name, desired);
1819 }
1820
1821 /* If we are parsing tentatively, remember that an error has occurred
1822    during this tentative parse.  Returns true if the error was
1823    simulated; false if a messgae should be issued by the caller.  */
1824
1825 static bool
1826 cp_parser_simulate_error (cp_parser* parser)
1827 {
1828   if (cp_parser_parsing_tentatively (parser)
1829       && !cp_parser_committed_to_tentative_parse (parser))
1830     {
1831       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1832       return true;
1833     }
1834   return false;
1835 }
1836
1837 /* This function is called when a type is defined.  If type
1838    definitions are forbidden at this point, an error message is
1839    issued.  */
1840
1841 static void
1842 cp_parser_check_type_definition (cp_parser* parser)
1843 {
1844   /* If types are forbidden here, issue a message.  */
1845   if (parser->type_definition_forbidden_message)
1846     /* Use `%s' to print the string in case there are any escape
1847        characters in the message.  */
1848     error ("%s", parser->type_definition_forbidden_message);
1849 }
1850
1851 /* This function is called when a declaration is parsed.  If
1852    DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1853    indicates that a type was defined in the decl-specifiers for DECL,
1854    then an error is issued.  */
1855
1856 static void
1857 cp_parser_check_for_definition_in_return_type (tree declarator, 
1858                                                int declares_class_or_enum)
1859 {
1860   /* [dcl.fct] forbids type definitions in return types.
1861      Unfortunately, it's not easy to know whether or not we are
1862      processing a return type until after the fact.  */
1863   while (declarator
1864          && (TREE_CODE (declarator) == INDIRECT_REF
1865              || TREE_CODE (declarator) == ADDR_EXPR))
1866     declarator = TREE_OPERAND (declarator, 0);
1867   if (declarator
1868       && TREE_CODE (declarator) == CALL_EXPR 
1869       && declares_class_or_enum & 2)
1870     error ("new types may not be defined in a return type");
1871 }
1872
1873 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1874    "<" in any valid C++ program.  If the next token is indeed "<",
1875    issue a message warning the user about what appears to be an
1876    invalid attempt to form a template-id.  */
1877
1878 static void
1879 cp_parser_check_for_invalid_template_id (cp_parser* parser, 
1880                                          tree type)
1881 {
1882   ptrdiff_t start;
1883   cp_token *token;
1884
1885   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1886     {
1887       if (TYPE_P (type))
1888         error ("`%T' is not a template", type);
1889       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1890         error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1891       else
1892         error ("invalid template-id");
1893       /* Remember the location of the invalid "<".  */
1894       if (cp_parser_parsing_tentatively (parser)
1895           && !cp_parser_committed_to_tentative_parse (parser))
1896         {
1897           token = cp_lexer_peek_token (parser->lexer);
1898           token = cp_lexer_prev_token (parser->lexer, token);
1899           start = cp_lexer_token_difference (parser->lexer,
1900                                              parser->lexer->first_token,
1901                                              token);
1902         }
1903       else
1904         start = -1;
1905       /* Consume the "<".  */
1906       cp_lexer_consume_token (parser->lexer);
1907       /* Parse the template arguments.  */
1908       cp_parser_enclosed_template_argument_list (parser);
1909       /* Permanently remove the invalid template arguments so that
1910          this error message is not issued again.  */
1911       if (start >= 0)
1912         {
1913           token = cp_lexer_advance_token (parser->lexer,
1914                                           parser->lexer->first_token,
1915                                           start);
1916           cp_lexer_purge_tokens_after (parser->lexer, token);
1917         }
1918     }
1919 }
1920
1921 /* If parsing an integral constant-expression, issue an error message
1922    about the fact that THING appeared and return true.  Otherwise,
1923    return false, marking the current expression as non-constant.  */
1924
1925 static bool
1926 cp_parser_non_integral_constant_expression (cp_parser  *parser,
1927                                             const char *thing)
1928 {
1929   if (parser->integral_constant_expression_p)
1930     {
1931       if (!parser->allow_non_integral_constant_expression_p)
1932         {
1933           error ("%s cannot appear in a constant-expression", thing);
1934           return true;
1935         }
1936       parser->non_integral_constant_expression_p = true;
1937     }
1938   return false;
1939 }
1940
1941 /* Check for a common situation where a type-name should be present,
1942    but is not, and issue a sensible error message.  Returns true if an
1943    invalid type-name was detected.  */
1944
1945 static bool
1946 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1947 {
1948   /* If the next two tokens are both identifiers, the code is
1949      erroneous. The usual cause of this situation is code like:
1950
1951        T t;
1952
1953      where "T" should name a type -- but does not.  */
1954   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1955       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1956     {
1957       tree name;
1958
1959       /* If parsing tentatively, we should commit; we really are
1960          looking at a declaration.  */
1961       /* Consume the first identifier.  */
1962       name = cp_lexer_consume_token (parser->lexer)->value;
1963       /* Issue an error message.  */
1964       error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1965       /* If we're in a template class, it's possible that the user was
1966          referring to a type from a base class.  For example:
1967
1968            template <typename T> struct A { typedef T X; };
1969            template <typename T> struct B : public A<T> { X x; };
1970
1971          The user should have said "typename A<T>::X".  */
1972       if (processing_template_decl && current_class_type)
1973         {
1974           tree b;
1975
1976           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1977                b;
1978                b = TREE_CHAIN (b))
1979             {
1980               tree base_type = BINFO_TYPE (b);
1981               if (CLASS_TYPE_P (base_type) 
1982                   && dependent_type_p (base_type))
1983                 {
1984                   tree field;
1985                   /* Go from a particular instantiation of the
1986                      template (which will have an empty TYPE_FIELDs),
1987                      to the main version.  */
1988                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1989                   for (field = TYPE_FIELDS (base_type);
1990                        field;
1991                        field = TREE_CHAIN (field))
1992                     if (TREE_CODE (field) == TYPE_DECL
1993                         && DECL_NAME (field) == name)
1994                       {
1995                         error ("(perhaps `typename %T::%s' was intended)",
1996                                BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1997                         break;
1998                       }
1999                   if (field)
2000                     break;
2001                 }
2002             }
2003         }
2004       /* Skip to the end of the declaration; there's no point in
2005          trying to process it.  */
2006       cp_parser_skip_to_end_of_statement (parser);
2007       
2008       return true;
2009     }
2010
2011   return false;
2012 }
2013
2014 /* Consume tokens up to, and including, the next non-nested closing `)'. 
2015    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2016    are doing error recovery. Returns -1 if OR_COMMA is true and we
2017    found an unnested comma.  */
2018
2019 static int
2020 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2021                                        bool recovering, 
2022                                        bool or_comma,
2023                                        bool consume_paren)
2024 {
2025   unsigned paren_depth = 0;
2026   unsigned brace_depth = 0;
2027
2028   if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2029       && !cp_parser_committed_to_tentative_parse (parser))
2030     return 0;
2031   
2032   while (true)
2033     {
2034       cp_token *token;
2035       
2036       /* If we've run out of tokens, then there is no closing `)'.  */
2037       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2038         return 0;
2039
2040       token = cp_lexer_peek_token (parser->lexer);
2041       
2042       /* This matches the processing in skip_to_end_of_statement.  */
2043       if (token->type == CPP_SEMICOLON && !brace_depth)
2044         return 0;
2045       if (token->type == CPP_OPEN_BRACE)
2046         ++brace_depth;
2047       if (token->type == CPP_CLOSE_BRACE)
2048         {
2049           if (!brace_depth--)
2050             return 0;
2051         }
2052       if (recovering && or_comma && token->type == CPP_COMMA
2053           && !brace_depth && !paren_depth)
2054         return -1;
2055       
2056       if (!brace_depth)
2057         {
2058           /* If it is an `(', we have entered another level of nesting.  */
2059           if (token->type == CPP_OPEN_PAREN)
2060             ++paren_depth;
2061           /* If it is a `)', then we might be done.  */
2062           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2063             {
2064               if (consume_paren)
2065                 cp_lexer_consume_token (parser->lexer);
2066               return 1;
2067             }
2068         }
2069       
2070       /* Consume the token.  */
2071       cp_lexer_consume_token (parser->lexer);
2072     }
2073 }
2074
2075 /* Consume tokens until we reach the end of the current statement.
2076    Normally, that will be just before consuming a `;'.  However, if a
2077    non-nested `}' comes first, then we stop before consuming that.  */
2078
2079 static void
2080 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2081 {
2082   unsigned nesting_depth = 0;
2083
2084   while (true)
2085     {
2086       cp_token *token;
2087
2088       /* Peek at the next token.  */
2089       token = cp_lexer_peek_token (parser->lexer);
2090       /* If we've run out of tokens, stop.  */
2091       if (token->type == CPP_EOF)
2092         break;
2093       /* If the next token is a `;', we have reached the end of the
2094          statement.  */
2095       if (token->type == CPP_SEMICOLON && !nesting_depth)
2096         break;
2097       /* If the next token is a non-nested `}', then we have reached
2098          the end of the current block.  */
2099       if (token->type == CPP_CLOSE_BRACE)
2100         {
2101           /* If this is a non-nested `}', stop before consuming it.
2102              That way, when confronted with something like:
2103
2104                { 3 + } 
2105
2106              we stop before consuming the closing `}', even though we
2107              have not yet reached a `;'.  */
2108           if (nesting_depth == 0)
2109             break;
2110           /* If it is the closing `}' for a block that we have
2111              scanned, stop -- but only after consuming the token.
2112              That way given:
2113
2114                 void f g () { ... }
2115                 typedef int I;
2116
2117              we will stop after the body of the erroneously declared
2118              function, but before consuming the following `typedef'
2119              declaration.  */
2120           if (--nesting_depth == 0)
2121             {
2122               cp_lexer_consume_token (parser->lexer);
2123               break;
2124             }
2125         }
2126       /* If it the next token is a `{', then we are entering a new
2127          block.  Consume the entire block.  */
2128       else if (token->type == CPP_OPEN_BRACE)
2129         ++nesting_depth;
2130       /* Consume the token.  */
2131       cp_lexer_consume_token (parser->lexer);
2132     }
2133 }
2134
2135 /* This function is called at the end of a statement or declaration.
2136    If the next token is a semicolon, it is consumed; otherwise, error
2137    recovery is attempted.  */
2138
2139 static void
2140 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2141 {
2142   /* Look for the trailing `;'.  */
2143   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2144     {
2145       /* If there is additional (erroneous) input, skip to the end of
2146          the statement.  */
2147       cp_parser_skip_to_end_of_statement (parser);
2148       /* If the next token is now a `;', consume it.  */
2149       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2150         cp_lexer_consume_token (parser->lexer);
2151     }
2152 }
2153
2154 /* Skip tokens until we have consumed an entire block, or until we
2155    have consumed a non-nested `;'.  */
2156
2157 static void
2158 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2159 {
2160   unsigned nesting_depth = 0;
2161
2162   while (true)
2163     {
2164       cp_token *token;
2165
2166       /* Peek at the next token.  */
2167       token = cp_lexer_peek_token (parser->lexer);
2168       /* If we've run out of tokens, stop.  */
2169       if (token->type == CPP_EOF)
2170         break;
2171       /* If the next token is a `;', we have reached the end of the
2172          statement.  */
2173       if (token->type == CPP_SEMICOLON && !nesting_depth)
2174         {
2175           /* Consume the `;'.  */
2176           cp_lexer_consume_token (parser->lexer);
2177           break;
2178         }
2179       /* Consume the token.  */
2180       token = cp_lexer_consume_token (parser->lexer);
2181       /* If the next token is a non-nested `}', then we have reached
2182          the end of the current block.  */
2183       if (token->type == CPP_CLOSE_BRACE 
2184           && (nesting_depth == 0 || --nesting_depth == 0))
2185         break;
2186       /* If it the next token is a `{', then we are entering a new
2187          block.  Consume the entire block.  */
2188       if (token->type == CPP_OPEN_BRACE)
2189         ++nesting_depth;
2190     }
2191 }
2192
2193 /* Skip tokens until a non-nested closing curly brace is the next
2194    token.  */
2195
2196 static void
2197 cp_parser_skip_to_closing_brace (cp_parser *parser)
2198 {
2199   unsigned nesting_depth = 0;
2200
2201   while (true)
2202     {
2203       cp_token *token;
2204
2205       /* Peek at the next token.  */
2206       token = cp_lexer_peek_token (parser->lexer);
2207       /* If we've run out of tokens, stop.  */
2208       if (token->type == CPP_EOF)
2209         break;
2210       /* If the next token is a non-nested `}', then we have reached
2211          the end of the current block.  */
2212       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2213         break;
2214       /* If it the next token is a `{', then we are entering a new
2215          block.  Consume the entire block.  */
2216       else if (token->type == CPP_OPEN_BRACE)
2217         ++nesting_depth;
2218       /* Consume the token.  */
2219       cp_lexer_consume_token (parser->lexer);
2220     }
2221 }
2222
2223 /* Create a new C++ parser.  */
2224
2225 static cp_parser *
2226 cp_parser_new (void)
2227 {
2228   cp_parser *parser;
2229   cp_lexer *lexer;
2230
2231   /* cp_lexer_new_main is called before calling ggc_alloc because
2232      cp_lexer_new_main might load a PCH file.  */
2233   lexer = cp_lexer_new_main ();
2234
2235   parser = ggc_alloc_cleared (sizeof (cp_parser));
2236   parser->lexer = lexer;
2237   parser->context = cp_parser_context_new (NULL);
2238
2239   /* For now, we always accept GNU extensions.  */
2240   parser->allow_gnu_extensions_p = 1;
2241
2242   /* The `>' token is a greater-than operator, not the end of a
2243      template-id.  */
2244   parser->greater_than_is_operator_p = true;
2245
2246   parser->default_arg_ok_p = true;
2247   
2248   /* We are not parsing a constant-expression.  */
2249   parser->integral_constant_expression_p = false;
2250   parser->allow_non_integral_constant_expression_p = false;
2251   parser->non_integral_constant_expression_p = false;
2252
2253   /* We are not parsing offsetof.  */
2254   parser->in_offsetof_p = false;
2255
2256   /* Local variable names are not forbidden.  */
2257   parser->local_variables_forbidden_p = false;
2258
2259   /* We are not processing an `extern "C"' declaration.  */
2260   parser->in_unbraced_linkage_specification_p = false;
2261
2262   /* We are not processing a declarator.  */
2263   parser->in_declarator_p = false;
2264
2265   /* We are not processing a template-argument-list.  */
2266   parser->in_template_argument_list_p = false;
2267
2268   /* We are not in an iteration statement.  */
2269   parser->in_iteration_statement_p = false;
2270
2271   /* We are not in a switch statement.  */
2272   parser->in_switch_statement_p = false;
2273
2274   /* We are not parsing a type-id inside an expression.  */
2275   parser->in_type_id_in_expr_p = false;
2276
2277   /* The unparsed function queue is empty.  */
2278   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2279
2280   /* There are no classes being defined.  */
2281   parser->num_classes_being_defined = 0;
2282
2283   /* No template parameters apply.  */
2284   parser->num_template_parameter_lists = 0;
2285
2286   return parser;
2287 }
2288
2289 /* Lexical conventions [gram.lex]  */
2290
2291 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2292    identifier.  */
2293
2294 static tree 
2295 cp_parser_identifier (cp_parser* parser)
2296 {
2297   cp_token *token;
2298
2299   /* Look for the identifier.  */
2300   token = cp_parser_require (parser, CPP_NAME, "identifier");
2301   /* Return the value.  */
2302   return token ? token->value : error_mark_node;
2303 }
2304
2305 /* Basic concepts [gram.basic]  */
2306
2307 /* Parse a translation-unit.
2308
2309    translation-unit:
2310      declaration-seq [opt]  
2311
2312    Returns TRUE if all went well.  */
2313
2314 static bool
2315 cp_parser_translation_unit (cp_parser* parser)
2316 {
2317   while (true)
2318     {
2319       cp_parser_declaration_seq_opt (parser);
2320
2321       /* If there are no tokens left then all went well.  */
2322       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2323         break;
2324       
2325       /* Otherwise, issue an error message.  */
2326       cp_parser_error (parser, "expected declaration");
2327       return false;
2328     }
2329
2330   /* Consume the EOF token.  */
2331   cp_parser_require (parser, CPP_EOF, "end-of-file");
2332   
2333   /* Finish up.  */
2334   finish_translation_unit ();
2335
2336   /* All went well.  */
2337   return true;
2338 }
2339
2340 /* Expressions [gram.expr] */
2341
2342 /* Parse a primary-expression.
2343
2344    primary-expression:
2345      literal
2346      this
2347      ( expression )
2348      id-expression
2349
2350    GNU Extensions:
2351
2352    primary-expression:
2353      ( compound-statement )
2354      __builtin_va_arg ( assignment-expression , type-id )
2355
2356    literal:
2357      __null
2358
2359    Returns a representation of the expression.  
2360
2361    *IDK indicates what kind of id-expression (if any) was present.  
2362
2363    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2364    used as the operand of a pointer-to-member.  In that case,
2365    *QUALIFYING_CLASS gives the class that is used as the qualifying
2366    class in the pointer-to-member.  */
2367
2368 static tree
2369 cp_parser_primary_expression (cp_parser *parser, 
2370                               cp_id_kind *idk,
2371                               tree *qualifying_class)
2372 {
2373   cp_token *token;
2374
2375   /* Assume the primary expression is not an id-expression.  */
2376   *idk = CP_ID_KIND_NONE;
2377   /* And that it cannot be used as pointer-to-member.  */
2378   *qualifying_class = NULL_TREE;
2379
2380   /* Peek at the next token.  */
2381   token = cp_lexer_peek_token (parser->lexer);
2382   switch (token->type)
2383     {
2384       /* literal:
2385            integer-literal
2386            character-literal
2387            floating-literal
2388            string-literal
2389            boolean-literal  */
2390     case CPP_CHAR:
2391     case CPP_WCHAR:
2392     case CPP_STRING:
2393     case CPP_WSTRING:
2394     case CPP_NUMBER:
2395       token = cp_lexer_consume_token (parser->lexer);
2396       return token->value;
2397
2398     case CPP_OPEN_PAREN:
2399       {
2400         tree expr;
2401         bool saved_greater_than_is_operator_p;
2402
2403         /* Consume the `('.  */
2404         cp_lexer_consume_token (parser->lexer);
2405         /* Within a parenthesized expression, a `>' token is always
2406            the greater-than operator.  */
2407         saved_greater_than_is_operator_p 
2408           = parser->greater_than_is_operator_p;
2409         parser->greater_than_is_operator_p = true;
2410         /* If we see `( { ' then we are looking at the beginning of
2411            a GNU statement-expression.  */
2412         if (cp_parser_allow_gnu_extensions_p (parser)
2413             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2414           {
2415             /* Statement-expressions are not allowed by the standard.  */
2416             if (pedantic)
2417               pedwarn ("ISO C++ forbids braced-groups within expressions");  
2418             
2419             /* And they're not allowed outside of a function-body; you
2420                cannot, for example, write:
2421                
2422                  int i = ({ int j = 3; j + 1; });
2423                
2424                at class or namespace scope.  */
2425             if (!at_function_scope_p ())
2426               error ("statement-expressions are allowed only inside functions");
2427             /* Start the statement-expression.  */
2428             expr = begin_stmt_expr ();
2429             /* Parse the compound-statement.  */
2430             cp_parser_compound_statement (parser, true);
2431             /* Finish up.  */
2432             expr = finish_stmt_expr (expr, false);
2433           }
2434         else
2435           {
2436             /* Parse the parenthesized expression.  */
2437             expr = cp_parser_expression (parser);
2438             /* Let the front end know that this expression was
2439                enclosed in parentheses. This matters in case, for
2440                example, the expression is of the form `A::B', since
2441                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2442                not.  */
2443             finish_parenthesized_expr (expr);
2444           }
2445         /* The `>' token might be the end of a template-id or
2446            template-parameter-list now.  */
2447         parser->greater_than_is_operator_p 
2448           = saved_greater_than_is_operator_p;
2449         /* Consume the `)'.  */
2450         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2451           cp_parser_skip_to_end_of_statement (parser);
2452
2453         return expr;
2454       }
2455
2456     case CPP_KEYWORD:
2457       switch (token->keyword)
2458         {
2459           /* These two are the boolean literals.  */
2460         case RID_TRUE:
2461           cp_lexer_consume_token (parser->lexer);
2462           return boolean_true_node;
2463         case RID_FALSE:
2464           cp_lexer_consume_token (parser->lexer);
2465           return boolean_false_node;
2466           
2467           /* The `__null' literal.  */
2468         case RID_NULL:
2469           cp_lexer_consume_token (parser->lexer);
2470           return null_node;
2471
2472           /* Recognize the `this' keyword.  */
2473         case RID_THIS:
2474           cp_lexer_consume_token (parser->lexer);
2475           if (parser->local_variables_forbidden_p)
2476             {
2477               error ("`this' may not be used in this context");
2478               return error_mark_node;
2479             }
2480           /* Pointers cannot appear in constant-expressions.  */
2481           if (cp_parser_non_integral_constant_expression (parser,
2482                                                           "`this'"))
2483             return error_mark_node;
2484           return finish_this_expr ();
2485
2486           /* The `operator' keyword can be the beginning of an
2487              id-expression.  */
2488         case RID_OPERATOR:
2489           goto id_expression;
2490
2491         case RID_FUNCTION_NAME:
2492         case RID_PRETTY_FUNCTION_NAME:
2493         case RID_C99_FUNCTION_NAME:
2494           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2495              __func__ are the names of variables -- but they are
2496              treated specially.  Therefore, they are handled here,
2497              rather than relying on the generic id-expression logic
2498              below.  Grammatically, these names are id-expressions.  
2499
2500              Consume the token.  */
2501           token = cp_lexer_consume_token (parser->lexer);
2502           /* Look up the name.  */
2503           return finish_fname (token->value);
2504
2505         case RID_VA_ARG:
2506           {
2507             tree expression;
2508             tree type;
2509
2510             /* The `__builtin_va_arg' construct is used to handle
2511                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2512             cp_lexer_consume_token (parser->lexer);
2513             /* Look for the opening `('.  */
2514             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2515             /* Now, parse the assignment-expression.  */
2516             expression = cp_parser_assignment_expression (parser);
2517             /* Look for the `,'.  */
2518             cp_parser_require (parser, CPP_COMMA, "`,'");
2519             /* Parse the type-id.  */
2520             type = cp_parser_type_id (parser);
2521             /* Look for the closing `)'.  */
2522             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2523             /* Using `va_arg' in a constant-expression is not
2524                allowed.  */
2525             if (cp_parser_non_integral_constant_expression (parser,
2526                                                             "`va_arg'"))
2527               return error_mark_node;
2528             return build_x_va_arg (expression, type);
2529           }
2530
2531         case RID_OFFSETOF:
2532           {
2533             tree expression;
2534             bool saved_in_offsetof_p;
2535
2536             /* Consume the "__offsetof__" token.  */
2537             cp_lexer_consume_token (parser->lexer);
2538             /* Consume the opening `('.  */
2539             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2540             /* Parse the parenthesized (almost) constant-expression.  */
2541             saved_in_offsetof_p = parser->in_offsetof_p;
2542             parser->in_offsetof_p = true;
2543             expression 
2544               = cp_parser_constant_expression (parser,
2545                                                /*allow_non_constant_p=*/false,
2546                                                /*non_constant_p=*/NULL);
2547             parser->in_offsetof_p = saved_in_offsetof_p;
2548             /* Consume the closing ')'.  */
2549             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2550
2551             return expression;
2552           }
2553
2554         default:
2555           cp_parser_error (parser, "expected primary-expression");
2556           return error_mark_node;
2557         }
2558
2559       /* An id-expression can start with either an identifier, a
2560          `::' as the beginning of a qualified-id, or the "operator"
2561          keyword.  */
2562     case CPP_NAME:
2563     case CPP_SCOPE:
2564     case CPP_TEMPLATE_ID:
2565     case CPP_NESTED_NAME_SPECIFIER:
2566       {
2567         tree id_expression;
2568         tree decl;
2569         const char *error_msg;
2570
2571       id_expression:
2572         /* Parse the id-expression.  */
2573         id_expression 
2574           = cp_parser_id_expression (parser, 
2575                                      /*template_keyword_p=*/false,
2576                                      /*check_dependency_p=*/true,
2577                                      /*template_p=*/NULL,
2578                                      /*declarator_p=*/false);
2579         if (id_expression == error_mark_node)
2580           return error_mark_node;
2581         /* If we have a template-id, then no further lookup is
2582            required.  If the template-id was for a template-class, we
2583            will sometimes have a TYPE_DECL at this point.  */
2584         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2585             || TREE_CODE (id_expression) == TYPE_DECL)
2586           decl = id_expression;
2587         /* Look up the name.  */
2588         else 
2589           {
2590             decl = cp_parser_lookup_name_simple (parser, id_expression);
2591             /* If name lookup gives us a SCOPE_REF, then the
2592                qualifying scope was dependent.  Just propagate the
2593                name.  */
2594             if (TREE_CODE (decl) == SCOPE_REF)
2595               {
2596                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2597                   *qualifying_class = TREE_OPERAND (decl, 0);
2598                 return decl;
2599               }
2600             /* Check to see if DECL is a local variable in a context
2601                where that is forbidden.  */
2602             if (parser->local_variables_forbidden_p
2603                 && local_variable_p (decl))
2604               {
2605                 /* It might be that we only found DECL because we are
2606                    trying to be generous with pre-ISO scoping rules.
2607                    For example, consider:
2608
2609                      int i;
2610                      void g() {
2611                        for (int i = 0; i < 10; ++i) {}
2612                        extern void f(int j = i);
2613                      }
2614
2615                    Here, name look up will originally find the out 
2616                    of scope `i'.  We need to issue a warning message,
2617                    but then use the global `i'.  */
2618                 decl = check_for_out_of_scope_variable (decl);
2619                 if (local_variable_p (decl))
2620                   {
2621                     error ("local variable `%D' may not appear in this context",
2622                            decl);
2623                     return error_mark_node;
2624                   }
2625               }
2626           }
2627         
2628         decl = finish_id_expression (id_expression, decl, parser->scope, 
2629                                      idk, qualifying_class,
2630                                      parser->integral_constant_expression_p,
2631                                      parser->allow_non_integral_constant_expression_p,
2632                                      &parser->non_integral_constant_expression_p,
2633                                      &error_msg);
2634         if (error_msg)
2635           cp_parser_error (parser, error_msg);
2636         return decl;
2637       }
2638
2639       /* Anything else is an error.  */
2640     default:
2641       cp_parser_error (parser, "expected primary-expression");
2642       return error_mark_node;
2643     }
2644 }
2645
2646 /* Parse an id-expression.
2647
2648    id-expression:
2649      unqualified-id
2650      qualified-id
2651
2652    qualified-id:
2653      :: [opt] nested-name-specifier template [opt] unqualified-id
2654      :: identifier
2655      :: operator-function-id
2656      :: template-id
2657
2658    Return a representation of the unqualified portion of the
2659    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2660    a `::' or nested-name-specifier.
2661
2662    Often, if the id-expression was a qualified-id, the caller will
2663    want to make a SCOPE_REF to represent the qualified-id.  This
2664    function does not do this in order to avoid wastefully creating
2665    SCOPE_REFs when they are not required.
2666
2667    If TEMPLATE_KEYWORD_P is true, then we have just seen the
2668    `template' keyword.
2669
2670    If CHECK_DEPENDENCY_P is false, then names are looked up inside
2671    uninstantiated templates.  
2672
2673    If *TEMPLATE_P is non-NULL, it is set to true iff the
2674    `template' keyword is used to explicitly indicate that the entity
2675    named is a template.  
2676
2677    If DECLARATOR_P is true, the id-expression is appearing as part of
2678    a declarator, rather than as part of an expression.  */
2679
2680 static tree
2681 cp_parser_id_expression (cp_parser *parser,
2682                          bool template_keyword_p,
2683                          bool check_dependency_p,
2684                          bool *template_p,
2685                          bool declarator_p)
2686 {
2687   bool global_scope_p;
2688   bool nested_name_specifier_p;
2689
2690   /* Assume the `template' keyword was not used.  */
2691   if (template_p)
2692     *template_p = false;
2693
2694   /* Look for the optional `::' operator.  */
2695   global_scope_p 
2696     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) 
2697        != NULL_TREE);
2698   /* Look for the optional nested-name-specifier.  */
2699   nested_name_specifier_p 
2700     = (cp_parser_nested_name_specifier_opt (parser,
2701                                             /*typename_keyword_p=*/false,
2702                                             check_dependency_p,
2703                                             /*type_p=*/false,
2704                                             declarator_p)
2705        != NULL_TREE);
2706   /* If there is a nested-name-specifier, then we are looking at
2707      the first qualified-id production.  */
2708   if (nested_name_specifier_p)
2709     {
2710       tree saved_scope;
2711       tree saved_object_scope;
2712       tree saved_qualifying_scope;
2713       tree unqualified_id;
2714       bool is_template;
2715
2716       /* See if the next token is the `template' keyword.  */
2717       if (!template_p)
2718         template_p = &is_template;
2719       *template_p = cp_parser_optional_template_keyword (parser);
2720       /* Name lookup we do during the processing of the
2721          unqualified-id might obliterate SCOPE.  */
2722       saved_scope = parser->scope;
2723       saved_object_scope = parser->object_scope;
2724       saved_qualifying_scope = parser->qualifying_scope;
2725       /* Process the final unqualified-id.  */
2726       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2727                                                  check_dependency_p,
2728                                                  declarator_p);
2729       /* Restore the SAVED_SCOPE for our caller.  */
2730       parser->scope = saved_scope;
2731       parser->object_scope = saved_object_scope;
2732       parser->qualifying_scope = saved_qualifying_scope;
2733
2734       return unqualified_id;
2735     }
2736   /* Otherwise, if we are in global scope, then we are looking at one
2737      of the other qualified-id productions.  */
2738   else if (global_scope_p)
2739     {
2740       cp_token *token;
2741       tree id;
2742
2743       /* Peek at the next token.  */
2744       token = cp_lexer_peek_token (parser->lexer);
2745
2746       /* If it's an identifier, and the next token is not a "<", then
2747          we can avoid the template-id case.  This is an optimization
2748          for this common case.  */
2749       if (token->type == CPP_NAME 
2750           && !cp_parser_nth_token_starts_template_argument_list_p 
2751                (parser, 2))
2752         return cp_parser_identifier (parser);
2753
2754       cp_parser_parse_tentatively (parser);
2755       /* Try a template-id.  */
2756       id = cp_parser_template_id (parser, 
2757                                   /*template_keyword_p=*/false,
2758                                   /*check_dependency_p=*/true,
2759                                   declarator_p);
2760       /* If that worked, we're done.  */
2761       if (cp_parser_parse_definitely (parser))
2762         return id;
2763
2764       /* Peek at the next token.  (Changes in the token buffer may
2765          have invalidated the pointer obtained above.)  */
2766       token = cp_lexer_peek_token (parser->lexer);
2767
2768       switch (token->type)
2769         {
2770         case CPP_NAME:
2771           return cp_parser_identifier (parser);
2772
2773         case CPP_KEYWORD:
2774           if (token->keyword == RID_OPERATOR)
2775             return cp_parser_operator_function_id (parser);
2776           /* Fall through.  */
2777           
2778         default:
2779           cp_parser_error (parser, "expected id-expression");
2780           return error_mark_node;
2781         }
2782     }
2783   else
2784     return cp_parser_unqualified_id (parser, template_keyword_p,
2785                                      /*check_dependency_p=*/true,
2786                                      declarator_p);
2787 }
2788
2789 /* Parse an unqualified-id.
2790
2791    unqualified-id:
2792      identifier
2793      operator-function-id
2794      conversion-function-id
2795      ~ class-name
2796      template-id
2797
2798    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2799    keyword, in a construct like `A::template ...'.
2800
2801    Returns a representation of unqualified-id.  For the `identifier'
2802    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
2803    production a BIT_NOT_EXPR is returned; the operand of the
2804    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
2805    other productions, see the documentation accompanying the
2806    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
2807    names are looked up in uninstantiated templates.  If DECLARATOR_P
2808    is true, the unqualified-id is appearing as part of a declarator,
2809    rather than as part of an expression.  */
2810
2811 static tree
2812 cp_parser_unqualified_id (cp_parser* parser, 
2813                           bool template_keyword_p,
2814                           bool check_dependency_p,
2815                           bool declarator_p)
2816 {
2817   cp_token *token;
2818
2819   /* Peek at the next token.  */
2820   token = cp_lexer_peek_token (parser->lexer);
2821   
2822   switch (token->type)
2823     {
2824     case CPP_NAME:
2825       {
2826         tree id;
2827
2828         /* We don't know yet whether or not this will be a
2829            template-id.  */
2830         cp_parser_parse_tentatively (parser);
2831         /* Try a template-id.  */
2832         id = cp_parser_template_id (parser, template_keyword_p,
2833                                     check_dependency_p,
2834                                     declarator_p);
2835         /* If it worked, we're done.  */
2836         if (cp_parser_parse_definitely (parser))
2837           return id;
2838         /* Otherwise, it's an ordinary identifier.  */
2839         return cp_parser_identifier (parser);
2840       }
2841
2842     case CPP_TEMPLATE_ID:
2843       return cp_parser_template_id (parser, template_keyword_p,
2844                                     check_dependency_p,
2845                                     declarator_p);
2846
2847     case CPP_COMPL:
2848       {
2849         tree type_decl;
2850         tree qualifying_scope;
2851         tree object_scope;
2852         tree scope;
2853
2854         /* Consume the `~' token.  */
2855         cp_lexer_consume_token (parser->lexer);
2856         /* Parse the class-name.  The standard, as written, seems to
2857            say that:
2858
2859              template <typename T> struct S { ~S (); };
2860              template <typename T> S<T>::~S() {}
2861
2862            is invalid, since `~' must be followed by a class-name, but
2863            `S<T>' is dependent, and so not known to be a class.
2864            That's not right; we need to look in uninstantiated
2865            templates.  A further complication arises from:
2866
2867              template <typename T> void f(T t) {
2868                t.T::~T();
2869              } 
2870
2871            Here, it is not possible to look up `T' in the scope of `T'
2872            itself.  We must look in both the current scope, and the
2873            scope of the containing complete expression.  
2874
2875            Yet another issue is:
2876
2877              struct S {
2878                int S;
2879                ~S();
2880              };
2881
2882              S::~S() {}
2883
2884            The standard does not seem to say that the `S' in `~S'
2885            should refer to the type `S' and not the data member
2886            `S::S'.  */
2887
2888         /* DR 244 says that we look up the name after the "~" in the
2889            same scope as we looked up the qualifying name.  That idea
2890            isn't fully worked out; it's more complicated than that.  */
2891         scope = parser->scope;
2892         object_scope = parser->object_scope;
2893         qualifying_scope = parser->qualifying_scope;
2894
2895         /* If the name is of the form "X::~X" it's OK.  */
2896         if (scope && TYPE_P (scope)
2897             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2898             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
2899                 == CPP_OPEN_PAREN)
2900             && (cp_lexer_peek_token (parser->lexer)->value 
2901                 == TYPE_IDENTIFIER (scope)))
2902           {
2903             cp_lexer_consume_token (parser->lexer);
2904             return build_nt (BIT_NOT_EXPR, scope);
2905           }
2906
2907         /* If there was an explicit qualification (S::~T), first look
2908            in the scope given by the qualification (i.e., S).  */
2909         if (scope)
2910           {
2911             cp_parser_parse_tentatively (parser);
2912             type_decl = cp_parser_class_name (parser, 
2913                                               /*typename_keyword_p=*/false,
2914                                               /*template_keyword_p=*/false,
2915                                               /*type_p=*/false,
2916                                               /*check_dependency=*/false,
2917                                               /*class_head_p=*/false,
2918                                               declarator_p);
2919             if (cp_parser_parse_definitely (parser))
2920               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2921           }
2922         /* In "N::S::~S", look in "N" as well.  */
2923         if (scope && qualifying_scope)
2924           {
2925             cp_parser_parse_tentatively (parser);
2926             parser->scope = qualifying_scope;
2927             parser->object_scope = NULL_TREE;
2928             parser->qualifying_scope = NULL_TREE;
2929             type_decl 
2930               = cp_parser_class_name (parser, 
2931                                       /*typename_keyword_p=*/false,
2932                                       /*template_keyword_p=*/false,
2933                                       /*type_p=*/false,
2934                                       /*check_dependency=*/false,
2935                                       /*class_head_p=*/false,
2936                                       declarator_p);
2937             if (cp_parser_parse_definitely (parser))
2938               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2939           }
2940         /* In "p->S::~T", look in the scope given by "*p" as well.  */
2941         else if (object_scope)
2942           {
2943             cp_parser_parse_tentatively (parser);
2944             parser->scope = object_scope;
2945             parser->object_scope = NULL_TREE;
2946             parser->qualifying_scope = NULL_TREE;
2947             type_decl 
2948               = cp_parser_class_name (parser, 
2949                                       /*typename_keyword_p=*/false,
2950                                       /*template_keyword_p=*/false,
2951                                       /*type_p=*/false,
2952                                       /*check_dependency=*/false,
2953                                       /*class_head_p=*/false,
2954                                       declarator_p);
2955             if (cp_parser_parse_definitely (parser))
2956               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2957           }
2958         /* Look in the surrounding context.  */
2959         parser->scope = NULL_TREE;
2960         parser->object_scope = NULL_TREE;
2961         parser->qualifying_scope = NULL_TREE;
2962         type_decl 
2963           = cp_parser_class_name (parser, 
2964                                   /*typename_keyword_p=*/false,
2965                                   /*template_keyword_p=*/false,
2966                                   /*type_p=*/false,
2967                                   /*check_dependency=*/false,
2968                                   /*class_head_p=*/false,
2969                                   declarator_p);
2970         /* If an error occurred, assume that the name of the
2971            destructor is the same as the name of the qualifying
2972            class.  That allows us to keep parsing after running
2973            into ill-formed destructor names.  */
2974         if (type_decl == error_mark_node && scope && TYPE_P (scope))
2975           return build_nt (BIT_NOT_EXPR, scope);
2976         else if (type_decl == error_mark_node)
2977           return error_mark_node;
2978
2979         /* [class.dtor]
2980
2981            A typedef-name that names a class shall not be used as the
2982            identifier in the declarator for a destructor declaration.  */
2983         if (declarator_p 
2984             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2985             && !DECL_SELF_REFERENCE_P (type_decl))
2986           error ("typedef-name `%D' used as destructor declarator",
2987                  type_decl);
2988
2989         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2990       }
2991
2992     case CPP_KEYWORD:
2993       if (token->keyword == RID_OPERATOR)
2994         {
2995           tree id;
2996
2997           /* This could be a template-id, so we try that first.  */
2998           cp_parser_parse_tentatively (parser);
2999           /* Try a template-id.  */
3000           id = cp_parser_template_id (parser, template_keyword_p,
3001                                       /*check_dependency_p=*/true,
3002                                       declarator_p);
3003           /* If that worked, we're done.  */
3004           if (cp_parser_parse_definitely (parser))
3005             return id;
3006           /* We still don't know whether we're looking at an
3007              operator-function-id or a conversion-function-id.  */
3008           cp_parser_parse_tentatively (parser);
3009           /* Try an operator-function-id.  */
3010           id = cp_parser_operator_function_id (parser);
3011           /* If that didn't work, try a conversion-function-id.  */
3012           if (!cp_parser_parse_definitely (parser))
3013             id = cp_parser_conversion_function_id (parser);
3014
3015           return id;
3016         }
3017       /* Fall through.  */
3018
3019     default:
3020       cp_parser_error (parser, "expected unqualified-id");
3021       return error_mark_node;
3022     }
3023 }
3024
3025 /* Parse an (optional) nested-name-specifier.
3026
3027    nested-name-specifier:
3028      class-or-namespace-name :: nested-name-specifier [opt]
3029      class-or-namespace-name :: template nested-name-specifier [opt]
3030
3031    PARSER->SCOPE should be set appropriately before this function is
3032    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3033    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3034    in name lookups.
3035
3036    Sets PARSER->SCOPE to the class (TYPE) or namespace
3037    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3038    it unchanged if there is no nested-name-specifier.  Returns the new
3039    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.  
3040
3041    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3042    part of a declaration and/or decl-specifier.  */
3043
3044 static tree
3045 cp_parser_nested_name_specifier_opt (cp_parser *parser, 
3046                                      bool typename_keyword_p, 
3047                                      bool check_dependency_p,
3048                                      bool type_p,
3049                                      bool is_declaration)
3050 {
3051   bool success = false;
3052   tree access_check = NULL_TREE;
3053   ptrdiff_t start;
3054   cp_token* token;
3055
3056   /* If the next token corresponds to a nested name specifier, there
3057      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3058      false, it may have been true before, in which case something 
3059      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3060      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3061      CHECK_DEPENDENCY_P is false, we have to fall through into the
3062      main loop.  */
3063   if (check_dependency_p
3064       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3065     {
3066       cp_parser_pre_parsed_nested_name_specifier (parser);
3067       return parser->scope;
3068     }
3069
3070   /* Remember where the nested-name-specifier starts.  */
3071   if (cp_parser_parsing_tentatively (parser)
3072       && !cp_parser_committed_to_tentative_parse (parser))
3073     {
3074       token = cp_lexer_peek_token (parser->lexer);
3075       start = cp_lexer_token_difference (parser->lexer,
3076                                          parser->lexer->first_token,
3077                                          token);
3078     }
3079   else
3080     start = -1;
3081
3082   push_deferring_access_checks (dk_deferred);
3083
3084   while (true)
3085     {
3086       tree new_scope;
3087       tree old_scope;
3088       tree saved_qualifying_scope;
3089       bool template_keyword_p;
3090
3091       /* Spot cases that cannot be the beginning of a
3092          nested-name-specifier.  */
3093       token = cp_lexer_peek_token (parser->lexer);
3094
3095       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3096          the already parsed nested-name-specifier.  */
3097       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3098         {
3099           /* Grab the nested-name-specifier and continue the loop.  */
3100           cp_parser_pre_parsed_nested_name_specifier (parser);
3101           success = true;
3102           continue;
3103         }
3104
3105       /* Spot cases that cannot be the beginning of a
3106          nested-name-specifier.  On the second and subsequent times
3107          through the loop, we look for the `template' keyword.  */
3108       if (success && token->keyword == RID_TEMPLATE)
3109         ;
3110       /* A template-id can start a nested-name-specifier.  */
3111       else if (token->type == CPP_TEMPLATE_ID)
3112         ;
3113       else
3114         {
3115           /* If the next token is not an identifier, then it is
3116              definitely not a class-or-namespace-name.  */
3117           if (token->type != CPP_NAME)
3118             break;
3119           /* If the following token is neither a `<' (to begin a
3120              template-id), nor a `::', then we are not looking at a
3121              nested-name-specifier.  */
3122           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3123           if (token->type != CPP_SCOPE
3124               && !cp_parser_nth_token_starts_template_argument_list_p
3125                   (parser, 2))
3126             break;
3127         }
3128
3129       /* The nested-name-specifier is optional, so we parse
3130          tentatively.  */
3131       cp_parser_parse_tentatively (parser);
3132
3133       /* Look for the optional `template' keyword, if this isn't the
3134          first time through the loop.  */
3135       if (success)
3136         template_keyword_p = cp_parser_optional_template_keyword (parser);
3137       else
3138         template_keyword_p = false;
3139
3140       /* Save the old scope since the name lookup we are about to do
3141          might destroy it.  */
3142       old_scope = parser->scope;
3143       saved_qualifying_scope = parser->qualifying_scope;
3144       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3145          look up names in "X<T>::I" in order to determine that "Y" is
3146          a template.  So, if we have a typename at this point, we make
3147          an effort to look through it.  */
3148       if (is_declaration 
3149           && !typename_keyword_p
3150           && parser->scope 
3151           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3152         parser->scope = resolve_typename_type (parser->scope, 
3153                                                /*only_current_p=*/false);
3154       /* Parse the qualifying entity.  */
3155       new_scope 
3156         = cp_parser_class_or_namespace_name (parser,
3157                                              typename_keyword_p,
3158                                              template_keyword_p,
3159                                              check_dependency_p,
3160                                              type_p,
3161                                              is_declaration);
3162       /* Look for the `::' token.  */
3163       cp_parser_require (parser, CPP_SCOPE, "`::'");
3164
3165       /* If we found what we wanted, we keep going; otherwise, we're
3166          done.  */
3167       if (!cp_parser_parse_definitely (parser))
3168         {
3169           bool error_p = false;
3170
3171           /* Restore the OLD_SCOPE since it was valid before the
3172              failed attempt at finding the last
3173              class-or-namespace-name.  */
3174           parser->scope = old_scope;
3175           parser->qualifying_scope = saved_qualifying_scope;
3176           /* If the next token is an identifier, and the one after
3177              that is a `::', then any valid interpretation would have
3178              found a class-or-namespace-name.  */
3179           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3180                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
3181                      == CPP_SCOPE)
3182                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
3183                      != CPP_COMPL))
3184             {
3185               token = cp_lexer_consume_token (parser->lexer);
3186               if (!error_p) 
3187                 {
3188                   tree decl;
3189
3190                   decl = cp_parser_lookup_name_simple (parser, token->value);
3191                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3192                     error ("`%D' used without template parameters",
3193                            decl);
3194                   else
3195                     cp_parser_name_lookup_error 
3196                       (parser, token->value, decl, 
3197                        "is not a class or namespace");
3198                   parser->scope = NULL_TREE;
3199                   error_p = true;
3200                   /* Treat this as a successful nested-name-specifier
3201                      due to:
3202
3203                      [basic.lookup.qual]
3204
3205                      If the name found is not a class-name (clause
3206                      _class_) or namespace-name (_namespace.def_), the
3207                      program is ill-formed.  */
3208                   success = true;
3209                 }
3210               cp_lexer_consume_token (parser->lexer);
3211             }
3212           break;
3213         }
3214
3215       /* We've found one valid nested-name-specifier.  */
3216       success = true;
3217       /* Make sure we look in the right scope the next time through
3218          the loop.  */
3219       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL 
3220                        ? TREE_TYPE (new_scope)
3221                        : new_scope);
3222       /* If it is a class scope, try to complete it; we are about to
3223          be looking up names inside the class.  */
3224       if (TYPE_P (parser->scope)
3225           /* Since checking types for dependency can be expensive,
3226              avoid doing it if the type is already complete.  */
3227           && !COMPLETE_TYPE_P (parser->scope)
3228           /* Do not try to complete dependent types.  */
3229           && !dependent_type_p (parser->scope))
3230         complete_type (parser->scope);
3231     }
3232
3233   /* Retrieve any deferred checks.  Do not pop this access checks yet
3234      so the memory will not be reclaimed during token replacing below.  */
3235   access_check = get_deferred_access_checks ();
3236
3237   /* If parsing tentatively, replace the sequence of tokens that makes
3238      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3239      token.  That way, should we re-parse the token stream, we will
3240      not have to repeat the effort required to do the parse, nor will
3241      we issue duplicate error messages.  */
3242   if (success && start >= 0)
3243     {
3244       /* Find the token that corresponds to the start of the
3245          template-id.  */
3246       token = cp_lexer_advance_token (parser->lexer, 
3247                                       parser->lexer->first_token,
3248                                       start);
3249
3250       /* Reset the contents of the START token.  */
3251       token->type = CPP_NESTED_NAME_SPECIFIER;
3252       token->value = build_tree_list (access_check, parser->scope);
3253       TREE_TYPE (token->value) = parser->qualifying_scope;
3254       token->keyword = RID_MAX;
3255       /* Purge all subsequent tokens.  */
3256       cp_lexer_purge_tokens_after (parser->lexer, token);
3257     }
3258
3259   pop_deferring_access_checks ();
3260   return success ? parser->scope : NULL_TREE;
3261 }
3262
3263 /* Parse a nested-name-specifier.  See
3264    cp_parser_nested_name_specifier_opt for details.  This function
3265    behaves identically, except that it will an issue an error if no
3266    nested-name-specifier is present, and it will return
3267    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3268    is present.  */
3269
3270 static tree
3271 cp_parser_nested_name_specifier (cp_parser *parser, 
3272                                  bool typename_keyword_p, 
3273                                  bool check_dependency_p,
3274                                  bool type_p,
3275                                  bool is_declaration)
3276 {
3277   tree scope;
3278
3279   /* Look for the nested-name-specifier.  */
3280   scope = cp_parser_nested_name_specifier_opt (parser,
3281                                                typename_keyword_p,
3282                                                check_dependency_p,
3283                                                type_p,
3284                                                is_declaration);
3285   /* If it was not present, issue an error message.  */
3286   if (!scope)
3287     {
3288       cp_parser_error (parser, "expected nested-name-specifier");
3289       parser->scope = NULL_TREE;
3290       return error_mark_node;
3291     }
3292
3293   return scope;
3294 }
3295
3296 /* Parse a class-or-namespace-name.
3297
3298    class-or-namespace-name:
3299      class-name
3300      namespace-name
3301
3302    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3303    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3304    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3305    TYPE_P is TRUE iff the next name should be taken as a class-name,
3306    even the same name is declared to be another entity in the same
3307    scope.
3308
3309    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3310    specified by the class-or-namespace-name.  If neither is found the
3311    ERROR_MARK_NODE is returned.  */
3312
3313 static tree
3314 cp_parser_class_or_namespace_name (cp_parser *parser, 
3315                                    bool typename_keyword_p,
3316                                    bool template_keyword_p,
3317                                    bool check_dependency_p,
3318                                    bool type_p,
3319                                    bool is_declaration)
3320 {
3321   tree saved_scope;
3322   tree saved_qualifying_scope;
3323   tree saved_object_scope;
3324   tree scope;
3325   bool only_class_p;
3326
3327   /* Before we try to parse the class-name, we must save away the
3328      current PARSER->SCOPE since cp_parser_class_name will destroy
3329      it.  */
3330   saved_scope = parser->scope;
3331   saved_qualifying_scope = parser->qualifying_scope;
3332   saved_object_scope = parser->object_scope;
3333   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3334      there is no need to look for a namespace-name.  */
3335   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3336   if (!only_class_p)
3337     cp_parser_parse_tentatively (parser);
3338   scope = cp_parser_class_name (parser, 
3339                                 typename_keyword_p,
3340                                 template_keyword_p,
3341                                 type_p,
3342                                 check_dependency_p,
3343                                 /*class_head_p=*/false,
3344                                 is_declaration);
3345   /* If that didn't work, try for a namespace-name.  */
3346   if (!only_class_p && !cp_parser_parse_definitely (parser))
3347     {
3348       /* Restore the saved scope.  */
3349       parser->scope = saved_scope;
3350       parser->qualifying_scope = saved_qualifying_scope;
3351       parser->object_scope = saved_object_scope;
3352       /* If we are not looking at an identifier followed by the scope
3353          resolution operator, then this is not part of a
3354          nested-name-specifier.  (Note that this function is only used
3355          to parse the components of a nested-name-specifier.)  */
3356       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3357           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3358         return error_mark_node;
3359       scope = cp_parser_namespace_name (parser);
3360     }
3361
3362   return scope;
3363 }
3364
3365 /* Parse a postfix-expression.
3366
3367    postfix-expression:
3368      primary-expression
3369      postfix-expression [ expression ]
3370      postfix-expression ( expression-list [opt] )
3371      simple-type-specifier ( expression-list [opt] )
3372      typename :: [opt] nested-name-specifier identifier 
3373        ( expression-list [opt] )
3374      typename :: [opt] nested-name-specifier template [opt] template-id
3375        ( expression-list [opt] )
3376      postfix-expression . template [opt] id-expression
3377      postfix-expression -> template [opt] id-expression
3378      postfix-expression . pseudo-destructor-name
3379      postfix-expression -> pseudo-destructor-name
3380      postfix-expression ++
3381      postfix-expression --
3382      dynamic_cast < type-id > ( expression )
3383      static_cast < type-id > ( expression )
3384      reinterpret_cast < type-id > ( expression )
3385      const_cast < type-id > ( expression )
3386      typeid ( expression )
3387      typeid ( type-id )
3388
3389    GNU Extension:
3390      
3391    postfix-expression:
3392      ( type-id ) { initializer-list , [opt] }
3393
3394    This extension is a GNU version of the C99 compound-literal
3395    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3396    but they are essentially the same concept.)
3397
3398    If ADDRESS_P is true, the postfix expression is the operand of the
3399    `&' operator.
3400
3401    Returns a representation of the expression.  */
3402
3403 static tree
3404 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3405 {
3406   cp_token *token;
3407   enum rid keyword;
3408   cp_id_kind idk = CP_ID_KIND_NONE;
3409   tree postfix_expression = NULL_TREE;
3410   /* Non-NULL only if the current postfix-expression can be used to
3411      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3412      class used to qualify the member.  */
3413   tree qualifying_class = NULL_TREE;
3414
3415   /* Peek at the next token.  */
3416   token = cp_lexer_peek_token (parser->lexer);
3417   /* Some of the productions are determined by keywords.  */
3418   keyword = token->keyword;
3419   switch (keyword)
3420     {
3421     case RID_DYNCAST:
3422     case RID_STATCAST:
3423     case RID_REINTCAST:
3424     case RID_CONSTCAST:
3425       {
3426         tree type;
3427         tree expression;
3428         const char *saved_message;
3429
3430         /* All of these can be handled in the same way from the point
3431            of view of parsing.  Begin by consuming the token
3432            identifying the cast.  */
3433         cp_lexer_consume_token (parser->lexer);
3434         
3435         /* New types cannot be defined in the cast.  */
3436         saved_message = parser->type_definition_forbidden_message;
3437         parser->type_definition_forbidden_message
3438           = "types may not be defined in casts";
3439
3440         /* Look for the opening `<'.  */
3441         cp_parser_require (parser, CPP_LESS, "`<'");
3442         /* Parse the type to which we are casting.  */
3443         type = cp_parser_type_id (parser);
3444         /* Look for the closing `>'.  */
3445         cp_parser_require (parser, CPP_GREATER, "`>'");
3446         /* Restore the old message.  */
3447         parser->type_definition_forbidden_message = saved_message;
3448
3449         /* And the expression which is being cast.  */
3450         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3451         expression = cp_parser_expression (parser);
3452         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3453
3454         /* Only type conversions to integral or enumeration types
3455            can be used in constant-expressions.  */
3456         if (parser->integral_constant_expression_p
3457             && !dependent_type_p (type)
3458             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3459             /* A cast to pointer or reference type is allowed in the
3460                implementation of "offsetof".  */
3461             && !(parser->in_offsetof_p && POINTER_TYPE_P (type))
3462             && (cp_parser_non_integral_constant_expression 
3463                 (parser,
3464                  "a cast to a type other than an integral or "
3465                  "enumeration type")))
3466           return error_mark_node;
3467
3468         switch (keyword)
3469           {
3470           case RID_DYNCAST:
3471             postfix_expression
3472               = build_dynamic_cast (type, expression);
3473             break;
3474           case RID_STATCAST:
3475             postfix_expression
3476               = build_static_cast (type, expression);
3477             break;
3478           case RID_REINTCAST:
3479             postfix_expression
3480               = build_reinterpret_cast (type, expression);
3481             break;
3482           case RID_CONSTCAST:
3483             postfix_expression
3484               = build_const_cast (type, expression);
3485             break;
3486           default:
3487             abort ();
3488           }
3489       }
3490       break;
3491
3492     case RID_TYPEID:
3493       {
3494         tree type;
3495         const char *saved_message;
3496         bool saved_in_type_id_in_expr_p;
3497
3498         /* Consume the `typeid' token.  */
3499         cp_lexer_consume_token (parser->lexer);
3500         /* Look for the `(' token.  */
3501         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3502         /* Types cannot be defined in a `typeid' expression.  */
3503         saved_message = parser->type_definition_forbidden_message;
3504         parser->type_definition_forbidden_message
3505           = "types may not be defined in a `typeid\' expression";
3506         /* We can't be sure yet whether we're looking at a type-id or an
3507            expression.  */
3508         cp_parser_parse_tentatively (parser);
3509         /* Try a type-id first.  */
3510         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3511         parser->in_type_id_in_expr_p = true;
3512         type = cp_parser_type_id (parser);
3513         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3514         /* Look for the `)' token.  Otherwise, we can't be sure that
3515            we're not looking at an expression: consider `typeid (int
3516            (3))', for example.  */
3517         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3518         /* If all went well, simply lookup the type-id.  */
3519         if (cp_parser_parse_definitely (parser))
3520           postfix_expression = get_typeid (type);
3521         /* Otherwise, fall back to the expression variant.  */
3522         else
3523           {
3524             tree expression;
3525
3526             /* Look for an expression.  */
3527             expression = cp_parser_expression (parser);
3528             /* Compute its typeid.  */
3529             postfix_expression = build_typeid (expression);
3530             /* Look for the `)' token.  */
3531             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3532           }
3533         /* `typeid' may not appear in an integral constant expression.  */
3534         if (cp_parser_non_integral_constant_expression(parser, 
3535                                                        "`typeid' operator"))
3536           return error_mark_node;
3537         /* Restore the saved message.  */
3538         parser->type_definition_forbidden_message = saved_message;
3539       }
3540       break;
3541       
3542     case RID_TYPENAME:
3543       {
3544         bool template_p = false;
3545         tree id;
3546         tree type;
3547
3548         /* Consume the `typename' token.  */
3549         cp_lexer_consume_token (parser->lexer);
3550         /* Look for the optional `::' operator.  */
3551         cp_parser_global_scope_opt (parser, 
3552                                     /*current_scope_valid_p=*/false);
3553         /* Look for the nested-name-specifier.  */
3554         cp_parser_nested_name_specifier (parser,
3555                                          /*typename_keyword_p=*/true,
3556                                          /*check_dependency_p=*/true,
3557                                          /*type_p=*/true,
3558                                          /*is_declaration=*/true);
3559         /* Look for the optional `template' keyword.  */
3560         template_p = cp_parser_optional_template_keyword (parser);
3561         /* We don't know whether we're looking at a template-id or an
3562            identifier.  */
3563         cp_parser_parse_tentatively (parser);
3564         /* Try a template-id.  */
3565         id = cp_parser_template_id (parser, template_p,
3566                                     /*check_dependency_p=*/true,
3567                                     /*is_declaration=*/true);
3568         /* If that didn't work, try an identifier.  */
3569         if (!cp_parser_parse_definitely (parser))
3570           id = cp_parser_identifier (parser);
3571         /* If we look up a template-id in a non-dependent qualifying
3572            scope, there's no need to create a dependent type.  */
3573         if (TREE_CODE (id) == TYPE_DECL
3574             && !dependent_type_p (parser->scope))
3575           type = TREE_TYPE (id);
3576         /* Create a TYPENAME_TYPE to represent the type to which the
3577            functional cast is being performed.  */
3578         else
3579           type = make_typename_type (parser->scope, id, 
3580                                      /*complain=*/1);
3581
3582         postfix_expression = cp_parser_functional_cast (parser, type);
3583       }
3584       break;
3585
3586     default:
3587       {
3588         tree type;
3589
3590         /* If the next thing is a simple-type-specifier, we may be
3591            looking at a functional cast.  We could also be looking at
3592            an id-expression.  So, we try the functional cast, and if
3593            that doesn't work we fall back to the primary-expression.  */
3594         cp_parser_parse_tentatively (parser);
3595         /* Look for the simple-type-specifier.  */
3596         type = cp_parser_simple_type_specifier (parser, 
3597                                                 CP_PARSER_FLAGS_NONE,
3598                                                 /*identifier_p=*/false);
3599         /* Parse the cast itself.  */
3600         if (!cp_parser_error_occurred (parser))
3601           postfix_expression 
3602             = cp_parser_functional_cast (parser, type);
3603         /* If that worked, we're done.  */
3604         if (cp_parser_parse_definitely (parser))
3605           break;
3606
3607         /* If the functional-cast didn't work out, try a
3608            compound-literal.  */
3609         if (cp_parser_allow_gnu_extensions_p (parser)
3610             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3611           {
3612             tree initializer_list = NULL_TREE;
3613             bool saved_in_type_id_in_expr_p;
3614
3615             cp_parser_parse_tentatively (parser);
3616             /* Consume the `('.  */
3617             cp_lexer_consume_token (parser->lexer);
3618             /* Parse the type.  */
3619             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3620             parser->in_type_id_in_expr_p = true;
3621             type = cp_parser_type_id (parser);
3622             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3623             /* Look for the `)'.  */
3624             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3625             /* Look for the `{'.  */
3626             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3627             /* If things aren't going well, there's no need to
3628                keep going.  */
3629             if (!cp_parser_error_occurred (parser))
3630               {
3631                 bool non_constant_p;
3632                 /* Parse the initializer-list.  */
3633                 initializer_list 
3634                   = cp_parser_initializer_list (parser, &non_constant_p);
3635                 /* Allow a trailing `,'.  */
3636                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3637                   cp_lexer_consume_token (parser->lexer);
3638                 /* Look for the final `}'.  */
3639                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3640               }
3641             /* If that worked, we're definitely looking at a
3642                compound-literal expression.  */
3643             if (cp_parser_parse_definitely (parser))
3644               {
3645                 /* Warn the user that a compound literal is not
3646                    allowed in standard C++.  */
3647                 if (pedantic)
3648                   pedwarn ("ISO C++ forbids compound-literals");
3649                 /* Form the representation of the compound-literal.  */
3650                 postfix_expression 
3651                   = finish_compound_literal (type, initializer_list);
3652                 break;
3653               }
3654           }
3655
3656         /* It must be a primary-expression.  */
3657         postfix_expression = cp_parser_primary_expression (parser, 
3658                                                            &idk,
3659                                                            &qualifying_class);
3660       }
3661       break;
3662     }
3663
3664   /* If we were avoiding committing to the processing of a
3665      qualified-id until we knew whether or not we had a
3666      pointer-to-member, we now know.  */
3667   if (qualifying_class)
3668     {
3669       bool done;
3670
3671       /* Peek at the next token.  */
3672       token = cp_lexer_peek_token (parser->lexer);
3673       done = (token->type != CPP_OPEN_SQUARE
3674               && token->type != CPP_OPEN_PAREN
3675               && token->type != CPP_DOT
3676               && token->type != CPP_DEREF
3677               && token->type != CPP_PLUS_PLUS
3678               && token->type != CPP_MINUS_MINUS);
3679
3680       postfix_expression = finish_qualified_id_expr (qualifying_class,
3681                                                      postfix_expression,
3682                                                      done,
3683                                                      address_p);
3684       if (done)
3685         return postfix_expression;
3686     }
3687
3688   /* Keep looping until the postfix-expression is complete.  */
3689   while (true)
3690     {
3691       if (idk == CP_ID_KIND_UNQUALIFIED
3692           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3693           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3694         /* It is not a Koenig lookup function call.  */
3695         postfix_expression 
3696           = unqualified_name_lookup_error (postfix_expression);
3697       
3698       /* Peek at the next token.  */
3699       token = cp_lexer_peek_token (parser->lexer);
3700
3701       switch (token->type)
3702         {
3703         case CPP_OPEN_SQUARE:
3704           /* postfix-expression [ expression ] */
3705           {
3706             tree index;
3707
3708             /* Consume the `[' token.  */
3709             cp_lexer_consume_token (parser->lexer);
3710             /* Parse the index expression.  */
3711             index = cp_parser_expression (parser);
3712             /* Look for the closing `]'.  */
3713             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3714
3715             /* Build the ARRAY_REF.  */
3716             postfix_expression 
3717               = grok_array_decl (postfix_expression, index);
3718             idk = CP_ID_KIND_NONE;
3719             /* Array references are not permitted in
3720                constant-expressions (but they are allowed
3721                in offsetof).  */
3722             if (!parser->in_offsetof_p
3723                 && cp_parser_non_integral_constant_expression
3724                     (parser, "an array reference"))
3725               postfix_expression = error_mark_node;
3726           }
3727           break;
3728
3729         case CPP_OPEN_PAREN:
3730           /* postfix-expression ( expression-list [opt] ) */
3731           {
3732             bool koenig_p;
3733             tree args = (cp_parser_parenthesized_expression_list 
3734                          (parser, false, /*non_constant_p=*/NULL));
3735
3736             if (args == error_mark_node)
3737               {
3738                 postfix_expression = error_mark_node;
3739                 break;
3740               }
3741             
3742             /* Function calls are not permitted in
3743                constant-expressions.  */
3744             if (cp_parser_non_integral_constant_expression (parser,
3745                                                             "a function call"))
3746               {
3747                 postfix_expression = error_mark_node;
3748                 break;
3749               }
3750
3751             koenig_p = false;
3752             if (idk == CP_ID_KIND_UNQUALIFIED)
3753               {
3754                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3755                   {
3756                     if (args)
3757                       {
3758                         koenig_p = true;
3759                         postfix_expression
3760                           = perform_koenig_lookup (postfix_expression, args);
3761                       }
3762                     else
3763                       postfix_expression
3764                         = unqualified_fn_lookup_error (postfix_expression);
3765                   }
3766                 /* We do not perform argument-dependent lookup if
3767                    normal lookup finds a non-function, in accordance
3768                    with the expected resolution of DR 218.  */
3769                 else if (args && is_overloaded_fn (postfix_expression))
3770                   {
3771                     tree fn = get_first_fn (postfix_expression);
3772                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3773                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
3774                     /* Only do argument dependent lookup if regular
3775                        lookup does not find a set of member functions.
3776                        [basic.lookup.koenig]/2a  */
3777                     if (!DECL_FUNCTION_MEMBER_P (fn))
3778                       {
3779                         koenig_p = true;
3780                         postfix_expression
3781                           = perform_koenig_lookup (postfix_expression, args);
3782                       }
3783                   }
3784               }
3785           
3786             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3787               {
3788                 tree instance = TREE_OPERAND (postfix_expression, 0);
3789                 tree fn = TREE_OPERAND (postfix_expression, 1);
3790
3791                 if (processing_template_decl
3792                     && (type_dependent_expression_p (instance)
3793                         || (!BASELINK_P (fn)
3794                             && TREE_CODE (fn) != FIELD_DECL)
3795                         || type_dependent_expression_p (fn)
3796                         || any_type_dependent_arguments_p (args)))
3797                   {
3798                     postfix_expression
3799                       = build_min_nt (CALL_EXPR, postfix_expression, args);
3800                     break;
3801                   }
3802
3803                 if (BASELINK_P (fn))
3804                   postfix_expression
3805                     = (build_new_method_call 
3806                        (instance, fn, args, NULL_TREE, 
3807                         (idk == CP_ID_KIND_QUALIFIED 
3808                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3809                 else
3810                   postfix_expression
3811                     = finish_call_expr (postfix_expression, args,
3812                                         /*disallow_virtual=*/false,
3813                                         /*koenig_p=*/false);
3814               }
3815             else if (TREE_CODE (postfix_expression) == OFFSET_REF
3816                      || TREE_CODE (postfix_expression) == MEMBER_REF
3817                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3818               postfix_expression = (build_offset_ref_call_from_tree
3819                                     (postfix_expression, args));
3820             else if (idk == CP_ID_KIND_QUALIFIED)
3821               /* A call to a static class member, or a namespace-scope
3822                  function.  */
3823               postfix_expression
3824                 = finish_call_expr (postfix_expression, args,
3825                                     /*disallow_virtual=*/true,
3826                                     koenig_p);
3827             else
3828               /* All other function calls.  */
3829               postfix_expression 
3830                 = finish_call_expr (postfix_expression, args, 
3831                                     /*disallow_virtual=*/false,
3832                                     koenig_p);
3833
3834             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
3835             idk = CP_ID_KIND_NONE;
3836           }
3837           break;
3838           
3839         case CPP_DOT:
3840         case CPP_DEREF:
3841           /* postfix-expression . template [opt] id-expression  
3842              postfix-expression . pseudo-destructor-name 
3843              postfix-expression -> template [opt] id-expression
3844              postfix-expression -> pseudo-destructor-name */
3845           {
3846             tree name;
3847             bool dependent_p;
3848             bool template_p;
3849             bool pseudo_destructor_p;
3850             tree scope = NULL_TREE;
3851             enum cpp_ttype token_type = token->type;
3852
3853             /* If this is a `->' operator, dereference the pointer.  */
3854             if (token->type == CPP_DEREF)
3855               postfix_expression = build_x_arrow (postfix_expression);
3856             /* Check to see whether or not the expression is
3857                type-dependent.  */
3858             dependent_p = type_dependent_expression_p (postfix_expression);
3859             /* The identifier following the `->' or `.' is not
3860                qualified.  */
3861             parser->scope = NULL_TREE;
3862             parser->qualifying_scope = NULL_TREE;
3863             parser->object_scope = NULL_TREE;
3864             idk = CP_ID_KIND_NONE;
3865             /* Enter the scope corresponding to the type of the object
3866                given by the POSTFIX_EXPRESSION.  */
3867             if (!dependent_p 
3868                 && TREE_TYPE (postfix_expression) != NULL_TREE)
3869               {
3870                 scope = TREE_TYPE (postfix_expression);
3871                 /* According to the standard, no expression should
3872                    ever have reference type.  Unfortunately, we do not
3873                    currently match the standard in this respect in
3874                    that our internal representation of an expression
3875                    may have reference type even when the standard says
3876                    it does not.  Therefore, we have to manually obtain
3877                    the underlying type here.  */
3878                 scope = non_reference (scope);
3879                 /* The type of the POSTFIX_EXPRESSION must be
3880                    complete.  */
3881                 scope = complete_type_or_else (scope, NULL_TREE);
3882                 /* Let the name lookup machinery know that we are
3883                    processing a class member access expression.  */
3884                 parser->context->object_type = scope;
3885                 /* If something went wrong, we want to be able to
3886                    discern that case, as opposed to the case where
3887                    there was no SCOPE due to the type of expression
3888                    being dependent.  */
3889                 if (!scope)
3890                   scope = error_mark_node;
3891                 /* If the SCOPE was erroneous, make the various
3892                    semantic analysis functions exit quickly -- and
3893                    without issuing additional error messages.  */
3894                 if (scope == error_mark_node)
3895                   postfix_expression = error_mark_node;
3896               }
3897
3898             /* Consume the `.' or `->' operator.  */
3899             cp_lexer_consume_token (parser->lexer);
3900             
3901             /* Assume this expression is not a pseudo-destructor access.  */
3902             pseudo_destructor_p = false;
3903
3904             /* If the SCOPE is a scalar type, then, if this is a valid program,
3905                we must be looking at a pseudo-destructor-name.  */
3906             if (scope && SCALAR_TYPE_P (scope))
3907               {
3908                 tree s = NULL_TREE;
3909                 tree type;
3910
3911                 cp_parser_parse_tentatively (parser);
3912                 /* Parse the pseudo-destructor-name.  */
3913                 cp_parser_pseudo_destructor_name (parser, &s, &type);
3914                 if (cp_parser_parse_definitely (parser))
3915                   {
3916                     pseudo_destructor_p = true;
3917                     postfix_expression
3918                       = finish_pseudo_destructor_expr (postfix_expression,
3919                                                        s, TREE_TYPE (type));
3920                   }
3921               }
3922
3923             if (!pseudo_destructor_p)
3924               {
3925                 /* If the SCOPE is not a scalar type, we are looking
3926                    at an ordinary class member access expression,
3927                    rather than a pseudo-destructor-name.  */
3928                 template_p = cp_parser_optional_template_keyword (parser);
3929                 /* Parse the id-expression.  */
3930                 name = cp_parser_id_expression (parser,
3931                                                 template_p,
3932                                                 /*check_dependency_p=*/true,
3933                                                 /*template_p=*/NULL,
3934                                                 /*declarator_p=*/false);
3935                 /* In general, build a SCOPE_REF if the member name is
3936                    qualified.  However, if the name was not dependent
3937                    and has already been resolved; there is no need to
3938                    build the SCOPE_REF.  For example;
3939
3940                      struct X { void f(); };
3941                      template <typename T> void f(T* t) { t->X::f(); }
3942  
3943                    Even though "t" is dependent, "X::f" is not and has
3944                    been resolved to a BASELINK; there is no need to
3945                    include scope information.  */
3946
3947                 /* But we do need to remember that there was an explicit
3948                    scope for virtual function calls.  */
3949                 if (parser->scope)
3950                   idk = CP_ID_KIND_QUALIFIED;
3951
3952                 if (name != error_mark_node 
3953                     && !BASELINK_P (name)
3954                     && parser->scope)
3955                   {
3956                     name = build_nt (SCOPE_REF, parser->scope, name);
3957                     parser->scope = NULL_TREE;
3958                     parser->qualifying_scope = NULL_TREE;
3959                     parser->object_scope = NULL_TREE;
3960                   }
3961                 if (scope && name && BASELINK_P (name))
3962                   adjust_result_of_qualified_name_lookup 
3963                     (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
3964                 postfix_expression 
3965                   = finish_class_member_access_expr (postfix_expression, name);
3966               }
3967
3968             /* We no longer need to look up names in the scope of the
3969                object on the left-hand side of the `.' or `->'
3970                operator.  */
3971             parser->context->object_type = NULL_TREE;
3972             /* These operators may not appear in constant-expressions.  */
3973             if (/* The "->" operator is allowed in the implementation
3974                    of "offsetof".  The "." operator may appear in the
3975                    name of the member.  */
3976                 !parser->in_offsetof_p
3977                 && (cp_parser_non_integral_constant_expression 
3978                     (parser,
3979                      token_type == CPP_DEREF ? "'->'" : "`.'")))
3980               postfix_expression = error_mark_node;
3981           }
3982           break;
3983
3984         case CPP_PLUS_PLUS:
3985           /* postfix-expression ++  */
3986           /* Consume the `++' token.  */
3987           cp_lexer_consume_token (parser->lexer);
3988           /* Generate a representation for the complete expression.  */
3989           postfix_expression 
3990             = finish_increment_expr (postfix_expression, 
3991                                      POSTINCREMENT_EXPR);
3992           /* Increments may not appear in constant-expressions.  */
3993           if (cp_parser_non_integral_constant_expression (parser,
3994                                                           "an increment"))
3995             postfix_expression = error_mark_node;
3996           idk = CP_ID_KIND_NONE;
3997           break;
3998
3999         case CPP_MINUS_MINUS:
4000           /* postfix-expression -- */
4001           /* Consume the `--' token.  */
4002           cp_lexer_consume_token (parser->lexer);
4003           /* Generate a representation for the complete expression.  */
4004           postfix_expression 
4005             = finish_increment_expr (postfix_expression, 
4006                                      POSTDECREMENT_EXPR);
4007           /* Decrements may not appear in constant-expressions.  */
4008           if (cp_parser_non_integral_constant_expression (parser,
4009                                                           "a decrement"))
4010             postfix_expression = error_mark_node;
4011           idk = CP_ID_KIND_NONE;
4012           break;
4013
4014         default:
4015           return postfix_expression;
4016         }
4017     }
4018
4019   /* We should never get here.  */
4020   abort ();
4021   return error_mark_node;
4022 }
4023
4024 /* Parse a parenthesized expression-list.
4025
4026    expression-list:
4027      assignment-expression
4028      expression-list, assignment-expression
4029
4030    attribute-list:
4031      expression-list
4032      identifier
4033      identifier, expression-list
4034
4035    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4036    representation of an assignment-expression.  Note that a TREE_LIST
4037    is returned even if there is only a single expression in the list.
4038    error_mark_node is returned if the ( and or ) are
4039    missing. NULL_TREE is returned on no expressions. The parentheses
4040    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4041    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4042    indicates whether or not all of the expressions in the list were
4043    constant.  */
4044
4045 static tree
4046 cp_parser_parenthesized_expression_list (cp_parser* parser, 
4047                                          bool is_attribute_list,
4048                                          bool *non_constant_p)
4049 {
4050   tree expression_list = NULL_TREE;
4051   tree identifier = NULL_TREE;
4052
4053   /* Assume all the expressions will be constant.  */
4054   if (non_constant_p)
4055     *non_constant_p = false;
4056
4057   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4058     return error_mark_node;
4059   
4060   /* Consume expressions until there are no more.  */
4061   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4062     while (true)
4063       {
4064         tree expr;
4065         
4066         /* At the beginning of attribute lists, check to see if the
4067            next token is an identifier.  */
4068         if (is_attribute_list
4069             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4070           {
4071             cp_token *token;
4072             
4073             /* Consume the identifier.  */
4074             token = cp_lexer_consume_token (parser->lexer);
4075             /* Save the identifier.  */
4076             identifier = token->value;
4077           }
4078         else
4079           {
4080             /* Parse the next assignment-expression.  */
4081             if (non_constant_p)
4082               {
4083                 bool expr_non_constant_p;
4084                 expr = (cp_parser_constant_expression 
4085                         (parser, /*allow_non_constant_p=*/true,
4086                          &expr_non_constant_p));
4087                 if (expr_non_constant_p)
4088                   *non_constant_p = true;
4089               }
4090             else
4091               expr = cp_parser_assignment_expression (parser);
4092
4093              /* Add it to the list.  We add error_mark_node
4094                 expressions to the list, so that we can still tell if
4095                 the correct form for a parenthesized expression-list
4096                 is found. That gives better errors.  */
4097             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4098
4099             if (expr == error_mark_node)
4100               goto skip_comma;
4101           }
4102
4103         /* After the first item, attribute lists look the same as
4104            expression lists.  */
4105         is_attribute_list = false;
4106         
4107       get_comma:;
4108         /* If the next token isn't a `,', then we are done.  */
4109         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4110           break;
4111
4112         /* Otherwise, consume the `,' and keep going.  */
4113         cp_lexer_consume_token (parser->lexer);
4114       }
4115   
4116   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4117     {
4118       int ending;
4119       
4120     skip_comma:;
4121       /* We try and resync to an unnested comma, as that will give the
4122          user better diagnostics.  */
4123       ending = cp_parser_skip_to_closing_parenthesis (parser, 
4124                                                       /*recovering=*/true, 
4125                                                       /*or_comma=*/true,
4126                                                       /*consume_paren=*/true);
4127       if (ending < 0)
4128         goto get_comma;
4129       if (!ending)
4130         return error_mark_node;
4131     }
4132
4133   /* We built up the list in reverse order so we must reverse it now.  */
4134   expression_list = nreverse (expression_list);
4135   if (identifier)
4136     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4137   
4138   return expression_list;
4139 }
4140
4141 /* Parse a pseudo-destructor-name.
4142
4143    pseudo-destructor-name:
4144      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4145      :: [opt] nested-name-specifier template template-id :: ~ type-name
4146      :: [opt] nested-name-specifier [opt] ~ type-name
4147
4148    If either of the first two productions is used, sets *SCOPE to the
4149    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4150    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4151    or ERROR_MARK_NODE if the parse fails.  */
4152
4153 static void
4154 cp_parser_pseudo_destructor_name (cp_parser* parser, 
4155                                   tree* scope, 
4156                                   tree* type)
4157 {
4158   bool nested_name_specifier_p;
4159
4160   /* Look for the optional `::' operator.  */
4161   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4162   /* Look for the optional nested-name-specifier.  */
4163   nested_name_specifier_p 
4164     = (cp_parser_nested_name_specifier_opt (parser,
4165                                             /*typename_keyword_p=*/false,
4166                                             /*check_dependency_p=*/true,
4167                                             /*type_p=*/false,
4168                                             /*is_declaration=*/true) 
4169        != NULL_TREE);
4170   /* Now, if we saw a nested-name-specifier, we might be doing the
4171      second production.  */
4172   if (nested_name_specifier_p 
4173       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4174     {
4175       /* Consume the `template' keyword.  */
4176       cp_lexer_consume_token (parser->lexer);
4177       /* Parse the template-id.  */
4178       cp_parser_template_id (parser, 
4179                              /*template_keyword_p=*/true,
4180                              /*check_dependency_p=*/false,
4181                              /*is_declaration=*/true);
4182       /* Look for the `::' token.  */
4183       cp_parser_require (parser, CPP_SCOPE, "`::'");
4184     }
4185   /* If the next token is not a `~', then there might be some
4186      additional qualification.  */
4187   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4188     {
4189       /* Look for the type-name.  */
4190       *scope = TREE_TYPE (cp_parser_type_name (parser));
4191
4192       /* If we didn't get an aggregate type, or we don't have ::~,
4193          then something has gone wrong.  Since the only caller of this
4194          function is looking for something after `.' or `->' after a
4195          scalar type, most likely the program is trying to get a
4196          member of a non-aggregate type.  */
4197       if (*scope == error_mark_node
4198           || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4199           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4200         {
4201           cp_parser_error (parser, "request for member of non-aggregate type");
4202           *type = error_mark_node;
4203           return;
4204         }
4205
4206       /* Look for the `::' token.  */
4207       cp_parser_require (parser, CPP_SCOPE, "`::'");
4208     }
4209   else
4210     *scope = NULL_TREE;
4211
4212   /* Look for the `~'.  */
4213   cp_parser_require (parser, CPP_COMPL, "`~'");
4214   /* Look for the type-name again.  We are not responsible for
4215      checking that it matches the first type-name.  */
4216   *type = cp_parser_type_name (parser);
4217 }
4218
4219 /* Parse a unary-expression.
4220
4221    unary-expression:
4222      postfix-expression
4223      ++ cast-expression
4224      -- cast-expression
4225      unary-operator cast-expression
4226      sizeof unary-expression
4227      sizeof ( type-id )
4228      new-expression
4229      delete-expression
4230
4231    GNU Extensions:
4232
4233    unary-expression:
4234      __extension__ cast-expression
4235      __alignof__ unary-expression
4236      __alignof__ ( type-id )
4237      __real__ cast-expression
4238      __imag__ cast-expression
4239      && identifier
4240
4241    ADDRESS_P is true iff the unary-expression is appearing as the
4242    operand of the `&' operator.
4243
4244    Returns a representation of the expression.  */
4245
4246 static tree
4247 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4248 {
4249   cp_token *token;
4250   enum tree_code unary_operator;
4251
4252   /* Peek at the next token.  */
4253   token = cp_lexer_peek_token (parser->lexer);
4254   /* Some keywords give away the kind of expression.  */
4255   if (token->type == CPP_KEYWORD)
4256     {
4257       enum rid keyword = token->keyword;
4258
4259       switch (keyword)
4260         {
4261         case RID_ALIGNOF:
4262         case RID_SIZEOF:
4263           {
4264             tree operand;
4265             enum tree_code op;
4266             
4267             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4268             /* Consume the token.  */
4269             cp_lexer_consume_token (parser->lexer);
4270             /* Parse the operand.  */
4271             operand = cp_parser_sizeof_operand (parser, keyword);
4272
4273             if (TYPE_P (operand))
4274               return cxx_sizeof_or_alignof_type (operand, op, true);
4275             else
4276               return cxx_sizeof_or_alignof_expr (operand, op);
4277           }
4278
4279         case RID_NEW:
4280           return cp_parser_new_expression (parser);
4281
4282         case RID_DELETE:
4283           return cp_parser_delete_expression (parser);
4284           
4285         case RID_EXTENSION:
4286           {
4287             /* The saved value of the PEDANTIC flag.  */
4288             int saved_pedantic;
4289             tree expr;
4290
4291             /* Save away the PEDANTIC flag.  */
4292             cp_parser_extension_opt (parser, &saved_pedantic);
4293             /* Parse the cast-expression.  */
4294             expr = cp_parser_simple_cast_expression (parser);
4295             /* Restore the PEDANTIC flag.  */
4296             pedantic = saved_pedantic;
4297
4298             return expr;
4299           }
4300
4301         case RID_REALPART:
4302         case RID_IMAGPART:
4303           {
4304             tree expression;
4305
4306             /* Consume the `__real__' or `__imag__' token.  */
4307             cp_lexer_consume_token (parser->lexer);
4308             /* Parse the cast-expression.  */
4309             expression = cp_parser_simple_cast_expression (parser);
4310             /* Create the complete representation.  */
4311             return build_x_unary_op ((keyword == RID_REALPART
4312                                       ? REALPART_EXPR : IMAGPART_EXPR),
4313                                      expression);
4314           }
4315           break;
4316
4317         default:
4318           break;
4319         }
4320     }
4321
4322   /* Look for the `:: new' and `:: delete', which also signal the
4323      beginning of a new-expression, or delete-expression,
4324      respectively.  If the next token is `::', then it might be one of
4325      these.  */
4326   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4327     {
4328       enum rid keyword;
4329
4330       /* See if the token after the `::' is one of the keywords in
4331          which we're interested.  */
4332       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4333       /* If it's `new', we have a new-expression.  */
4334       if (keyword == RID_NEW)
4335         return cp_parser_new_expression (parser);
4336       /* Similarly, for `delete'.  */
4337       else if (keyword == RID_DELETE)
4338         return cp_parser_delete_expression (parser);
4339     }
4340
4341   /* Look for a unary operator.  */
4342   unary_operator = cp_parser_unary_operator (token);
4343   /* The `++' and `--' operators can be handled similarly, even though
4344      they are not technically unary-operators in the grammar.  */
4345   if (unary_operator == ERROR_MARK)
4346     {
4347       if (token->type == CPP_PLUS_PLUS)
4348         unary_operator = PREINCREMENT_EXPR;
4349       else if (token->type == CPP_MINUS_MINUS)
4350         unary_operator = PREDECREMENT_EXPR;
4351       /* Handle the GNU address-of-label extension.  */
4352       else if (cp_parser_allow_gnu_extensions_p (parser)
4353                && token->type == CPP_AND_AND)
4354         {
4355           tree identifier;
4356
4357           /* Consume the '&&' token.  */
4358           cp_lexer_consume_token (parser->lexer);
4359           /* Look for the identifier.  */
4360           identifier = cp_parser_identifier (parser);
4361           /* Create an expression representing the address.  */
4362           return finish_label_address_expr (identifier);
4363         }
4364     }
4365   if (unary_operator != ERROR_MARK)
4366     {
4367       tree cast_expression;
4368       tree expression = error_mark_node;
4369       const char *non_constant_p = NULL;
4370
4371       /* Consume the operator token.  */
4372       token = cp_lexer_consume_token (parser->lexer);
4373       /* Parse the cast-expression.  */
4374       cast_expression 
4375         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4376       /* Now, build an appropriate representation.  */
4377       switch (unary_operator)
4378         {
4379         case INDIRECT_REF:
4380           non_constant_p = "`*'";
4381           expression = build_x_indirect_ref (cast_expression, "unary *");
4382           break;
4383
4384         case ADDR_EXPR:
4385           /* The "&" operator is allowed in the implementation of
4386              "offsetof".  */
4387           if (!parser->in_offsetof_p)
4388             non_constant_p = "`&'";
4389           /* Fall through.  */
4390         case BIT_NOT_EXPR:
4391           expression = build_x_unary_op (unary_operator, cast_expression);
4392           break;
4393
4394         case PREINCREMENT_EXPR:
4395         case PREDECREMENT_EXPR:
4396           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4397                             ? "`++'" : "`--'");
4398           /* Fall through.  */
4399         case CONVERT_EXPR:
4400         case NEGATE_EXPR:
4401         case TRUTH_NOT_EXPR:
4402           expression = finish_unary_op_expr (unary_operator, cast_expression);
4403           break;
4404
4405         default:
4406           abort ();
4407         }
4408
4409       if (non_constant_p 
4410           && cp_parser_non_integral_constant_expression (parser,
4411                                                          non_constant_p))
4412         expression = error_mark_node;
4413
4414       return expression;
4415     }
4416
4417   return cp_parser_postfix_expression (parser, address_p);
4418 }
4419
4420 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4421    unary-operator, the corresponding tree code is returned.  */
4422
4423 static enum tree_code
4424 cp_parser_unary_operator (cp_token* token)
4425 {
4426   switch (token->type)
4427     {
4428     case CPP_MULT:
4429       return INDIRECT_REF;
4430
4431     case CPP_AND:
4432       return ADDR_EXPR;
4433
4434     case CPP_PLUS:
4435       return CONVERT_EXPR;
4436
4437     case CPP_MINUS:
4438       return NEGATE_EXPR;
4439
4440     case CPP_NOT:
4441       return TRUTH_NOT_EXPR;
4442       
4443     case CPP_COMPL:
4444       return BIT_NOT_EXPR;
4445
4446     default:
4447       return ERROR_MARK;
4448     }
4449 }
4450
4451 /* Parse a new-expression.
4452
4453    new-expression:
4454      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4455      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4456
4457    Returns a representation of the expression.  */
4458
4459 static tree
4460 cp_parser_new_expression (cp_parser* parser)
4461 {
4462   bool global_scope_p;
4463   tree placement;
4464   tree type;
4465   tree initializer;
4466
4467   /* Look for the optional `::' operator.  */
4468   global_scope_p 
4469     = (cp_parser_global_scope_opt (parser,
4470                                    /*current_scope_valid_p=*/false)
4471        != NULL_TREE);
4472   /* Look for the `new' operator.  */
4473   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4474   /* There's no easy way to tell a new-placement from the
4475      `( type-id )' construct.  */
4476   cp_parser_parse_tentatively (parser);
4477   /* Look for a new-placement.  */
4478   placement = cp_parser_new_placement (parser);
4479   /* If that didn't work out, there's no new-placement.  */
4480   if (!cp_parser_parse_definitely (parser))
4481     placement = NULL_TREE;
4482
4483   /* If the next token is a `(', then we have a parenthesized
4484      type-id.  */
4485   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4486     {
4487       /* Consume the `('.  */
4488       cp_lexer_consume_token (parser->lexer);
4489       /* Parse the type-id.  */
4490       type = cp_parser_type_id (parser);
4491       /* Look for the closing `)'.  */
4492       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4493       /* There should not be a direct-new-declarator in this production, 
4494          but GCC used to allowed this, so we check and emit a sensible error
4495          message for this case.  */
4496       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4497         {
4498           error ("array bound forbidden after parenthesized type-id");
4499           inform ("try removing the parentheses around the type-id");
4500           cp_parser_direct_new_declarator (parser);
4501         }
4502     }
4503   /* Otherwise, there must be a new-type-id.  */
4504   else
4505     type = cp_parser_new_type_id (parser);
4506
4507   /* If the next token is a `(', then we have a new-initializer.  */
4508   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4509     initializer = cp_parser_new_initializer (parser);
4510   else
4511     initializer = NULL_TREE;
4512
4513   /* A new-expression may not appear in an integral constant
4514      expression.  */
4515   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4516     return error_mark_node;
4517
4518   /* Create a representation of the new-expression.  */
4519   return build_new (placement, type, initializer, global_scope_p);
4520 }
4521
4522 /* Parse a new-placement.
4523
4524    new-placement:
4525      ( expression-list )
4526
4527    Returns the same representation as for an expression-list.  */
4528
4529 static tree
4530 cp_parser_new_placement (cp_parser* parser)
4531 {
4532   tree expression_list;
4533
4534   /* Parse the expression-list.  */
4535   expression_list = (cp_parser_parenthesized_expression_list 
4536                      (parser, false, /*non_constant_p=*/NULL));
4537
4538   return expression_list;
4539 }
4540
4541 /* Parse a new-type-id.
4542
4543    new-type-id:
4544      type-specifier-seq new-declarator [opt]
4545
4546    Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4547    and whose TREE_VALUE is the new-declarator.  */
4548
4549 static tree
4550 cp_parser_new_type_id (cp_parser* parser)
4551 {
4552   tree type_specifier_seq;
4553   tree declarator;
4554   const char *saved_message;
4555
4556   /* The type-specifier sequence must not contain type definitions.
4557      (It cannot contain declarations of new types either, but if they
4558      are not definitions we will catch that because they are not
4559      complete.)  */
4560   saved_message = parser->type_definition_forbidden_message;
4561   parser->type_definition_forbidden_message
4562     = "types may not be defined in a new-type-id";
4563   /* Parse the type-specifier-seq.  */
4564   type_specifier_seq = cp_parser_type_specifier_seq (parser);
4565   /* Restore the old message.  */
4566   parser->type_definition_forbidden_message = saved_message;
4567   /* Parse the new-declarator.  */
4568   declarator = cp_parser_new_declarator_opt (parser);
4569
4570   return build_tree_list (type_specifier_seq, declarator);
4571 }
4572
4573 /* Parse an (optional) new-declarator.
4574
4575    new-declarator:
4576      ptr-operator new-declarator [opt]
4577      direct-new-declarator
4578
4579    Returns a representation of the declarator.  See
4580    cp_parser_declarator for the representations used.  */
4581
4582 static tree
4583 cp_parser_new_declarator_opt (cp_parser* parser)
4584 {
4585   enum tree_code code;
4586   tree type;
4587   tree cv_qualifier_seq;
4588
4589   /* We don't know if there's a ptr-operator next, or not.  */
4590   cp_parser_parse_tentatively (parser);
4591   /* Look for a ptr-operator.  */
4592   code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4593   /* If that worked, look for more new-declarators.  */
4594   if (cp_parser_parse_definitely (parser))
4595     {
4596       tree declarator;
4597
4598       /* Parse another optional declarator.  */
4599       declarator = cp_parser_new_declarator_opt (parser);
4600
4601       /* Create the representation of the declarator.  */
4602       if (code == INDIRECT_REF)
4603         declarator = make_pointer_declarator (cv_qualifier_seq,
4604                                               declarator);
4605       else
4606         declarator = make_reference_declarator (cv_qualifier_seq,
4607                                                 declarator);
4608
4609      /* Handle the pointer-to-member case.  */
4610      if (type)
4611        declarator = build_nt (SCOPE_REF, type, declarator);
4612
4613       return declarator;
4614     }
4615
4616   /* If the next token is a `[', there is a direct-new-declarator.  */
4617   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4618     return cp_parser_direct_new_declarator (parser);
4619
4620   return NULL_TREE;
4621 }
4622
4623 /* Parse a direct-new-declarator.
4624
4625    direct-new-declarator:
4626      [ expression ]
4627      direct-new-declarator [constant-expression]  
4628
4629    Returns an ARRAY_REF, following the same conventions as are
4630    documented for cp_parser_direct_declarator.  */
4631
4632 static tree
4633 cp_parser_direct_new_declarator (cp_parser* parser)
4634 {
4635   tree declarator = NULL_TREE;
4636
4637   while (true)
4638     {
4639       tree expression;
4640
4641       /* Look for the opening `['.  */
4642       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4643       /* The first expression is not required to be constant.  */
4644       if (!declarator)
4645         {
4646           expression = cp_parser_expression (parser);
4647           /* The standard requires that the expression have integral
4648              type.  DR 74 adds enumeration types.  We believe that the
4649              real intent is that these expressions be handled like the
4650              expression in a `switch' condition, which also allows
4651              classes with a single conversion to integral or
4652              enumeration type.  */
4653           if (!processing_template_decl)
4654             {
4655               expression 
4656                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4657                                               expression,
4658                                               /*complain=*/true);
4659               if (!expression)
4660                 {
4661                   error ("expression in new-declarator must have integral or enumeration type");
4662                   expression = error_mark_node;
4663                 }
4664             }
4665         }
4666       /* But all the other expressions must be.  */
4667       else
4668         expression 
4669           = cp_parser_constant_expression (parser, 
4670                                            /*allow_non_constant=*/false,
4671                                            NULL);
4672       /* Look for the closing `]'.  */
4673       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4674
4675       /* Add this bound to the declarator.  */
4676       declarator = build_nt (ARRAY_REF, declarator, expression);
4677
4678       /* If the next token is not a `[', then there are no more
4679          bounds.  */
4680       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4681         break;
4682     }
4683
4684   return declarator;
4685 }
4686
4687 /* Parse a new-initializer.
4688
4689    new-initializer:
4690      ( expression-list [opt] )
4691
4692    Returns a representation of the expression-list.  If there is no
4693    expression-list, VOID_ZERO_NODE is returned.  */
4694
4695 static tree
4696 cp_parser_new_initializer (cp_parser* parser)
4697 {
4698   tree expression_list;
4699
4700   expression_list = (cp_parser_parenthesized_expression_list 
4701                      (parser, false, /*non_constant_p=*/NULL));
4702   if (!expression_list)
4703     expression_list = void_zero_node;
4704
4705   return expression_list;
4706 }
4707
4708 /* Parse a delete-expression.
4709
4710    delete-expression:
4711      :: [opt] delete cast-expression
4712      :: [opt] delete [ ] cast-expression
4713
4714    Returns a representation of the expression.  */
4715
4716 static tree
4717 cp_parser_delete_expression (cp_parser* parser)
4718 {
4719   bool global_scope_p;
4720   bool array_p;
4721   tree expression;
4722
4723   /* Look for the optional `::' operator.  */
4724   global_scope_p
4725     = (cp_parser_global_scope_opt (parser,
4726                                    /*current_scope_valid_p=*/false)
4727        != NULL_TREE);
4728   /* Look for the `delete' keyword.  */
4729   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4730   /* See if the array syntax is in use.  */
4731   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4732     {
4733       /* Consume the `[' token.  */
4734       cp_lexer_consume_token (parser->lexer);
4735       /* Look for the `]' token.  */
4736       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4737       /* Remember that this is the `[]' construct.  */
4738       array_p = true;
4739     }
4740   else
4741     array_p = false;
4742
4743   /* Parse the cast-expression.  */
4744   expression = cp_parser_simple_cast_expression (parser);
4745
4746   /* A delete-expression may not appear in an integral constant
4747      expression.  */
4748   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4749     return error_mark_node;
4750
4751   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4752 }
4753
4754 /* Parse a cast-expression.
4755
4756    cast-expression:
4757      unary-expression
4758      ( type-id ) cast-expression
4759
4760    Returns a representation of the expression.  */
4761
4762 static tree
4763 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4764 {
4765   /* If it's a `(', then we might be looking at a cast.  */
4766   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4767     {
4768       tree type = NULL_TREE;
4769       tree expr = NULL_TREE;
4770       bool compound_literal_p;
4771       const char *saved_message;
4772
4773       /* There's no way to know yet whether or not this is a cast.
4774          For example, `(int (3))' is a unary-expression, while `(int)
4775          3' is a cast.  So, we resort to parsing tentatively.  */
4776       cp_parser_parse_tentatively (parser);
4777       /* Types may not be defined in a cast.  */
4778       saved_message = parser->type_definition_forbidden_message;
4779       parser->type_definition_forbidden_message
4780         = "types may not be defined in casts";
4781       /* Consume the `('.  */
4782       cp_lexer_consume_token (parser->lexer);
4783       /* A very tricky bit is that `(struct S) { 3 }' is a
4784          compound-literal (which we permit in C++ as an extension).
4785          But, that construct is not a cast-expression -- it is a
4786          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
4787          is legal; if the compound-literal were a cast-expression,
4788          you'd need an extra set of parentheses.)  But, if we parse
4789          the type-id, and it happens to be a class-specifier, then we
4790          will commit to the parse at that point, because we cannot
4791          undo the action that is done when creating a new class.  So,
4792          then we cannot back up and do a postfix-expression.  
4793
4794          Therefore, we scan ahead to the closing `)', and check to see
4795          if the token after the `)' is a `{'.  If so, we are not
4796          looking at a cast-expression.  
4797
4798          Save tokens so that we can put them back.  */
4799       cp_lexer_save_tokens (parser->lexer);
4800       /* Skip tokens until the next token is a closing parenthesis.
4801          If we find the closing `)', and the next token is a `{', then
4802          we are looking at a compound-literal.  */
4803       compound_literal_p 
4804         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4805                                                   /*consume_paren=*/true)
4806            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4807       /* Roll back the tokens we skipped.  */
4808       cp_lexer_rollback_tokens (parser->lexer);
4809       /* If we were looking at a compound-literal, simulate an error
4810          so that the call to cp_parser_parse_definitely below will
4811          fail.  */
4812       if (compound_literal_p)
4813         cp_parser_simulate_error (parser);
4814       else
4815         {
4816           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4817           parser->in_type_id_in_expr_p = true;
4818           /* Look for the type-id.  */
4819           type = cp_parser_type_id (parser);
4820           /* Look for the closing `)'.  */
4821           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4822           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4823         }
4824
4825       /* Restore the saved message.  */
4826       parser->type_definition_forbidden_message = saved_message;
4827
4828       /* If ok so far, parse the dependent expression. We cannot be
4829          sure it is a cast. Consider `(T ())'.  It is a parenthesized
4830          ctor of T, but looks like a cast to function returning T
4831          without a dependent expression.  */
4832       if (!cp_parser_error_occurred (parser))
4833         expr = cp_parser_simple_cast_expression (parser);
4834
4835       if (cp_parser_parse_definitely (parser))
4836         {
4837           /* Warn about old-style casts, if so requested.  */
4838           if (warn_old_style_cast 
4839               && !in_system_header 
4840               && !VOID_TYPE_P (type) 
4841               && current_lang_name != lang_name_c)
4842             warning ("use of old-style cast");
4843
4844           /* Only type conversions to integral or enumeration types
4845              can be used in constant-expressions.  */
4846           if (parser->integral_constant_expression_p
4847               && !dependent_type_p (type)
4848               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4849               && (cp_parser_non_integral_constant_expression 
4850                   (parser,
4851                    "a casts to a type other than an integral or "
4852                    "enumeration type")))
4853             return error_mark_node;
4854
4855           /* Perform the cast.  */
4856           expr = build_c_cast (type, expr);
4857           return expr;
4858         }
4859     }
4860
4861   /* If we get here, then it's not a cast, so it must be a
4862      unary-expression.  */
4863   return cp_parser_unary_expression (parser, address_p);
4864 }
4865
4866 /* Parse a pm-expression.
4867
4868    pm-expression:
4869      cast-expression
4870      pm-expression .* cast-expression
4871      pm-expression ->* cast-expression
4872
4873      Returns a representation of the expression.  */
4874
4875 static tree
4876 cp_parser_pm_expression (cp_parser* parser)
4877 {
4878   static const cp_parser_token_tree_map map = {
4879     { CPP_DEREF_STAR, MEMBER_REF },
4880     { CPP_DOT_STAR, DOTSTAR_EXPR },
4881     { CPP_EOF, ERROR_MARK }
4882   };
4883
4884   return cp_parser_binary_expression (parser, map, 
4885                                       cp_parser_simple_cast_expression);
4886 }
4887
4888 /* Parse a multiplicative-expression.
4889
4890    mulitplicative-expression:
4891      pm-expression
4892      multiplicative-expression * pm-expression
4893      multiplicative-expression / pm-expression
4894      multiplicative-expression % pm-expression
4895
4896    Returns a representation of the expression.  */
4897
4898 static tree
4899 cp_parser_multiplicative_expression (cp_parser* parser)
4900 {
4901   static const cp_parser_token_tree_map map = {
4902     { CPP_MULT, MULT_EXPR },
4903     { CPP_DIV, TRUNC_DIV_EXPR },
4904     { CPP_MOD, TRUNC_MOD_EXPR },
4905     { CPP_EOF, ERROR_MARK }
4906   };
4907
4908   return cp_parser_binary_expression (parser,
4909                                       map,
4910                                       cp_parser_pm_expression);
4911 }
4912
4913 /* Parse an additive-expression.
4914
4915    additive-expression:
4916      multiplicative-expression
4917      additive-expression + multiplicative-expression
4918      additive-expression - multiplicative-expression
4919
4920    Returns a representation of the expression.  */
4921
4922 static tree
4923 cp_parser_additive_expression (cp_parser* parser)
4924 {
4925   static const cp_parser_token_tree_map map = {
4926     { CPP_PLUS, PLUS_EXPR },
4927     { CPP_MINUS, MINUS_EXPR },
4928     { CPP_EOF, ERROR_MARK }
4929   };
4930
4931   return cp_parser_binary_expression (parser,
4932                                       map,
4933                                       cp_parser_multiplicative_expression);
4934 }
4935
4936 /* Parse a shift-expression.
4937
4938    shift-expression:
4939      additive-expression
4940      shift-expression << additive-expression
4941      shift-expression >> additive-expression
4942
4943    Returns a representation of the expression.  */
4944
4945 static tree
4946 cp_parser_shift_expression (cp_parser* parser)
4947 {
4948   static const cp_parser_token_tree_map map = {
4949     { CPP_LSHIFT, LSHIFT_EXPR },
4950     { CPP_RSHIFT, RSHIFT_EXPR },
4951     { CPP_EOF, ERROR_MARK }
4952   };
4953
4954   return cp_parser_binary_expression (parser,
4955                                       map,
4956                                       cp_parser_additive_expression);
4957 }
4958
4959 /* Parse a relational-expression.
4960
4961    relational-expression:
4962      shift-expression
4963      relational-expression < shift-expression
4964      relational-expression > shift-expression
4965      relational-expression <= shift-expression
4966      relational-expression >= shift-expression
4967
4968    GNU Extension:
4969
4970    relational-expression:
4971      relational-expression <? shift-expression
4972      relational-expression >? shift-expression
4973
4974    Returns a representation of the expression.  */
4975
4976 static tree
4977 cp_parser_relational_expression (cp_parser* parser)
4978 {
4979   static const cp_parser_token_tree_map map = {
4980     { CPP_LESS, LT_EXPR },
4981     { CPP_GREATER, GT_EXPR },
4982     { CPP_LESS_EQ, LE_EXPR },
4983     { CPP_GREATER_EQ, GE_EXPR },
4984     { CPP_MIN, MIN_EXPR },
4985     { CPP_MAX, MAX_EXPR },
4986     { CPP_EOF, ERROR_MARK }
4987   };
4988
4989   return cp_parser_binary_expression (parser,
4990                                       map,
4991                                       cp_parser_shift_expression);
4992 }
4993
4994 /* Parse an equality-expression.
4995
4996    equality-expression:
4997      relational-expression
4998      equality-expression == relational-expression
4999      equality-expression != relational-expression
5000
5001    Returns a representation of the expression.  */
5002
5003 static tree
5004 cp_parser_equality_expression (cp_parser* parser)
5005 {
5006   static const cp_parser_token_tree_map map = {
5007     { CPP_EQ_EQ, EQ_EXPR },
5008     { CPP_NOT_EQ, NE_EXPR },
5009     { CPP_EOF, ERROR_MARK }
5010   };
5011
5012   return cp_parser_binary_expression (parser,
5013                                       map,
5014                                       cp_parser_relational_expression);
5015 }
5016
5017 /* Parse an and-expression.
5018
5019    and-expression:
5020      equality-expression
5021      and-expression & equality-expression
5022
5023    Returns a representation of the expression.  */
5024
5025 static tree
5026 cp_parser_and_expression (cp_parser* parser)
5027 {
5028   static const cp_parser_token_tree_map map = {
5029     { CPP_AND, BIT_AND_EXPR },
5030     { CPP_EOF, ERROR_MARK }
5031   };
5032
5033   return cp_parser_binary_expression (parser,
5034                                       map,
5035                                       cp_parser_equality_expression);
5036 }
5037
5038 /* Parse an exclusive-or-expression.
5039
5040    exclusive-or-expression:
5041      and-expression
5042      exclusive-or-expression ^ and-expression
5043
5044    Returns a representation of the expression.  */
5045
5046 static tree
5047 cp_parser_exclusive_or_expression (cp_parser* parser)
5048 {
5049   static const cp_parser_token_tree_map map = {
5050     { CPP_XOR, BIT_XOR_EXPR },
5051     { CPP_EOF, ERROR_MARK }
5052   };
5053
5054   return cp_parser_binary_expression (parser,
5055                                       map,
5056                                       cp_parser_and_expression);
5057 }
5058
5059
5060 /* Parse an inclusive-or-expression.
5061
5062    inclusive-or-expression:
5063      exclusive-or-expression
5064      inclusive-or-expression | exclusive-or-expression
5065
5066    Returns a representation of the expression.  */
5067
5068 static tree
5069 cp_parser_inclusive_or_expression (cp_parser* parser)
5070 {
5071   static const cp_parser_token_tree_map map = {
5072     { CPP_OR, BIT_IOR_EXPR },
5073     { CPP_EOF, ERROR_MARK }
5074   };
5075
5076   return cp_parser_binary_expression (parser,
5077                                       map,
5078                                       cp_parser_exclusive_or_expression);
5079 }
5080
5081 /* Parse a logical-and-expression.
5082
5083    logical-and-expression:
5084      inclusive-or-expression
5085      logical-and-expression && inclusive-or-expression
5086
5087    Returns a representation of the expression.  */
5088
5089 static tree
5090 cp_parser_logical_and_expression (cp_parser* parser)
5091 {
5092   static const cp_parser_token_tree_map map = {
5093     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5094     { CPP_EOF, ERROR_MARK }
5095   };
5096
5097   return cp_parser_binary_expression (parser,
5098                                       map,
5099                                       cp_parser_inclusive_or_expression);
5100 }
5101
5102 /* Parse a logical-or-expression.
5103
5104    logical-or-expression:
5105      logical-and-expression
5106      logical-or-expression || logical-and-expression
5107
5108    Returns a representation of the expression.  */
5109
5110 static tree
5111 cp_parser_logical_or_expression (cp_parser* parser)
5112 {
5113   static const cp_parser_token_tree_map map = {
5114     { CPP_OR_OR, TRUTH_ORIF_EXPR },
5115     { CPP_EOF, ERROR_MARK }
5116   };
5117
5118   return cp_parser_binary_expression (parser,
5119                                       map,
5120                                       cp_parser_logical_and_expression);
5121 }
5122
5123 /* Parse the `? expression : assignment-expression' part of a
5124    conditional-expression.  The LOGICAL_OR_EXPR is the
5125    logical-or-expression that started the conditional-expression.
5126    Returns a representation of the entire conditional-expression.
5127
5128    This routine is used by cp_parser_assignment_expression.
5129
5130      ? expression : assignment-expression
5131    
5132    GNU Extensions:
5133    
5134      ? : assignment-expression */
5135
5136 static tree
5137 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5138 {
5139   tree expr;
5140   tree assignment_expr;
5141
5142   /* Consume the `?' token.  */
5143   cp_lexer_consume_token (parser->lexer);
5144   if (cp_parser_allow_gnu_extensions_p (parser)
5145       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5146     /* Implicit true clause.  */
5147     expr = NULL_TREE;
5148   else
5149     /* Parse the expression.  */
5150     expr = cp_parser_expression (parser);
5151   
5152   /* The next token should be a `:'.  */
5153   cp_parser_require (parser, CPP_COLON, "`:'");
5154   /* Parse the assignment-expression.  */
5155   assignment_expr = cp_parser_assignment_expression (parser);
5156
5157   /* Build the conditional-expression.  */
5158   return build_x_conditional_expr (logical_or_expr,
5159                                    expr,
5160                                    assignment_expr);
5161 }
5162
5163 /* Parse an assignment-expression.
5164
5165    assignment-expression:
5166      conditional-expression
5167      logical-or-expression assignment-operator assignment_expression
5168      throw-expression
5169
5170    Returns a representation for the expression.  */
5171
5172 static tree
5173 cp_parser_assignment_expression (cp_parser* parser)
5174 {
5175   tree expr;
5176
5177   /* If the next token is the `throw' keyword, then we're looking at
5178      a throw-expression.  */
5179   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5180     expr = cp_parser_throw_expression (parser);
5181   /* Otherwise, it must be that we are looking at a
5182      logical-or-expression.  */
5183   else
5184     {
5185       /* Parse the logical-or-expression.  */
5186       expr = cp_parser_logical_or_expression (parser);
5187       /* If the next token is a `?' then we're actually looking at a
5188          conditional-expression.  */
5189       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5190         return cp_parser_question_colon_clause (parser, expr);
5191       else 
5192         {
5193           enum tree_code assignment_operator;
5194
5195           /* If it's an assignment-operator, we're using the second
5196              production.  */
5197           assignment_operator 
5198             = cp_parser_assignment_operator_opt (parser);
5199           if (assignment_operator != ERROR_MARK)
5200             {
5201               tree rhs;
5202
5203               /* Parse the right-hand side of the assignment.  */
5204               rhs = cp_parser_assignment_expression (parser);
5205               /* An assignment may not appear in a
5206                  constant-expression.  */
5207               if (cp_parser_non_integral_constant_expression (parser,
5208                                                               "an assignment"))
5209                 return error_mark_node;
5210               /* Build the assignment expression.  */
5211               expr = build_x_modify_expr (expr, 
5212                                           assignment_operator, 
5213                                           rhs);
5214             }
5215         }
5216     }
5217
5218   return expr;
5219 }
5220
5221 /* Parse an (optional) assignment-operator.
5222
5223    assignment-operator: one of 
5224      = *= /= %= += -= >>= <<= &= ^= |=  
5225
5226    GNU Extension:
5227    
5228    assignment-operator: one of
5229      <?= >?=
5230
5231    If the next token is an assignment operator, the corresponding tree
5232    code is returned, and the token is consumed.  For example, for
5233    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5234    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5235    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5236    operator, ERROR_MARK is returned.  */
5237
5238 static enum tree_code
5239 cp_parser_assignment_operator_opt (cp_parser* parser)
5240 {
5241   enum tree_code op;
5242   cp_token *token;
5243
5244   /* Peek at the next toen.  */
5245   token = cp_lexer_peek_token (parser->lexer);
5246
5247   switch (token->type)
5248     {
5249     case CPP_EQ:
5250       op = NOP_EXPR;
5251       break;
5252
5253     case CPP_MULT_EQ:
5254       op = MULT_EXPR;
5255       break;
5256
5257     case CPP_DIV_EQ:
5258       op = TRUNC_DIV_EXPR;
5259       break;
5260
5261     case CPP_MOD_EQ:
5262       op = TRUNC_MOD_EXPR;
5263       break;
5264
5265     case CPP_PLUS_EQ:
5266       op = PLUS_EXPR;
5267       break;
5268
5269     case CPP_MINUS_EQ:
5270       op = MINUS_EXPR;
5271       break;
5272
5273     case CPP_RSHIFT_EQ:
5274       op = RSHIFT_EXPR;
5275       break;
5276
5277     case CPP_LSHIFT_EQ:
5278       op = LSHIFT_EXPR;
5279       break;
5280
5281     case CPP_AND_EQ:
5282       op = BIT_AND_EXPR;
5283       break;
5284
5285     case CPP_XOR_EQ:
5286       op = BIT_XOR_EXPR;
5287       break;
5288
5289     case CPP_OR_EQ:
5290       op = BIT_IOR_EXPR;
5291       break;
5292
5293     case CPP_MIN_EQ:
5294       op = MIN_EXPR;
5295       break;
5296
5297     case CPP_MAX_EQ:
5298       op = MAX_EXPR;
5299       break;
5300
5301     default: 
5302       /* Nothing else is an assignment operator.  */
5303       op = ERROR_MARK;
5304     }
5305
5306   /* If it was an assignment operator, consume it.  */
5307   if (op != ERROR_MARK)
5308     cp_lexer_consume_token (parser->lexer);
5309
5310   return op;
5311 }
5312
5313 /* Parse an expression.
5314
5315    expression:
5316      assignment-expression
5317      expression , assignment-expression
5318
5319    Returns a representation of the expression.  */
5320
5321 static tree
5322 cp_parser_expression (cp_parser* parser)
5323 {
5324   tree expression = NULL_TREE;
5325
5326   while (true)
5327     {
5328       tree assignment_expression;
5329
5330       /* Parse the next assignment-expression.  */
5331       assignment_expression 
5332         = cp_parser_assignment_expression (parser);
5333       /* If this is the first assignment-expression, we can just
5334          save it away.  */
5335       if (!expression)
5336         expression = assignment_expression;
5337       else
5338         expression = build_x_compound_expr (expression,
5339                                             assignment_expression);
5340       /* If the next token is not a comma, then we are done with the
5341          expression.  */
5342       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5343         break;
5344       /* Consume the `,'.  */
5345       cp_lexer_consume_token (parser->lexer);
5346       /* A comma operator cannot appear in a constant-expression.  */
5347       if (cp_parser_non_integral_constant_expression (parser,
5348                                                       "a comma operator"))
5349         expression = error_mark_node;
5350     }
5351
5352   return expression;
5353 }
5354
5355 /* Parse a constant-expression. 
5356
5357    constant-expression:
5358      conditional-expression  
5359
5360   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5361   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5362   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5363   is false, NON_CONSTANT_P should be NULL.  */
5364
5365 static tree
5366 cp_parser_constant_expression (cp_parser* parser, 
5367                                bool allow_non_constant_p,
5368                                bool *non_constant_p)
5369 {
5370   bool saved_integral_constant_expression_p;
5371   bool saved_allow_non_integral_constant_expression_p;
5372   bool saved_non_integral_constant_expression_p;
5373   tree expression;
5374
5375   /* It might seem that we could simply parse the
5376      conditional-expression, and then check to see if it were
5377      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5378      one that the compiler can figure out is constant, possibly after
5379      doing some simplifications or optimizations.  The standard has a
5380      precise definition of constant-expression, and we must honor
5381      that, even though it is somewhat more restrictive.
5382
5383      For example:
5384
5385        int i[(2, 3)];
5386
5387      is not a legal declaration, because `(2, 3)' is not a
5388      constant-expression.  The `,' operator is forbidden in a
5389      constant-expression.  However, GCC's constant-folding machinery
5390      will fold this operation to an INTEGER_CST for `3'.  */
5391
5392   /* Save the old settings.  */
5393   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5394   saved_allow_non_integral_constant_expression_p 
5395     = parser->allow_non_integral_constant_expression_p;
5396   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5397   /* We are now parsing a constant-expression.  */
5398   parser->integral_constant_expression_p = true;
5399   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5400   parser->non_integral_constant_expression_p = false;
5401   /* Although the grammar says "conditional-expression", we parse an
5402      "assignment-expression", which also permits "throw-expression"
5403      and the use of assignment operators.  In the case that
5404      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5405      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5406      actually essential that we look for an assignment-expression.
5407      For example, cp_parser_initializer_clauses uses this function to
5408      determine whether a particular assignment-expression is in fact
5409      constant.  */
5410   expression = cp_parser_assignment_expression (parser);
5411   /* Restore the old settings.  */
5412   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5413   parser->allow_non_integral_constant_expression_p 
5414     = saved_allow_non_integral_constant_expression_p;
5415   if (allow_non_constant_p)
5416     *non_constant_p = parser->non_integral_constant_expression_p;
5417   parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5418
5419   return expression;
5420 }
5421
5422 /* Statements [gram.stmt.stmt]  */
5423
5424 /* Parse a statement.  
5425
5426    statement:
5427      labeled-statement
5428      expression-statement
5429      compound-statement
5430      selection-statement
5431      iteration-statement
5432      jump-statement
5433      declaration-statement
5434      try-block  */
5435
5436 static void
5437 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5438 {
5439   tree statement;
5440   cp_token *token;
5441   int statement_line_number;
5442
5443   /* There is no statement yet.  */
5444   statement = NULL_TREE;
5445   /* Peek at the next token.  */
5446   token = cp_lexer_peek_token (parser->lexer);
5447   /* Remember the line number of the first token in the statement.  */
5448   statement_line_number = token->location.line;
5449   /* If this is a keyword, then that will often determine what kind of
5450      statement we have.  */
5451   if (token->type == CPP_KEYWORD)
5452     {
5453       enum rid keyword = token->keyword;
5454
5455       switch (keyword)
5456         {
5457         case RID_CASE:
5458         case RID_DEFAULT:
5459           statement = cp_parser_labeled_statement (parser,
5460                                                    in_statement_expr_p);
5461           break;
5462
5463         case RID_IF:
5464         case RID_SWITCH:
5465           statement = cp_parser_selection_statement (parser);
5466           break;
5467
5468         case RID_WHILE:
5469         case RID_DO:
5470         case RID_FOR:
5471           statement = cp_parser_iteration_statement (parser);
5472           break;
5473
5474         case RID_BREAK:
5475         case RID_CONTINUE:
5476         case RID_RETURN:
5477         case RID_GOTO:
5478           statement = cp_parser_jump_statement (parser);
5479           break;
5480
5481         case RID_TRY:
5482           statement = cp_parser_try_block (parser);
5483           break;
5484
5485         default:
5486           /* It might be a keyword like `int' that can start a
5487              declaration-statement.  */
5488           break;
5489         }
5490     }
5491   else if (token->type == CPP_NAME)
5492     {
5493       /* If the next token is a `:', then we are looking at a
5494          labeled-statement.  */
5495       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5496       if (token->type == CPP_COLON)
5497         statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5498     }
5499   /* Anything that starts with a `{' must be a compound-statement.  */
5500   else if (token->type == CPP_OPEN_BRACE)
5501     statement = cp_parser_compound_statement (parser, false);
5502
5503   /* Everything else must be a declaration-statement or an
5504      expression-statement.  Try for the declaration-statement 
5505      first, unless we are looking at a `;', in which case we know that
5506      we have an expression-statement.  */
5507   if (!statement)
5508     {
5509       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5510         {
5511           cp_parser_parse_tentatively (parser);
5512           /* Try to parse the declaration-statement.  */
5513           cp_parser_declaration_statement (parser);
5514           /* If that worked, we're done.  */
5515           if (cp_parser_parse_definitely (parser))
5516             return;
5517         }
5518       /* Look for an expression-statement instead.  */
5519       statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5520     }
5521
5522   /* Set the line number for the statement.  */
5523   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5524     STMT_LINENO (statement) = statement_line_number;
5525 }
5526
5527 /* Parse a labeled-statement.
5528
5529    labeled-statement:
5530      identifier : statement
5531      case constant-expression : statement
5532      default : statement
5533
5534    GNU Extension:
5535    
5536    labeled-statement:
5537      case constant-expression ... constant-expression : statement
5538
5539    Returns the new CASE_LABEL, for a `case' or `default' label.  For
5540    an ordinary label, returns a LABEL_STMT.  */
5541
5542 static tree
5543 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5544 {
5545   cp_token *token;
5546   tree statement = error_mark_node;
5547
5548   /* The next token should be an identifier.  */
5549   token = cp_lexer_peek_token (parser->lexer);
5550   if (token->type != CPP_NAME
5551       && token->type != CPP_KEYWORD)
5552     {
5553       cp_parser_error (parser, "expected labeled-statement");
5554       return error_mark_node;
5555     }
5556
5557   switch (token->keyword)
5558     {
5559     case RID_CASE:
5560       {
5561         tree expr, expr_hi;
5562         cp_token *ellipsis;
5563
5564         /* Consume the `case' token.  */
5565         cp_lexer_consume_token (parser->lexer);
5566         /* Parse the constant-expression.  */
5567         expr = cp_parser_constant_expression (parser, 
5568                                               /*allow_non_constant_p=*/false,
5569                                               NULL);
5570
5571         ellipsis = cp_lexer_peek_token (parser->lexer);
5572         if (ellipsis->type == CPP_ELLIPSIS)
5573           {
5574             /* Consume the `...' token.  */
5575             cp_lexer_consume_token (parser->lexer);
5576             expr_hi =
5577               cp_parser_constant_expression (parser,
5578                                              /*allow_non_constant_p=*/false,
5579                                              NULL);
5580             /* We don't need to emit warnings here, as the common code
5581                will do this for us.  */
5582           }
5583         else
5584           expr_hi = NULL_TREE;
5585
5586         if (!parser->in_switch_statement_p)
5587           error ("case label `%E' not within a switch statement", expr);
5588         else
5589           statement = finish_case_label (expr, expr_hi);
5590       }
5591       break;
5592
5593     case RID_DEFAULT:
5594       /* Consume the `default' token.  */
5595       cp_lexer_consume_token (parser->lexer);
5596       if (!parser->in_switch_statement_p)
5597         error ("case label not within a switch statement");
5598       else
5599         statement = finish_case_label (NULL_TREE, NULL_TREE);
5600       break;
5601
5602     default:
5603       /* Anything else must be an ordinary label.  */
5604       statement = finish_label_stmt (cp_parser_identifier (parser));
5605       break;
5606     }
5607
5608   /* Require the `:' token.  */
5609   cp_parser_require (parser, CPP_COLON, "`:'");
5610   /* Parse the labeled statement.  */
5611   cp_parser_statement (parser, in_statement_expr_p);
5612
5613   /* Return the label, in the case of a `case' or `default' label.  */
5614   return statement;
5615 }
5616
5617 /* Parse an expression-statement.
5618
5619    expression-statement:
5620      expression [opt] ;
5621
5622    Returns the new EXPR_STMT -- or NULL_TREE if the expression
5623    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5624    indicates whether this expression-statement is part of an
5625    expression statement.  */
5626
5627 static tree
5628 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5629 {
5630   tree statement = NULL_TREE;
5631
5632   /* If the next token is a ';', then there is no expression
5633      statement.  */
5634   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5635     statement = cp_parser_expression (parser);
5636   
5637   /* Consume the final `;'.  */
5638   cp_parser_consume_semicolon_at_end_of_statement (parser);
5639
5640   if (in_statement_expr_p
5641       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5642     {
5643       /* This is the final expression statement of a statement
5644          expression.  */
5645       statement = finish_stmt_expr_expr (statement);
5646     }
5647   else if (statement)
5648     statement = finish_expr_stmt (statement);
5649   else
5650     finish_stmt ();
5651   
5652   return statement;
5653 }
5654
5655 /* Parse a compound-statement.
5656
5657    compound-statement:
5658      { statement-seq [opt] }
5659      
5660    Returns a COMPOUND_STMT representing the statement.  */
5661
5662 static tree
5663 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5664 {
5665   tree compound_stmt;
5666
5667   /* Consume the `{'.  */
5668   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5669     return error_mark_node;
5670   /* Begin the compound-statement.  */
5671   compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5672   /* Parse an (optional) statement-seq.  */
5673   cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5674   /* Finish the compound-statement.  */
5675   finish_compound_stmt (compound_stmt);
5676   /* Consume the `}'.  */
5677   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5678
5679   return compound_stmt;
5680 }
5681
5682 /* Parse an (optional) statement-seq.
5683
5684    statement-seq:
5685      statement
5686      statement-seq [opt] statement  */
5687
5688 static void
5689 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5690 {
5691   /* Scan statements until there aren't any more.  */
5692   while (true)
5693     {
5694       /* If we're looking at a `}', then we've run out of statements.  */
5695       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5696           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5697         break;
5698
5699       /* Parse the statement.  */
5700       cp_parser_statement (parser, in_statement_expr_p);
5701     }
5702 }
5703
5704 /* Parse a selection-statement.
5705
5706    selection-statement:
5707      if ( condition ) statement
5708      if ( condition ) statement else statement
5709      switch ( condition ) statement  
5710
5711    Returns the new IF_STMT or SWITCH_STMT.  */
5712
5713 static tree
5714 cp_parser_selection_statement (cp_parser* parser)
5715 {
5716   cp_token *token;
5717   enum rid keyword;
5718
5719   /* Peek at the next token.  */
5720   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5721
5722   /* See what kind of keyword it is.  */
5723   keyword = token->keyword;
5724   switch (keyword)
5725     {
5726     case RID_IF:
5727     case RID_SWITCH:
5728       {
5729         tree statement;
5730         tree condition;
5731
5732         /* Look for the `('.  */
5733         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5734           {
5735             cp_parser_skip_to_end_of_statement (parser);
5736             return error_mark_node;
5737           }
5738
5739         /* Begin the selection-statement.  */
5740         if (keyword == RID_IF)
5741           statement = begin_if_stmt ();
5742         else
5743           statement = begin_switch_stmt ();
5744
5745         /* Parse the condition.  */
5746         condition = cp_parser_condition (parser);
5747         /* Look for the `)'.  */
5748         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5749           cp_parser_skip_to_closing_parenthesis (parser, true, false,
5750                                                  /*consume_paren=*/true);
5751
5752         if (keyword == RID_IF)
5753           {
5754             tree then_stmt;
5755
5756             /* Add the condition.  */
5757             finish_if_stmt_cond (condition, statement);
5758
5759             /* Parse the then-clause.  */
5760             then_stmt = cp_parser_implicitly_scoped_statement (parser);
5761             finish_then_clause (statement);
5762
5763             /* If the next token is `else', parse the else-clause.  */
5764             if (cp_lexer_next_token_is_keyword (parser->lexer,
5765                                                 RID_ELSE))
5766               {
5767                 tree else_stmt;
5768
5769                 /* Consume the `else' keyword.  */
5770                 cp_lexer_consume_token (parser->lexer);
5771                 /* Parse the else-clause.  */
5772                 else_stmt 
5773                   = cp_parser_implicitly_scoped_statement (parser);
5774                 finish_else_clause (statement);
5775               }
5776
5777             /* Now we're all done with the if-statement.  */
5778             finish_if_stmt ();
5779           }
5780         else
5781           {
5782             tree body;
5783             bool in_switch_statement_p;
5784
5785             /* Add the condition.  */
5786             finish_switch_cond (condition, statement);
5787
5788             /* Parse the body of the switch-statement.  */
5789             in_switch_statement_p = parser->in_switch_statement_p;
5790             parser->in_switch_statement_p = true;
5791             body = cp_parser_implicitly_scoped_statement (parser);
5792             parser->in_switch_statement_p = in_switch_statement_p;
5793
5794             /* Now we're all done with the switch-statement.  */
5795             finish_switch_stmt (statement);
5796           }
5797
5798         return statement;
5799       }
5800       break;
5801
5802     default:
5803       cp_parser_error (parser, "expected selection-statement");
5804       return error_mark_node;
5805     }
5806 }
5807
5808 /* Parse a condition. 
5809
5810    condition:
5811      expression
5812      type-specifier-seq declarator = assignment-expression  
5813
5814    GNU Extension:
5815    
5816    condition:
5817      type-specifier-seq declarator asm-specification [opt] 
5818        attributes [opt] = assignment-expression
5819  
5820    Returns the expression that should be tested.  */
5821
5822 static tree
5823 cp_parser_condition (cp_parser* parser)
5824 {
5825   tree type_specifiers;
5826   const char *saved_message;
5827
5828   /* Try the declaration first.  */
5829   cp_parser_parse_tentatively (parser);
5830   /* New types are not allowed in the type-specifier-seq for a
5831      condition.  */
5832   saved_message = parser->type_definition_forbidden_message;
5833   parser->type_definition_forbidden_message
5834     = "types may not be defined in conditions";
5835   /* Parse the type-specifier-seq.  */
5836   type_specifiers = cp_parser_type_specifier_seq (parser);
5837   /* Restore the saved message.  */
5838   parser->type_definition_forbidden_message = saved_message;
5839   /* If all is well, we might be looking at a declaration.  */
5840   if (!cp_parser_error_occurred (parser))
5841     {
5842       tree decl;
5843       tree asm_specification;
5844       tree attributes;
5845       tree declarator;
5846       tree initializer = NULL_TREE;
5847       
5848       /* Parse the declarator.  */
5849       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5850                                          /*ctor_dtor_or_conv_p=*/NULL,
5851                                          /*parenthesized_p=*/NULL,
5852                                          /*member_p=*/false);
5853       /* Parse the attributes.  */
5854       attributes = cp_parser_attributes_opt (parser);
5855       /* Parse the asm-specification.  */
5856       asm_specification = cp_parser_asm_specification_opt (parser);
5857       /* If the next token is not an `=', then we might still be
5858          looking at an expression.  For example:
5859          
5860            if (A(a).x)
5861           
5862          looks like a decl-specifier-seq and a declarator -- but then
5863          there is no `=', so this is an expression.  */
5864       cp_parser_require (parser, CPP_EQ, "`='");
5865       /* If we did see an `=', then we are looking at a declaration
5866          for sure.  */
5867       if (cp_parser_parse_definitely (parser))
5868         {
5869           /* Create the declaration.  */
5870           decl = start_decl (declarator, type_specifiers, 
5871                              /*initialized_p=*/true,
5872                              attributes, /*prefix_attributes=*/NULL_TREE);
5873           /* Parse the assignment-expression.  */
5874           initializer = cp_parser_assignment_expression (parser);
5875           
5876           /* Process the initializer.  */
5877           cp_finish_decl (decl, 
5878                           initializer, 
5879                           asm_specification, 
5880                           LOOKUP_ONLYCONVERTING);
5881           
5882           return convert_from_reference (decl);
5883         }
5884     }
5885   /* If we didn't even get past the declarator successfully, we are
5886      definitely not looking at a declaration.  */
5887   else
5888     cp_parser_abort_tentative_parse (parser);
5889
5890   /* Otherwise, we are looking at an expression.  */
5891   return cp_parser_expression (parser);
5892 }
5893
5894 /* Parse an iteration-statement.
5895
5896    iteration-statement:
5897      while ( condition ) statement
5898      do statement while ( expression ) ;
5899      for ( for-init-statement condition [opt] ; expression [opt] )
5900        statement
5901
5902    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
5903
5904 static tree
5905 cp_parser_iteration_statement (cp_parser* parser)
5906 {
5907   cp_token *token;
5908   enum rid keyword;
5909   tree statement;
5910   bool in_iteration_statement_p;
5911
5912
5913   /* Peek at the next token.  */
5914   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5915   if (!token)
5916     return error_mark_node;
5917
5918   /* Remember whether or not we are already within an iteration
5919      statement.  */ 
5920   in_iteration_statement_p = parser->in_iteration_statement_p;
5921
5922   /* See what kind of keyword it is.  */
5923   keyword = token->keyword;
5924   switch (keyword)
5925     {
5926     case RID_WHILE:
5927       {
5928         tree condition;
5929
5930         /* Begin the while-statement.  */
5931         statement = begin_while_stmt ();
5932         /* Look for the `('.  */
5933         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5934         /* Parse the condition.  */
5935         condition = cp_parser_condition (parser);
5936         finish_while_stmt_cond (condition, statement);
5937         /* Look for the `)'.  */
5938         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5939         /* Parse the dependent statement.  */
5940         parser->in_iteration_statement_p = true;
5941         cp_parser_already_scoped_statement (parser);
5942         parser->in_iteration_statement_p = in_iteration_statement_p;
5943         /* We're done with the while-statement.  */
5944         finish_while_stmt (statement);
5945       }
5946       break;
5947
5948     case RID_DO:
5949       {
5950         tree expression;
5951
5952         /* Begin the do-statement.  */
5953         statement = begin_do_stmt ();
5954         /* Parse the body of the do-statement.  */
5955         parser->in_iteration_statement_p = true;
5956         cp_parser_implicitly_scoped_statement (parser);
5957         parser->in_iteration_statement_p = in_iteration_statement_p;
5958         finish_do_body (statement);
5959         /* Look for the `while' keyword.  */
5960         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5961         /* Look for the `('.  */
5962         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5963         /* Parse the expression.  */
5964         expression = cp_parser_expression (parser);
5965         /* We're done with the do-statement.  */
5966         finish_do_stmt (expression, statement);
5967         /* Look for the `)'.  */
5968         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5969         /* Look for the `;'.  */
5970         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5971       }
5972       break;
5973
5974     case RID_FOR:
5975       {
5976         tree condition = NULL_TREE;
5977         tree expression = NULL_TREE;
5978
5979         /* Begin the for-statement.  */
5980         statement = begin_for_stmt ();
5981         /* Look for the `('.  */
5982         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5983         /* Parse the initialization.  */
5984         cp_parser_for_init_statement (parser);
5985         finish_for_init_stmt (statement);
5986
5987         /* If there's a condition, process it.  */
5988         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5989           condition = cp_parser_condition (parser);
5990         finish_for_cond (condition, statement);
5991         /* Look for the `;'.  */
5992         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5993
5994         /* If there's an expression, process it.  */
5995         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5996           expression = cp_parser_expression (parser);
5997         finish_for_expr (expression, statement);
5998         /* Look for the `)'.  */
5999         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6000         
6001         /* Parse the body of the for-statement.  */
6002         parser->in_iteration_statement_p = true;
6003         cp_parser_already_scoped_statement (parser);
6004         parser->in_iteration_statement_p = in_iteration_statement_p;
6005
6006         /* We're done with the for-statement.  */
6007         finish_for_stmt (statement);
6008       }
6009       break;
6010
6011     default:
6012       cp_parser_error (parser, "expected iteration-statement");
6013       statement = error_mark_node;
6014       break;
6015     }
6016
6017   return statement;
6018 }
6019
6020 /* Parse a for-init-statement.
6021
6022    for-init-statement:
6023      expression-statement
6024      simple-declaration  */
6025
6026 static void
6027 cp_parser_for_init_statement (cp_parser* parser)
6028 {
6029   /* If the next token is a `;', then we have an empty
6030      expression-statement.  Grammatically, this is also a
6031      simple-declaration, but an invalid one, because it does not
6032      declare anything.  Therefore, if we did not handle this case
6033      specially, we would issue an error message about an invalid
6034      declaration.  */
6035   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6036     {
6037       /* We're going to speculatively look for a declaration, falling back
6038          to an expression, if necessary.  */
6039       cp_parser_parse_tentatively (parser);
6040       /* Parse the declaration.  */
6041       cp_parser_simple_declaration (parser,
6042                                     /*function_definition_allowed_p=*/false);
6043       /* If the tentative parse failed, then we shall need to look for an
6044          expression-statement.  */
6045       if (cp_parser_parse_definitely (parser))
6046         return;
6047     }
6048
6049   cp_parser_expression_statement (parser, false);
6050 }
6051
6052 /* Parse a jump-statement.
6053
6054    jump-statement:
6055      break ;
6056      continue ;
6057      return expression [opt] ;
6058      goto identifier ;  
6059
6060    GNU extension:
6061
6062    jump-statement:
6063      goto * expression ;
6064
6065    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6066    GOTO_STMT.  */
6067
6068 static tree
6069 cp_parser_jump_statement (cp_parser* parser)
6070 {
6071   tree statement = error_mark_node;
6072   cp_token *token;
6073   enum rid keyword;
6074
6075   /* Peek at the next token.  */
6076   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6077   if (!token)
6078     return error_mark_node;
6079
6080   /* See what kind of keyword it is.  */
6081   keyword = token->keyword;
6082   switch (keyword)
6083     {
6084     case RID_BREAK:
6085       if (!parser->in_switch_statement_p
6086           && !parser->in_iteration_statement_p)
6087         {
6088           error ("break statement not within loop or switch");
6089           statement = error_mark_node;
6090         }
6091       else
6092         statement = finish_break_stmt ();
6093       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6094       break;
6095
6096     case RID_CONTINUE:
6097       if (!parser->in_iteration_statement_p)
6098         {
6099           error ("continue statement not within a loop");
6100           statement = error_mark_node;
6101         }
6102       else
6103         statement = finish_continue_stmt ();
6104       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6105       break;
6106
6107     case RID_RETURN:
6108       {
6109         tree expr;
6110
6111         /* If the next token is a `;', then there is no 
6112            expression.  */
6113         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6114           expr = cp_parser_expression (parser);
6115         else
6116           expr = NULL_TREE;
6117         /* Build the return-statement.  */
6118         statement = finish_return_stmt (expr);
6119         /* Look for the final `;'.  */
6120         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6121       }
6122       break;
6123
6124     case RID_GOTO:
6125       /* Create the goto-statement.  */
6126       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6127         {
6128           /* Issue a warning about this use of a GNU extension.  */
6129           if (pedantic)
6130             pedwarn ("ISO C++ forbids computed gotos");
6131           /* Consume the '*' token.  */
6132           cp_lexer_consume_token (parser->lexer);
6133           /* Parse the dependent expression.  */
6134           finish_goto_stmt (cp_parser_expression (parser));
6135         }
6136       else
6137         finish_goto_stmt (cp_parser_identifier (parser));
6138       /* Look for the final `;'.  */
6139       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6140       break;
6141
6142     default:
6143       cp_parser_error (parser, "expected jump-statement");
6144       break;
6145     }
6146
6147   return statement;
6148 }
6149
6150 /* Parse a declaration-statement.
6151
6152    declaration-statement:
6153      block-declaration  */
6154
6155 static void
6156 cp_parser_declaration_statement (cp_parser* parser)
6157 {
6158   /* Parse the block-declaration.  */
6159   cp_parser_block_declaration (parser, /*statement_p=*/true);
6160
6161   /* Finish off the statement.  */
6162   finish_stmt ();
6163 }
6164
6165 /* Some dependent statements (like `if (cond) statement'), are
6166    implicitly in their own scope.  In other words, if the statement is
6167    a single statement (as opposed to a compound-statement), it is
6168    none-the-less treated as if it were enclosed in braces.  Any
6169    declarations appearing in the dependent statement are out of scope
6170    after control passes that point.  This function parses a statement,
6171    but ensures that is in its own scope, even if it is not a
6172    compound-statement.  
6173
6174    Returns the new statement.  */
6175
6176 static tree
6177 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6178 {
6179   tree statement;
6180
6181   /* If the token is not a `{', then we must take special action.  */
6182   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6183     {
6184       /* Create a compound-statement.  */
6185       statement = begin_compound_stmt (/*has_no_scope=*/false);
6186       /* Parse the dependent-statement.  */
6187       cp_parser_statement (parser, false);
6188       /* Finish the dummy compound-statement.  */
6189       finish_compound_stmt (statement);
6190     }
6191   /* Otherwise, we simply parse the statement directly.  */
6192   else
6193     statement = cp_parser_compound_statement (parser, false);
6194
6195   /* Return the statement.  */
6196   return statement;
6197 }
6198
6199 /* For some dependent statements (like `while (cond) statement'), we
6200    have already created a scope.  Therefore, even if the dependent
6201    statement is a compound-statement, we do not want to create another
6202    scope.  */
6203
6204 static void
6205 cp_parser_already_scoped_statement (cp_parser* parser)
6206 {
6207   /* If the token is not a `{', then we must take special action.  */
6208   if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6209     {
6210       tree statement;
6211
6212       /* Create a compound-statement.  */
6213       statement = begin_compound_stmt (/*has_no_scope=*/true);
6214       /* Parse the dependent-statement.  */
6215       cp_parser_statement (parser, false);
6216       /* Finish the dummy compound-statement.  */
6217       finish_compound_stmt (statement);
6218     }
6219   /* Otherwise, we simply parse the statement directly.  */
6220   else
6221     cp_parser_statement (parser, false);
6222 }
6223
6224 /* Declarations [gram.dcl.dcl] */
6225
6226 /* Parse an optional declaration-sequence.
6227
6228    declaration-seq:
6229      declaration
6230      declaration-seq declaration  */
6231
6232 static void
6233 cp_parser_declaration_seq_opt (cp_parser* parser)
6234 {
6235   while (true)
6236     {
6237       cp_token *token;
6238
6239       token = cp_lexer_peek_token (parser->lexer);
6240
6241       if (token->type == CPP_CLOSE_BRACE
6242           || token->type == CPP_EOF)
6243         break;
6244
6245       if (token->type == CPP_SEMICOLON) 
6246         {
6247           /* A declaration consisting of a single semicolon is
6248              invalid.  Allow it unless we're being pedantic.  */
6249           if (pedantic && !in_system_header)
6250             pedwarn ("extra `;'");
6251           cp_lexer_consume_token (parser->lexer);
6252           continue;
6253         }
6254
6255       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6256          parser to enter or exit implicit `extern "C"' blocks.  */
6257       while (pending_lang_change > 0)
6258         {
6259           push_lang_context (lang_name_c);
6260           --pending_lang_change;
6261         }
6262       while (pending_lang_change < 0)
6263         {
6264           pop_lang_context ();
6265           ++pending_lang_change;
6266         }
6267
6268       /* Parse the declaration itself.  */
6269       cp_parser_declaration (parser);
6270     }
6271 }
6272
6273 /* Parse a declaration.
6274
6275    declaration:
6276      block-declaration
6277      function-definition
6278      template-declaration
6279      explicit-instantiation
6280      explicit-specialization
6281      linkage-specification
6282      namespace-definition    
6283
6284    GNU extension:
6285
6286    declaration:
6287       __extension__ declaration */
6288
6289 static void
6290 cp_parser_declaration (cp_parser* parser)
6291 {
6292   cp_token token1;
6293   cp_token token2;
6294   int saved_pedantic;
6295
6296   /* Check for the `__extension__' keyword.  */
6297   if (cp_parser_extension_opt (parser, &saved_pedantic))
6298     {
6299       /* Parse the qualified declaration.  */
6300       cp_parser_declaration (parser);
6301       /* Restore the PEDANTIC flag.  */
6302       pedantic = saved_pedantic;
6303
6304       return;
6305     }
6306
6307   /* Try to figure out what kind of declaration is present.  */
6308   token1 = *cp_lexer_peek_token (parser->lexer);
6309   if (token1.type != CPP_EOF)
6310     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6311
6312   /* If the next token is `extern' and the following token is a string
6313      literal, then we have a linkage specification.  */
6314   if (token1.keyword == RID_EXTERN
6315       && cp_parser_is_string_literal (&token2))
6316     cp_parser_linkage_specification (parser);
6317   /* If the next token is `template', then we have either a template
6318      declaration, an explicit instantiation, or an explicit
6319      specialization.  */
6320   else if (token1.keyword == RID_TEMPLATE)
6321     {
6322       /* `template <>' indicates a template specialization.  */
6323       if (token2.type == CPP_LESS
6324           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6325         cp_parser_explicit_specialization (parser);
6326       /* `template <' indicates a template declaration.  */
6327       else if (token2.type == CPP_LESS)
6328         cp_parser_template_declaration (parser, /*member_p=*/false);
6329       /* Anything else must be an explicit instantiation.  */
6330       else
6331         cp_parser_explicit_instantiation (parser);
6332     }
6333   /* If the next token is `export', then we have a template
6334      declaration.  */
6335   else if (token1.keyword == RID_EXPORT)
6336     cp_parser_template_declaration (parser, /*member_p=*/false);
6337   /* If the next token is `extern', 'static' or 'inline' and the one
6338      after that is `template', we have a GNU extended explicit
6339      instantiation directive.  */
6340   else if (cp_parser_allow_gnu_extensions_p (parser)
6341            && (token1.keyword == RID_EXTERN
6342                || token1.keyword == RID_STATIC
6343                || token1.keyword == RID_INLINE)
6344            && token2.keyword == RID_TEMPLATE)
6345     cp_parser_explicit_instantiation (parser);
6346   /* If the next token is `namespace', check for a named or unnamed
6347      namespace definition.  */
6348   else if (token1.keyword == RID_NAMESPACE
6349            && (/* A named namespace definition.  */
6350                (token2.type == CPP_NAME
6351                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
6352                     == CPP_OPEN_BRACE))
6353                /* An unnamed namespace definition.  */
6354                || token2.type == CPP_OPEN_BRACE))
6355     cp_parser_namespace_definition (parser);
6356   /* We must have either a block declaration or a function
6357      definition.  */
6358   else
6359     /* Try to parse a block-declaration, or a function-definition.  */
6360     cp_parser_block_declaration (parser, /*statement_p=*/false);
6361 }
6362
6363 /* Parse a block-declaration.  
6364
6365    block-declaration:
6366      simple-declaration
6367      asm-definition
6368      namespace-alias-definition
6369      using-declaration
6370      using-directive  
6371
6372    GNU Extension:
6373
6374    block-declaration:
6375      __extension__ block-declaration 
6376      label-declaration
6377
6378    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6379    part of a declaration-statement.  */
6380
6381 static void
6382 cp_parser_block_declaration (cp_parser *parser, 
6383                              bool      statement_p)
6384 {
6385   cp_token *token1;
6386   int saved_pedantic;
6387
6388   /* Check for the `__extension__' keyword.  */
6389   if (cp_parser_extension_opt (parser, &saved_pedantic))
6390     {
6391       /* Parse the qualified declaration.  */
6392       cp_parser_block_declaration (parser, statement_p);
6393       /* Restore the PEDANTIC flag.  */
6394       pedantic = saved_pedantic;
6395
6396       return;
6397     }
6398
6399   /* Peek at the next token to figure out which kind of declaration is
6400      present.  */
6401   token1 = cp_lexer_peek_token (parser->lexer);
6402
6403   /* If the next keyword is `asm', we have an asm-definition.  */
6404   if (token1->keyword == RID_ASM)
6405     {
6406       if (statement_p)
6407         cp_parser_commit_to_tentative_parse (parser);
6408       cp_parser_asm_definition (parser);
6409     }
6410   /* If the next keyword is `namespace', we have a
6411      namespace-alias-definition.  */
6412   else if (token1->keyword == RID_NAMESPACE)
6413     cp_parser_namespace_alias_definition (parser);
6414   /* If the next keyword is `using', we have either a
6415      using-declaration or a using-directive.  */
6416   else if (token1->keyword == RID_USING)
6417     {
6418       cp_token *token2;
6419
6420       if (statement_p)
6421         cp_parser_commit_to_tentative_parse (parser);
6422       /* If the token after `using' is `namespace', then we have a
6423          using-directive.  */
6424       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6425       if (token2->keyword == RID_NAMESPACE)
6426         cp_parser_using_directive (parser);
6427       /* Otherwise, it's a using-declaration.  */
6428       else
6429         cp_parser_using_declaration (parser);
6430     }
6431   /* If the next keyword is `__label__' we have a label declaration.  */
6432   else if (token1->keyword == RID_LABEL)
6433     {
6434       if (statement_p)
6435         cp_parser_commit_to_tentative_parse (parser);
6436       cp_parser_label_declaration (parser);
6437     }
6438   /* Anything else must be a simple-declaration.  */
6439   else
6440     cp_parser_simple_declaration (parser, !statement_p);
6441 }
6442
6443 /* Parse a simple-declaration.
6444
6445    simple-declaration:
6446      decl-specifier-seq [opt] init-declarator-list [opt] ;  
6447
6448    init-declarator-list:
6449      init-declarator
6450      init-declarator-list , init-declarator 
6451
6452    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6453    function-definition as a simple-declaration.  */
6454
6455 static void
6456 cp_parser_simple_declaration (cp_parser* parser, 
6457                               bool function_definition_allowed_p)
6458 {
6459   tree decl_specifiers;
6460   tree attributes;
6461   int declares_class_or_enum;
6462   bool saw_declarator;
6463
6464   /* Defer access checks until we know what is being declared; the
6465      checks for names appearing in the decl-specifier-seq should be
6466      done as if we were in the scope of the thing being declared.  */
6467   push_deferring_access_checks (dk_deferred);
6468
6469   /* Parse the decl-specifier-seq.  We have to keep track of whether
6470      or not the decl-specifier-seq declares a named class or
6471      enumeration type, since that is the only case in which the
6472      init-declarator-list is allowed to be empty.  
6473
6474      [dcl.dcl]
6475
6476      In a simple-declaration, the optional init-declarator-list can be
6477      omitted only when declaring a class or enumeration, that is when
6478      the decl-specifier-seq contains either a class-specifier, an
6479      elaborated-type-specifier, or an enum-specifier.  */
6480   decl_specifiers
6481     = cp_parser_decl_specifier_seq (parser, 
6482                                     CP_PARSER_FLAGS_OPTIONAL,
6483                                     &attributes,
6484                                     &declares_class_or_enum);
6485   /* We no longer need to defer access checks.  */
6486   stop_deferring_access_checks ();
6487
6488   /* In a block scope, a valid declaration must always have a
6489      decl-specifier-seq.  By not trying to parse declarators, we can
6490      resolve the declaration/expression ambiguity more quickly.  */
6491   if (!function_definition_allowed_p && !decl_specifiers)
6492     {
6493       cp_parser_error (parser, "expected declaration");
6494       goto done;
6495     }
6496
6497   /* If the next two tokens are both identifiers, the code is
6498      erroneous. The usual cause of this situation is code like:
6499
6500        T t;
6501
6502      where "T" should name a type -- but does not.  */
6503   if (cp_parser_diagnose_invalid_type_name (parser))
6504     {
6505       /* If parsing tentatively, we should commit; we really are
6506          looking at a declaration.  */
6507       cp_parser_commit_to_tentative_parse (parser);
6508       /* Give up.  */
6509       goto done;
6510     }
6511
6512   /* Keep going until we hit the `;' at the end of the simple
6513      declaration.  */
6514   saw_declarator = false;
6515   while (cp_lexer_next_token_is_not (parser->lexer, 
6516                                      CPP_SEMICOLON))
6517     {
6518       cp_token *token;
6519       bool function_definition_p;
6520       tree decl;
6521
6522       saw_declarator = true;
6523       /* Parse the init-declarator.  */
6524       decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6525                                         function_definition_allowed_p,
6526                                         /*member_p=*/false,
6527                                         declares_class_or_enum,
6528                                         &function_definition_p);
6529       /* If an error occurred while parsing tentatively, exit quickly.
6530          (That usually happens when in the body of a function; each
6531          statement is treated as a declaration-statement until proven
6532          otherwise.)  */
6533       if (cp_parser_error_occurred (parser))
6534         goto done;
6535       /* Handle function definitions specially.  */
6536       if (function_definition_p)
6537         {
6538           /* If the next token is a `,', then we are probably
6539              processing something like:
6540
6541                void f() {}, *p;
6542
6543              which is erroneous.  */
6544           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6545             error ("mixing declarations and function-definitions is forbidden");
6546           /* Otherwise, we're done with the list of declarators.  */
6547           else
6548             {
6549               pop_deferring_access_checks ();
6550               return;
6551             }
6552         }
6553       /* The next token should be either a `,' or a `;'.  */
6554       token = cp_lexer_peek_token (parser->lexer);
6555       /* If it's a `,', there are more declarators to come.  */
6556       if (token->type == CPP_COMMA)
6557         cp_lexer_consume_token (parser->lexer);
6558       /* If it's a `;', we are done.  */
6559       else if (token->type == CPP_SEMICOLON)
6560         break;
6561       /* Anything else is an error.  */
6562       else
6563         {
6564           cp_parser_error (parser, "expected `,' or `;'");
6565           /* Skip tokens until we reach the end of the statement.  */
6566           cp_parser_skip_to_end_of_statement (parser);
6567           /* If the next token is now a `;', consume it.  */
6568           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6569             cp_lexer_consume_token (parser->lexer);
6570           goto done;
6571         }
6572       /* After the first time around, a function-definition is not
6573          allowed -- even if it was OK at first.  For example:
6574
6575            int i, f() {}
6576
6577          is not valid.  */
6578       function_definition_allowed_p = false;
6579     }
6580
6581   /* Issue an error message if no declarators are present, and the
6582      decl-specifier-seq does not itself declare a class or
6583      enumeration.  */
6584   if (!saw_declarator)
6585     {
6586       if (cp_parser_declares_only_class_p (parser))
6587         shadow_tag (decl_specifiers);
6588       /* Perform any deferred access checks.  */
6589       perform_deferred_access_checks ();
6590     }
6591
6592   /* Consume the `;'.  */
6593   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6594
6595  done:
6596   pop_deferring_access_checks ();
6597 }
6598
6599 /* Parse a decl-specifier-seq.
6600
6601    decl-specifier-seq:
6602      decl-specifier-seq [opt] decl-specifier
6603
6604    decl-specifier:
6605      storage-class-specifier
6606      type-specifier
6607      function-specifier
6608      friend
6609      typedef  
6610
6611    GNU Extension:
6612
6613    decl-specifier:
6614      attributes
6615
6616    Returns a TREE_LIST, giving the decl-specifiers in the order they
6617    appear in the source code.  The TREE_VALUE of each node is the
6618    decl-specifier.  For a keyword (such as `auto' or `friend'), the
6619    TREE_VALUE is simply the corresponding TREE_IDENTIFIER.  For the
6620    representation of a type-specifier, see cp_parser_type_specifier.  
6621
6622    If there are attributes, they will be stored in *ATTRIBUTES,
6623    represented as described above cp_parser_attributes.  
6624
6625    If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6626    appears, and the entity that will be a friend is not going to be a
6627    class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
6628    even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6629    friendship is granted might not be a class.  
6630
6631    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6632    flags:
6633
6634      1: one of the decl-specifiers is an elaborated-type-specifier
6635         (i.e., a type declaration)
6636      2: one of the decl-specifiers is an enum-specifier or a
6637         class-specifier (i.e., a type definition)
6638
6639    */
6640
6641 static tree
6642 cp_parser_decl_specifier_seq (cp_parser* parser, 
6643                               cp_parser_flags flags, 
6644                               tree* attributes,
6645                               int* declares_class_or_enum)
6646 {
6647   tree decl_specs = NULL_TREE;
6648   bool friend_p = false;
6649   bool constructor_possible_p = !parser->in_declarator_p;
6650   
6651   /* Assume no class or enumeration type is declared.  */
6652   *declares_class_or_enum = 0;
6653
6654   /* Assume there are no attributes.  */
6655   *attributes = NULL_TREE;
6656
6657   /* Keep reading specifiers until there are no more to read.  */
6658   while (true)
6659     {
6660       tree decl_spec = NULL_TREE;
6661       bool constructor_p;
6662       cp_token *token;
6663
6664       /* Peek at the next token.  */
6665       token = cp_lexer_peek_token (parser->lexer);
6666       /* Handle attributes.  */
6667       if (token->keyword == RID_ATTRIBUTE)
6668         {
6669           /* Parse the attributes.  */
6670           decl_spec = cp_parser_attributes_opt (parser);
6671           /* Add them to the list.  */
6672           *attributes = chainon (*attributes, decl_spec);
6673           continue;
6674         }
6675       /* If the next token is an appropriate keyword, we can simply
6676          add it to the list.  */
6677       switch (token->keyword)
6678         {
6679         case RID_FRIEND:
6680           /* decl-specifier:
6681                friend  */
6682           if (friend_p)
6683             error ("duplicate `friend'");
6684           else
6685             friend_p = true;
6686           /* The representation of the specifier is simply the
6687              appropriate TREE_IDENTIFIER node.  */
6688           decl_spec = token->value;
6689           /* Consume the token.  */
6690           cp_lexer_consume_token (parser->lexer);
6691           break;
6692
6693           /* function-specifier:
6694                inline
6695                virtual
6696                explicit  */
6697         case RID_INLINE:
6698         case RID_VIRTUAL:
6699         case RID_EXPLICIT:
6700           decl_spec = cp_parser_function_specifier_opt (parser);
6701           break;
6702           
6703           /* decl-specifier:
6704                typedef  */
6705         case RID_TYPEDEF:
6706           /* The representation of the specifier is simply the
6707              appropriate TREE_IDENTIFIER node.  */
6708           decl_spec = token->value;
6709           /* Consume the token.  */
6710           cp_lexer_consume_token (parser->lexer);
6711           /* A constructor declarator cannot appear in a typedef.  */
6712           constructor_possible_p = false;
6713           /* The "typedef" keyword can only occur in a declaration; we
6714              may as well commit at this point.  */
6715           cp_parser_commit_to_tentative_parse (parser);
6716           break;
6717
6718           /* storage-class-specifier:
6719                auto
6720                register
6721                static
6722                extern
6723                mutable  
6724
6725              GNU Extension:
6726                thread  */
6727         case RID_AUTO:
6728         case RID_REGISTER:
6729         case RID_STATIC:
6730         case RID_EXTERN:
6731         case RID_MUTABLE:
6732         case RID_THREAD:
6733           decl_spec = cp_parser_storage_class_specifier_opt (parser);
6734           break;
6735           
6736         default:
6737           break;
6738         }
6739
6740       /* Constructors are a special case.  The `S' in `S()' is not a
6741          decl-specifier; it is the beginning of the declarator.  */
6742       constructor_p = (!decl_spec 
6743                        && constructor_possible_p
6744                        && cp_parser_constructor_declarator_p (parser,
6745                                                               friend_p));
6746
6747       /* If we don't have a DECL_SPEC yet, then we must be looking at
6748          a type-specifier.  */
6749       if (!decl_spec && !constructor_p)
6750         {
6751           int decl_spec_declares_class_or_enum;
6752           bool is_cv_qualifier;
6753
6754           decl_spec
6755             = cp_parser_type_specifier (parser, flags,
6756                                         friend_p,
6757                                         /*is_declaration=*/true,
6758                                         &decl_spec_declares_class_or_enum,
6759                                         &is_cv_qualifier);
6760
6761           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6762
6763           /* If this type-specifier referenced a user-defined type
6764              (a typedef, class-name, etc.), then we can't allow any
6765              more such type-specifiers henceforth.
6766
6767              [dcl.spec]
6768
6769              The longest sequence of decl-specifiers that could
6770              possibly be a type name is taken as the
6771              decl-specifier-seq of a declaration.  The sequence shall
6772              be self-consistent as described below.
6773
6774              [dcl.type]
6775
6776              As a general rule, at most one type-specifier is allowed
6777              in the complete decl-specifier-seq of a declaration.  The
6778              only exceptions are the following:
6779
6780              -- const or volatile can be combined with any other
6781                 type-specifier. 
6782
6783              -- signed or unsigned can be combined with char, long,
6784                 short, or int.
6785
6786              -- ..
6787
6788              Example:
6789
6790                typedef char* Pc;
6791                void g (const int Pc);
6792
6793              Here, Pc is *not* part of the decl-specifier seq; it's
6794              the declarator.  Therefore, once we see a type-specifier
6795              (other than a cv-qualifier), we forbid any additional
6796              user-defined types.  We *do* still allow things like `int
6797              int' to be considered a decl-specifier-seq, and issue the
6798              error message later.  */
6799           if (decl_spec && !is_cv_qualifier)
6800             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6801           /* A constructor declarator cannot follow a type-specifier.  */
6802           if (decl_spec)
6803             constructor_possible_p = false;
6804         }
6805
6806       /* If we still do not have a DECL_SPEC, then there are no more
6807          decl-specifiers.  */
6808       if (!decl_spec)
6809         {
6810           /* Issue an error message, unless the entire construct was
6811              optional.  */
6812           if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6813             {
6814               cp_parser_error (parser, "expected decl specifier");
6815               return error_mark_node;
6816             }
6817
6818           break;
6819         }
6820
6821       /* Add the DECL_SPEC to the list of specifiers.  */
6822       if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6823         decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6824
6825       /* After we see one decl-specifier, further decl-specifiers are
6826          always optional.  */
6827       flags |= CP_PARSER_FLAGS_OPTIONAL;
6828     }
6829
6830   /* Don't allow a friend specifier with a class definition.  */
6831   if (friend_p && (*declares_class_or_enum & 2))
6832     error ("class definition may not be declared a friend");
6833
6834   /* We have built up the DECL_SPECS in reverse order.  Return them in
6835      the correct order.  */
6836   return nreverse (decl_specs);
6837 }
6838
6839 /* Parse an (optional) storage-class-specifier. 
6840
6841    storage-class-specifier:
6842      auto
6843      register
6844      static
6845      extern
6846      mutable  
6847
6848    GNU Extension:
6849
6850    storage-class-specifier:
6851      thread
6852
6853    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6854    
6855 static tree
6856 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6857 {
6858   switch (cp_lexer_peek_token (parser->lexer)->keyword)
6859     {
6860     case RID_AUTO:
6861     case RID_REGISTER:
6862     case RID_STATIC:
6863     case RID_EXTERN:
6864     case RID_MUTABLE:
6865     case RID_THREAD:
6866       /* Consume the token.  */
6867       return cp_lexer_consume_token (parser->lexer)->value;
6868
6869     default:
6870       return NULL_TREE;
6871     }
6872 }
6873
6874 /* Parse an (optional) function-specifier. 
6875
6876    function-specifier:
6877      inline
6878      virtual
6879      explicit
6880
6881    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6882    
6883 static tree
6884 cp_parser_function_specifier_opt (cp_parser* parser)
6885 {
6886   switch (cp_lexer_peek_token (parser->lexer)->keyword)
6887     {
6888     case RID_INLINE:
6889     case RID_VIRTUAL:
6890     case RID_EXPLICIT:
6891       /* Consume the token.  */
6892       return cp_lexer_consume_token (parser->lexer)->value;
6893
6894     default:
6895       return NULL_TREE;
6896     }
6897 }
6898
6899 /* Parse a linkage-specification.
6900
6901    linkage-specification:
6902      extern string-literal { declaration-seq [opt] }
6903      extern string-literal declaration  */
6904
6905 static void
6906 cp_parser_linkage_specification (cp_parser* parser)
6907 {
6908   cp_token *token;
6909   tree linkage;
6910
6911   /* Look for the `extern' keyword.  */
6912   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6913
6914   /* Peek at the next token.  */
6915   token = cp_lexer_peek_token (parser->lexer);
6916   /* If it's not a string-literal, then there's a problem.  */
6917   if (!cp_parser_is_string_literal (token))
6918     {
6919       cp_parser_error (parser, "expected language-name");
6920       return;
6921     }
6922   /* Consume the token.  */
6923   cp_lexer_consume_token (parser->lexer);
6924
6925   /* Transform the literal into an identifier.  If the literal is a
6926      wide-character string, or contains embedded NULs, then we can't
6927      handle it as the user wants.  */
6928   if (token->type == CPP_WSTRING
6929       || (strlen (TREE_STRING_POINTER (token->value))
6930           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6931     {
6932       cp_parser_error (parser, "invalid linkage-specification");
6933       /* Assume C++ linkage.  */
6934       linkage = get_identifier ("c++");
6935     }
6936   /* If it's a simple string constant, things are easier.  */
6937   else
6938     linkage = get_identifier (TREE_STRING_POINTER (token->value));
6939
6940   /* We're now using the new linkage.  */
6941   push_lang_context (linkage);
6942
6943   /* If the next token is a `{', then we're using the first
6944      production.  */
6945   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6946     {
6947       /* Consume the `{' token.  */
6948       cp_lexer_consume_token (parser->lexer);
6949       /* Parse the declarations.  */
6950       cp_parser_declaration_seq_opt (parser);
6951       /* Look for the closing `}'.  */
6952       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6953     }
6954   /* Otherwise, there's just one declaration.  */
6955   else
6956     {
6957       bool saved_in_unbraced_linkage_specification_p;
6958
6959       saved_in_unbraced_linkage_specification_p 
6960         = parser->in_unbraced_linkage_specification_p;
6961       parser->in_unbraced_linkage_specification_p = true;
6962       have_extern_spec = true;
6963       cp_parser_declaration (parser);
6964       have_extern_spec = false;
6965       parser->in_unbraced_linkage_specification_p 
6966         = saved_in_unbraced_linkage_specification_p;
6967     }
6968
6969   /* We're done with the linkage-specification.  */
6970   pop_lang_context ();
6971 }
6972
6973 /* Special member functions [gram.special] */
6974
6975 /* Parse a conversion-function-id.
6976
6977    conversion-function-id:
6978      operator conversion-type-id  
6979
6980    Returns an IDENTIFIER_NODE representing the operator.  */
6981
6982 static tree 
6983 cp_parser_conversion_function_id (cp_parser* parser)
6984 {
6985   tree type;
6986   tree saved_scope;
6987   tree saved_qualifying_scope;
6988   tree saved_object_scope;
6989   bool pop_p = false;
6990
6991   /* Look for the `operator' token.  */
6992   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6993     return error_mark_node;
6994   /* When we parse the conversion-type-id, the current scope will be
6995      reset.  However, we need that information in able to look up the
6996      conversion function later, so we save it here.  */
6997   saved_scope = parser->scope;
6998   saved_qualifying_scope = parser->qualifying_scope;
6999   saved_object_scope = parser->object_scope;
7000   /* We must enter the scope of the class so that the names of
7001      entities declared within the class are available in the
7002      conversion-type-id.  For example, consider:
7003
7004        struct S { 
7005          typedef int I;
7006          operator I();
7007        };
7008
7009        S::operator I() { ... }
7010
7011      In order to see that `I' is a type-name in the definition, we
7012      must be in the scope of `S'.  */
7013   if (saved_scope)
7014     pop_p = push_scope (saved_scope);
7015   /* Parse the conversion-type-id.  */
7016   type = cp_parser_conversion_type_id (parser);
7017   /* Leave the scope of the class, if any.  */
7018   if (pop_p)
7019     pop_scope (saved_scope);
7020   /* Restore the saved scope.  */
7021   parser->scope = saved_scope;
7022   parser->qualifying_scope = saved_qualifying_scope;
7023   parser->object_scope = saved_object_scope;
7024   /* If the TYPE is invalid, indicate failure.  */
7025   if (type == error_mark_node)
7026     return error_mark_node;
7027   return mangle_conv_op_name_for_type (type);
7028 }
7029
7030 /* Parse a conversion-type-id:
7031
7032    conversion-type-id:
7033      type-specifier-seq conversion-declarator [opt]
7034
7035    Returns the TYPE specified.  */
7036
7037 static tree
7038 cp_parser_conversion_type_id (cp_parser* parser)
7039 {
7040   tree attributes;
7041   tree type_specifiers;
7042   tree declarator;
7043
7044   /* Parse the attributes.  */
7045   attributes = cp_parser_attributes_opt (parser);
7046   /* Parse the type-specifiers.  */
7047   type_specifiers = cp_parser_type_specifier_seq (parser);
7048   /* If that didn't work, stop.  */
7049   if (type_specifiers == error_mark_node)
7050     return error_mark_node;
7051   /* Parse the conversion-declarator.  */
7052   declarator = cp_parser_conversion_declarator_opt (parser);
7053
7054   return grokdeclarator (declarator, type_specifiers, TYPENAME,
7055                          /*initialized=*/0, &attributes);
7056 }
7057
7058 /* Parse an (optional) conversion-declarator.
7059
7060    conversion-declarator:
7061      ptr-operator conversion-declarator [opt]  
7062
7063    Returns a representation of the declarator.  See
7064    cp_parser_declarator for details.  */
7065
7066 static tree
7067 cp_parser_conversion_declarator_opt (cp_parser* parser)
7068 {
7069   enum tree_code code;
7070   tree class_type;
7071   tree cv_qualifier_seq;
7072
7073   /* We don't know if there's a ptr-operator next, or not.  */
7074   cp_parser_parse_tentatively (parser);
7075   /* Try the ptr-operator.  */
7076   code = cp_parser_ptr_operator (parser, &class_type, 
7077                                  &cv_qualifier_seq);
7078   /* If it worked, look for more conversion-declarators.  */
7079   if (cp_parser_parse_definitely (parser))
7080     {
7081      tree declarator;
7082
7083      /* Parse another optional declarator.  */
7084      declarator = cp_parser_conversion_declarator_opt (parser);
7085
7086      /* Create the representation of the declarator.  */
7087      if (code == INDIRECT_REF)
7088        declarator = make_pointer_declarator (cv_qualifier_seq,
7089                                              declarator);
7090      else
7091        declarator =  make_reference_declarator (cv_qualifier_seq,
7092                                                 declarator);
7093
7094      /* Handle the pointer-to-member case.  */
7095      if (class_type)
7096        declarator = build_nt (SCOPE_REF, class_type, declarator);
7097
7098      return declarator;
7099    }
7100
7101   return NULL_TREE;
7102 }
7103
7104 /* Parse an (optional) ctor-initializer.
7105
7106    ctor-initializer:
7107      : mem-initializer-list  
7108
7109    Returns TRUE iff the ctor-initializer was actually present.  */
7110
7111 static bool
7112 cp_parser_ctor_initializer_opt (cp_parser* parser)
7113 {
7114   /* If the next token is not a `:', then there is no
7115      ctor-initializer.  */
7116   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7117     {
7118       /* Do default initialization of any bases and members.  */
7119       if (DECL_CONSTRUCTOR_P (current_function_decl))
7120         finish_mem_initializers (NULL_TREE);
7121
7122       return false;
7123     }
7124
7125   /* Consume the `:' token.  */
7126   cp_lexer_consume_token (parser->lexer);
7127   /* And the mem-initializer-list.  */
7128   cp_parser_mem_initializer_list (parser);
7129
7130   return true;
7131 }
7132
7133 /* Parse a mem-initializer-list.
7134
7135    mem-initializer-list:
7136      mem-initializer
7137      mem-initializer , mem-initializer-list  */
7138
7139 static void
7140 cp_parser_mem_initializer_list (cp_parser* parser)
7141 {
7142   tree mem_initializer_list = NULL_TREE;
7143
7144   /* Let the semantic analysis code know that we are starting the
7145      mem-initializer-list.  */
7146   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7147     error ("only constructors take base initializers");
7148
7149   /* Loop through the list.  */
7150   while (true)
7151     {
7152       tree mem_initializer;
7153
7154       /* Parse the mem-initializer.  */
7155       mem_initializer = cp_parser_mem_initializer (parser);
7156       /* Add it to the list, unless it was erroneous.  */
7157       if (mem_initializer)
7158         {
7159           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7160           mem_initializer_list = mem_initializer;
7161         }
7162       /* If the next token is not a `,', we're done.  */
7163       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7164         break;
7165       /* Consume the `,' token.  */
7166       cp_lexer_consume_token (parser->lexer);
7167     }
7168
7169   /* Perform semantic analysis.  */
7170   if (DECL_CONSTRUCTOR_P (current_function_decl))
7171     finish_mem_initializers (mem_initializer_list);
7172 }
7173
7174 /* Parse a mem-initializer.
7175
7176    mem-initializer:
7177      mem-initializer-id ( expression-list [opt] )  
7178
7179    GNU extension:
7180   
7181    mem-initializer:
7182      ( expression-list [opt] )
7183
7184    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7185    class) or FIELD_DECL (for a non-static data member) to initialize;
7186    the TREE_VALUE is the expression-list.  */
7187
7188 static tree
7189 cp_parser_mem_initializer (cp_parser* parser)
7190 {
7191   tree mem_initializer_id;
7192   tree expression_list;
7193   tree member;
7194   
7195   /* Find out what is being initialized.  */
7196   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7197     {
7198       pedwarn ("anachronistic old-style base class initializer");
7199       mem_initializer_id = NULL_TREE;
7200     }
7201   else
7202     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7203   member = expand_member_init (mem_initializer_id);
7204   if (member && !DECL_P (member))
7205     in_base_initializer = 1;
7206
7207   expression_list 
7208     = cp_parser_parenthesized_expression_list (parser, false,
7209                                                /*non_constant_p=*/NULL);
7210   if (!expression_list)
7211     expression_list = void_type_node;
7212
7213   in_base_initializer = 0;
7214   
7215   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7216 }
7217
7218 /* Parse a mem-initializer-id.
7219
7220    mem-initializer-id:
7221      :: [opt] nested-name-specifier [opt] class-name
7222      identifier  
7223
7224    Returns a TYPE indicating the class to be initializer for the first
7225    production.  Returns an IDENTIFIER_NODE indicating the data member
7226    to be initialized for the second production.  */
7227
7228 static tree
7229 cp_parser_mem_initializer_id (cp_parser* parser)
7230 {
7231   bool global_scope_p;
7232   bool nested_name_specifier_p;
7233   bool template_p = false;
7234   tree id;
7235
7236   /* `typename' is not allowed in this context ([temp.res]).  */
7237   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7238     {
7239       error ("keyword `typename' not allowed in this context (a qualified "
7240              "member initializer is implicitly a type)");
7241       cp_lexer_consume_token (parser->lexer);
7242     }
7243   /* Look for the optional `::' operator.  */
7244   global_scope_p 
7245     = (cp_parser_global_scope_opt (parser, 
7246                                    /*current_scope_valid_p=*/false) 
7247        != NULL_TREE);
7248   /* Look for the optional nested-name-specifier.  The simplest way to
7249      implement:
7250
7251        [temp.res]
7252
7253        The keyword `typename' is not permitted in a base-specifier or
7254        mem-initializer; in these contexts a qualified name that
7255        depends on a template-parameter is implicitly assumed to be a
7256        type name.
7257
7258      is to assume that we have seen the `typename' keyword at this
7259      point.  */
7260   nested_name_specifier_p 
7261     = (cp_parser_nested_name_specifier_opt (parser,
7262                                             /*typename_keyword_p=*/true,
7263                                             /*check_dependency_p=*/true,
7264                                             /*type_p=*/true,
7265                                             /*is_declaration=*/true)
7266        != NULL_TREE);
7267   if (nested_name_specifier_p)
7268     template_p = cp_parser_optional_template_keyword (parser);
7269   /* If there is a `::' operator or a nested-name-specifier, then we
7270      are definitely looking for a class-name.  */
7271   if (global_scope_p || nested_name_specifier_p)
7272     return cp_parser_class_name (parser,
7273                                  /*typename_keyword_p=*/true,
7274                                  /*template_keyword_p=*/template_p,
7275                                  /*type_p=*/false,
7276                                  /*check_dependency_p=*/true,
7277                                  /*class_head_p=*/false,
7278                                  /*is_declaration=*/true);
7279   /* Otherwise, we could also be looking for an ordinary identifier.  */
7280   cp_parser_parse_tentatively (parser);
7281   /* Try a class-name.  */
7282   id = cp_parser_class_name (parser, 
7283                              /*typename_keyword_p=*/true,
7284                              /*template_keyword_p=*/false,
7285                              /*type_p=*/false,
7286                              /*check_dependency_p=*/true,
7287                              /*class_head_p=*/false,
7288                              /*is_declaration=*/true);
7289   /* If we found one, we're done.  */
7290   if (cp_parser_parse_definitely (parser))
7291     return id;
7292   /* Otherwise, look for an ordinary identifier.  */
7293   return cp_parser_identifier (parser);
7294 }
7295
7296 /* Overloading [gram.over] */
7297
7298 /* Parse an operator-function-id.
7299
7300    operator-function-id:
7301      operator operator  
7302
7303    Returns an IDENTIFIER_NODE for the operator which is a
7304    human-readable spelling of the identifier, e.g., `operator +'.  */
7305
7306 static tree 
7307 cp_parser_operator_function_id (cp_parser* parser)
7308 {
7309   /* Look for the `operator' keyword.  */
7310   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7311     return error_mark_node;
7312   /* And then the name of the operator itself.  */
7313   return cp_parser_operator (parser);
7314 }
7315
7316 /* Parse an operator.
7317
7318    operator:
7319      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7320      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7321      || ++ -- , ->* -> () []
7322
7323    GNU Extensions:
7324    
7325    operator:
7326      <? >? <?= >?=
7327
7328    Returns an IDENTIFIER_NODE for the operator which is a
7329    human-readable spelling of the identifier, e.g., `operator +'.  */
7330    
7331 static tree
7332 cp_parser_operator (cp_parser* parser)
7333 {
7334   tree id = NULL_TREE;
7335   cp_token *token;
7336
7337   /* Peek at the next token.  */
7338   token = cp_lexer_peek_token (parser->lexer);
7339   /* Figure out which operator we have.  */
7340   switch (token->type)
7341     {
7342     case CPP_KEYWORD:
7343       {
7344         enum tree_code op;
7345
7346         /* The keyword should be either `new' or `delete'.  */
7347         if (token->keyword == RID_NEW)
7348           op = NEW_EXPR;
7349         else if (token->keyword == RID_DELETE)
7350           op = DELETE_EXPR;
7351         else
7352           break;
7353
7354         /* Consume the `new' or `delete' token.  */
7355         cp_lexer_consume_token (parser->lexer);
7356
7357         /* Peek at the next token.  */
7358         token = cp_lexer_peek_token (parser->lexer);
7359         /* If it's a `[' token then this is the array variant of the
7360            operator.  */
7361         if (token->type == CPP_OPEN_SQUARE)
7362           {
7363             /* Consume the `[' token.  */
7364             cp_lexer_consume_token (parser->lexer);
7365             /* Look for the `]' token.  */
7366             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7367             id = ansi_opname (op == NEW_EXPR 
7368                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7369           }
7370         /* Otherwise, we have the non-array variant.  */
7371         else
7372           id = ansi_opname (op);
7373
7374         return id;
7375       }
7376
7377     case CPP_PLUS:
7378       id = ansi_opname (PLUS_EXPR);
7379       break;
7380
7381     case CPP_MINUS:
7382       id = ansi_opname (MINUS_EXPR);
7383       break;
7384
7385     case CPP_MULT:
7386       id = ansi_opname (MULT_EXPR);
7387       break;
7388
7389     case CPP_DIV:
7390       id = ansi_opname (TRUNC_DIV_EXPR);
7391       break;
7392
7393     case CPP_MOD:
7394       id = ansi_opname (TRUNC_MOD_EXPR);
7395       break;
7396
7397     case CPP_XOR:
7398       id = ansi_opname (BIT_XOR_EXPR);
7399       break;
7400
7401     case CPP_AND:
7402       id = ansi_opname (BIT_AND_EXPR);
7403       break;
7404
7405     case CPP_OR:
7406       id = ansi_opname (BIT_IOR_EXPR);
7407       break;
7408
7409     case CPP_COMPL:
7410       id = ansi_opname (BIT_NOT_EXPR);
7411       break;
7412       
7413     case CPP_NOT:
7414       id = ansi_opname (TRUTH_NOT_EXPR);
7415       break;
7416
7417     case CPP_EQ:
7418       id = ansi_assopname (NOP_EXPR);
7419       break;
7420
7421     case CPP_LESS:
7422       id = ansi_opname (LT_EXPR);
7423       break;
7424
7425     case CPP_GREATER:
7426       id = ansi_opname (GT_EXPR);
7427       break;
7428
7429     case CPP_PLUS_EQ:
7430       id = ansi_assopname (PLUS_EXPR);
7431       break;
7432
7433     case CPP_MINUS_EQ:
7434       id = ansi_assopname (MINUS_EXPR);
7435       break;
7436
7437     case CPP_MULT_EQ:
7438       id = ansi_assopname (MULT_EXPR);
7439       break;
7440
7441     case CPP_DIV_EQ:
7442       id = ansi_assopname (TRUNC_DIV_EXPR);
7443       break;
7444
7445     case CPP_MOD_EQ:
7446       id = ansi_assopname (TRUNC_MOD_EXPR);
7447       break;
7448
7449     case CPP_XOR_EQ:
7450       id = ansi_assopname (BIT_XOR_EXPR);
7451       break;
7452
7453     case CPP_AND_EQ:
7454       id = ansi_assopname (BIT_AND_EXPR);
7455       break;
7456
7457     case CPP_OR_EQ:
7458       id = ansi_assopname (BIT_IOR_EXPR);
7459       break;
7460
7461     case CPP_LSHIFT:
7462       id = ansi_opname (LSHIFT_EXPR);
7463       break;
7464
7465     case CPP_RSHIFT:
7466       id = ansi_opname (RSHIFT_EXPR);
7467       break;
7468
7469     case CPP_LSHIFT_EQ:
7470       id = ansi_assopname (LSHIFT_EXPR);
7471       break;
7472
7473     case CPP_RSHIFT_EQ:
7474       id = ansi_assopname (RSHIFT_EXPR);
7475       break;
7476
7477     case CPP_EQ_EQ:
7478       id = ansi_opname (EQ_EXPR);
7479       break;
7480
7481     case CPP_NOT_EQ:
7482       id = ansi_opname (NE_EXPR);
7483       break;
7484
7485     case CPP_LESS_EQ:
7486       id = ansi_opname (LE_EXPR);
7487       break;
7488
7489     case CPP_GREATER_EQ:
7490       id = ansi_opname (GE_EXPR);
7491       break;
7492
7493     case CPP_AND_AND:
7494       id = ansi_opname (TRUTH_ANDIF_EXPR);
7495       break;
7496
7497     case CPP_OR_OR:
7498       id = ansi_opname (TRUTH_ORIF_EXPR);
7499       break;
7500       
7501     case CPP_PLUS_PLUS:
7502       id = ansi_opname (POSTINCREMENT_EXPR);
7503       break;
7504
7505     case CPP_MINUS_MINUS:
7506       id = ansi_opname (PREDECREMENT_EXPR);
7507       break;
7508
7509     case CPP_COMMA:
7510       id = ansi_opname (COMPOUND_EXPR);
7511       break;
7512
7513     case CPP_DEREF_STAR:
7514       id = ansi_opname (MEMBER_REF);
7515       break;
7516
7517     case CPP_DEREF:
7518       id = ansi_opname (COMPONENT_REF);
7519       break;
7520
7521     case CPP_OPEN_PAREN:
7522       /* Consume the `('.  */
7523       cp_lexer_consume_token (parser->lexer);
7524       /* Look for the matching `)'.  */
7525       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7526       return ansi_opname (CALL_EXPR);
7527
7528     case CPP_OPEN_SQUARE:
7529       /* Consume the `['.  */
7530       cp_lexer_consume_token (parser->lexer);
7531       /* Look for the matching `]'.  */
7532       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7533       return ansi_opname (ARRAY_REF);
7534
7535       /* Extensions.  */
7536     case CPP_MIN:
7537       id = ansi_opname (MIN_EXPR);
7538       break;
7539
7540     case CPP_MAX:
7541       id = ansi_opname (MAX_EXPR);
7542       break;
7543
7544     case CPP_MIN_EQ:
7545       id = ansi_assopname (MIN_EXPR);
7546       break;
7547
7548     case CPP_MAX_EQ:
7549       id = ansi_assopname (MAX_EXPR);
7550       break;
7551
7552     default:
7553       /* Anything else is an error.  */
7554       break;
7555     }
7556
7557   /* If we have selected an identifier, we need to consume the
7558      operator token.  */
7559   if (id)
7560     cp_lexer_consume_token (parser->lexer);
7561   /* Otherwise, no valid operator name was present.  */
7562   else
7563     {
7564       cp_parser_error (parser, "expected operator");
7565       id = error_mark_node;
7566     }
7567
7568   return id;
7569 }
7570
7571 /* Parse a template-declaration.
7572
7573    template-declaration:
7574      export [opt] template < template-parameter-list > declaration  
7575
7576    If MEMBER_P is TRUE, this template-declaration occurs within a
7577    class-specifier.  
7578
7579    The grammar rule given by the standard isn't correct.  What
7580    is really meant is:
7581
7582    template-declaration:
7583      export [opt] template-parameter-list-seq 
7584        decl-specifier-seq [opt] init-declarator [opt] ;
7585      export [opt] template-parameter-list-seq 
7586        function-definition
7587
7588    template-parameter-list-seq:
7589      template-parameter-list-seq [opt]
7590      template < template-parameter-list >  */
7591
7592 static void
7593 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7594 {
7595   /* Check for `export'.  */
7596   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7597     {
7598       /* Consume the `export' token.  */
7599       cp_lexer_consume_token (parser->lexer);
7600       /* Warn that we do not support `export'.  */
7601       warning ("keyword `export' not implemented, and will be ignored");
7602     }
7603
7604   cp_parser_template_declaration_after_export (parser, member_p);
7605 }
7606
7607 /* Parse a template-parameter-list.
7608
7609    template-parameter-list:
7610      template-parameter
7611      template-parameter-list , template-parameter
7612
7613    Returns a TREE_LIST.  Each node represents a template parameter.
7614    The nodes are connected via their TREE_CHAINs.  */
7615
7616 static tree
7617 cp_parser_template_parameter_list (cp_parser* parser)
7618 {
7619   tree parameter_list = NULL_TREE;
7620
7621   while (true)
7622     {
7623       tree parameter;
7624       cp_token *token;
7625
7626       /* Parse the template-parameter.  */
7627       parameter = cp_parser_template_parameter (parser);
7628       /* Add it to the list.  */
7629       parameter_list = process_template_parm (parameter_list,
7630                                               parameter);
7631
7632       /* Peek at the next token.  */
7633       token = cp_lexer_peek_token (parser->lexer);
7634       /* If it's not a `,', we're done.  */
7635       if (token->type != CPP_COMMA)
7636         break;
7637       /* Otherwise, consume the `,' token.  */
7638       cp_lexer_consume_token (parser->lexer);
7639     }
7640
7641   return parameter_list;
7642 }
7643
7644 /* Parse a template-parameter.
7645
7646    template-parameter:
7647      type-parameter
7648      parameter-declaration
7649
7650    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
7651    TREE_PURPOSE is the default value, if any.  */
7652
7653 static tree
7654 cp_parser_template_parameter (cp_parser* parser)
7655 {
7656   cp_token *token;
7657
7658   /* Peek at the next token.  */
7659   token = cp_lexer_peek_token (parser->lexer);
7660   /* If it is `class' or `template', we have a type-parameter.  */
7661   if (token->keyword == RID_TEMPLATE)
7662     return cp_parser_type_parameter (parser);
7663   /* If it is `class' or `typename' we do not know yet whether it is a
7664      type parameter or a non-type parameter.  Consider:
7665
7666        template <typename T, typename T::X X> ...
7667
7668      or:
7669      
7670        template <class C, class D*> ...
7671
7672      Here, the first parameter is a type parameter, and the second is
7673      a non-type parameter.  We can tell by looking at the token after
7674      the identifier -- if it is a `,', `=', or `>' then we have a type
7675      parameter.  */
7676   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7677     {
7678       /* Peek at the token after `class' or `typename'.  */
7679       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7680       /* If it's an identifier, skip it.  */
7681       if (token->type == CPP_NAME)
7682         token = cp_lexer_peek_nth_token (parser->lexer, 3);
7683       /* Now, see if the token looks like the end of a template
7684          parameter.  */
7685       if (token->type == CPP_COMMA 
7686           || token->type == CPP_EQ
7687           || token->type == CPP_GREATER)
7688         return cp_parser_type_parameter (parser);
7689     }
7690
7691   /* Otherwise, it is a non-type parameter.  
7692
7693      [temp.param]
7694
7695      When parsing a default template-argument for a non-type
7696      template-parameter, the first non-nested `>' is taken as the end
7697      of the template parameter-list rather than a greater-than
7698      operator.  */
7699   return 
7700     cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7701                                      /*parenthesized_p=*/NULL);
7702 }
7703
7704 /* Parse a type-parameter.
7705
7706    type-parameter:
7707      class identifier [opt]
7708      class identifier [opt] = type-id
7709      typename identifier [opt]
7710      typename identifier [opt] = type-id
7711      template < template-parameter-list > class identifier [opt]
7712      template < template-parameter-list > class identifier [opt] 
7713        = id-expression  
7714
7715    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
7716    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
7717    the declaration of the parameter.  */
7718
7719 static tree
7720 cp_parser_type_parameter (cp_parser* parser)
7721 {
7722   cp_token *token;
7723   tree parameter;
7724
7725   /* Look for a keyword to tell us what kind of parameter this is.  */
7726   token = cp_parser_require (parser, CPP_KEYWORD, 
7727                              "`class', `typename', or `template'");
7728   if (!token)
7729     return error_mark_node;
7730
7731   switch (token->keyword)
7732     {
7733     case RID_CLASS:
7734     case RID_TYPENAME:
7735       {
7736         tree identifier;
7737         tree default_argument;
7738
7739         /* If the next token is an identifier, then it names the
7740            parameter.  */
7741         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7742           identifier = cp_parser_identifier (parser);
7743         else
7744           identifier = NULL_TREE;
7745
7746         /* Create the parameter.  */
7747         parameter = finish_template_type_parm (class_type_node, identifier);
7748
7749         /* If the next token is an `=', we have a default argument.  */
7750         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7751           {
7752             /* Consume the `=' token.  */
7753             cp_lexer_consume_token (parser->lexer);
7754             /* Parse the default-argument.  */
7755             default_argument = cp_parser_type_id (parser);
7756           }
7757         else
7758           default_argument = NULL_TREE;
7759
7760         /* Create the combined representation of the parameter and the
7761            default argument.  */
7762         parameter = build_tree_list (default_argument, parameter);
7763       }
7764       break;
7765
7766     case RID_TEMPLATE:
7767       {
7768         tree parameter_list;
7769         tree identifier;
7770         tree default_argument;
7771
7772         /* Look for the `<'.  */
7773         cp_parser_require (parser, CPP_LESS, "`<'");
7774         /* Parse the template-parameter-list.  */
7775         begin_template_parm_list ();
7776         parameter_list 
7777           = cp_parser_template_parameter_list (parser);
7778         parameter_list = end_template_parm_list (parameter_list);
7779         /* Look for the `>'.  */
7780         cp_parser_require (parser, CPP_GREATER, "`>'");
7781         /* Look for the `class' keyword.  */
7782         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7783         /* If the next token is an `=', then there is a
7784            default-argument.  If the next token is a `>', we are at
7785            the end of the parameter-list.  If the next token is a `,',
7786            then we are at the end of this parameter.  */
7787         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7788             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7789             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7790           identifier = cp_parser_identifier (parser);
7791         else
7792           identifier = NULL_TREE;
7793         /* Create the template parameter.  */
7794         parameter = finish_template_template_parm (class_type_node,
7795                                                    identifier);
7796                                                    
7797         /* If the next token is an `=', then there is a
7798            default-argument.  */
7799         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7800           {
7801             bool is_template;
7802
7803             /* Consume the `='.  */
7804             cp_lexer_consume_token (parser->lexer);
7805             /* Parse the id-expression.  */
7806             default_argument 
7807               = cp_parser_id_expression (parser,
7808                                          /*template_keyword_p=*/false,
7809                                          /*check_dependency_p=*/true,
7810                                          /*template_p=*/&is_template,
7811                                          /*declarator_p=*/false);
7812             if (TREE_CODE (default_argument) == TYPE_DECL)
7813               /* If the id-expression was a template-id that refers to
7814                  a template-class, we already have the declaration here,
7815                  so no further lookup is needed.  */
7816                  ;
7817             else
7818               /* Look up the name.  */
7819               default_argument 
7820                 = cp_parser_lookup_name (parser, default_argument,
7821                                         /*is_type=*/false,
7822                                         /*is_template=*/is_template,
7823                                         /*is_namespace=*/false,
7824                                         /*check_dependency=*/true);
7825             /* See if the default argument is valid.  */
7826             default_argument
7827               = check_template_template_default_arg (default_argument);
7828           }
7829         else
7830           default_argument = NULL_TREE;
7831
7832         /* Create the combined representation of the parameter and the
7833            default argument.  */
7834         parameter =  build_tree_list (default_argument, parameter);
7835       }
7836       break;
7837
7838     default:
7839       /* Anything else is an error.  */
7840       cp_parser_error (parser,
7841                        "expected `class', `typename', or `template'");
7842       parameter = error_mark_node;
7843     }
7844   
7845   return parameter;
7846 }
7847
7848 /* Parse a template-id.
7849
7850    template-id:
7851      template-name < template-argument-list [opt] >
7852
7853    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7854    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
7855    returned.  Otherwise, if the template-name names a function, or set
7856    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
7857    names a class, returns a TYPE_DECL for the specialization.  
7858
7859    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7860    uninstantiated templates.  */
7861
7862 static tree
7863 cp_parser_template_id (cp_parser *parser, 
7864                        bool template_keyword_p, 
7865                        bool check_dependency_p,
7866                        bool is_declaration)
7867 {
7868   tree template;
7869   tree arguments;
7870   tree template_id;
7871   ptrdiff_t start_of_id;
7872   tree access_check = NULL_TREE;
7873   cp_token *next_token, *next_token_2;
7874   bool is_identifier;
7875
7876   /* If the next token corresponds to a template-id, there is no need
7877      to reparse it.  */
7878   next_token = cp_lexer_peek_token (parser->lexer);
7879   if (next_token->type == CPP_TEMPLATE_ID)
7880     {
7881       tree value;
7882       tree check;
7883
7884       /* Get the stored value.  */
7885       value = cp_lexer_consume_token (parser->lexer)->value;
7886       /* Perform any access checks that were deferred.  */
7887       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7888         perform_or_defer_access_check (TREE_PURPOSE (check),
7889                                        TREE_VALUE (check));
7890       /* Return the stored value.  */
7891       return TREE_VALUE (value);
7892     }
7893
7894   /* Avoid performing name lookup if there is no possibility of
7895      finding a template-id.  */
7896   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7897       || (next_token->type == CPP_NAME
7898           && !cp_parser_nth_token_starts_template_argument_list_p 
7899                (parser, 2)))
7900     {
7901       cp_parser_error (parser, "expected template-id");
7902       return error_mark_node;
7903     }
7904
7905   /* Remember where the template-id starts.  */
7906   if (cp_parser_parsing_tentatively (parser)
7907       && !cp_parser_committed_to_tentative_parse (parser))
7908     {
7909       next_token = cp_lexer_peek_token (parser->lexer);
7910       start_of_id = cp_lexer_token_difference (parser->lexer,
7911                                                parser->lexer->first_token,
7912                                                next_token);
7913     }
7914   else
7915     start_of_id = -1;
7916
7917   push_deferring_access_checks (dk_deferred);
7918
7919   /* Parse the template-name.  */
7920   is_identifier = false;
7921   template = cp_parser_template_name (parser, template_keyword_p,
7922                                       check_dependency_p,
7923                                       is_declaration,
7924                                       &is_identifier);
7925   if (template == error_mark_node || is_identifier)
7926     {
7927       pop_deferring_access_checks ();
7928       return template;
7929     }
7930
7931   /* If we find the sequence `[:' after a template-name, it's probably 
7932      a digraph-typo for `< ::'. Substitute the tokens and check if we can
7933      parse correctly the argument list.  */
7934   next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7935   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7936   if (next_token->type == CPP_OPEN_SQUARE 
7937       && next_token->flags & DIGRAPH
7938       && next_token_2->type == CPP_COLON 
7939       && !(next_token_2->flags & PREV_WHITE))
7940     {
7941       cp_parser_parse_tentatively (parser);
7942       /* Change `:' into `::'.  */
7943       next_token_2->type = CPP_SCOPE;
7944       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7945          CPP_LESS.  */
7946       cp_lexer_consume_token (parser->lexer);
7947       /* Parse the arguments.  */
7948       arguments = cp_parser_enclosed_template_argument_list (parser);
7949       if (!cp_parser_parse_definitely (parser))
7950         {
7951           /* If we couldn't parse an argument list, then we revert our changes
7952              and return simply an error. Maybe this is not a template-id
7953              after all.  */
7954           next_token_2->type = CPP_COLON;
7955           cp_parser_error (parser, "expected `<'");
7956           pop_deferring_access_checks ();
7957           return error_mark_node;
7958         }
7959       /* Otherwise, emit an error about the invalid digraph, but continue
7960          parsing because we got our argument list.  */
7961       pedwarn ("`<::' cannot begin a template-argument list");
7962       inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7963               "between `<' and `::'");
7964       if (!flag_permissive)
7965         {
7966           static bool hint;
7967           if (!hint)
7968             {
7969               inform ("(if you use `-fpermissive' G++ will accept your code)");
7970               hint = true;
7971             }
7972         }
7973     }
7974   else
7975     {
7976       /* Look for the `<' that starts the template-argument-list.  */
7977       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7978         {
7979           pop_deferring_access_checks ();
7980           return error_mark_node;
7981         }
7982       /* Parse the arguments.  */
7983       arguments = cp_parser_enclosed_template_argument_list (parser);
7984     }
7985
7986   /* Build a representation of the specialization.  */
7987   if (TREE_CODE (template) == IDENTIFIER_NODE)
7988     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7989   else if (DECL_CLASS_TEMPLATE_P (template)
7990            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7991     template_id 
7992       = finish_template_type (template, arguments, 
7993                               cp_lexer_next_token_is (parser->lexer, 
7994                                                       CPP_SCOPE));
7995   else
7996     {
7997       /* If it's not a class-template or a template-template, it should be
7998          a function-template.  */
7999       my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8000                            || TREE_CODE (template) == OVERLOAD
8001                            || BASELINK_P (template)),
8002                           20010716);
8003       
8004       template_id = lookup_template_function (template, arguments);
8005     }
8006   
8007   /* Retrieve any deferred checks.  Do not pop this access checks yet
8008      so the memory will not be reclaimed during token replacing below.  */
8009   access_check = get_deferred_access_checks ();
8010
8011   /* If parsing tentatively, replace the sequence of tokens that makes
8012      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8013      should we re-parse the token stream, we will not have to repeat
8014      the effort required to do the parse, nor will we issue duplicate
8015      error messages about problems during instantiation of the
8016      template.  */
8017   if (start_of_id >= 0)
8018     {
8019       cp_token *token;
8020
8021       /* Find the token that corresponds to the start of the
8022          template-id.  */
8023       token = cp_lexer_advance_token (parser->lexer, 
8024                                       parser->lexer->first_token,
8025                                       start_of_id);
8026
8027       /* Reset the contents of the START_OF_ID token.  */
8028       token->type = CPP_TEMPLATE_ID;
8029       token->value = build_tree_list (access_check, template_id);
8030       token->keyword = RID_MAX;
8031       /* Purge all subsequent tokens.  */
8032       cp_lexer_purge_tokens_after (parser->lexer, token);
8033     }
8034
8035   pop_deferring_access_checks ();
8036   return template_id;
8037 }
8038
8039 /* Parse a template-name.
8040
8041    template-name:
8042      identifier
8043  
8044    The standard should actually say:
8045
8046    template-name:
8047      identifier
8048      operator-function-id
8049
8050    A defect report has been filed about this issue.
8051
8052    A conversion-function-id cannot be a template name because they cannot
8053    be part of a template-id. In fact, looking at this code:
8054
8055    a.operator K<int>()
8056
8057    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8058    It is impossible to call a templated conversion-function-id with an 
8059    explicit argument list, since the only allowed template parameter is
8060    the type to which it is converting.
8061
8062    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8063    `template' keyword, in a construction like:
8064
8065      T::template f<3>()
8066
8067    In that case `f' is taken to be a template-name, even though there
8068    is no way of knowing for sure.
8069
8070    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8071    name refers to a set of overloaded functions, at least one of which
8072    is a template, or an IDENTIFIER_NODE with the name of the template,
8073    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8074    names are looked up inside uninstantiated templates.  */
8075
8076 static tree
8077 cp_parser_template_name (cp_parser* parser, 
8078                          bool template_keyword_p, 
8079                          bool check_dependency_p,
8080                          bool is_declaration,
8081                          bool *is_identifier)
8082 {
8083   tree identifier;
8084   tree decl;
8085   tree fns;
8086
8087   /* If the next token is `operator', then we have either an
8088      operator-function-id or a conversion-function-id.  */
8089   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8090     {
8091       /* We don't know whether we're looking at an
8092          operator-function-id or a conversion-function-id.  */
8093       cp_parser_parse_tentatively (parser);
8094       /* Try an operator-function-id.  */
8095       identifier = cp_parser_operator_function_id (parser);
8096       /* If that didn't work, try a conversion-function-id.  */
8097       if (!cp_parser_parse_definitely (parser))
8098         {
8099           cp_parser_error (parser, "expected template-name");
8100           return error_mark_node;
8101         }
8102     }
8103   /* Look for the identifier.  */
8104   else
8105     identifier = cp_parser_identifier (parser);
8106   
8107   /* If we didn't find an identifier, we don't have a template-id.  */
8108   if (identifier == error_mark_node)
8109     return error_mark_node;
8110
8111   /* If the name immediately followed the `template' keyword, then it
8112      is a template-name.  However, if the next token is not `<', then
8113      we do not treat it as a template-name, since it is not being used
8114      as part of a template-id.  This enables us to handle constructs
8115      like:
8116
8117        template <typename T> struct S { S(); };
8118        template <typename T> S<T>::S();
8119
8120      correctly.  We would treat `S' as a template -- if it were `S<T>'
8121      -- but we do not if there is no `<'.  */
8122
8123   if (processing_template_decl
8124       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8125     {
8126       /* In a declaration, in a dependent context, we pretend that the
8127          "template" keyword was present in order to improve error
8128          recovery.  For example, given:
8129          
8130            template <typename T> void f(T::X<int>);
8131          
8132          we want to treat "X<int>" as a template-id.  */
8133       if (is_declaration 
8134           && !template_keyword_p 
8135           && parser->scope && TYPE_P (parser->scope)
8136           && check_dependency_p
8137           && dependent_type_p (parser->scope)
8138           /* Do not do this for dtors (or ctors), since they never
8139              need the template keyword before their name.  */
8140           && !constructor_name_p (identifier, parser->scope))
8141         {
8142           ptrdiff_t start;
8143           cp_token* token;
8144           /* Explain what went wrong.  */
8145           error ("non-template `%D' used as template", identifier);
8146           inform ("use `%T::template %D' to indicate that it is a template",
8147                   parser->scope, identifier);
8148           /* If parsing tentatively, find the location of the "<"
8149              token.  */
8150           if (cp_parser_parsing_tentatively (parser)
8151               && !cp_parser_committed_to_tentative_parse (parser))
8152             {
8153               cp_parser_simulate_error (parser);
8154               token = cp_lexer_peek_token (parser->lexer);
8155               token = cp_lexer_prev_token (parser->lexer, token);
8156               start = cp_lexer_token_difference (parser->lexer,
8157                                                  parser->lexer->first_token,
8158                                                  token);
8159             }
8160           else
8161             start = -1;
8162           /* Parse the template arguments so that we can issue error
8163              messages about them.  */
8164           cp_lexer_consume_token (parser->lexer);
8165           cp_parser_enclosed_template_argument_list (parser);
8166           /* Skip tokens until we find a good place from which to
8167              continue parsing.  */
8168           cp_parser_skip_to_closing_parenthesis (parser,
8169                                                  /*recovering=*/true,
8170                                                  /*or_comma=*/true,
8171                                                  /*consume_paren=*/false);
8172           /* If parsing tentatively, permanently remove the
8173              template argument list.  That will prevent duplicate
8174              error messages from being issued about the missing
8175              "template" keyword.  */
8176           if (start >= 0)
8177             {
8178               token = cp_lexer_advance_token (parser->lexer,
8179                                               parser->lexer->first_token,
8180                                               start);
8181               cp_lexer_purge_tokens_after (parser->lexer, token);
8182             }
8183           if (is_identifier)
8184             *is_identifier = true;
8185           return identifier;
8186         }
8187
8188       /* If the "template" keyword is present, then there is generally
8189          no point in doing name-lookup, so we just return IDENTIFIER.
8190          But, if the qualifying scope is non-dependent then we can
8191          (and must) do name-lookup normally.  */
8192       if (template_keyword_p
8193           && (!parser->scope
8194               || (TYPE_P (parser->scope) 
8195                   && dependent_type_p (parser->scope))))
8196         return identifier;
8197     }
8198
8199   /* Look up the name.  */
8200   decl = cp_parser_lookup_name (parser, identifier,
8201                                 /*is_type=*/false,
8202                                 /*is_template=*/false,
8203                                 /*is_namespace=*/false,
8204                                 check_dependency_p);
8205   decl = maybe_get_template_decl_from_type_decl (decl);
8206
8207   /* If DECL is a template, then the name was a template-name.  */
8208   if (TREE_CODE (decl) == TEMPLATE_DECL)
8209     ;
8210   else 
8211     {
8212       /* The standard does not explicitly indicate whether a name that
8213          names a set of overloaded declarations, some of which are
8214          templates, is a template-name.  However, such a name should
8215          be a template-name; otherwise, there is no way to form a
8216          template-id for the overloaded templates.  */
8217       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8218       if (TREE_CODE (fns) == OVERLOAD)
8219         {
8220           tree fn;
8221           
8222           for (fn = fns; fn; fn = OVL_NEXT (fn))
8223             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8224               break;
8225         }
8226       else
8227         {
8228           /* Otherwise, the name does not name a template.  */
8229           cp_parser_error (parser, "expected template-name");
8230           return error_mark_node;
8231         }
8232     }
8233
8234   /* If DECL is dependent, and refers to a function, then just return
8235      its name; we will look it up again during template instantiation.  */
8236   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8237     {
8238       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8239       if (TYPE_P (scope) && dependent_type_p (scope))
8240         return identifier;
8241     }
8242
8243   return decl;
8244 }
8245
8246 /* Parse a template-argument-list.
8247
8248    template-argument-list:
8249      template-argument
8250      template-argument-list , template-argument
8251
8252    Returns a TREE_VEC containing the arguments.  */
8253
8254 static tree
8255 cp_parser_template_argument_list (cp_parser* parser)
8256 {
8257   tree fixed_args[10];
8258   unsigned n_args = 0;
8259   unsigned alloced = 10;
8260   tree *arg_ary = fixed_args;
8261   tree vec;
8262   bool saved_in_template_argument_list_p;
8263
8264   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8265   parser->in_template_argument_list_p = true;
8266   do
8267     {
8268       tree argument;
8269
8270       if (n_args)
8271         /* Consume the comma.  */
8272         cp_lexer_consume_token (parser->lexer);
8273       
8274       /* Parse the template-argument.  */
8275       argument = cp_parser_template_argument (parser);
8276       if (n_args == alloced)
8277         {
8278           alloced *= 2;
8279           
8280           if (arg_ary == fixed_args)
8281             {
8282               arg_ary = xmalloc (sizeof (tree) * alloced);
8283               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8284             }
8285           else
8286             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8287         }
8288       arg_ary[n_args++] = argument;
8289     }
8290   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8291
8292   vec = make_tree_vec (n_args);
8293
8294   while (n_args--)
8295     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8296   
8297   if (arg_ary != fixed_args)
8298     free (arg_ary);
8299   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8300   return vec;
8301 }
8302
8303 /* Parse a template-argument.
8304
8305    template-argument:
8306      assignment-expression
8307      type-id
8308      id-expression
8309
8310    The representation is that of an assignment-expression, type-id, or
8311    id-expression -- except that the qualified id-expression is
8312    evaluated, so that the value returned is either a DECL or an
8313    OVERLOAD.  
8314
8315    Although the standard says "assignment-expression", it forbids
8316    throw-expressions or assignments in the template argument.
8317    Therefore, we use "conditional-expression" instead.  */
8318
8319 static tree
8320 cp_parser_template_argument (cp_parser* parser)
8321 {
8322   tree argument;
8323   bool template_p;
8324   bool address_p;
8325   bool maybe_type_id = false;
8326   cp_token *token;
8327   cp_id_kind idk;
8328   tree qualifying_class;
8329
8330   /* There's really no way to know what we're looking at, so we just
8331      try each alternative in order.  
8332
8333        [temp.arg]
8334
8335        In a template-argument, an ambiguity between a type-id and an
8336        expression is resolved to a type-id, regardless of the form of
8337        the corresponding template-parameter.  
8338
8339      Therefore, we try a type-id first.  */
8340   cp_parser_parse_tentatively (parser);
8341   argument = cp_parser_type_id (parser);
8342   /* If there was no error parsing the type-id but the next token is a '>>',
8343      we probably found a typo for '> >'. But there are type-id which are 
8344      also valid expressions. For instance:
8345
8346      struct X { int operator >> (int); };
8347      template <int V> struct Foo {};
8348      Foo<X () >> 5> r;
8349
8350      Here 'X()' is a valid type-id of a function type, but the user just
8351      wanted to write the expression "X() >> 5". Thus, we remember that we
8352      found a valid type-id, but we still try to parse the argument as an
8353      expression to see what happens.  */
8354   if (!cp_parser_error_occurred (parser)
8355       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8356     {
8357       maybe_type_id = true;
8358       cp_parser_abort_tentative_parse (parser);
8359     }
8360   else
8361     {
8362       /* If the next token isn't a `,' or a `>', then this argument wasn't
8363       really finished. This means that the argument is not a valid
8364       type-id.  */
8365       if (!cp_parser_next_token_ends_template_argument_p (parser))
8366         cp_parser_error (parser, "expected template-argument");
8367       /* If that worked, we're done.  */
8368       if (cp_parser_parse_definitely (parser))
8369         return argument;
8370     }
8371   /* We're still not sure what the argument will be.  */
8372   cp_parser_parse_tentatively (parser);
8373   /* Try a template.  */
8374   argument = cp_parser_id_expression (parser, 
8375                                       /*template_keyword_p=*/false,
8376                                       /*check_dependency_p=*/true,
8377                                       &template_p,
8378                                       /*declarator_p=*/false);
8379   /* If the next token isn't a `,' or a `>', then this argument wasn't
8380      really finished.  */
8381   if (!cp_parser_next_token_ends_template_argument_p (parser))
8382     cp_parser_error (parser, "expected template-argument");
8383   if (!cp_parser_error_occurred (parser))
8384     {
8385       /* Figure out what is being referred to.  If the id-expression
8386          was for a class template specialization, then we will have a
8387          TYPE_DECL at this point.  There is no need to do name lookup
8388          at this point in that case.  */
8389       if (TREE_CODE (argument) != TYPE_DECL)
8390         argument = cp_parser_lookup_name (parser, argument,
8391                                           /*is_type=*/false,
8392                                           /*is_template=*/template_p,
8393                                           /*is_namespace=*/false,
8394                                           /*check_dependency=*/true);
8395       if (TREE_CODE (argument) != TEMPLATE_DECL
8396           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8397         cp_parser_error (parser, "expected template-name");
8398     }
8399   if (cp_parser_parse_definitely (parser))
8400     return argument;
8401   /* It must be a non-type argument.  There permitted cases are given
8402      in [temp.arg.nontype]:
8403
8404      -- an integral constant-expression of integral or enumeration
8405         type; or
8406
8407      -- the name of a non-type template-parameter; or
8408
8409      -- the name of an object or function with external linkage...
8410
8411      -- the address of an object or function with external linkage...
8412
8413      -- a pointer to member...  */
8414   /* Look for a non-type template parameter.  */
8415   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8416     {
8417       cp_parser_parse_tentatively (parser);
8418       argument = cp_parser_primary_expression (parser,
8419                                                &idk,
8420                                                &qualifying_class);
8421       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8422           || !cp_parser_next_token_ends_template_argument_p (parser))
8423         cp_parser_simulate_error (parser);
8424       if (cp_parser_parse_definitely (parser))
8425         return argument;
8426     }
8427   /* If the next token is "&", the argument must be the address of an
8428      object or function with external linkage.  */
8429   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8430   if (address_p)
8431     cp_lexer_consume_token (parser->lexer);
8432   /* See if we might have an id-expression.  */
8433   token = cp_lexer_peek_token (parser->lexer);
8434   if (token->type == CPP_NAME
8435       || token->keyword == RID_OPERATOR
8436       || token->type == CPP_SCOPE
8437       || token->type == CPP_TEMPLATE_ID
8438       || token->type == CPP_NESTED_NAME_SPECIFIER)
8439     {
8440       cp_parser_parse_tentatively (parser);
8441       argument = cp_parser_primary_expression (parser,
8442                                                &idk,
8443                                                &qualifying_class);
8444       if (cp_parser_error_occurred (parser)
8445           || !cp_parser_next_token_ends_template_argument_p (parser))
8446         cp_parser_abort_tentative_parse (parser);
8447       else
8448         {
8449           if (qualifying_class)
8450             argument = finish_qualified_id_expr (qualifying_class,
8451                                                  argument,
8452                                                  /*done=*/true,
8453                                                  address_p);
8454           if (TREE_CODE (argument) == VAR_DECL)
8455             {
8456               /* A variable without external linkage might still be a
8457                  valid constant-expression, so no error is issued here
8458                  if the external-linkage check fails.  */
8459               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8460                 cp_parser_simulate_error (parser);
8461             }
8462           else if (is_overloaded_fn (argument))
8463             /* All overloaded functions are allowed; if the external
8464                linkage test does not pass, an error will be issued
8465                later.  */
8466             ;
8467           else if (address_p
8468                    && (TREE_CODE (argument) == OFFSET_REF 
8469                        || TREE_CODE (argument) == SCOPE_REF))
8470             /* A pointer-to-member.  */
8471             ;
8472           else
8473             cp_parser_simulate_error (parser);
8474
8475           if (cp_parser_parse_definitely (parser))
8476             {
8477               if (address_p)
8478                 argument = build_x_unary_op (ADDR_EXPR, argument);
8479               return argument;
8480             }
8481         }
8482     }
8483   /* If the argument started with "&", there are no other valid
8484      alternatives at this point.  */
8485   if (address_p)
8486     {
8487       cp_parser_error (parser, "invalid non-type template argument");
8488       return error_mark_node;
8489     }
8490   /* If the argument wasn't successfully parsed as a type-id followed
8491      by '>>', the argument can only be a constant expression now.  
8492      Otherwise, we try parsing the constant-expression tentatively,
8493      because the argument could really be a type-id.  */
8494   if (maybe_type_id)
8495     cp_parser_parse_tentatively (parser);
8496   argument = cp_parser_constant_expression (parser, 
8497                                             /*allow_non_constant_p=*/false,
8498                                             /*non_constant_p=*/NULL);
8499   argument = fold_non_dependent_expr (argument);
8500   if (!maybe_type_id)
8501     return argument;
8502   if (!cp_parser_next_token_ends_template_argument_p (parser))
8503     cp_parser_error (parser, "expected template-argument");
8504   if (cp_parser_parse_definitely (parser))
8505     return argument;
8506   /* We did our best to parse the argument as a non type-id, but that
8507      was the only alternative that matched (albeit with a '>' after
8508      it). We can assume it's just a typo from the user, and a 
8509      diagnostic will then be issued.  */
8510   return cp_parser_type_id (parser);
8511 }
8512
8513 /* Parse an explicit-instantiation.
8514
8515    explicit-instantiation:
8516      template declaration  
8517
8518    Although the standard says `declaration', what it really means is:
8519
8520    explicit-instantiation:
8521      template decl-specifier-seq [opt] declarator [opt] ; 
8522
8523    Things like `template int S<int>::i = 5, int S<double>::j;' are not
8524    supposed to be allowed.  A defect report has been filed about this
8525    issue.  
8526
8527    GNU Extension:
8528   
8529    explicit-instantiation:
8530      storage-class-specifier template 
8531        decl-specifier-seq [opt] declarator [opt] ;
8532      function-specifier template 
8533        decl-specifier-seq [opt] declarator [opt] ;  */
8534
8535 static void
8536 cp_parser_explicit_instantiation (cp_parser* parser)
8537 {
8538   int declares_class_or_enum;
8539   tree decl_specifiers;
8540   tree attributes;
8541   tree extension_specifier = NULL_TREE;
8542
8543   /* Look for an (optional) storage-class-specifier or
8544      function-specifier.  */
8545   if (cp_parser_allow_gnu_extensions_p (parser))
8546     {
8547       extension_specifier 
8548         = cp_parser_storage_class_specifier_opt (parser);
8549       if (!extension_specifier)
8550         extension_specifier = cp_parser_function_specifier_opt (parser);
8551     }
8552
8553   /* Look for the `template' keyword.  */
8554   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8555   /* Let the front end know that we are processing an explicit
8556      instantiation.  */
8557   begin_explicit_instantiation ();
8558   /* [temp.explicit] says that we are supposed to ignore access
8559      control while processing explicit instantiation directives.  */
8560   push_deferring_access_checks (dk_no_check);
8561   /* Parse a decl-specifier-seq.  */
8562   decl_specifiers 
8563     = cp_parser_decl_specifier_seq (parser,
8564                                     CP_PARSER_FLAGS_OPTIONAL,
8565                                     &attributes,
8566                                     &declares_class_or_enum);
8567   /* If there was exactly one decl-specifier, and it declared a class,
8568      and there's no declarator, then we have an explicit type
8569      instantiation.  */
8570   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8571     {
8572       tree type;
8573
8574       type = check_tag_decl (decl_specifiers);
8575       /* Turn access control back on for names used during
8576          template instantiation.  */
8577       pop_deferring_access_checks ();
8578       if (type)
8579         do_type_instantiation (type, extension_specifier, /*complain=*/1);
8580     }
8581   else
8582     {
8583       tree declarator;
8584       tree decl;
8585
8586       /* Parse the declarator.  */
8587       declarator 
8588         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8589                                 /*ctor_dtor_or_conv_p=*/NULL,
8590                                 /*parenthesized_p=*/NULL,
8591                                 /*member_p=*/false);
8592       cp_parser_check_for_definition_in_return_type (declarator, 
8593                                                      declares_class_or_enum);
8594       if (declarator != error_mark_node)
8595         {
8596           decl = grokdeclarator (declarator, decl_specifiers, 
8597                                  NORMAL, 0, NULL);
8598           /* Turn access control back on for names used during
8599              template instantiation.  */
8600           pop_deferring_access_checks ();
8601           /* Do the explicit instantiation.  */
8602           do_decl_instantiation (decl, extension_specifier);
8603         }
8604       else
8605         {
8606           pop_deferring_access_checks ();
8607           /* Skip the body of the explicit instantiation.  */
8608           cp_parser_skip_to_end_of_statement (parser);
8609         }
8610     }
8611   /* We're done with the instantiation.  */
8612   end_explicit_instantiation ();
8613
8614   cp_parser_consume_semicolon_at_end_of_statement (parser);
8615 }
8616
8617 /* Parse an explicit-specialization.
8618
8619    explicit-specialization:
8620      template < > declaration  
8621
8622    Although the standard says `declaration', what it really means is:
8623
8624    explicit-specialization:
8625      template <> decl-specifier [opt] init-declarator [opt] ;
8626      template <> function-definition 
8627      template <> explicit-specialization
8628      template <> template-declaration  */
8629
8630 static void
8631 cp_parser_explicit_specialization (cp_parser* parser)
8632 {
8633   /* Look for the `template' keyword.  */
8634   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8635   /* Look for the `<'.  */
8636   cp_parser_require (parser, CPP_LESS, "`<'");
8637   /* Look for the `>'.  */
8638   cp_parser_require (parser, CPP_GREATER, "`>'");
8639   /* We have processed another parameter list.  */
8640   ++parser->num_template_parameter_lists;
8641   /* Let the front end know that we are beginning a specialization.  */
8642   begin_specialization ();
8643
8644   /* If the next keyword is `template', we need to figure out whether
8645      or not we're looking a template-declaration.  */
8646   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8647     {
8648       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8649           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8650         cp_parser_template_declaration_after_export (parser,
8651                                                      /*member_p=*/false);
8652       else
8653         cp_parser_explicit_specialization (parser);
8654     }
8655   else
8656     /* Parse the dependent declaration.  */
8657     cp_parser_single_declaration (parser, 
8658                                   /*member_p=*/false,
8659                                   /*friend_p=*/NULL);
8660
8661   /* We're done with the specialization.  */
8662   end_specialization ();
8663   /* We're done with this parameter list.  */
8664   --parser->num_template_parameter_lists;
8665 }
8666
8667 /* Parse a type-specifier.
8668
8669    type-specifier:
8670      simple-type-specifier
8671      class-specifier
8672      enum-specifier
8673      elaborated-type-specifier
8674      cv-qualifier
8675
8676    GNU Extension:
8677
8678    type-specifier:
8679      __complex__
8680
8681    Returns a representation of the type-specifier.  If the
8682    type-specifier is a keyword (like `int' or `const', or
8683    `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8684    For a class-specifier, enum-specifier, or elaborated-type-specifier
8685    a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8686
8687    If IS_FRIEND is TRUE then this type-specifier is being declared a
8688    `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
8689    appearing in a decl-specifier-seq.
8690
8691    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8692    class-specifier, enum-specifier, or elaborated-type-specifier, then
8693    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
8694    if a type is declared; 2 if it is defined.  Otherwise, it is set to
8695    zero.
8696
8697    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8698    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
8699    is set to FALSE.  */
8700
8701 static tree
8702 cp_parser_type_specifier (cp_parser* parser, 
8703                           cp_parser_flags flags, 
8704                           bool is_friend,
8705                           bool is_declaration,
8706                           int* declares_class_or_enum,
8707                           bool* is_cv_qualifier)
8708 {
8709   tree type_spec = NULL_TREE;
8710   cp_token *token;
8711   enum rid keyword;
8712
8713   /* Assume this type-specifier does not declare a new type.  */
8714   if (declares_class_or_enum)
8715     *declares_class_or_enum = 0;
8716   /* And that it does not specify a cv-qualifier.  */
8717   if (is_cv_qualifier)
8718     *is_cv_qualifier = false;
8719   /* Peek at the next token.  */
8720   token = cp_lexer_peek_token (parser->lexer);
8721
8722   /* If we're looking at a keyword, we can use that to guide the
8723      production we choose.  */
8724   keyword = token->keyword;
8725   switch (keyword)
8726     {
8727       /* Any of these indicate either a class-specifier, or an
8728          elaborated-type-specifier.  */
8729     case RID_CLASS:
8730     case RID_STRUCT:
8731     case RID_UNION:
8732     case RID_ENUM:
8733       /* Parse tentatively so that we can back up if we don't find a
8734          class-specifier or enum-specifier.  */
8735       cp_parser_parse_tentatively (parser);
8736       /* Look for the class-specifier or enum-specifier.  */
8737       if (keyword == RID_ENUM)
8738         type_spec = cp_parser_enum_specifier (parser);
8739       else
8740         type_spec = cp_parser_class_specifier (parser);
8741
8742       /* If that worked, we're done.  */
8743       if (cp_parser_parse_definitely (parser))
8744         {
8745           if (declares_class_or_enum)
8746             *declares_class_or_enum = 2;
8747           return type_spec;
8748         }
8749
8750       /* Fall through.  */
8751
8752     case RID_TYPENAME:
8753       /* Look for an elaborated-type-specifier.  */
8754       type_spec = cp_parser_elaborated_type_specifier (parser,
8755                                                        is_friend,
8756                                                        is_declaration);
8757       /* We're declaring a class or enum -- unless we're using
8758          `typename'.  */
8759       if (declares_class_or_enum && keyword != RID_TYPENAME)
8760         *declares_class_or_enum = 1;
8761       return type_spec;
8762
8763     case RID_CONST:
8764     case RID_VOLATILE:
8765     case RID_RESTRICT:
8766       type_spec = cp_parser_cv_qualifier_opt (parser);
8767       /* Even though we call a routine that looks for an optional
8768          qualifier, we know that there should be one.  */
8769       my_friendly_assert (type_spec != NULL, 20000328);
8770       /* This type-specifier was a cv-qualified.  */
8771       if (is_cv_qualifier)
8772         *is_cv_qualifier = true;
8773
8774       return type_spec;
8775
8776     case RID_COMPLEX:
8777       /* The `__complex__' keyword is a GNU extension.  */
8778       return cp_lexer_consume_token (parser->lexer)->value;
8779
8780     default:
8781       break;
8782     }
8783
8784   /* If we do not already have a type-specifier, assume we are looking
8785      at a simple-type-specifier.  */
8786   type_spec = cp_parser_simple_type_specifier (parser, flags, 
8787                                                /*identifier_p=*/true);
8788
8789   /* If we didn't find a type-specifier, and a type-specifier was not
8790      optional in this context, issue an error message.  */
8791   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8792     {
8793       cp_parser_error (parser, "expected type specifier");
8794       return error_mark_node;
8795     }
8796
8797   return type_spec;
8798 }
8799
8800 /* Parse a simple-type-specifier.
8801
8802    simple-type-specifier:
8803      :: [opt] nested-name-specifier [opt] type-name
8804      :: [opt] nested-name-specifier template template-id
8805      char
8806      wchar_t
8807      bool
8808      short
8809      int
8810      long
8811      signed
8812      unsigned
8813      float
8814      double
8815      void  
8816
8817    GNU Extension:
8818
8819    simple-type-specifier:
8820      __typeof__ unary-expression
8821      __typeof__ ( type-id )
8822
8823    For the various keywords, the value returned is simply the
8824    TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8825    For the first two productions, and if IDENTIFIER_P is false, the
8826    value returned is the indicated TYPE_DECL.  */
8827
8828 static tree
8829 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8830                                  bool identifier_p)
8831 {
8832   tree type = NULL_TREE;
8833   cp_token *token;
8834
8835   /* Peek at the next token.  */
8836   token = cp_lexer_peek_token (parser->lexer);
8837
8838   /* If we're looking at a keyword, things are easy.  */
8839   switch (token->keyword)
8840     {
8841     case RID_CHAR:
8842       type = char_type_node;
8843       break;
8844     case RID_WCHAR:
8845       type = wchar_type_node;
8846       break;
8847     case RID_BOOL:
8848       type = boolean_type_node;
8849       break;
8850     case RID_SHORT:
8851       type = short_integer_type_node;
8852       break;
8853     case RID_INT:
8854       type = integer_type_node;
8855       break;
8856     case RID_LONG:
8857       type = long_integer_type_node;
8858       break;
8859     case RID_SIGNED:
8860       type = integer_type_node;
8861       break;
8862     case RID_UNSIGNED:
8863       type = unsigned_type_node;
8864       break;
8865     case RID_FLOAT:
8866       type = float_type_node;
8867       break;
8868     case RID_DOUBLE:
8869       type = double_type_node;
8870       break;
8871     case RID_VOID:
8872       type = void_type_node;
8873       break;
8874
8875     case RID_TYPEOF:
8876       {
8877         tree operand;
8878
8879         /* Consume the `typeof' token.  */
8880         cp_lexer_consume_token (parser->lexer);
8881         /* Parse the operand to `typeof'.  */
8882         operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8883         /* If it is not already a TYPE, take its type.  */
8884         if (!TYPE_P (operand))
8885           operand = finish_typeof (operand);
8886
8887         return operand;
8888       }
8889
8890     default:
8891       break;
8892     }
8893
8894   /* If the type-specifier was for a built-in type, we're done.  */
8895   if (type)
8896     {
8897       tree id;
8898
8899       /* Consume the token.  */
8900       id = cp_lexer_consume_token (parser->lexer)->value;
8901
8902       /* There is no valid C++ program where a non-template type is
8903          followed by a "<".  That usually indicates that the user thought
8904          that the type was a template.  */
8905       cp_parser_check_for_invalid_template_id (parser, type);
8906
8907       return identifier_p ? id : TYPE_NAME (type);
8908     }
8909
8910   /* The type-specifier must be a user-defined type.  */
8911   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) 
8912     {
8913       bool qualified_p;
8914       bool global_p;
8915
8916       /* Don't gobble tokens or issue error messages if this is an
8917          optional type-specifier.  */
8918       if (flags & CP_PARSER_FLAGS_OPTIONAL)
8919         cp_parser_parse_tentatively (parser);
8920
8921       /* Look for the optional `::' operator.  */
8922       global_p
8923         = (cp_parser_global_scope_opt (parser,
8924                                        /*current_scope_valid_p=*/false)
8925            != NULL_TREE);
8926       /* Look for the nested-name specifier.  */
8927       qualified_p
8928         = (cp_parser_nested_name_specifier_opt (parser,
8929                                                 /*typename_keyword_p=*/false,
8930                                                 /*check_dependency_p=*/true,
8931                                                 /*type_p=*/false,
8932                                                 /*is_declaration=*/false)
8933            != NULL_TREE);
8934       /* If we have seen a nested-name-specifier, and the next token
8935          is `template', then we are using the template-id production.  */
8936       if (parser->scope 
8937           && cp_parser_optional_template_keyword (parser))
8938         {
8939           /* Look for the template-id.  */
8940           type = cp_parser_template_id (parser, 
8941                                         /*template_keyword_p=*/true,
8942                                         /*check_dependency_p=*/true,
8943                                         /*is_declaration=*/false);
8944           /* If the template-id did not name a type, we are out of
8945              luck.  */
8946           if (TREE_CODE (type) != TYPE_DECL)
8947             {
8948               cp_parser_error (parser, "expected template-id for type");
8949               type = NULL_TREE;
8950             }
8951         }
8952       /* Otherwise, look for a type-name.  */
8953       else
8954         type = cp_parser_type_name (parser);
8955       /* Keep track of all name-lookups performed in class scopes.  */
8956       if (type  
8957           && !global_p
8958           && !qualified_p
8959           && TREE_CODE (type) == TYPE_DECL 
8960           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
8961         maybe_note_name_used_in_class (DECL_NAME (type), type);
8962       /* If it didn't work out, we don't have a TYPE.  */
8963       if ((flags & CP_PARSER_FLAGS_OPTIONAL) 
8964           && !cp_parser_parse_definitely (parser))
8965         type = NULL_TREE;
8966     }
8967
8968   /* If we didn't get a type-name, issue an error message.  */
8969   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8970     {
8971       cp_parser_error (parser, "expected type-name");
8972       return error_mark_node;
8973     }
8974
8975   /* There is no valid C++ program where a non-template type is
8976      followed by a "<".  That usually indicates that the user thought
8977      that the type was a template.  */
8978   if (type && type != error_mark_node)
8979     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8980
8981   return type;
8982 }
8983
8984 /* Parse a type-name.
8985
8986    type-name:
8987      class-name
8988      enum-name
8989      typedef-name  
8990
8991    enum-name:
8992      identifier
8993
8994    typedef-name:
8995      identifier 
8996
8997    Returns a TYPE_DECL for the the type.  */
8998
8999 static tree
9000 cp_parser_type_name (cp_parser* parser)
9001 {
9002   tree type_decl;
9003   tree identifier;
9004
9005   /* We can't know yet whether it is a class-name or not.  */
9006   cp_parser_parse_tentatively (parser);
9007   /* Try a class-name.  */
9008   type_decl = cp_parser_class_name (parser, 
9009                                     /*typename_keyword_p=*/false,
9010                                     /*template_keyword_p=*/false,
9011                                     /*type_p=*/false,
9012                                     /*check_dependency_p=*/true,
9013                                     /*class_head_p=*/false,
9014                                     /*is_declaration=*/false);
9015   /* If it's not a class-name, keep looking.  */
9016   if (!cp_parser_parse_definitely (parser))
9017     {
9018       /* It must be a typedef-name or an enum-name.  */
9019       identifier = cp_parser_identifier (parser);
9020       if (identifier == error_mark_node)
9021         return error_mark_node;
9022       
9023       /* Look up the type-name.  */
9024       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9025       /* Issue an error if we did not find a type-name.  */
9026       if (TREE_CODE (type_decl) != TYPE_DECL)
9027         {
9028           if (!cp_parser_simulate_error (parser))
9029             cp_parser_name_lookup_error (parser, identifier, type_decl, 
9030                                          "is not a type");
9031           type_decl = error_mark_node;
9032         }
9033       /* Remember that the name was used in the definition of the
9034          current class so that we can check later to see if the
9035          meaning would have been different after the class was
9036          entirely defined.  */
9037       else if (type_decl != error_mark_node
9038                && !parser->scope)
9039         maybe_note_name_used_in_class (identifier, type_decl);
9040     }
9041   
9042   return type_decl;
9043 }
9044
9045
9046 /* Parse an elaborated-type-specifier.  Note that the grammar given
9047    here incorporates the resolution to DR68.
9048
9049    elaborated-type-specifier:
9050      class-key :: [opt] nested-name-specifier [opt] identifier
9051      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9052      enum :: [opt] nested-name-specifier [opt] identifier
9053      typename :: [opt] nested-name-specifier identifier
9054      typename :: [opt] nested-name-specifier template [opt] 
9055        template-id 
9056
9057    GNU extension:
9058
9059    elaborated-type-specifier:
9060      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9061      class-key attributes :: [opt] nested-name-specifier [opt] 
9062                template [opt] template-id
9063      enum attributes :: [opt] nested-name-specifier [opt] identifier
9064
9065    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9066    declared `friend'.  If IS_DECLARATION is TRUE, then this
9067    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9068    something is being declared.
9069
9070    Returns the TYPE specified.  */
9071
9072 static tree
9073 cp_parser_elaborated_type_specifier (cp_parser* parser, 
9074                                      bool is_friend, 
9075                                      bool is_declaration)
9076 {
9077   enum tag_types tag_type;
9078   tree identifier;
9079   tree type = NULL_TREE;
9080   tree attributes = NULL_TREE;
9081
9082   /* See if we're looking at the `enum' keyword.  */
9083   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9084     {
9085       /* Consume the `enum' token.  */
9086       cp_lexer_consume_token (parser->lexer);
9087       /* Remember that it's an enumeration type.  */
9088       tag_type = enum_type;
9089       /* Parse the attributes.  */
9090       attributes = cp_parser_attributes_opt (parser);
9091     }
9092   /* Or, it might be `typename'.  */
9093   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9094                                            RID_TYPENAME))
9095     {
9096       /* Consume the `typename' token.  */
9097       cp_lexer_consume_token (parser->lexer);
9098       /* Remember that it's a `typename' type.  */
9099       tag_type = typename_type;
9100       /* The `typename' keyword is only allowed in templates.  */
9101       if (!processing_template_decl)
9102         pedwarn ("using `typename' outside of template");
9103     }
9104   /* Otherwise it must be a class-key.  */
9105   else
9106     {
9107       tag_type = cp_parser_class_key (parser);
9108       if (tag_type == none_type)
9109         return error_mark_node;
9110       /* Parse the attributes.  */
9111       attributes = cp_parser_attributes_opt (parser);
9112     }
9113
9114   /* Look for the `::' operator.  */
9115   cp_parser_global_scope_opt (parser, 
9116                               /*current_scope_valid_p=*/false);
9117   /* Look for the nested-name-specifier.  */
9118   if (tag_type == typename_type)
9119     {
9120       if (cp_parser_nested_name_specifier (parser,
9121                                            /*typename_keyword_p=*/true,
9122                                            /*check_dependency_p=*/true,
9123                                            /*type_p=*/true,
9124                                            is_declaration) 
9125           == error_mark_node)
9126         return error_mark_node;
9127     }
9128   else
9129     /* Even though `typename' is not present, the proposed resolution
9130        to Core Issue 180 says that in `class A<T>::B', `B' should be
9131        considered a type-name, even if `A<T>' is dependent.  */
9132     cp_parser_nested_name_specifier_opt (parser,
9133                                          /*typename_keyword_p=*/true,
9134                                          /*check_dependency_p=*/true,
9135                                          /*type_p=*/true,
9136                                          is_declaration);
9137   /* For everything but enumeration types, consider a template-id.  */
9138   if (tag_type != enum_type)
9139     {
9140       bool template_p = false;
9141       tree decl;
9142
9143       /* Allow the `template' keyword.  */
9144       template_p = cp_parser_optional_template_keyword (parser);
9145       /* If we didn't see `template', we don't know if there's a
9146          template-id or not.  */
9147       if (!template_p)
9148         cp_parser_parse_tentatively (parser);
9149       /* Parse the template-id.  */
9150       decl = cp_parser_template_id (parser, template_p,
9151                                     /*check_dependency_p=*/true,
9152                                     is_declaration);
9153       /* If we didn't find a template-id, look for an ordinary
9154          identifier.  */
9155       if (!template_p && !cp_parser_parse_definitely (parser))
9156         ;
9157       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9158          in effect, then we must assume that, upon instantiation, the
9159          template will correspond to a class.  */
9160       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9161                && tag_type == typename_type)
9162         type = make_typename_type (parser->scope, decl,
9163                                    /*complain=*/1);
9164       else 
9165         type = TREE_TYPE (decl);
9166     }
9167
9168   /* For an enumeration type, consider only a plain identifier.  */
9169   if (!type)
9170     {
9171       identifier = cp_parser_identifier (parser);
9172
9173       if (identifier == error_mark_node)
9174         {
9175           parser->scope = NULL_TREE;
9176           return error_mark_node;
9177         }
9178
9179       /* For a `typename', we needn't call xref_tag.  */
9180       if (tag_type == typename_type)
9181         return make_typename_type (parser->scope, identifier, 
9182                                    /*complain=*/1);
9183       /* Look up a qualified name in the usual way.  */
9184       if (parser->scope)
9185         {
9186           tree decl;
9187
9188           /* In an elaborated-type-specifier, names are assumed to name
9189              types, so we set IS_TYPE to TRUE when calling
9190              cp_parser_lookup_name.  */
9191           decl = cp_parser_lookup_name (parser, identifier, 
9192                                         /*is_type=*/true,
9193                                         /*is_template=*/false,
9194                                         /*is_namespace=*/false,
9195                                         /*check_dependency=*/true);
9196
9197           /* If we are parsing friend declaration, DECL may be a
9198              TEMPLATE_DECL tree node here.  However, we need to check
9199              whether this TEMPLATE_DECL results in valid code.  Consider
9200              the following example:
9201
9202                namespace N {
9203                  template <class T> class C {};
9204                }
9205                class X {
9206                  template <class T> friend class N::C; // #1, valid code
9207                };
9208                template <class T> class Y {
9209                  friend class N::C;                    // #2, invalid code
9210                };
9211
9212              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9213              name lookup of `N::C'.  We see that friend declaration must
9214              be template for the code to be valid.  Note that
9215              processing_template_decl does not work here since it is
9216              always 1 for the above two cases.  */
9217
9218           decl = (cp_parser_maybe_treat_template_as_class 
9219                   (decl, /*tag_name_p=*/is_friend
9220                          && parser->num_template_parameter_lists));
9221
9222           if (TREE_CODE (decl) != TYPE_DECL)
9223             {
9224               error ("expected type-name");
9225               return error_mark_node;
9226             }
9227
9228           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9229             check_elaborated_type_specifier 
9230               (tag_type, decl,
9231                (parser->num_template_parameter_lists
9232                 || DECL_SELF_REFERENCE_P (decl)));
9233
9234           type = TREE_TYPE (decl);
9235         }
9236       else 
9237         {
9238           /* An elaborated-type-specifier sometimes introduces a new type and
9239              sometimes names an existing type.  Normally, the rule is that it
9240              introduces a new type only if there is not an existing type of
9241              the same name already in scope.  For example, given:
9242
9243                struct S {};
9244                void f() { struct S s; }
9245
9246              the `struct S' in the body of `f' is the same `struct S' as in
9247              the global scope; the existing definition is used.  However, if
9248              there were no global declaration, this would introduce a new 
9249              local class named `S'.
9250
9251              An exception to this rule applies to the following code:
9252
9253                namespace N { struct S; }
9254
9255              Here, the elaborated-type-specifier names a new type
9256              unconditionally; even if there is already an `S' in the
9257              containing scope this declaration names a new type.
9258              This exception only applies if the elaborated-type-specifier
9259              forms the complete declaration:
9260
9261                [class.name] 
9262
9263                A declaration consisting solely of `class-key identifier ;' is
9264                either a redeclaration of the name in the current scope or a
9265                forward declaration of the identifier as a class name.  It
9266                introduces the name into the current scope.
9267
9268              We are in this situation precisely when the next token is a `;'.
9269
9270              An exception to the exception is that a `friend' declaration does
9271              *not* name a new type; i.e., given:
9272
9273                struct S { friend struct T; };
9274
9275              `T' is not a new type in the scope of `S'.  
9276
9277              Also, `new struct S' or `sizeof (struct S)' never results in the
9278              definition of a new type; a new type can only be declared in a
9279              declaration context.  */
9280
9281           /* Warn about attributes. They are ignored.  */
9282           if (attributes)
9283             warning ("type attributes are honored only at type definition");
9284
9285           type = xref_tag (tag_type, identifier, 
9286                            (is_friend 
9287                             || !is_declaration
9288                             || cp_lexer_next_token_is_not (parser->lexer, 
9289                                                            CPP_SEMICOLON)),
9290                            parser->num_template_parameter_lists);
9291         }
9292     }
9293   if (tag_type != enum_type)
9294     cp_parser_check_class_key (tag_type, type);
9295
9296   /* A "<" cannot follow an elaborated type specifier.  If that
9297      happens, the user was probably trying to form a template-id.  */
9298   cp_parser_check_for_invalid_template_id (parser, type);
9299
9300   return type;
9301 }
9302
9303 /* Parse an enum-specifier.
9304
9305    enum-specifier:
9306      enum identifier [opt] { enumerator-list [opt] }
9307
9308    Returns an ENUM_TYPE representing the enumeration.  */
9309
9310 static tree
9311 cp_parser_enum_specifier (cp_parser* parser)
9312 {
9313   cp_token *token;
9314   tree identifier = NULL_TREE;
9315   tree type;
9316
9317   /* Look for the `enum' keyword.  */
9318   if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9319     return error_mark_node;
9320   /* Peek at the next token.  */
9321   token = cp_lexer_peek_token (parser->lexer);
9322
9323   /* See if it is an identifier.  */
9324   if (token->type == CPP_NAME)
9325     identifier = cp_parser_identifier (parser);
9326
9327   /* Look for the `{'.  */
9328   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9329     return error_mark_node;
9330
9331   /* At this point, we're going ahead with the enum-specifier, even
9332      if some other problem occurs.  */
9333   cp_parser_commit_to_tentative_parse (parser);
9334
9335   /* Issue an error message if type-definitions are forbidden here.  */
9336   cp_parser_check_type_definition (parser);
9337
9338   /* Create the new type.  */
9339   type = start_enum (identifier ? identifier : make_anon_name ());
9340
9341   /* Peek at the next token.  */
9342   token = cp_lexer_peek_token (parser->lexer);
9343   /* If it's not a `}', then there are some enumerators.  */
9344   if (token->type != CPP_CLOSE_BRACE)
9345     cp_parser_enumerator_list (parser, type);
9346   /* Look for the `}'.  */
9347   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9348
9349   /* Finish up the enumeration.  */
9350   finish_enum (type);
9351
9352   return type;
9353 }
9354
9355 /* Parse an enumerator-list.  The enumerators all have the indicated
9356    TYPE.  
9357
9358    enumerator-list:
9359      enumerator-definition
9360      enumerator-list , enumerator-definition  */
9361
9362 static void
9363 cp_parser_enumerator_list (cp_parser* parser, tree type)
9364 {
9365   while (true)
9366     {
9367       cp_token *token;
9368
9369       /* Parse an enumerator-definition.  */
9370       cp_parser_enumerator_definition (parser, type);
9371       /* Peek at the next token.  */
9372       token = cp_lexer_peek_token (parser->lexer);
9373       /* If it's not a `,', then we've reached the end of the 
9374          list.  */
9375       if (token->type != CPP_COMMA)
9376         break;
9377       /* Otherwise, consume the `,' and keep going.  */
9378       cp_lexer_consume_token (parser->lexer);
9379       /* If the next token is a `}', there is a trailing comma.  */
9380       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9381         {
9382           if (pedantic && !in_system_header)
9383             pedwarn ("comma at end of enumerator list");
9384           break;
9385         }
9386     }
9387 }
9388
9389 /* Parse an enumerator-definition.  The enumerator has the indicated
9390    TYPE.
9391
9392    enumerator-definition:
9393      enumerator
9394      enumerator = constant-expression
9395     
9396    enumerator:
9397      identifier  */
9398
9399 static void
9400 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9401 {
9402   cp_token *token;
9403   tree identifier;
9404   tree value;
9405
9406   /* Look for the identifier.  */
9407   identifier = cp_parser_identifier (parser);
9408   if (identifier == error_mark_node)
9409     return;
9410   
9411   /* Peek at the next token.  */
9412   token = cp_lexer_peek_token (parser->lexer);
9413   /* If it's an `=', then there's an explicit value.  */
9414   if (token->type == CPP_EQ)
9415     {
9416       /* Consume the `=' token.  */
9417       cp_lexer_consume_token (parser->lexer);
9418       /* Parse the value.  */
9419       value = cp_parser_constant_expression (parser, 
9420                                              /*allow_non_constant_p=*/false,
9421                                              NULL);
9422     }
9423   else
9424     value = NULL_TREE;
9425
9426   /* Create the enumerator.  */
9427   build_enumerator (identifier, value, type);
9428 }
9429
9430 /* Parse a namespace-name.
9431
9432    namespace-name:
9433      original-namespace-name
9434      namespace-alias
9435
9436    Returns the NAMESPACE_DECL for the namespace.  */
9437
9438 static tree
9439 cp_parser_namespace_name (cp_parser* parser)
9440 {
9441   tree identifier;
9442   tree namespace_decl;
9443
9444   /* Get the name of the namespace.  */
9445   identifier = cp_parser_identifier (parser);
9446   if (identifier == error_mark_node)
9447     return error_mark_node;
9448
9449   /* Look up the identifier in the currently active scope.  Look only
9450      for namespaces, due to:
9451
9452        [basic.lookup.udir]
9453
9454        When looking up a namespace-name in a using-directive or alias
9455        definition, only namespace names are considered.  
9456
9457      And:
9458
9459        [basic.lookup.qual]
9460
9461        During the lookup of a name preceding the :: scope resolution
9462        operator, object, function, and enumerator names are ignored.  
9463
9464      (Note that cp_parser_class_or_namespace_name only calls this
9465      function if the token after the name is the scope resolution
9466      operator.)  */
9467   namespace_decl = cp_parser_lookup_name (parser, identifier,
9468                                           /*is_type=*/false,
9469                                           /*is_template=*/false,
9470                                           /*is_namespace=*/true,
9471                                           /*check_dependency=*/true);
9472   /* If it's not a namespace, issue an error.  */
9473   if (namespace_decl == error_mark_node
9474       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9475     {
9476       cp_parser_error (parser, "expected namespace-name");
9477       namespace_decl = error_mark_node;
9478     }
9479   
9480   return namespace_decl;
9481 }
9482
9483 /* Parse a namespace-definition.
9484
9485    namespace-definition:
9486      named-namespace-definition
9487      unnamed-namespace-definition  
9488
9489    named-namespace-definition:
9490      original-namespace-definition
9491      extension-namespace-definition
9492
9493    original-namespace-definition:
9494      namespace identifier { namespace-body }
9495    
9496    extension-namespace-definition:
9497      namespace original-namespace-name { namespace-body }
9498  
9499    unnamed-namespace-definition:
9500      namespace { namespace-body } */
9501
9502 static void
9503 cp_parser_namespace_definition (cp_parser* parser)
9504 {
9505   tree identifier;
9506
9507   /* Look for the `namespace' keyword.  */
9508   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9509
9510   /* Get the name of the namespace.  We do not attempt to distinguish
9511      between an original-namespace-definition and an
9512      extension-namespace-definition at this point.  The semantic
9513      analysis routines are responsible for that.  */
9514   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9515     identifier = cp_parser_identifier (parser);
9516   else
9517     identifier = NULL_TREE;
9518
9519   /* Look for the `{' to start the namespace.  */
9520   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9521   /* Start the namespace.  */
9522   push_namespace (identifier);
9523   /* Parse the body of the namespace.  */
9524   cp_parser_namespace_body (parser);
9525   /* Finish the namespace.  */
9526   pop_namespace ();
9527   /* Look for the final `}'.  */
9528   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9529 }
9530
9531 /* Parse a namespace-body.
9532
9533    namespace-body:
9534      declaration-seq [opt]  */
9535
9536 static void
9537 cp_parser_namespace_body (cp_parser* parser)
9538 {
9539   cp_parser_declaration_seq_opt (parser);
9540 }
9541
9542 /* Parse a namespace-alias-definition.
9543
9544    namespace-alias-definition:
9545      namespace identifier = qualified-namespace-specifier ;  */
9546
9547 static void
9548 cp_parser_namespace_alias_definition (cp_parser* parser)
9549 {
9550   tree identifier;
9551   tree namespace_specifier;
9552
9553   /* Look for the `namespace' keyword.  */
9554   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9555   /* Look for the identifier.  */
9556   identifier = cp_parser_identifier (parser);
9557   if (identifier == error_mark_node)
9558     return;
9559   /* Look for the `=' token.  */
9560   cp_parser_require (parser, CPP_EQ, "`='");
9561   /* Look for the qualified-namespace-specifier.  */
9562   namespace_specifier 
9563     = cp_parser_qualified_namespace_specifier (parser);
9564   /* Look for the `;' token.  */
9565   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9566
9567   /* Register the alias in the symbol table.  */
9568   do_namespace_alias (identifier, namespace_specifier);
9569 }
9570
9571 /* Parse a qualified-namespace-specifier.
9572
9573    qualified-namespace-specifier:
9574      :: [opt] nested-name-specifier [opt] namespace-name
9575
9576    Returns a NAMESPACE_DECL corresponding to the specified
9577    namespace.  */
9578
9579 static tree
9580 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9581 {
9582   /* Look for the optional `::'.  */
9583   cp_parser_global_scope_opt (parser, 
9584                               /*current_scope_valid_p=*/false);
9585
9586   /* Look for the optional nested-name-specifier.  */
9587   cp_parser_nested_name_specifier_opt (parser,
9588                                        /*typename_keyword_p=*/false,
9589                                        /*check_dependency_p=*/true,
9590                                        /*type_p=*/false,
9591                                        /*is_declaration=*/true);
9592
9593   return cp_parser_namespace_name (parser);
9594 }
9595
9596 /* Parse a using-declaration.
9597
9598    using-declaration:
9599      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9600      using :: unqualified-id ;  */
9601
9602 static void
9603 cp_parser_using_declaration (cp_parser* parser)
9604 {
9605   cp_token *token;
9606   bool typename_p = false;
9607   bool global_scope_p;
9608   tree decl;
9609   tree identifier;
9610   tree scope;
9611   tree qscope;
9612
9613   /* Look for the `using' keyword.  */
9614   cp_parser_require_keyword (parser, RID_USING, "`using'");
9615   
9616   /* Peek at the next token.  */
9617   token = cp_lexer_peek_token (parser->lexer);
9618   /* See if it's `typename'.  */
9619   if (token->keyword == RID_TYPENAME)
9620     {
9621       /* Remember that we've seen it.  */
9622       typename_p = true;
9623       /* Consume the `typename' token.  */
9624       cp_lexer_consume_token (parser->lexer);
9625     }
9626
9627   /* Look for the optional global scope qualification.  */
9628   global_scope_p 
9629     = (cp_parser_global_scope_opt (parser,
9630                                    /*current_scope_valid_p=*/false) 
9631        != NULL_TREE);
9632
9633   /* If we saw `typename', or didn't see `::', then there must be a
9634      nested-name-specifier present.  */
9635   if (typename_p || !global_scope_p)
9636     qscope = cp_parser_nested_name_specifier (parser, typename_p, 
9637                                               /*check_dependency_p=*/true,
9638                                               /*type_p=*/false,
9639                                               /*is_declaration=*/true);
9640   /* Otherwise, we could be in either of the two productions.  In that
9641      case, treat the nested-name-specifier as optional.  */
9642   else
9643     qscope = cp_parser_nested_name_specifier_opt (parser,
9644                                                   /*typename_keyword_p=*/false,
9645                                                   /*check_dependency_p=*/true,
9646                                                   /*type_p=*/false,
9647                                                   /*is_declaration=*/true);
9648   if (!qscope)
9649     qscope = global_namespace;
9650
9651   /* Parse the unqualified-id.  */
9652   identifier = cp_parser_unqualified_id (parser, 
9653                                          /*template_keyword_p=*/false,
9654                                          /*check_dependency_p=*/true,
9655                                          /*declarator_p=*/true);
9656
9657   /* The function we call to handle a using-declaration is different
9658      depending on what scope we are in.  */
9659   if (identifier == error_mark_node)
9660     ;
9661   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9662            && TREE_CODE (identifier) != BIT_NOT_EXPR)
9663     /* [namespace.udecl]
9664
9665        A using declaration shall not name a template-id.  */
9666     error ("a template-id may not appear in a using-declaration");
9667   else
9668     {
9669       scope = current_scope ();
9670       if (scope && TYPE_P (scope))
9671         {
9672           /* Create the USING_DECL.  */
9673           decl = do_class_using_decl (build_nt (SCOPE_REF,
9674                                                 parser->scope,
9675                                                 identifier));
9676           /* Add it to the list of members in this class.  */
9677           finish_member_declaration (decl);
9678         }
9679       else
9680         {
9681           decl = cp_parser_lookup_name_simple (parser, identifier);
9682           if (decl == error_mark_node)
9683             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9684           else if (scope)
9685             do_local_using_decl (decl, qscope, identifier);
9686           else
9687             do_toplevel_using_decl (decl, qscope, identifier);
9688         }
9689     }
9690
9691   /* Look for the final `;'.  */
9692   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9693 }
9694
9695 /* Parse a using-directive.  
9696  
9697    using-directive:
9698      using namespace :: [opt] nested-name-specifier [opt]
9699        namespace-name ;  */
9700
9701 static void
9702 cp_parser_using_directive (cp_parser* parser)
9703 {
9704   tree namespace_decl;
9705   tree attribs;
9706
9707   /* Look for the `using' keyword.  */
9708   cp_parser_require_keyword (parser, RID_USING, "`using'");
9709   /* And the `namespace' keyword.  */
9710   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9711   /* Look for the optional `::' operator.  */
9712   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9713   /* And the optional nested-name-specifier.  */
9714   cp_parser_nested_name_specifier_opt (parser,
9715                                        /*typename_keyword_p=*/false,
9716                                        /*check_dependency_p=*/true,
9717                                        /*type_p=*/false,
9718                                        /*is_declaration=*/true);
9719   /* Get the namespace being used.  */
9720   namespace_decl = cp_parser_namespace_name (parser);
9721   /* And any specified attributes.  */
9722   attribs = cp_parser_attributes_opt (parser);
9723   /* Update the symbol table.  */
9724   parse_using_directive (namespace_decl, attribs);
9725   /* Look for the final `;'.  */
9726   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9727 }
9728
9729 /* Parse an asm-definition.
9730
9731    asm-definition:
9732      asm ( string-literal ) ;  
9733
9734    GNU Extension:
9735
9736    asm-definition:
9737      asm volatile [opt] ( string-literal ) ;
9738      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9739      asm volatile [opt] ( string-literal : asm-operand-list [opt]
9740                           : asm-operand-list [opt] ) ;
9741      asm volatile [opt] ( string-literal : asm-operand-list [opt] 
9742                           : asm-operand-list [opt] 
9743                           : asm-operand-list [opt] ) ;  */
9744
9745 static void
9746 cp_parser_asm_definition (cp_parser* parser)
9747 {
9748   cp_token *token;
9749   tree string;
9750   tree outputs = NULL_TREE;
9751   tree inputs = NULL_TREE;
9752   tree clobbers = NULL_TREE;
9753   tree asm_stmt;
9754   bool volatile_p = false;
9755   bool extended_p = false;
9756
9757   /* Look for the `asm' keyword.  */
9758   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9759   /* See if the next token is `volatile'.  */
9760   if (cp_parser_allow_gnu_extensions_p (parser)
9761       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9762     {
9763       /* Remember that we saw the `volatile' keyword.  */
9764       volatile_p = true;
9765       /* Consume the token.  */
9766       cp_lexer_consume_token (parser->lexer);
9767     }
9768   /* Look for the opening `('.  */
9769   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9770   /* Look for the string.  */
9771   token = cp_parser_require (parser, CPP_STRING, "asm body");
9772   if (!token)
9773     return;
9774   string = token->value;
9775   /* If we're allowing GNU extensions, check for the extended assembly
9776      syntax.  Unfortunately, the `:' tokens need not be separated by 
9777      a space in C, and so, for compatibility, we tolerate that here
9778      too.  Doing that means that we have to treat the `::' operator as
9779      two `:' tokens.  */
9780   if (cp_parser_allow_gnu_extensions_p (parser)
9781       && at_function_scope_p ()
9782       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9783           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9784     {
9785       bool inputs_p = false;
9786       bool clobbers_p = false;
9787
9788       /* The extended syntax was used.  */
9789       extended_p = true;
9790
9791       /* Look for outputs.  */
9792       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9793         {
9794           /* Consume the `:'.  */
9795           cp_lexer_consume_token (parser->lexer);
9796           /* Parse the output-operands.  */
9797           if (cp_lexer_next_token_is_not (parser->lexer, 
9798                                           CPP_COLON)
9799               && cp_lexer_next_token_is_not (parser->lexer,
9800                                              CPP_SCOPE)
9801               && cp_lexer_next_token_is_not (parser->lexer,
9802                                              CPP_CLOSE_PAREN))
9803             outputs = cp_parser_asm_operand_list (parser);
9804         }
9805       /* If the next token is `::', there are no outputs, and the
9806          next token is the beginning of the inputs.  */
9807       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9808         /* The inputs are coming next.  */
9809         inputs_p = true;
9810
9811       /* Look for inputs.  */
9812       if (inputs_p
9813           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9814         {
9815           /* Consume the `:' or `::'.  */
9816           cp_lexer_consume_token (parser->lexer);
9817           /* Parse the output-operands.  */
9818           if (cp_lexer_next_token_is_not (parser->lexer, 
9819                                           CPP_COLON)
9820               && cp_lexer_next_token_is_not (parser->lexer,
9821                                              CPP_CLOSE_PAREN))
9822             inputs = cp_parser_asm_operand_list (parser);
9823         }
9824       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9825         /* The clobbers are coming next.  */
9826         clobbers_p = true;
9827
9828       /* Look for clobbers.  */
9829       if (clobbers_p 
9830           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9831         {
9832           /* Consume the `:' or `::'.  */
9833           cp_lexer_consume_token (parser->lexer);
9834           /* Parse the clobbers.  */
9835           if (cp_lexer_next_token_is_not (parser->lexer,
9836                                           CPP_CLOSE_PAREN))
9837             clobbers = cp_parser_asm_clobber_list (parser);
9838         }
9839     }
9840   /* Look for the closing `)'.  */
9841   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9842     cp_parser_skip_to_closing_parenthesis (parser, true, false,
9843                                            /*consume_paren=*/true);
9844   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9845
9846   /* Create the ASM_STMT.  */
9847   if (at_function_scope_p ())
9848     {
9849       asm_stmt = 
9850         finish_asm_stmt (volatile_p 
9851                          ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9852                          string, outputs, inputs, clobbers);
9853       /* If the extended syntax was not used, mark the ASM_STMT.  */
9854       if (!extended_p)
9855         ASM_INPUT_P (asm_stmt) = 1;
9856     }
9857   else
9858     assemble_asm (string);
9859 }
9860
9861 /* Declarators [gram.dcl.decl] */
9862
9863 /* Parse an init-declarator.
9864
9865    init-declarator:
9866      declarator initializer [opt]
9867
9868    GNU Extension:
9869
9870    init-declarator:
9871      declarator asm-specification [opt] attributes [opt] initializer [opt]
9872
9873    function-definition:
9874      decl-specifier-seq [opt] declarator ctor-initializer [opt]
9875        function-body 
9876      decl-specifier-seq [opt] declarator function-try-block  
9877
9878    GNU Extension:
9879
9880    function-definition:
9881      __extension__ function-definition 
9882
9883    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9884    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
9885    then this declarator appears in a class scope.  The new DECL created
9886    by this declarator is returned.
9887
9888    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9889    for a function-definition here as well.  If the declarator is a
9890    declarator for a function-definition, *FUNCTION_DEFINITION_P will
9891    be TRUE upon return.  By that point, the function-definition will
9892    have been completely parsed.
9893
9894    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9895    is FALSE.  */
9896
9897 static tree
9898 cp_parser_init_declarator (cp_parser* parser, 
9899                            tree decl_specifiers, 
9900                            tree prefix_attributes,
9901                            bool function_definition_allowed_p,
9902                            bool member_p,
9903                            int declares_class_or_enum,
9904                            bool* function_definition_p)
9905 {
9906   cp_token *token;
9907   tree declarator;
9908   tree attributes;
9909   tree asm_specification;
9910   tree initializer;
9911   tree decl = NULL_TREE;
9912   tree scope;
9913   bool is_initialized;
9914   bool is_parenthesized_init;
9915   bool is_non_constant_init;
9916   int ctor_dtor_or_conv_p;
9917   bool friend_p;
9918   bool pop_p = false;
9919
9920   /* Assume that this is not the declarator for a function
9921      definition.  */
9922   if (function_definition_p)
9923     *function_definition_p = false;
9924
9925   /* Defer access checks while parsing the declarator; we cannot know
9926      what names are accessible until we know what is being 
9927      declared.  */
9928   resume_deferring_access_checks ();
9929
9930   /* Parse the declarator.  */
9931   declarator 
9932     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9933                             &ctor_dtor_or_conv_p,
9934                             /*parenthesized_p=*/NULL,
9935                             /*member_p=*/false);
9936   /* Gather up the deferred checks.  */
9937   stop_deferring_access_checks ();
9938
9939   /* If the DECLARATOR was erroneous, there's no need to go
9940      further.  */
9941   if (declarator == error_mark_node)
9942     return error_mark_node;
9943
9944   cp_parser_check_for_definition_in_return_type (declarator,
9945                                                  declares_class_or_enum);
9946
9947   /* Figure out what scope the entity declared by the DECLARATOR is
9948      located in.  `grokdeclarator' sometimes changes the scope, so
9949      we compute it now.  */
9950   scope = get_scope_of_declarator (declarator);
9951
9952   /* If we're allowing GNU extensions, look for an asm-specification
9953      and attributes.  */
9954   if (cp_parser_allow_gnu_extensions_p (parser))
9955     {
9956       /* Look for an asm-specification.  */
9957       asm_specification = cp_parser_asm_specification_opt (parser);
9958       /* And attributes.  */
9959       attributes = cp_parser_attributes_opt (parser);
9960     }
9961   else
9962     {
9963       asm_specification = NULL_TREE;
9964       attributes = NULL_TREE;
9965     }
9966
9967   /* Peek at the next token.  */
9968   token = cp_lexer_peek_token (parser->lexer);
9969   /* Check to see if the token indicates the start of a
9970      function-definition.  */
9971   if (cp_parser_token_starts_function_definition_p (token))
9972     {
9973       if (!function_definition_allowed_p)
9974         {
9975           /* If a function-definition should not appear here, issue an
9976              error message.  */
9977           cp_parser_error (parser,
9978                            "a function-definition is not allowed here");
9979           return error_mark_node;
9980         }
9981       else
9982         {
9983           /* Neither attributes nor an asm-specification are allowed
9984              on a function-definition.  */
9985           if (asm_specification)
9986             error ("an asm-specification is not allowed on a function-definition");
9987           if (attributes)
9988             error ("attributes are not allowed on a function-definition");
9989           /* This is a function-definition.  */
9990           *function_definition_p = true;
9991
9992           /* Parse the function definition.  */
9993           if (member_p)
9994             decl = cp_parser_save_member_function_body (parser,
9995                                                         decl_specifiers,
9996                                                         declarator,
9997                                                         prefix_attributes);
9998           else
9999             decl 
10000               = (cp_parser_function_definition_from_specifiers_and_declarator
10001                  (parser, decl_specifiers, prefix_attributes, declarator));
10002
10003           return decl;
10004         }
10005     }
10006
10007   /* [dcl.dcl]
10008
10009      Only in function declarations for constructors, destructors, and
10010      type conversions can the decl-specifier-seq be omitted.  
10011
10012      We explicitly postpone this check past the point where we handle
10013      function-definitions because we tolerate function-definitions
10014      that are missing their return types in some modes.  */
10015   if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
10016     {
10017       cp_parser_error (parser, 
10018                        "expected constructor, destructor, or type conversion");
10019       return error_mark_node;
10020     }
10021
10022   /* An `=' or an `(' indicates an initializer.  */
10023   is_initialized = (token->type == CPP_EQ 
10024                      || token->type == CPP_OPEN_PAREN);
10025   /* If the init-declarator isn't initialized and isn't followed by a
10026      `,' or `;', it's not a valid init-declarator.  */
10027   if (!is_initialized 
10028       && token->type != CPP_COMMA
10029       && token->type != CPP_SEMICOLON)
10030     {
10031       cp_parser_error (parser, "expected init-declarator");
10032       return error_mark_node;
10033     }
10034
10035   /* Because start_decl has side-effects, we should only call it if we
10036      know we're going ahead.  By this point, we know that we cannot
10037      possibly be looking at any other construct.  */
10038   cp_parser_commit_to_tentative_parse (parser);
10039
10040   /* If the decl specifiers were bad, issue an error now that we're
10041      sure this was intended to be a declarator.  Then continue
10042      declaring the variable(s), as int, to try to cut down on further
10043      errors.  */
10044   if (decl_specifiers != NULL
10045       && TREE_VALUE (decl_specifiers) == error_mark_node)
10046     {
10047       cp_parser_error (parser, "invalid type in declaration");
10048       TREE_VALUE (decl_specifiers) = integer_type_node;
10049     }
10050
10051   /* Check to see whether or not this declaration is a friend.  */
10052   friend_p = cp_parser_friend_p (decl_specifiers);
10053
10054   /* Check that the number of template-parameter-lists is OK.  */
10055   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10056     return error_mark_node;
10057
10058   /* Enter the newly declared entry in the symbol table.  If we're
10059      processing a declaration in a class-specifier, we wait until
10060      after processing the initializer.  */
10061   if (!member_p)
10062     {
10063       if (parser->in_unbraced_linkage_specification_p)
10064         {
10065           decl_specifiers = tree_cons (error_mark_node,
10066                                        get_identifier ("extern"),
10067                                        decl_specifiers);
10068           have_extern_spec = false;
10069         }
10070       decl = start_decl (declarator, decl_specifiers,
10071                          is_initialized, attributes, prefix_attributes);
10072     }
10073
10074   /* Enter the SCOPE.  That way unqualified names appearing in the
10075      initializer will be looked up in SCOPE.  */
10076   if (scope)
10077     pop_p = push_scope (scope);
10078
10079   /* Perform deferred access control checks, now that we know in which
10080      SCOPE the declared entity resides.  */
10081   if (!member_p && decl) 
10082     {
10083       tree saved_current_function_decl = NULL_TREE;
10084
10085       /* If the entity being declared is a function, pretend that we
10086          are in its scope.  If it is a `friend', it may have access to
10087          things that would not otherwise be accessible.  */
10088       if (TREE_CODE (decl) == FUNCTION_DECL)
10089         {
10090           saved_current_function_decl = current_function_decl;
10091           current_function_decl = decl;
10092         }
10093         
10094       /* Perform the access control checks for the declarator and the
10095          the decl-specifiers.  */
10096       perform_deferred_access_checks ();
10097
10098       /* Restore the saved value.  */
10099       if (TREE_CODE (decl) == FUNCTION_DECL)
10100         current_function_decl = saved_current_function_decl;
10101     }
10102
10103   /* Parse the initializer.  */
10104   if (is_initialized)
10105     initializer = cp_parser_initializer (parser, 
10106                                          &is_parenthesized_init,
10107                                          &is_non_constant_init);
10108   else
10109     {
10110       initializer = NULL_TREE;
10111       is_parenthesized_init = false;
10112       is_non_constant_init = true;
10113     }
10114
10115   /* The old parser allows attributes to appear after a parenthesized
10116      initializer.  Mark Mitchell proposed removing this functionality
10117      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10118      attributes -- but ignores them.  */
10119   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10120     if (cp_parser_attributes_opt (parser))
10121       warning ("attributes after parenthesized initializer ignored");
10122
10123   /* Leave the SCOPE, now that we have processed the initializer.  It
10124      is important to do this before calling cp_finish_decl because it
10125      makes decisions about whether to create DECL_STMTs or not based
10126      on the current scope.  */
10127   if (pop_p)
10128     pop_scope (scope);
10129
10130   /* For an in-class declaration, use `grokfield' to create the
10131      declaration.  */
10132   if (member_p)
10133     {
10134       decl = grokfield (declarator, decl_specifiers,
10135                         initializer, /*asmspec=*/NULL_TREE,
10136                         /*attributes=*/NULL_TREE);
10137       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10138         cp_parser_save_default_args (parser, decl);
10139     }
10140   
10141   /* Finish processing the declaration.  But, skip friend
10142      declarations.  */
10143   if (!friend_p && decl)
10144     cp_finish_decl (decl, 
10145                     initializer, 
10146                     asm_specification,
10147                     /* If the initializer is in parentheses, then this is
10148                        a direct-initialization, which means that an
10149                        `explicit' constructor is OK.  Otherwise, an
10150                        `explicit' constructor cannot be used.  */
10151                     ((is_parenthesized_init || !is_initialized)
10152                      ? 0 : LOOKUP_ONLYCONVERTING));
10153
10154   /* Remember whether or not variables were initialized by
10155      constant-expressions.  */
10156   if (decl && TREE_CODE (decl) == VAR_DECL 
10157       && is_initialized && !is_non_constant_init)
10158     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10159
10160   return decl;
10161 }
10162
10163 /* Parse a declarator.
10164    
10165    declarator:
10166      direct-declarator
10167      ptr-operator declarator  
10168
10169    abstract-declarator:
10170      ptr-operator abstract-declarator [opt]
10171      direct-abstract-declarator
10172
10173    GNU Extensions:
10174
10175    declarator:
10176      attributes [opt] direct-declarator
10177      attributes [opt] ptr-operator declarator  
10178
10179    abstract-declarator:
10180      attributes [opt] ptr-operator abstract-declarator [opt]
10181      attributes [opt] direct-abstract-declarator
10182      
10183    Returns a representation of the declarator.  If the declarator has
10184    the form `* declarator', then an INDIRECT_REF is returned, whose
10185    only operand is the sub-declarator.  Analogously, `& declarator' is
10186    represented as an ADDR_EXPR.  For `X::* declarator', a SCOPE_REF is
10187    used.  The first operand is the TYPE for `X'.  The second operand
10188    is an INDIRECT_REF whose operand is the sub-declarator.
10189
10190    Otherwise, the representation is as for a direct-declarator.
10191
10192    (It would be better to define a structure type to represent
10193    declarators, rather than abusing `tree' nodes to represent
10194    declarators.  That would be much clearer and save some memory.
10195    There is no reason for declarators to be garbage-collected, for
10196    example; they are created during parser and no longer needed after
10197    `grokdeclarator' has been called.)
10198
10199    For a ptr-operator that has the optional cv-qualifier-seq,
10200    cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10201    node.
10202
10203    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10204    detect constructor, destructor or conversion operators. It is set
10205    to -1 if the declarator is a name, and +1 if it is a
10206    function. Otherwise it is set to zero. Usually you just want to
10207    test for >0, but internally the negative value is used.
10208    
10209    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10210    a decl-specifier-seq unless it declares a constructor, destructor,
10211    or conversion.  It might seem that we could check this condition in
10212    semantic analysis, rather than parsing, but that makes it difficult
10213    to handle something like `f()'.  We want to notice that there are
10214    no decl-specifiers, and therefore realize that this is an
10215    expression, not a declaration.)  
10216  
10217    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10218    the declarator is a direct-declarator of the form "(...)".
10219
10220    MEMBER_P is true iff this declarator is a member-declarator.  */
10221
10222 static tree
10223 cp_parser_declarator (cp_parser* parser, 
10224                       cp_parser_declarator_kind dcl_kind, 
10225                       int* ctor_dtor_or_conv_p,
10226                       bool* parenthesized_p,
10227                       bool member_p)
10228 {
10229   cp_token *token;
10230   tree declarator;
10231   enum tree_code code;
10232   tree cv_qualifier_seq;
10233   tree class_type;
10234   tree attributes = NULL_TREE;
10235
10236   /* Assume this is not a constructor, destructor, or type-conversion
10237      operator.  */
10238   if (ctor_dtor_or_conv_p)
10239     *ctor_dtor_or_conv_p = 0;
10240
10241   if (cp_parser_allow_gnu_extensions_p (parser))
10242     attributes = cp_parser_attributes_opt (parser);
10243   
10244   /* Peek at the next token.  */
10245   token = cp_lexer_peek_token (parser->lexer);
10246   
10247   /* Check for the ptr-operator production.  */
10248   cp_parser_parse_tentatively (parser);
10249   /* Parse the ptr-operator.  */
10250   code = cp_parser_ptr_operator (parser, 
10251                                  &class_type, 
10252                                  &cv_qualifier_seq);
10253   /* If that worked, then we have a ptr-operator.  */
10254   if (cp_parser_parse_definitely (parser))
10255     {
10256       /* If a ptr-operator was found, then this declarator was not
10257          parenthesized.  */
10258       if (parenthesized_p)
10259         *parenthesized_p = true;
10260       /* The dependent declarator is optional if we are parsing an
10261          abstract-declarator.  */
10262       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10263         cp_parser_parse_tentatively (parser);
10264
10265       /* Parse the dependent declarator.  */
10266       declarator = cp_parser_declarator (parser, dcl_kind,
10267                                          /*ctor_dtor_or_conv_p=*/NULL,
10268                                          /*parenthesized_p=*/NULL,
10269                                          /*member_p=*/false);
10270
10271       /* If we are parsing an abstract-declarator, we must handle the
10272          case where the dependent declarator is absent.  */
10273       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10274           && !cp_parser_parse_definitely (parser))
10275         declarator = NULL_TREE;
10276         
10277       /* Build the representation of the ptr-operator.  */
10278       if (code == INDIRECT_REF)
10279         declarator = make_pointer_declarator (cv_qualifier_seq, 
10280                                               declarator);
10281       else
10282         declarator = make_reference_declarator (cv_qualifier_seq,
10283                                                 declarator);
10284       /* Handle the pointer-to-member case.  */
10285       if (class_type)
10286         declarator = build_nt (SCOPE_REF, class_type, declarator);
10287     }
10288   /* Everything else is a direct-declarator.  */
10289   else
10290     {
10291       if (parenthesized_p)
10292         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10293                                                    CPP_OPEN_PAREN);
10294       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10295                                                 ctor_dtor_or_conv_p,
10296                                                 member_p);
10297     }
10298
10299   if (attributes && declarator != error_mark_node)
10300     declarator = tree_cons (attributes, declarator, NULL_TREE);
10301   
10302   return declarator;
10303 }
10304
10305 /* Parse a direct-declarator or direct-abstract-declarator.
10306
10307    direct-declarator:
10308      declarator-id
10309      direct-declarator ( parameter-declaration-clause )
10310        cv-qualifier-seq [opt] 
10311        exception-specification [opt]
10312      direct-declarator [ constant-expression [opt] ]
10313      ( declarator )  
10314
10315    direct-abstract-declarator:
10316      direct-abstract-declarator [opt]
10317        ( parameter-declaration-clause ) 
10318        cv-qualifier-seq [opt]
10319        exception-specification [opt]
10320      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10321      ( abstract-declarator )
10322
10323    Returns a representation of the declarator.  DCL_KIND is
10324    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10325    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10326    we are parsing a direct-declarator.  It is
10327    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10328    of ambiguity we prefer an abstract declarator, as per
10329    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
10330    cp_parser_declarator.
10331
10332    For the declarator-id production, the representation is as for an
10333    id-expression, except that a qualified name is represented as a
10334    SCOPE_REF.  A function-declarator is represented as a CALL_EXPR;
10335    see the documentation of the FUNCTION_DECLARATOR_* macros for
10336    information about how to find the various declarator components.
10337    An array-declarator is represented as an ARRAY_REF.  The
10338    direct-declarator is the first operand; the constant-expression
10339    indicating the size of the array is the second operand.  */
10340
10341 static tree
10342 cp_parser_direct_declarator (cp_parser* parser,
10343                              cp_parser_declarator_kind dcl_kind,
10344                              int* ctor_dtor_or_conv_p,
10345                              bool member_p)
10346 {
10347   cp_token *token;
10348   tree declarator = NULL_TREE;
10349   tree scope = NULL_TREE;
10350   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10351   bool saved_in_declarator_p = parser->in_declarator_p;
10352   bool first = true;
10353   bool pop_p = false;
10354
10355   while (true)
10356     {
10357       /* Peek at the next token.  */
10358       token = cp_lexer_peek_token (parser->lexer);
10359       if (token->type == CPP_OPEN_PAREN)
10360         {
10361           /* This is either a parameter-declaration-clause, or a
10362              parenthesized declarator. When we know we are parsing a
10363              named declarator, it must be a parenthesized declarator
10364              if FIRST is true. For instance, `(int)' is a
10365              parameter-declaration-clause, with an omitted
10366              direct-abstract-declarator. But `((*))', is a
10367              parenthesized abstract declarator. Finally, when T is a
10368              template parameter `(T)' is a
10369              parameter-declaration-clause, and not a parenthesized
10370              named declarator.
10371              
10372              We first try and parse a parameter-declaration-clause,
10373              and then try a nested declarator (if FIRST is true).
10374
10375              It is not an error for it not to be a
10376              parameter-declaration-clause, even when FIRST is
10377              false. Consider,
10378
10379                int i (int);
10380                int i (3);
10381
10382              The first is the declaration of a function while the
10383              second is a the definition of a variable, including its
10384              initializer.
10385
10386              Having seen only the parenthesis, we cannot know which of
10387              these two alternatives should be selected.  Even more
10388              complex are examples like:
10389
10390                int i (int (a));
10391                int i (int (3));
10392
10393              The former is a function-declaration; the latter is a
10394              variable initialization.  
10395
10396              Thus again, we try a parameter-declaration-clause, and if
10397              that fails, we back out and return.  */
10398
10399           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10400             {
10401               tree params;
10402               unsigned saved_num_template_parameter_lists;
10403               
10404               /* In a member-declarator, the only valid interpretation
10405                  of a parenthesis is the start of a
10406                  parameter-declaration-clause.  (It is invalid to
10407                  initialize a static data member with a parenthesized
10408                  initializer; only the "=" form of initialization is
10409                  permitted.)  */
10410               if (!member_p)
10411                 cp_parser_parse_tentatively (parser);
10412
10413               /* Consume the `('.  */
10414               cp_lexer_consume_token (parser->lexer);
10415               if (first)
10416                 {
10417                   /* If this is going to be an abstract declarator, we're
10418                      in a declarator and we can't have default args.  */
10419                   parser->default_arg_ok_p = false;
10420                   parser->in_declarator_p = true;
10421                 }
10422           
10423               /* Inside the function parameter list, surrounding
10424                  template-parameter-lists do not apply.  */
10425               saved_num_template_parameter_lists
10426                 = parser->num_template_parameter_lists;
10427               parser->num_template_parameter_lists = 0;
10428
10429               /* Parse the parameter-declaration-clause.  */
10430               params = cp_parser_parameter_declaration_clause (parser);
10431
10432               parser->num_template_parameter_lists
10433                 = saved_num_template_parameter_lists;
10434
10435               /* If all went well, parse the cv-qualifier-seq and the
10436                  exception-specification.  */
10437               if (member_p || cp_parser_parse_definitely (parser))
10438                 {
10439                   tree cv_qualifiers;
10440                   tree exception_specification;
10441
10442                   if (ctor_dtor_or_conv_p)
10443                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10444                   first = false;
10445                   /* Consume the `)'.  */
10446                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10447
10448                   /* Parse the cv-qualifier-seq.  */
10449                   cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10450                   /* And the exception-specification.  */
10451                   exception_specification 
10452                     = cp_parser_exception_specification_opt (parser);
10453
10454                   /* Create the function-declarator.  */
10455                   declarator = make_call_declarator (declarator,
10456                                                      params,
10457                                                      cv_qualifiers,
10458                                                      exception_specification);
10459                   /* Any subsequent parameter lists are to do with
10460                      return type, so are not those of the declared
10461                      function.  */
10462                   parser->default_arg_ok_p = false;
10463                   
10464                   /* Repeat the main loop.  */
10465                   continue;
10466                 }
10467             }
10468           
10469           /* If this is the first, we can try a parenthesized
10470              declarator.  */
10471           if (first)
10472             {
10473               bool saved_in_type_id_in_expr_p;
10474
10475               parser->default_arg_ok_p = saved_default_arg_ok_p;
10476               parser->in_declarator_p = saved_in_declarator_p;
10477               
10478               /* Consume the `('.  */
10479               cp_lexer_consume_token (parser->lexer);
10480               /* Parse the nested declarator.  */
10481               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10482               parser->in_type_id_in_expr_p = true;
10483               declarator 
10484                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10485                                         /*parenthesized_p=*/NULL,
10486                                         member_p);
10487               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10488               first = false;
10489               /* Expect a `)'.  */
10490               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10491                 declarator = error_mark_node;
10492               if (declarator == error_mark_node)
10493                 break;
10494               
10495               goto handle_declarator;
10496             }
10497           /* Otherwise, we must be done.  */
10498           else
10499             break;
10500         }
10501       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10502                && token->type == CPP_OPEN_SQUARE)
10503         {
10504           /* Parse an array-declarator.  */
10505           tree bounds;
10506
10507           if (ctor_dtor_or_conv_p)
10508             *ctor_dtor_or_conv_p = 0;
10509           
10510           first = false;
10511           parser->default_arg_ok_p = false;
10512           parser->in_declarator_p = true;
10513           /* Consume the `['.  */
10514           cp_lexer_consume_token (parser->lexer);
10515           /* Peek at the next token.  */
10516           token = cp_lexer_peek_token (parser->lexer);
10517           /* If the next token is `]', then there is no
10518              constant-expression.  */
10519           if (token->type != CPP_CLOSE_SQUARE)
10520             {
10521               bool non_constant_p;
10522
10523               bounds 
10524                 = cp_parser_constant_expression (parser,
10525                                                  /*allow_non_constant=*/true,
10526                                                  &non_constant_p);
10527               if (!non_constant_p)
10528                 bounds = fold_non_dependent_expr (bounds);
10529             }
10530           else
10531             bounds = NULL_TREE;
10532           /* Look for the closing `]'.  */
10533           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10534             {
10535               declarator = error_mark_node;
10536               break;
10537             }
10538
10539           declarator = build_nt (ARRAY_REF, declarator, bounds);
10540         }
10541       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10542         {
10543           /* Parse a declarator-id */
10544           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10545             cp_parser_parse_tentatively (parser);
10546           declarator = cp_parser_declarator_id (parser);
10547           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10548             {
10549               if (!cp_parser_parse_definitely (parser))
10550                 declarator = error_mark_node;
10551               else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10552                 {
10553                   cp_parser_error (parser, "expected unqualified-id");
10554                   declarator = error_mark_node;
10555                 }
10556             }
10557           
10558           if (declarator == error_mark_node)
10559             break;
10560           
10561           if (TREE_CODE (declarator) == SCOPE_REF
10562               && !current_scope ())
10563             {
10564               tree scope = TREE_OPERAND (declarator, 0);
10565
10566               /* In the declaration of a member of a template class
10567                  outside of the class itself, the SCOPE will sometimes
10568                  be a TYPENAME_TYPE.  For example, given:
10569                   
10570                  template <typename T>
10571                  int S<T>::R::i = 3;
10572                   
10573                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
10574                  this context, we must resolve S<T>::R to an ordinary
10575                  type, rather than a typename type.
10576                   
10577                  The reason we normally avoid resolving TYPENAME_TYPEs
10578                  is that a specialization of `S' might render
10579                  `S<T>::R' not a type.  However, if `S' is
10580                  specialized, then this `i' will not be used, so there
10581                  is no harm in resolving the types here.  */
10582               if (TREE_CODE (scope) == TYPENAME_TYPE)
10583                 {
10584                   tree type;
10585
10586                   /* Resolve the TYPENAME_TYPE.  */
10587                   type = resolve_typename_type (scope,
10588                                                 /*only_current_p=*/false);
10589                   /* If that failed, the declarator is invalid.  */
10590                   if (type == error_mark_node)
10591                     error ("`%T::%D' is not a type",
10592                            TYPE_CONTEXT (scope),
10593                            TYPE_IDENTIFIER (scope));
10594                   /* Build a new DECLARATOR.  */
10595                   declarator = build_nt (SCOPE_REF, 
10596                                          type,
10597                                          TREE_OPERAND (declarator, 1));
10598                 }
10599             }
10600       
10601           /* Check to see whether the declarator-id names a constructor, 
10602              destructor, or conversion.  */
10603           if (declarator && ctor_dtor_or_conv_p 
10604               && ((TREE_CODE (declarator) == SCOPE_REF 
10605                    && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10606                   || (TREE_CODE (declarator) != SCOPE_REF
10607                       && at_class_scope_p ())))
10608             {
10609               tree unqualified_name;
10610               tree class_type;
10611
10612               /* Get the unqualified part of the name.  */
10613               if (TREE_CODE (declarator) == SCOPE_REF)
10614                 {
10615                   class_type = TREE_OPERAND (declarator, 0);
10616                   unqualified_name = TREE_OPERAND (declarator, 1);
10617                 }
10618               else
10619                 {
10620                   class_type = current_class_type;
10621                   unqualified_name = declarator;
10622                 }
10623
10624               /* See if it names ctor, dtor or conv.  */
10625               if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10626                   || IDENTIFIER_TYPENAME_P (unqualified_name)
10627                   || constructor_name_p (unqualified_name, class_type)
10628                   || (TREE_CODE (unqualified_name) == TYPE_DECL
10629                       && same_type_p (TREE_TYPE (unqualified_name),
10630                                       class_type)))
10631                 *ctor_dtor_or_conv_p = -1;
10632             }
10633
10634         handle_declarator:;
10635           scope = get_scope_of_declarator (declarator);
10636           if (scope)
10637             /* Any names that appear after the declarator-id for a
10638                member are looked up in the containing scope.  */
10639             pop_p = push_scope (scope);
10640           parser->in_declarator_p = true;
10641           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10642               || (declarator
10643                   && (TREE_CODE (declarator) == SCOPE_REF
10644                       || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10645             /* Default args are only allowed on function
10646                declarations.  */
10647             parser->default_arg_ok_p = saved_default_arg_ok_p;
10648           else
10649             parser->default_arg_ok_p = false;
10650
10651           first = false;
10652         }
10653       /* We're done.  */
10654       else
10655         break;
10656     }
10657
10658   /* For an abstract declarator, we might wind up with nothing at this
10659      point.  That's an error; the declarator is not optional.  */
10660   if (!declarator)
10661     cp_parser_error (parser, "expected declarator");
10662
10663   /* If we entered a scope, we must exit it now.  */
10664   if (pop_p)
10665     pop_scope (scope);
10666
10667   parser->default_arg_ok_p = saved_default_arg_ok_p;
10668   parser->in_declarator_p = saved_in_declarator_p;
10669   
10670   return declarator;
10671 }
10672
10673 /* Parse a ptr-operator.  
10674
10675    ptr-operator:
10676      * cv-qualifier-seq [opt]
10677      &
10678      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10679
10680    GNU Extension:
10681
10682    ptr-operator:
10683      & cv-qualifier-seq [opt]
10684
10685    Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10686    used.  Returns ADDR_EXPR if a reference was used.  In the
10687    case of a pointer-to-member, *TYPE is filled in with the 
10688    TYPE containing the member.  *CV_QUALIFIER_SEQ is filled in
10689    with the cv-qualifier-seq, or NULL_TREE, if there are no
10690    cv-qualifiers.  Returns ERROR_MARK if an error occurred.  */
10691    
10692 static enum tree_code
10693 cp_parser_ptr_operator (cp_parser* parser, 
10694                         tree* type, 
10695                         tree* cv_qualifier_seq)
10696 {
10697   enum tree_code code = ERROR_MARK;
10698   cp_token *token;
10699
10700   /* Assume that it's not a pointer-to-member.  */
10701   *type = NULL_TREE;
10702   /* And that there are no cv-qualifiers.  */
10703   *cv_qualifier_seq = NULL_TREE;
10704
10705   /* Peek at the next token.  */
10706   token = cp_lexer_peek_token (parser->lexer);
10707   /* If it's a `*' or `&' we have a pointer or reference.  */
10708   if (token->type == CPP_MULT || token->type == CPP_AND)
10709     {
10710       /* Remember which ptr-operator we were processing.  */
10711       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10712
10713       /* Consume the `*' or `&'.  */
10714       cp_lexer_consume_token (parser->lexer);
10715
10716       /* A `*' can be followed by a cv-qualifier-seq, and so can a
10717          `&', if we are allowing GNU extensions.  (The only qualifier
10718          that can legally appear after `&' is `restrict', but that is
10719          enforced during semantic analysis.  */
10720       if (code == INDIRECT_REF 
10721           || cp_parser_allow_gnu_extensions_p (parser))
10722         *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10723     }
10724   else
10725     {
10726       /* Try the pointer-to-member case.  */
10727       cp_parser_parse_tentatively (parser);
10728       /* Look for the optional `::' operator.  */
10729       cp_parser_global_scope_opt (parser,
10730                                   /*current_scope_valid_p=*/false);
10731       /* Look for the nested-name specifier.  */
10732       cp_parser_nested_name_specifier (parser,
10733                                        /*typename_keyword_p=*/false,
10734                                        /*check_dependency_p=*/true,
10735                                        /*type_p=*/false,
10736                                        /*is_declaration=*/false);
10737       /* If we found it, and the next token is a `*', then we are
10738          indeed looking at a pointer-to-member operator.  */
10739       if (!cp_parser_error_occurred (parser)
10740           && cp_parser_require (parser, CPP_MULT, "`*'"))
10741         {
10742           /* The type of which the member is a member is given by the
10743              current SCOPE.  */
10744           *type = parser->scope;
10745           /* The next name will not be qualified.  */
10746           parser->scope = NULL_TREE;
10747           parser->qualifying_scope = NULL_TREE;
10748           parser->object_scope = NULL_TREE;
10749           /* Indicate that the `*' operator was used.  */
10750           code = INDIRECT_REF;
10751           /* Look for the optional cv-qualifier-seq.  */
10752           *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10753         }
10754       /* If that didn't work we don't have a ptr-operator.  */
10755       if (!cp_parser_parse_definitely (parser))
10756         cp_parser_error (parser, "expected ptr-operator");
10757     }
10758
10759   return code;
10760 }
10761
10762 /* Parse an (optional) cv-qualifier-seq.
10763
10764    cv-qualifier-seq:
10765      cv-qualifier cv-qualifier-seq [opt]  
10766
10767    Returns a TREE_LIST.  The TREE_VALUE of each node is the
10768    representation of a cv-qualifier.  */
10769
10770 static tree
10771 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10772 {
10773   tree cv_qualifiers = NULL_TREE;
10774   
10775   while (true)
10776     {
10777       tree cv_qualifier;
10778
10779       /* Look for the next cv-qualifier.  */
10780       cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10781       /* If we didn't find one, we're done.  */
10782       if (!cv_qualifier)
10783         break;
10784
10785       /* Add this cv-qualifier to the list.  */
10786       cv_qualifiers 
10787         = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10788     }
10789
10790   /* We built up the list in reverse order.  */
10791   return nreverse (cv_qualifiers);
10792 }
10793
10794 /* Parse an (optional) cv-qualifier.
10795
10796    cv-qualifier:
10797      const
10798      volatile  
10799
10800    GNU Extension:
10801
10802    cv-qualifier:
10803      __restrict__ */
10804
10805 static tree
10806 cp_parser_cv_qualifier_opt (cp_parser* parser)
10807 {
10808   cp_token *token;
10809   tree cv_qualifier = NULL_TREE;
10810
10811   /* Peek at the next token.  */
10812   token = cp_lexer_peek_token (parser->lexer);
10813   /* See if it's a cv-qualifier.  */
10814   switch (token->keyword)
10815     {
10816     case RID_CONST:
10817     case RID_VOLATILE:
10818     case RID_RESTRICT:
10819       /* Save the value of the token.  */
10820       cv_qualifier = token->value;
10821       /* Consume the token.  */
10822       cp_lexer_consume_token (parser->lexer);
10823       break;
10824
10825     default:
10826       break;
10827     }
10828
10829   return cv_qualifier;
10830 }
10831
10832 /* Parse a declarator-id.
10833
10834    declarator-id:
10835      id-expression
10836      :: [opt] nested-name-specifier [opt] type-name  
10837
10838    In the `id-expression' case, the value returned is as for
10839    cp_parser_id_expression if the id-expression was an unqualified-id.
10840    If the id-expression was a qualified-id, then a SCOPE_REF is
10841    returned.  The first operand is the scope (either a NAMESPACE_DECL
10842    or TREE_TYPE), but the second is still just a representation of an
10843    unqualified-id.  */
10844
10845 static tree
10846 cp_parser_declarator_id (cp_parser* parser)
10847 {
10848   tree id_expression;
10849
10850   /* The expression must be an id-expression.  Assume that qualified
10851      names are the names of types so that:
10852
10853        template <class T>
10854        int S<T>::R::i = 3;
10855
10856      will work; we must treat `S<T>::R' as the name of a type.
10857      Similarly, assume that qualified names are templates, where
10858      required, so that:
10859
10860        template <class T>
10861        int S<T>::R<T>::i = 3;
10862
10863      will work, too.  */
10864   id_expression = cp_parser_id_expression (parser,
10865                                            /*template_keyword_p=*/false,
10866                                            /*check_dependency_p=*/false,
10867                                            /*template_p=*/NULL,
10868                                            /*declarator_p=*/true);
10869   /* If the name was qualified, create a SCOPE_REF to represent 
10870      that.  */
10871   if (parser->scope)
10872     {
10873       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10874       parser->scope = NULL_TREE;
10875     }
10876
10877   return id_expression;
10878 }
10879
10880 /* Parse a type-id.
10881
10882    type-id:
10883      type-specifier-seq abstract-declarator [opt]
10884
10885    Returns the TYPE specified.  */
10886
10887 static tree
10888 cp_parser_type_id (cp_parser* parser)
10889 {
10890   tree type_specifier_seq;
10891   tree abstract_declarator;
10892
10893   /* Parse the type-specifier-seq.  */
10894   type_specifier_seq 
10895     = cp_parser_type_specifier_seq (parser);
10896   if (type_specifier_seq == error_mark_node)
10897     return error_mark_node;
10898
10899   /* There might or might not be an abstract declarator.  */
10900   cp_parser_parse_tentatively (parser);
10901   /* Look for the declarator.  */
10902   abstract_declarator 
10903     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10904                             /*parenthesized_p=*/NULL,
10905                             /*member_p=*/false);
10906   /* Check to see if there really was a declarator.  */
10907   if (!cp_parser_parse_definitely (parser))
10908     abstract_declarator = NULL_TREE;
10909
10910   return groktypename (build_tree_list (type_specifier_seq,
10911                                         abstract_declarator));
10912 }
10913
10914 /* Parse a type-specifier-seq.
10915
10916    type-specifier-seq:
10917      type-specifier type-specifier-seq [opt]
10918
10919    GNU extension:
10920
10921    type-specifier-seq:
10922      attributes type-specifier-seq [opt]
10923
10924    Returns a TREE_LIST.  Either the TREE_VALUE of each node is a
10925    type-specifier, or the TREE_PURPOSE is a list of attributes.  */
10926
10927 static tree
10928 cp_parser_type_specifier_seq (cp_parser* parser)
10929 {
10930   bool seen_type_specifier = false;
10931   tree type_specifier_seq = NULL_TREE;
10932
10933   /* Parse the type-specifiers and attributes.  */
10934   while (true)
10935     {
10936       tree type_specifier;
10937
10938       /* Check for attributes first.  */
10939       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10940         {
10941           type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10942                                           NULL_TREE,
10943                                           type_specifier_seq);
10944           continue;
10945         }
10946
10947       /* After the first type-specifier, others are optional.  */
10948       if (seen_type_specifier)
10949         cp_parser_parse_tentatively (parser);
10950       /* Look for the type-specifier.  */
10951       type_specifier = cp_parser_type_specifier (parser, 
10952                                                  CP_PARSER_FLAGS_NONE,
10953                                                  /*is_friend=*/false,
10954                                                  /*is_declaration=*/false,
10955                                                  NULL,
10956                                                  NULL);
10957       /* If the first type-specifier could not be found, this is not a
10958          type-specifier-seq at all.  */
10959       if (!seen_type_specifier && type_specifier == error_mark_node)
10960         return error_mark_node;
10961       /* If subsequent type-specifiers could not be found, the
10962          type-specifier-seq is complete.  */
10963       else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10964         break;
10965
10966       /* Add the new type-specifier to the list.  */
10967       type_specifier_seq 
10968         = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10969       seen_type_specifier = true;
10970     }
10971
10972   /* We built up the list in reverse order.  */
10973   return nreverse (type_specifier_seq);
10974 }
10975
10976 /* Parse a parameter-declaration-clause.
10977
10978    parameter-declaration-clause:
10979      parameter-declaration-list [opt] ... [opt]
10980      parameter-declaration-list , ...
10981
10982    Returns a representation for the parameter declarations.  Each node
10983    is a TREE_LIST.  (See cp_parser_parameter_declaration for the exact
10984    representation.)  If the parameter-declaration-clause ends with an
10985    ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10986    list.  A return value of NULL_TREE indicates a
10987    parameter-declaration-clause consisting only of an ellipsis.  */
10988
10989 static tree
10990 cp_parser_parameter_declaration_clause (cp_parser* parser)
10991 {
10992   tree parameters;
10993   cp_token *token;
10994   bool ellipsis_p;
10995
10996   /* Peek at the next token.  */
10997   token = cp_lexer_peek_token (parser->lexer);
10998   /* Check for trivial parameter-declaration-clauses.  */
10999   if (token->type == CPP_ELLIPSIS)
11000     {
11001       /* Consume the `...' token.  */
11002       cp_lexer_consume_token (parser->lexer);
11003       return NULL_TREE;
11004     }
11005   else if (token->type == CPP_CLOSE_PAREN)
11006     /* There are no parameters.  */
11007     {
11008 #ifndef NO_IMPLICIT_EXTERN_C
11009       if (in_system_header && current_class_type == NULL
11010           && current_lang_name == lang_name_c)
11011         return NULL_TREE;
11012       else
11013 #endif
11014         return void_list_node;
11015     }
11016   /* Check for `(void)', too, which is a special case.  */
11017   else if (token->keyword == RID_VOID
11018            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
11019                == CPP_CLOSE_PAREN))
11020     {
11021       /* Consume the `void' token.  */
11022       cp_lexer_consume_token (parser->lexer);
11023       /* There are no parameters.  */
11024       return void_list_node;
11025     }
11026   
11027   /* Parse the parameter-declaration-list.  */
11028   parameters = cp_parser_parameter_declaration_list (parser);
11029   /* If a parse error occurred while parsing the
11030      parameter-declaration-list, then the entire
11031      parameter-declaration-clause is erroneous.  */
11032   if (parameters == error_mark_node)
11033     return error_mark_node;
11034
11035   /* Peek at the next token.  */
11036   token = cp_lexer_peek_token (parser->lexer);
11037   /* If it's a `,', the clause should terminate with an ellipsis.  */
11038   if (token->type == CPP_COMMA)
11039     {
11040       /* Consume the `,'.  */
11041       cp_lexer_consume_token (parser->lexer);
11042       /* Expect an ellipsis.  */
11043       ellipsis_p 
11044         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11045     }
11046   /* It might also be `...' if the optional trailing `,' was 
11047      omitted.  */
11048   else if (token->type == CPP_ELLIPSIS)
11049     {
11050       /* Consume the `...' token.  */
11051       cp_lexer_consume_token (parser->lexer);
11052       /* And remember that we saw it.  */
11053       ellipsis_p = true;
11054     }
11055   else
11056     ellipsis_p = false;
11057
11058   /* Finish the parameter list.  */
11059   return finish_parmlist (parameters, ellipsis_p);
11060 }
11061
11062 /* Parse a parameter-declaration-list.
11063
11064    parameter-declaration-list:
11065      parameter-declaration
11066      parameter-declaration-list , parameter-declaration
11067
11068    Returns a representation of the parameter-declaration-list, as for
11069    cp_parser_parameter_declaration_clause.  However, the
11070    `void_list_node' is never appended to the list.  */
11071
11072 static tree
11073 cp_parser_parameter_declaration_list (cp_parser* parser)
11074 {
11075   tree parameters = NULL_TREE;
11076
11077   /* Look for more parameters.  */
11078   while (true)
11079     {
11080       tree parameter;
11081       bool parenthesized_p;
11082       /* Parse the parameter.  */
11083       parameter 
11084         = cp_parser_parameter_declaration (parser, 
11085                                            /*template_parm_p=*/false,
11086                                            &parenthesized_p);
11087
11088       /* If a parse error occurred parsing the parameter declaration,
11089          then the entire parameter-declaration-list is erroneous.  */
11090       if (parameter == error_mark_node)
11091         {
11092           parameters = error_mark_node;
11093           break;
11094         }
11095       /* Add the new parameter to the list.  */
11096       TREE_CHAIN (parameter) = parameters;
11097       parameters = parameter;
11098
11099       /* Peek at the next token.  */
11100       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11101           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11102         /* The parameter-declaration-list is complete.  */
11103         break;
11104       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11105         {
11106           cp_token *token;
11107
11108           /* Peek at the next token.  */
11109           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11110           /* If it's an ellipsis, then the list is complete.  */
11111           if (token->type == CPP_ELLIPSIS)
11112             break;
11113           /* Otherwise, there must be more parameters.  Consume the
11114              `,'.  */
11115           cp_lexer_consume_token (parser->lexer);
11116           /* When parsing something like:
11117
11118                 int i(float f, double d)
11119                 
11120              we can tell after seeing the declaration for "f" that we
11121              are not looking at an initialization of a variable "i",
11122              but rather at the declaration of a function "i".  
11123
11124              Due to the fact that the parsing of template arguments
11125              (as specified to a template-id) requires backtracking we
11126              cannot use this technique when inside a template argument
11127              list.  */
11128           if (!parser->in_template_argument_list_p
11129               && !parser->in_type_id_in_expr_p
11130               && cp_parser_parsing_tentatively (parser)
11131               && !cp_parser_committed_to_tentative_parse (parser)
11132               /* However, a parameter-declaration of the form
11133                  "foat(f)" (which is a valid declaration of a
11134                  parameter "f") can also be interpreted as an
11135                  expression (the conversion of "f" to "float").  */
11136               && !parenthesized_p)
11137             cp_parser_commit_to_tentative_parse (parser);
11138         }
11139       else
11140         {
11141           cp_parser_error (parser, "expected `,' or `...'");
11142           if (!cp_parser_parsing_tentatively (parser)
11143               || cp_parser_committed_to_tentative_parse (parser))
11144             cp_parser_skip_to_closing_parenthesis (parser, 
11145                                                    /*recovering=*/true,
11146                                                    /*or_comma=*/false,
11147                                                    /*consume_paren=*/false);
11148           break;
11149         }
11150     }
11151
11152   /* We built up the list in reverse order; straighten it out now.  */
11153   return nreverse (parameters);
11154 }
11155
11156 /* Parse a parameter declaration.
11157
11158    parameter-declaration:
11159      decl-specifier-seq declarator
11160      decl-specifier-seq declarator = assignment-expression
11161      decl-specifier-seq abstract-declarator [opt]
11162      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11163
11164    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11165    declares a template parameter.  (In that case, a non-nested `>'
11166    token encountered during the parsing of the assignment-expression
11167    is not interpreted as a greater-than operator.)
11168
11169    Returns a TREE_LIST representing the parameter-declaration.  The
11170    TREE_PURPOSE is the default argument expression, or NULL_TREE if
11171    there is no default argument.  The TREE_VALUE is a representation
11172    of the decl-specifier-seq and declarator.  In particular, the
11173    TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11174    decl-specifier-seq and whose TREE_VALUE represents the declarator.
11175    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11176    the declarator is of the form "(p)".  */
11177
11178 static tree
11179 cp_parser_parameter_declaration (cp_parser *parser, 
11180                                  bool template_parm_p,
11181                                  bool *parenthesized_p)
11182 {
11183   int declares_class_or_enum;
11184   bool greater_than_is_operator_p;
11185   tree decl_specifiers;
11186   tree attributes;
11187   tree declarator;
11188   tree default_argument;
11189   tree parameter;
11190   cp_token *token;
11191   const char *saved_message;
11192
11193   /* In a template parameter, `>' is not an operator.
11194
11195      [temp.param]
11196
11197      When parsing a default template-argument for a non-type
11198      template-parameter, the first non-nested `>' is taken as the end
11199      of the template parameter-list rather than a greater-than
11200      operator.  */
11201   greater_than_is_operator_p = !template_parm_p;
11202
11203   /* Type definitions may not appear in parameter types.  */
11204   saved_message = parser->type_definition_forbidden_message;
11205   parser->type_definition_forbidden_message 
11206     = "types may not be defined in parameter types";
11207
11208   /* Parse the declaration-specifiers.  */
11209   decl_specifiers 
11210     = cp_parser_decl_specifier_seq (parser,
11211                                     CP_PARSER_FLAGS_NONE,
11212                                     &attributes,
11213                                     &declares_class_or_enum);
11214   /* If an error occurred, there's no reason to attempt to parse the
11215      rest of the declaration.  */
11216   if (cp_parser_error_occurred (parser))
11217     {
11218       parser->type_definition_forbidden_message = saved_message;
11219       return error_mark_node;
11220     }
11221
11222   /* Peek at the next token.  */
11223   token = cp_lexer_peek_token (parser->lexer);
11224   /* If the next token is a `)', `,', `=', `>', or `...', then there
11225      is no declarator.  */
11226   if (token->type == CPP_CLOSE_PAREN 
11227       || token->type == CPP_COMMA
11228       || token->type == CPP_EQ
11229       || token->type == CPP_ELLIPSIS
11230       || token->type == CPP_GREATER)
11231     {
11232       declarator = NULL_TREE;
11233       if (parenthesized_p)
11234         *parenthesized_p = false;
11235     }
11236   /* Otherwise, there should be a declarator.  */
11237   else
11238     {
11239       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11240       parser->default_arg_ok_p = false;
11241   
11242       /* After seeing a decl-specifier-seq, if the next token is not a
11243          "(", there is no possibility that the code is a valid
11244          expression.  Therefore, if parsing tentatively, we commit at
11245          this point.  */
11246       if (!parser->in_template_argument_list_p
11247           /* In an expression context, having seen:
11248
11249                (int((char ...
11250
11251              we cannot be sure whether we are looking at a
11252              function-type (taking a "char" as a parameter) or a cast
11253              of some object of type "char" to "int".  */
11254           && !parser->in_type_id_in_expr_p
11255           && cp_parser_parsing_tentatively (parser)
11256           && !cp_parser_committed_to_tentative_parse (parser)
11257           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11258         cp_parser_commit_to_tentative_parse (parser);
11259       /* Parse the declarator.  */
11260       declarator = cp_parser_declarator (parser,
11261                                          CP_PARSER_DECLARATOR_EITHER,
11262                                          /*ctor_dtor_or_conv_p=*/NULL,
11263                                          parenthesized_p,
11264                                          /*member_p=*/false);
11265       parser->default_arg_ok_p = saved_default_arg_ok_p;
11266       /* After the declarator, allow more attributes.  */
11267       attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11268     }
11269
11270   /* The restriction on defining new types applies only to the type
11271      of the parameter, not to the default argument.  */
11272   parser->type_definition_forbidden_message = saved_message;
11273
11274   /* If the next token is `=', then process a default argument.  */
11275   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11276     {
11277       bool saved_greater_than_is_operator_p;
11278       /* Consume the `='.  */
11279       cp_lexer_consume_token (parser->lexer);
11280
11281       /* If we are defining a class, then the tokens that make up the
11282          default argument must be saved and processed later.  */
11283       if (!template_parm_p && at_class_scope_p () 
11284           && TYPE_BEING_DEFINED (current_class_type))
11285         {
11286           unsigned depth = 0;
11287
11288           /* Create a DEFAULT_ARG to represented the unparsed default
11289              argument.  */
11290           default_argument = make_node (DEFAULT_ARG);
11291           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11292
11293           /* Add tokens until we have processed the entire default
11294              argument.  */
11295           while (true)
11296             {
11297               bool done = false;
11298               cp_token *token;
11299
11300               /* Peek at the next token.  */
11301               token = cp_lexer_peek_token (parser->lexer);
11302               /* What we do depends on what token we have.  */
11303               switch (token->type)
11304                 {
11305                   /* In valid code, a default argument must be
11306                      immediately followed by a `,' `)', or `...'.  */
11307                 case CPP_COMMA:
11308                 case CPP_CLOSE_PAREN:
11309                 case CPP_ELLIPSIS:
11310                   /* If we run into a non-nested `;', `}', or `]',
11311                      then the code is invalid -- but the default
11312                      argument is certainly over.  */
11313                 case CPP_SEMICOLON:
11314                 case CPP_CLOSE_BRACE:
11315                 case CPP_CLOSE_SQUARE:
11316                   if (depth == 0)
11317                     done = true;
11318                   /* Update DEPTH, if necessary.  */
11319                   else if (token->type == CPP_CLOSE_PAREN
11320                            || token->type == CPP_CLOSE_BRACE
11321                            || token->type == CPP_CLOSE_SQUARE)
11322                     --depth;
11323                   break;
11324
11325                 case CPP_OPEN_PAREN:
11326                 case CPP_OPEN_SQUARE:
11327                 case CPP_OPEN_BRACE:
11328                   ++depth;
11329                   break;
11330
11331                 case CPP_GREATER:
11332                   /* If we see a non-nested `>', and `>' is not an
11333                      operator, then it marks the end of the default
11334                      argument.  */
11335                   if (!depth && !greater_than_is_operator_p)
11336                     done = true;
11337                   break;
11338
11339                   /* If we run out of tokens, issue an error message.  */
11340                 case CPP_EOF:
11341                   error ("file ends in default argument");
11342                   done = true;
11343                   break;
11344
11345                 case CPP_NAME:
11346                 case CPP_SCOPE:
11347                   /* In these cases, we should look for template-ids.
11348                      For example, if the default argument is 
11349                      `X<int, double>()', we need to do name lookup to
11350                      figure out whether or not `X' is a template; if
11351                      so, the `,' does not end the default argument.
11352
11353                      That is not yet done.  */
11354                   break;
11355
11356                 default:
11357                   break;
11358                 }
11359
11360               /* If we've reached the end, stop.  */
11361               if (done)
11362                 break;
11363               
11364               /* Add the token to the token block.  */
11365               token = cp_lexer_consume_token (parser->lexer);
11366               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11367                                          token);
11368             }
11369         }
11370       /* Outside of a class definition, we can just parse the
11371          assignment-expression.  */
11372       else
11373         {
11374           bool saved_local_variables_forbidden_p;
11375
11376           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11377              set correctly.  */
11378           saved_greater_than_is_operator_p 
11379             = parser->greater_than_is_operator_p;
11380           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11381           /* Local variable names (and the `this' keyword) may not
11382              appear in a default argument.  */
11383           saved_local_variables_forbidden_p 
11384             = parser->local_variables_forbidden_p;
11385           parser->local_variables_forbidden_p = true;
11386           /* Parse the assignment-expression.  */
11387           default_argument = cp_parser_assignment_expression (parser);
11388           /* Restore saved state.  */
11389           parser->greater_than_is_operator_p 
11390             = saved_greater_than_is_operator_p;
11391           parser->local_variables_forbidden_p 
11392             = saved_local_variables_forbidden_p; 
11393         }
11394       if (!parser->default_arg_ok_p)
11395         {
11396           if (!flag_pedantic_errors)
11397             warning ("deprecated use of default argument for parameter of non-function");
11398           else
11399             {
11400               error ("default arguments are only permitted for function parameters");
11401               default_argument = NULL_TREE;
11402             }
11403         }
11404     }
11405   else
11406     default_argument = NULL_TREE;
11407   
11408   /* Create the representation of the parameter.  */
11409   if (attributes)
11410     decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11411   parameter = build_tree_list (default_argument, 
11412                                build_tree_list (decl_specifiers,
11413                                                 declarator));
11414
11415   return parameter;
11416 }
11417
11418 /* Parse a function-body.
11419
11420    function-body:
11421      compound_statement  */
11422
11423 static void
11424 cp_parser_function_body (cp_parser *parser)
11425 {
11426   cp_parser_compound_statement (parser, false);
11427 }
11428
11429 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11430    true if a ctor-initializer was present.  */
11431
11432 static bool
11433 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11434 {
11435   tree body;
11436   bool ctor_initializer_p;
11437
11438   /* Begin the function body.  */
11439   body = begin_function_body ();
11440   /* Parse the optional ctor-initializer.  */
11441   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11442   /* Parse the function-body.  */
11443   cp_parser_function_body (parser);
11444   /* Finish the function body.  */
11445   finish_function_body (body);
11446
11447   return ctor_initializer_p;
11448 }
11449
11450 /* Parse an initializer.
11451
11452    initializer:
11453      = initializer-clause
11454      ( expression-list )  
11455
11456    Returns a expression representing the initializer.  If no
11457    initializer is present, NULL_TREE is returned.  
11458
11459    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11460    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11461    set to FALSE if there is no initializer present.  If there is an
11462    initializer, and it is not a constant-expression, *NON_CONSTANT_P
11463    is set to true; otherwise it is set to false.  */
11464
11465 static tree
11466 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11467                        bool* non_constant_p)
11468 {
11469   cp_token *token;
11470   tree init;
11471
11472   /* Peek at the next token.  */
11473   token = cp_lexer_peek_token (parser->lexer);
11474
11475   /* Let our caller know whether or not this initializer was
11476      parenthesized.  */
11477   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11478   /* Assume that the initializer is constant.  */
11479   *non_constant_p = false;
11480
11481   if (token->type == CPP_EQ)
11482     {
11483       /* Consume the `='.  */
11484       cp_lexer_consume_token (parser->lexer);
11485       /* Parse the initializer-clause.  */
11486       init = cp_parser_initializer_clause (parser, non_constant_p);
11487     }
11488   else if (token->type == CPP_OPEN_PAREN)
11489     init = cp_parser_parenthesized_expression_list (parser, false,
11490                                                     non_constant_p);
11491   else
11492     {
11493       /* Anything else is an error.  */
11494       cp_parser_error (parser, "expected initializer");
11495       init = error_mark_node;
11496     }
11497
11498   return init;
11499 }
11500
11501 /* Parse an initializer-clause.  
11502
11503    initializer-clause:
11504      assignment-expression
11505      { initializer-list , [opt] }
11506      { }
11507
11508    Returns an expression representing the initializer.  
11509
11510    If the `assignment-expression' production is used the value
11511    returned is simply a representation for the expression.  
11512
11513    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11514    the elements of the initializer-list (or NULL_TREE, if the last
11515    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11516    NULL_TREE.  There is no way to detect whether or not the optional
11517    trailing `,' was provided.  NON_CONSTANT_P is as for
11518    cp_parser_initializer.  */
11519
11520 static tree
11521 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11522 {
11523   tree initializer = NULL_TREE;
11524
11525   /* If it is not a `{', then we are looking at an
11526      assignment-expression.  */
11527   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11528     {
11529       /* Speed up common initializers (simply a literal).  */
11530       cp_token* token = cp_lexer_peek_token (parser->lexer);
11531       cp_token* token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11532
11533       if (token2->type == CPP_COMMA)
11534         switch (token->type)
11535           {
11536           case CPP_CHAR:
11537           case CPP_WCHAR:
11538           case CPP_NUMBER:
11539             token = cp_lexer_consume_token (parser->lexer);
11540             initializer = token->value;
11541             break;
11542
11543           case CPP_STRING:
11544           case CPP_WSTRING:
11545             token = cp_lexer_consume_token (parser->lexer);
11546             if (TREE_CHAIN (token->value))
11547               initializer = TREE_CHAIN (token->value);
11548             else
11549               initializer = token->value;
11550             break;
11551
11552           default:
11553             break;
11554           }
11555
11556       /* Otherwise, fall back to the generic assignment expression.  */
11557       if (!initializer)
11558         {
11559           initializer
11560             = cp_parser_constant_expression (parser,
11561                                             /*allow_non_constant_p=*/true,
11562                                             non_constant_p);
11563           if (!*non_constant_p)
11564             initializer = fold_non_dependent_expr (initializer);
11565         }
11566     }
11567   else
11568     {
11569       /* Consume the `{' token.  */
11570       cp_lexer_consume_token (parser->lexer);
11571       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11572       initializer = make_node (CONSTRUCTOR);
11573       /* Mark it with TREE_HAS_CONSTRUCTOR.  This should not be
11574          necessary, but check_initializer depends upon it, for 
11575          now.  */
11576       TREE_HAS_CONSTRUCTOR (initializer) = 1;
11577       /* If it's not a `}', then there is a non-trivial initializer.  */
11578       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11579         {
11580           /* Parse the initializer list.  */
11581           CONSTRUCTOR_ELTS (initializer)
11582             = cp_parser_initializer_list (parser, non_constant_p);
11583           /* A trailing `,' token is allowed.  */
11584           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11585             cp_lexer_consume_token (parser->lexer);
11586         }
11587       /* Now, there should be a trailing `}'.  */
11588       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11589     }
11590
11591   return initializer;
11592 }
11593
11594 /* Parse an initializer-list.
11595
11596    initializer-list:
11597      initializer-clause
11598      initializer-list , initializer-clause
11599
11600    GNU Extension:
11601    
11602    initializer-list:
11603      identifier : initializer-clause
11604      initializer-list, identifier : initializer-clause
11605
11606    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
11607    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
11608    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
11609    as for cp_parser_initializer.  */
11610
11611 static tree
11612 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11613 {
11614   tree initializers = NULL_TREE;
11615
11616   /* Assume all of the expressions are constant.  */
11617   *non_constant_p = false;
11618
11619   /* Parse the rest of the list.  */
11620   while (true)
11621     {
11622       cp_token *token;
11623       tree identifier;
11624       tree initializer;
11625       bool clause_non_constant_p;
11626
11627       /* If the next token is an identifier and the following one is a
11628          colon, we are looking at the GNU designated-initializer
11629          syntax.  */
11630       if (cp_parser_allow_gnu_extensions_p (parser)
11631           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11632           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11633         {
11634           /* Consume the identifier.  */
11635           identifier = cp_lexer_consume_token (parser->lexer)->value;
11636           /* Consume the `:'.  */
11637           cp_lexer_consume_token (parser->lexer);
11638         }
11639       else
11640         identifier = NULL_TREE;
11641
11642       /* Parse the initializer.  */
11643       initializer = cp_parser_initializer_clause (parser, 
11644                                                   &clause_non_constant_p);
11645       /* If any clause is non-constant, so is the entire initializer.  */
11646       if (clause_non_constant_p)
11647         *non_constant_p = true;
11648       /* Add it to the list.  */
11649       initializers = tree_cons (identifier, initializer, initializers);
11650
11651       /* If the next token is not a comma, we have reached the end of
11652          the list.  */
11653       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11654         break;
11655
11656       /* Peek at the next token.  */
11657       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11658       /* If the next token is a `}', then we're still done.  An
11659          initializer-clause can have a trailing `,' after the
11660          initializer-list and before the closing `}'.  */
11661       if (token->type == CPP_CLOSE_BRACE)
11662         break;
11663
11664       /* Consume the `,' token.  */
11665       cp_lexer_consume_token (parser->lexer);
11666     }
11667
11668   /* The initializers were built up in reverse order, so we need to
11669      reverse them now.  */
11670   return nreverse (initializers);
11671 }
11672
11673 /* Classes [gram.class] */
11674
11675 /* Parse a class-name.
11676
11677    class-name:
11678      identifier
11679      template-id
11680
11681    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11682    to indicate that names looked up in dependent types should be
11683    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
11684    keyword has been used to indicate that the name that appears next
11685    is a template.  TYPE_P is true iff the next name should be treated
11686    as class-name, even if it is declared to be some other kind of name
11687    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11688    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
11689    being defined in a class-head.
11690
11691    Returns the TYPE_DECL representing the class.  */
11692
11693 static tree
11694 cp_parser_class_name (cp_parser *parser, 
11695                       bool typename_keyword_p, 
11696                       bool template_keyword_p, 
11697                       bool type_p,
11698                       bool check_dependency_p,
11699                       bool class_head_p,
11700                       bool is_declaration)
11701 {
11702   tree decl;
11703   tree scope;
11704   bool typename_p;
11705   cp_token *token;
11706
11707   /* All class-names start with an identifier.  */
11708   token = cp_lexer_peek_token (parser->lexer);
11709   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11710     {
11711       cp_parser_error (parser, "expected class-name");
11712       return error_mark_node;
11713     }
11714     
11715   /* PARSER->SCOPE can be cleared when parsing the template-arguments
11716      to a template-id, so we save it here.  */
11717   scope = parser->scope;
11718   if (scope == error_mark_node)
11719     return error_mark_node;
11720   
11721   /* Any name names a type if we're following the `typename' keyword
11722      in a qualified name where the enclosing scope is type-dependent.  */
11723   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11724                 && dependent_type_p (scope));
11725   /* Handle the common case (an identifier, but not a template-id)
11726      efficiently.  */
11727   if (token->type == CPP_NAME 
11728       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11729     {
11730       tree identifier;
11731
11732       /* Look for the identifier.  */
11733       identifier = cp_parser_identifier (parser);
11734       /* If the next token isn't an identifier, we are certainly not
11735          looking at a class-name.  */
11736       if (identifier == error_mark_node)
11737         decl = error_mark_node;
11738       /* If we know this is a type-name, there's no need to look it
11739          up.  */
11740       else if (typename_p)
11741         decl = identifier;
11742       else
11743         {
11744           /* If the next token is a `::', then the name must be a type
11745              name.
11746
11747              [basic.lookup.qual]
11748
11749              During the lookup for a name preceding the :: scope
11750              resolution operator, object, function, and enumerator
11751              names are ignored.  */
11752           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11753             type_p = true;
11754           /* Look up the name.  */
11755           decl = cp_parser_lookup_name (parser, identifier, 
11756                                         type_p,
11757                                         /*is_template=*/false,
11758                                         /*is_namespace=*/false,
11759                                         check_dependency_p);
11760         }
11761     }
11762   else
11763     {
11764       /* Try a template-id.  */
11765       decl = cp_parser_template_id (parser, template_keyword_p,
11766                                     check_dependency_p,
11767                                     is_declaration);
11768       if (decl == error_mark_node)
11769         return error_mark_node;
11770     }
11771
11772   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11773
11774   /* If this is a typename, create a TYPENAME_TYPE.  */
11775   if (typename_p && decl != error_mark_node)
11776     {
11777       decl = make_typename_type (scope, decl, /*complain=*/1);
11778       if (decl != error_mark_node)
11779         decl = TYPE_NAME (decl);
11780     }
11781
11782   /* Check to see that it is really the name of a class.  */
11783   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 
11784       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11785       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11786     /* Situations like this:
11787
11788          template <typename T> struct A {
11789            typename T::template X<int>::I i; 
11790          };
11791
11792        are problematic.  Is `T::template X<int>' a class-name?  The
11793        standard does not seem to be definitive, but there is no other
11794        valid interpretation of the following `::'.  Therefore, those
11795        names are considered class-names.  */
11796     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11797   else if (decl == error_mark_node
11798            || TREE_CODE (decl) != TYPE_DECL
11799            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11800     {
11801       cp_parser_error (parser, "expected class-name");
11802       return error_mark_node;
11803     }
11804
11805   return decl;
11806 }
11807
11808 /* Parse a class-specifier.
11809
11810    class-specifier:
11811      class-head { member-specification [opt] }
11812
11813    Returns the TREE_TYPE representing the class.  */
11814
11815 static tree
11816 cp_parser_class_specifier (cp_parser* parser)
11817 {
11818   cp_token *token;
11819   tree type;
11820   tree attributes;
11821   int has_trailing_semicolon;
11822   bool nested_name_specifier_p;
11823   unsigned saved_num_template_parameter_lists;
11824   bool pop_p = false;
11825   tree scope = NULL_TREE;
11826
11827   push_deferring_access_checks (dk_no_deferred);
11828
11829   /* Parse the class-head.  */
11830   type = cp_parser_class_head (parser,
11831                                &nested_name_specifier_p,
11832                                &attributes);
11833   /* If the class-head was a semantic disaster, skip the entire body
11834      of the class.  */
11835   if (!type)
11836     {
11837       cp_parser_skip_to_end_of_block_or_statement (parser);
11838       pop_deferring_access_checks ();
11839       return error_mark_node;
11840     }
11841
11842   /* Look for the `{'.  */
11843   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11844     {
11845       pop_deferring_access_checks ();
11846       return error_mark_node;
11847     }
11848
11849   /* Issue an error message if type-definitions are forbidden here.  */
11850   cp_parser_check_type_definition (parser);
11851   /* Remember that we are defining one more class.  */
11852   ++parser->num_classes_being_defined;
11853   /* Inside the class, surrounding template-parameter-lists do not
11854      apply.  */
11855   saved_num_template_parameter_lists 
11856     = parser->num_template_parameter_lists; 
11857   parser->num_template_parameter_lists = 0;
11858
11859   /* Start the class.  */
11860   if (nested_name_specifier_p)
11861     {
11862       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
11863       pop_p = push_scope (scope);
11864     }
11865   type = begin_class_definition (type);
11866   if (type == error_mark_node)
11867     /* If the type is erroneous, skip the entire body of the class.  */
11868     cp_parser_skip_to_closing_brace (parser);
11869   else
11870     /* Parse the member-specification.  */
11871     cp_parser_member_specification_opt (parser);
11872   /* Look for the trailing `}'.  */
11873   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11874   /* We get better error messages by noticing a common problem: a
11875      missing trailing `;'.  */
11876   token = cp_lexer_peek_token (parser->lexer);
11877   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11878   /* Look for trailing attributes to apply to this class.  */
11879   if (cp_parser_allow_gnu_extensions_p (parser))
11880     {
11881       tree sub_attr = cp_parser_attributes_opt (parser);
11882       attributes = chainon (attributes, sub_attr);
11883     }
11884   if (type != error_mark_node)
11885     type = finish_struct (type, attributes);
11886   if (pop_p)
11887     pop_scope (scope);
11888   /* If this class is not itself within the scope of another class,
11889      then we need to parse the bodies of all of the queued function
11890      definitions.  Note that the queued functions defined in a class
11891      are not always processed immediately following the
11892      class-specifier for that class.  Consider:
11893
11894        struct A {
11895          struct B { void f() { sizeof (A); } };
11896        };
11897
11898      If `f' were processed before the processing of `A' were
11899      completed, there would be no way to compute the size of `A'.
11900      Note that the nesting we are interested in here is lexical --
11901      not the semantic nesting given by TYPE_CONTEXT.  In particular,
11902      for:
11903
11904        struct A { struct B; };
11905        struct A::B { void f() { } };
11906
11907      there is no need to delay the parsing of `A::B::f'.  */
11908   if (--parser->num_classes_being_defined == 0) 
11909     {
11910       tree queue_entry;
11911       tree fn;
11912
11913       /* In a first pass, parse default arguments to the functions.
11914          Then, in a second pass, parse the bodies of the functions.
11915          This two-phased approach handles cases like:
11916          
11917             struct S { 
11918               void f() { g(); } 
11919               void g(int i = 3);
11920             };
11921
11922          */
11923       for (TREE_PURPOSE (parser->unparsed_functions_queues)
11924              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11925            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11926            TREE_PURPOSE (parser->unparsed_functions_queues)
11927              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11928         {
11929           fn = TREE_VALUE (queue_entry);
11930           /* Make sure that any template parameters are in scope.  */
11931           maybe_begin_member_template_processing (fn);
11932           /* If there are default arguments that have not yet been processed,
11933              take care of them now.  */
11934           cp_parser_late_parsing_default_args (parser, fn);
11935           /* Remove any template parameters from the symbol table.  */
11936           maybe_end_member_template_processing ();
11937         }
11938       /* Now parse the body of the functions.  */
11939       for (TREE_VALUE (parser->unparsed_functions_queues)
11940              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11941            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11942            TREE_VALUE (parser->unparsed_functions_queues)
11943              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11944         {
11945           /* Figure out which function we need to process.  */
11946           fn = TREE_VALUE (queue_entry);
11947
11948           /* A hack to prevent garbage collection.  */
11949           function_depth++;
11950
11951           /* Parse the function.  */
11952           cp_parser_late_parsing_for_member (parser, fn);
11953           function_depth--;
11954         }
11955
11956     }
11957
11958   /* Put back any saved access checks.  */
11959   pop_deferring_access_checks ();
11960
11961   /* Restore the count of active template-parameter-lists.  */
11962   parser->num_template_parameter_lists
11963     = saved_num_template_parameter_lists;
11964
11965   return type;
11966 }
11967
11968 /* Parse a class-head.
11969
11970    class-head:
11971      class-key identifier [opt] base-clause [opt]
11972      class-key nested-name-specifier identifier base-clause [opt]
11973      class-key nested-name-specifier [opt] template-id 
11974        base-clause [opt]  
11975
11976    GNU Extensions:
11977      class-key attributes identifier [opt] base-clause [opt]
11978      class-key attributes nested-name-specifier identifier base-clause [opt]
11979      class-key attributes nested-name-specifier [opt] template-id 
11980        base-clause [opt]  
11981
11982    Returns the TYPE of the indicated class.  Sets
11983    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11984    involving a nested-name-specifier was used, and FALSE otherwise.
11985
11986    Returns NULL_TREE if the class-head is syntactically valid, but
11987    semantically invalid in a way that means we should skip the entire
11988    body of the class.  */
11989
11990 static tree
11991 cp_parser_class_head (cp_parser* parser, 
11992                       bool* nested_name_specifier_p,
11993                       tree *attributes_p)
11994 {
11995   cp_token *token;
11996   tree nested_name_specifier;
11997   enum tag_types class_key;
11998   tree id = NULL_TREE;
11999   tree type = NULL_TREE;
12000   tree attributes;
12001   bool template_id_p = false;
12002   bool qualified_p = false;
12003   bool invalid_nested_name_p = false;
12004   bool invalid_explicit_specialization_p = false;
12005   bool pop_p = false;
12006   unsigned num_templates;
12007
12008   /* Assume no nested-name-specifier will be present.  */
12009   *nested_name_specifier_p = false;
12010   /* Assume no template parameter lists will be used in defining the
12011      type.  */
12012   num_templates = 0;
12013
12014   /* Look for the class-key.  */
12015   class_key = cp_parser_class_key (parser);
12016   if (class_key == none_type)
12017     return error_mark_node;
12018
12019   /* Parse the attributes.  */
12020   attributes = cp_parser_attributes_opt (parser);
12021
12022   /* If the next token is `::', that is invalid -- but sometimes
12023      people do try to write:
12024
12025        struct ::S {};  
12026
12027      Handle this gracefully by accepting the extra qualifier, and then
12028      issuing an error about it later if this really is a
12029      class-head.  If it turns out just to be an elaborated type
12030      specifier, remain silent.  */
12031   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12032     qualified_p = true;
12033
12034   push_deferring_access_checks (dk_no_check);
12035
12036   /* Determine the name of the class.  Begin by looking for an
12037      optional nested-name-specifier.  */
12038   nested_name_specifier 
12039     = cp_parser_nested_name_specifier_opt (parser,
12040                                            /*typename_keyword_p=*/false,
12041                                            /*check_dependency_p=*/false,
12042                                            /*type_p=*/false,
12043                                            /*is_declaration=*/false);
12044   /* If there was a nested-name-specifier, then there *must* be an
12045      identifier.  */
12046   if (nested_name_specifier)
12047     {
12048       /* Although the grammar says `identifier', it really means
12049          `class-name' or `template-name'.  You are only allowed to
12050          define a class that has already been declared with this
12051          syntax.  
12052
12053          The proposed resolution for Core Issue 180 says that whever
12054          you see `class T::X' you should treat `X' as a type-name.
12055          
12056          It is OK to define an inaccessible class; for example:
12057          
12058            class A { class B; };
12059            class A::B {};
12060          
12061          We do not know if we will see a class-name, or a
12062          template-name.  We look for a class-name first, in case the
12063          class-name is a template-id; if we looked for the
12064          template-name first we would stop after the template-name.  */
12065       cp_parser_parse_tentatively (parser);
12066       type = cp_parser_class_name (parser,
12067                                    /*typename_keyword_p=*/false,
12068                                    /*template_keyword_p=*/false,
12069                                    /*type_p=*/true,
12070                                    /*check_dependency_p=*/false,
12071                                    /*class_head_p=*/true,
12072                                    /*is_declaration=*/false);
12073       /* If that didn't work, ignore the nested-name-specifier.  */
12074       if (!cp_parser_parse_definitely (parser))
12075         {
12076           invalid_nested_name_p = true;
12077           id = cp_parser_identifier (parser);
12078           if (id == error_mark_node)
12079             id = NULL_TREE;
12080         }
12081       /* If we could not find a corresponding TYPE, treat this
12082          declaration like an unqualified declaration.  */
12083       if (type == error_mark_node)
12084         nested_name_specifier = NULL_TREE;
12085       /* Otherwise, count the number of templates used in TYPE and its
12086          containing scopes.  */
12087       else 
12088         {
12089           tree scope;
12090
12091           for (scope = TREE_TYPE (type); 
12092                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12093                scope = (TYPE_P (scope) 
12094                         ? TYPE_CONTEXT (scope)
12095                         : DECL_CONTEXT (scope))) 
12096             if (TYPE_P (scope) 
12097                 && CLASS_TYPE_P (scope)
12098                 && CLASSTYPE_TEMPLATE_INFO (scope)
12099                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12100                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12101               ++num_templates;
12102         }
12103     }
12104   /* Otherwise, the identifier is optional.  */
12105   else
12106     {
12107       /* We don't know whether what comes next is a template-id,
12108          an identifier, or nothing at all.  */
12109       cp_parser_parse_tentatively (parser);
12110       /* Check for a template-id.  */
12111       id = cp_parser_template_id (parser, 
12112                                   /*template_keyword_p=*/false,
12113                                   /*check_dependency_p=*/true,
12114                                   /*is_declaration=*/true);
12115       /* If that didn't work, it could still be an identifier.  */
12116       if (!cp_parser_parse_definitely (parser))
12117         {
12118           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12119             id = cp_parser_identifier (parser);
12120           else
12121             id = NULL_TREE;
12122         }
12123       else
12124         {
12125           template_id_p = true;
12126           ++num_templates;
12127         }
12128     }
12129
12130   pop_deferring_access_checks ();
12131
12132   if (id)
12133     cp_parser_check_for_invalid_template_id (parser, id);
12134
12135   /* If it's not a `:' or a `{' then we can't really be looking at a
12136      class-head, since a class-head only appears as part of a
12137      class-specifier.  We have to detect this situation before calling
12138      xref_tag, since that has irreversible side-effects.  */
12139   if (!cp_parser_next_token_starts_class_definition_p (parser))
12140     {
12141       cp_parser_error (parser, "expected `{' or `:'");
12142       return error_mark_node;
12143     }
12144
12145   /* At this point, we're going ahead with the class-specifier, even
12146      if some other problem occurs.  */
12147   cp_parser_commit_to_tentative_parse (parser);
12148   /* Issue the error about the overly-qualified name now.  */
12149   if (qualified_p)
12150     cp_parser_error (parser,
12151                      "global qualification of class name is invalid");
12152   else if (invalid_nested_name_p)
12153     cp_parser_error (parser,
12154                      "qualified name does not name a class");
12155   else if (nested_name_specifier)
12156     {
12157       tree scope;
12158       /* Figure out in what scope the declaration is being placed.  */
12159       scope = current_scope ();
12160       if (!scope)
12161         scope = current_namespace;
12162       /* If that scope does not contain the scope in which the
12163          class was originally declared, the program is invalid.  */
12164       if (scope && !is_ancestor (scope, nested_name_specifier))
12165         {
12166           error ("declaration of `%D' in `%D' which does not "
12167                  "enclose `%D'", type, scope, nested_name_specifier);
12168           type = NULL_TREE;
12169           goto done;
12170         }
12171       /* [dcl.meaning]
12172
12173          A declarator-id shall not be qualified exception of the
12174          definition of a ... nested class outside of its class
12175          ... [or] a the definition or explicit instantiation of a
12176          class member of a namespace outside of its namespace.  */
12177       if (scope == nested_name_specifier)
12178         {
12179           pedwarn ("extra qualification ignored");
12180           nested_name_specifier = NULL_TREE;
12181           num_templates = 0;
12182         }
12183     }
12184   /* An explicit-specialization must be preceded by "template <>".  If
12185      it is not, try to recover gracefully.  */
12186   if (at_namespace_scope_p () 
12187       && parser->num_template_parameter_lists == 0
12188       && template_id_p)
12189     {
12190       error ("an explicit specialization must be preceded by 'template <>'");
12191       invalid_explicit_specialization_p = true;
12192       /* Take the same action that would have been taken by
12193          cp_parser_explicit_specialization.  */
12194       ++parser->num_template_parameter_lists;
12195       begin_specialization ();
12196     }
12197   /* There must be no "return" statements between this point and the
12198      end of this function; set "type "to the correct return value and
12199      use "goto done;" to return.  */
12200   /* Make sure that the right number of template parameters were
12201      present.  */
12202   if (!cp_parser_check_template_parameters (parser, num_templates))
12203     {
12204       /* If something went wrong, there is no point in even trying to
12205          process the class-definition.  */
12206       type = NULL_TREE;
12207       goto done;
12208     }
12209
12210   /* Look up the type.  */
12211   if (template_id_p)
12212     {
12213       type = TREE_TYPE (id);
12214       maybe_process_partial_specialization (type);
12215     }
12216   else if (!nested_name_specifier)
12217     {
12218       /* If the class was unnamed, create a dummy name.  */
12219       if (!id)
12220         id = make_anon_name ();
12221       type = xref_tag (class_key, id, /*globalize=*/false,
12222                        parser->num_template_parameter_lists);
12223     }
12224   else
12225     {
12226       tree class_type;
12227       bool pop_p = false;
12228
12229       /* Given:
12230
12231             template <typename T> struct S { struct T };
12232             template <typename T> struct S<T>::T { };
12233
12234          we will get a TYPENAME_TYPE when processing the definition of
12235          `S::T'.  We need to resolve it to the actual type before we
12236          try to define it.  */
12237       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12238         {
12239           class_type = resolve_typename_type (TREE_TYPE (type),
12240                                               /*only_current_p=*/false);
12241           if (class_type != error_mark_node)
12242             type = TYPE_NAME (class_type);
12243           else
12244             {
12245               cp_parser_error (parser, "could not resolve typename type");
12246               type = error_mark_node;
12247             }
12248         }
12249
12250       maybe_process_partial_specialization (TREE_TYPE (type));
12251       class_type = current_class_type;
12252       /* Enter the scope indicated by the nested-name-specifier.  */
12253       if (nested_name_specifier)
12254         pop_p = push_scope (nested_name_specifier);
12255       /* Get the canonical version of this type.  */
12256       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12257       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12258           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12259         type = push_template_decl (type);
12260       type = TREE_TYPE (type);
12261       if (nested_name_specifier)
12262         {
12263           *nested_name_specifier_p = true;
12264           if (pop_p)
12265             pop_scope (nested_name_specifier);
12266         }
12267     }
12268   /* Indicate whether this class was declared as a `class' or as a
12269      `struct'.  */
12270   if (TREE_CODE (type) == RECORD_TYPE)
12271     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12272   cp_parser_check_class_key (class_key, type);
12273
12274   /* Enter the scope containing the class; the names of base classes
12275      should be looked up in that context.  For example, given:
12276
12277        struct A { struct B {}; struct C; };
12278        struct A::C : B {};
12279
12280      is valid.  */
12281   if (nested_name_specifier)
12282     pop_p = push_scope (nested_name_specifier);
12283   /* Now, look for the base-clause.  */
12284   token = cp_lexer_peek_token (parser->lexer);
12285   if (token->type == CPP_COLON)
12286     {
12287       tree bases;
12288
12289       /* Get the list of base-classes.  */
12290       bases = cp_parser_base_clause (parser);
12291       /* Process them.  */
12292       xref_basetypes (type, bases);
12293     }
12294   /* Leave the scope given by the nested-name-specifier.  We will
12295      enter the class scope itself while processing the members.  */
12296   if (pop_p)
12297     pop_scope (nested_name_specifier);
12298
12299  done:
12300   if (invalid_explicit_specialization_p)
12301     {
12302       end_specialization ();
12303       --parser->num_template_parameter_lists;
12304     }
12305   *attributes_p = attributes;
12306   return type;
12307 }
12308
12309 /* Parse a class-key.
12310
12311    class-key:
12312      class
12313      struct
12314      union
12315
12316    Returns the kind of class-key specified, or none_type to indicate
12317    error.  */
12318
12319 static enum tag_types
12320 cp_parser_class_key (cp_parser* parser)
12321 {
12322   cp_token *token;
12323   enum tag_types tag_type;
12324
12325   /* Look for the class-key.  */
12326   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12327   if (!token)
12328     return none_type;
12329
12330   /* Check to see if the TOKEN is a class-key.  */
12331   tag_type = cp_parser_token_is_class_key (token);
12332   if (!tag_type)
12333     cp_parser_error (parser, "expected class-key");
12334   return tag_type;
12335 }
12336
12337 /* Parse an (optional) member-specification.
12338
12339    member-specification:
12340      member-declaration member-specification [opt]
12341      access-specifier : member-specification [opt]  */
12342
12343 static void
12344 cp_parser_member_specification_opt (cp_parser* parser)
12345 {
12346   while (true)
12347     {
12348       cp_token *token;
12349       enum rid keyword;
12350
12351       /* Peek at the next token.  */
12352       token = cp_lexer_peek_token (parser->lexer);
12353       /* If it's a `}', or EOF then we've seen all the members.  */
12354       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12355         break;
12356
12357       /* See if this token is a keyword.  */
12358       keyword = token->keyword;
12359       switch (keyword)
12360         {
12361         case RID_PUBLIC:
12362         case RID_PROTECTED:
12363         case RID_PRIVATE:
12364           /* Consume the access-specifier.  */
12365           cp_lexer_consume_token (parser->lexer);
12366           /* Remember which access-specifier is active.  */
12367           current_access_specifier = token->value;
12368           /* Look for the `:'.  */
12369           cp_parser_require (parser, CPP_COLON, "`:'");
12370           break;
12371
12372         default:
12373           /* Otherwise, the next construction must be a
12374              member-declaration.  */
12375           cp_parser_member_declaration (parser);
12376         }
12377     }
12378 }
12379
12380 /* Parse a member-declaration.  
12381
12382    member-declaration:
12383      decl-specifier-seq [opt] member-declarator-list [opt] ;
12384      function-definition ; [opt]
12385      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12386      using-declaration
12387      template-declaration 
12388
12389    member-declarator-list:
12390      member-declarator
12391      member-declarator-list , member-declarator
12392
12393    member-declarator:
12394      declarator pure-specifier [opt] 
12395      declarator constant-initializer [opt]
12396      identifier [opt] : constant-expression 
12397
12398    GNU Extensions:
12399
12400    member-declaration:
12401      __extension__ member-declaration
12402
12403    member-declarator:
12404      declarator attributes [opt] pure-specifier [opt]
12405      declarator attributes [opt] constant-initializer [opt]
12406      identifier [opt] attributes [opt] : constant-expression  */
12407
12408 static void
12409 cp_parser_member_declaration (cp_parser* parser)
12410 {
12411   tree decl_specifiers;
12412   tree prefix_attributes;
12413   tree decl;
12414   int declares_class_or_enum;
12415   bool friend_p;
12416   cp_token *token;
12417   int saved_pedantic;
12418
12419   /* Check for the `__extension__' keyword.  */
12420   if (cp_parser_extension_opt (parser, &saved_pedantic))
12421     {
12422       /* Recurse.  */
12423       cp_parser_member_declaration (parser);
12424       /* Restore the old value of the PEDANTIC flag.  */
12425       pedantic = saved_pedantic;
12426
12427       return;
12428     }
12429
12430   /* Check for a template-declaration.  */
12431   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12432     {
12433       /* Parse the template-declaration.  */
12434       cp_parser_template_declaration (parser, /*member_p=*/true);
12435
12436       return;
12437     }
12438
12439   /* Check for a using-declaration.  */
12440   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12441     {
12442       /* Parse the using-declaration.  */
12443       cp_parser_using_declaration (parser);
12444
12445       return;
12446     }
12447   
12448   /* Parse the decl-specifier-seq.  */
12449   decl_specifiers 
12450     = cp_parser_decl_specifier_seq (parser,
12451                                     CP_PARSER_FLAGS_OPTIONAL,
12452                                     &prefix_attributes,
12453                                     &declares_class_or_enum);
12454   /* Check for an invalid type-name.  */
12455   if (cp_parser_diagnose_invalid_type_name (parser))
12456     return;
12457   /* If there is no declarator, then the decl-specifier-seq should
12458      specify a type.  */
12459   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12460     {
12461       /* If there was no decl-specifier-seq, and the next token is a
12462          `;', then we have something like:
12463
12464            struct S { ; };
12465
12466          [class.mem]
12467
12468          Each member-declaration shall declare at least one member
12469          name of the class.  */
12470       if (!decl_specifiers)
12471         {
12472           if (pedantic)
12473             pedwarn ("extra semicolon");
12474         }
12475       else 
12476         {
12477           tree type;
12478           
12479           /* See if this declaration is a friend.  */
12480           friend_p = cp_parser_friend_p (decl_specifiers);
12481           /* If there were decl-specifiers, check to see if there was
12482              a class-declaration.  */
12483           type = check_tag_decl (decl_specifiers);
12484           /* Nested classes have already been added to the class, but
12485              a `friend' needs to be explicitly registered.  */
12486           if (friend_p)
12487             {
12488               /* If the `friend' keyword was present, the friend must
12489                  be introduced with a class-key.  */
12490                if (!declares_class_or_enum)
12491                  error ("a class-key must be used when declaring a friend");
12492                /* In this case:
12493
12494                     template <typename T> struct A { 
12495                       friend struct A<T>::B; 
12496                     };
12497  
12498                   A<T>::B will be represented by a TYPENAME_TYPE, and
12499                   therefore not recognized by check_tag_decl.  */
12500                if (!type)
12501                  {
12502                    tree specifier;
12503
12504                    for (specifier = decl_specifiers; 
12505                         specifier;
12506                         specifier = TREE_CHAIN (specifier))
12507                      {
12508                        tree s = TREE_VALUE (specifier);
12509
12510                        if (TREE_CODE (s) == IDENTIFIER_NODE)
12511                          get_global_value_if_present (s, &type);
12512                        if (TREE_CODE (s) == TYPE_DECL)
12513                          s = TREE_TYPE (s);
12514                        if (TYPE_P (s))
12515                          {
12516                            type = s;
12517                            break;
12518                          }
12519                      }
12520                  }
12521                if (!type || !TYPE_P (type))
12522                  error ("friend declaration does not name a class or "
12523                         "function");
12524                else
12525                  make_friend_class (current_class_type, type,
12526                                     /*complain=*/true);
12527             }
12528           /* If there is no TYPE, an error message will already have
12529              been issued.  */
12530           else if (!type)
12531             ;
12532           /* An anonymous aggregate has to be handled specially; such
12533              a declaration really declares a data member (with a
12534              particular type), as opposed to a nested class.  */
12535           else if (ANON_AGGR_TYPE_P (type))
12536             {
12537               /* Remove constructors and such from TYPE, now that we
12538                  know it is an anonymous aggregate.  */
12539               fixup_anonymous_aggr (type);
12540               /* And make the corresponding data member.  */
12541               decl = build_decl (FIELD_DECL, NULL_TREE, type);
12542               /* Add it to the class.  */
12543               finish_member_declaration (decl);
12544             }
12545           else
12546             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12547         }
12548     }
12549   else
12550     {
12551       /* See if these declarations will be friends.  */
12552       friend_p = cp_parser_friend_p (decl_specifiers);
12553
12554       /* Keep going until we hit the `;' at the end of the 
12555          declaration.  */
12556       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12557         {
12558           tree attributes = NULL_TREE;
12559           tree first_attribute;
12560
12561           /* Peek at the next token.  */
12562           token = cp_lexer_peek_token (parser->lexer);
12563
12564           /* Check for a bitfield declaration.  */
12565           if (token->type == CPP_COLON
12566               || (token->type == CPP_NAME
12567                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type 
12568                   == CPP_COLON))
12569             {
12570               tree identifier;
12571               tree width;
12572
12573               /* Get the name of the bitfield.  Note that we cannot just
12574                  check TOKEN here because it may have been invalidated by
12575                  the call to cp_lexer_peek_nth_token above.  */
12576               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12577                 identifier = cp_parser_identifier (parser);
12578               else
12579                 identifier = NULL_TREE;
12580
12581               /* Consume the `:' token.  */
12582               cp_lexer_consume_token (parser->lexer);
12583               /* Get the width of the bitfield.  */
12584               width 
12585                 = cp_parser_constant_expression (parser,
12586                                                  /*allow_non_constant=*/false,
12587                                                  NULL);
12588
12589               /* Look for attributes that apply to the bitfield.  */
12590               attributes = cp_parser_attributes_opt (parser);
12591               /* Remember which attributes are prefix attributes and
12592                  which are not.  */
12593               first_attribute = attributes;
12594               /* Combine the attributes.  */
12595               attributes = chainon (prefix_attributes, attributes);
12596
12597               /* Create the bitfield declaration.  */
12598               decl = grokbitfield (identifier, 
12599                                    decl_specifiers,
12600                                    width);
12601               /* Apply the attributes.  */
12602               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12603             }
12604           else
12605             {
12606               tree declarator;
12607               tree initializer;
12608               tree asm_specification;
12609               int ctor_dtor_or_conv_p;
12610
12611               /* Parse the declarator.  */
12612               declarator 
12613                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12614                                         &ctor_dtor_or_conv_p,
12615                                         /*parenthesized_p=*/NULL,
12616                                         /*member_p=*/true);
12617
12618               /* If something went wrong parsing the declarator, make sure
12619                  that we at least consume some tokens.  */
12620               if (declarator == error_mark_node)
12621                 {
12622                   /* Skip to the end of the statement.  */
12623                   cp_parser_skip_to_end_of_statement (parser);
12624                   /* If the next token is not a semicolon, that is
12625                      probably because we just skipped over the body of
12626                      a function.  So, we consume a semicolon if
12627                      present, but do not issue an error message if it
12628                      is not present.  */
12629                   if (cp_lexer_next_token_is (parser->lexer,
12630                                               CPP_SEMICOLON))
12631                     cp_lexer_consume_token (parser->lexer);
12632                   return;
12633                 }
12634
12635               cp_parser_check_for_definition_in_return_type 
12636                 (declarator, declares_class_or_enum);
12637
12638               /* Look for an asm-specification.  */
12639               asm_specification = cp_parser_asm_specification_opt (parser);
12640               /* Look for attributes that apply to the declaration.  */
12641               attributes = cp_parser_attributes_opt (parser);
12642               /* Remember which attributes are prefix attributes and
12643                  which are not.  */
12644               first_attribute = attributes;
12645               /* Combine the attributes.  */
12646               attributes = chainon (prefix_attributes, attributes);
12647
12648               /* If it's an `=', then we have a constant-initializer or a
12649                  pure-specifier.  It is not correct to parse the
12650                  initializer before registering the member declaration
12651                  since the member declaration should be in scope while
12652                  its initializer is processed.  However, the rest of the
12653                  front end does not yet provide an interface that allows
12654                  us to handle this correctly.  */
12655               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12656                 {
12657                   /* In [class.mem]:
12658
12659                      A pure-specifier shall be used only in the declaration of
12660                      a virtual function.  
12661
12662                      A member-declarator can contain a constant-initializer
12663                      only if it declares a static member of integral or
12664                      enumeration type.  
12665
12666                      Therefore, if the DECLARATOR is for a function, we look
12667                      for a pure-specifier; otherwise, we look for a
12668                      constant-initializer.  When we call `grokfield', it will
12669                      perform more stringent semantics checks.  */
12670                   if (TREE_CODE (declarator) == CALL_EXPR)
12671                     initializer = cp_parser_pure_specifier (parser);
12672                   else
12673                     /* Parse the initializer.  */
12674                     initializer = cp_parser_constant_initializer (parser);
12675                 }
12676               /* Otherwise, there is no initializer.  */
12677               else
12678                 initializer = NULL_TREE;
12679
12680               /* See if we are probably looking at a function
12681                  definition.  We are certainly not looking at at a
12682                  member-declarator.  Calling `grokfield' has
12683                  side-effects, so we must not do it unless we are sure
12684                  that we are looking at a member-declarator.  */
12685               if (cp_parser_token_starts_function_definition_p 
12686                   (cp_lexer_peek_token (parser->lexer)))
12687                 {
12688                   /* The grammar does not allow a pure-specifier to be
12689                      used when a member function is defined.  (It is
12690                      possible that this fact is an oversight in the
12691                      standard, since a pure function may be defined
12692                      outside of the class-specifier.  */
12693                   if (initializer)
12694                     error ("pure-specifier on function-definition");
12695                   decl = cp_parser_save_member_function_body (parser,
12696                                                               decl_specifiers,
12697                                                               declarator,
12698                                                               attributes);
12699                   /* If the member was not a friend, declare it here.  */
12700                   if (!friend_p)
12701                     finish_member_declaration (decl);
12702                   /* Peek at the next token.  */
12703                   token = cp_lexer_peek_token (parser->lexer);
12704                   /* If the next token is a semicolon, consume it.  */
12705                   if (token->type == CPP_SEMICOLON)
12706                     cp_lexer_consume_token (parser->lexer);
12707                   return;
12708                 }
12709               else
12710                 {
12711                   /* Create the declaration.  */
12712                   decl = grokfield (declarator, decl_specifiers, 
12713                                     initializer, asm_specification,
12714                                     attributes);
12715                   /* Any initialization must have been from a
12716                      constant-expression.  */
12717                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12718                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12719                 }
12720             }
12721
12722           /* Reset PREFIX_ATTRIBUTES.  */
12723           while (attributes && TREE_CHAIN (attributes) != first_attribute)
12724             attributes = TREE_CHAIN (attributes);
12725           if (attributes)
12726             TREE_CHAIN (attributes) = NULL_TREE;
12727
12728           /* If there is any qualification still in effect, clear it
12729              now; we will be starting fresh with the next declarator.  */
12730           parser->scope = NULL_TREE;
12731           parser->qualifying_scope = NULL_TREE;
12732           parser->object_scope = NULL_TREE;
12733           /* If it's a `,', then there are more declarators.  */
12734           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12735             cp_lexer_consume_token (parser->lexer);
12736           /* If the next token isn't a `;', then we have a parse error.  */
12737           else if (cp_lexer_next_token_is_not (parser->lexer,
12738                                                CPP_SEMICOLON))
12739             {
12740               cp_parser_error (parser, "expected `;'");
12741               /* Skip tokens until we find a `;'.  */
12742               cp_parser_skip_to_end_of_statement (parser);
12743
12744               break;
12745             }
12746
12747           if (decl)
12748             {
12749               /* Add DECL to the list of members.  */
12750               if (!friend_p)
12751                 finish_member_declaration (decl);
12752
12753               if (TREE_CODE (decl) == FUNCTION_DECL)
12754                 cp_parser_save_default_args (parser, decl);
12755             }
12756         }
12757     }
12758
12759   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12760 }
12761
12762 /* Parse a pure-specifier.
12763
12764    pure-specifier:
12765      = 0
12766
12767    Returns INTEGER_ZERO_NODE if a pure specifier is found.
12768    Otherwise, ERROR_MARK_NODE is returned.  */
12769
12770 static tree
12771 cp_parser_pure_specifier (cp_parser* parser)
12772 {
12773   cp_token *token;
12774
12775   /* Look for the `=' token.  */
12776   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12777     return error_mark_node;
12778   /* Look for the `0' token.  */
12779   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12780   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
12781      to get information from the lexer about how the number was
12782      spelled in order to fix this problem.  */
12783   if (!token || !integer_zerop (token->value))
12784     return error_mark_node;
12785
12786   return integer_zero_node;
12787 }
12788
12789 /* Parse a constant-initializer.
12790
12791    constant-initializer:
12792      = constant-expression
12793
12794    Returns a representation of the constant-expression.  */
12795
12796 static tree
12797 cp_parser_constant_initializer (cp_parser* parser)
12798 {
12799   /* Look for the `=' token.  */
12800   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12801     return error_mark_node;
12802
12803   /* It is invalid to write:
12804
12805        struct S { static const int i = { 7 }; };
12806
12807      */
12808   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12809     {
12810       cp_parser_error (parser,
12811                        "a brace-enclosed initializer is not allowed here");
12812       /* Consume the opening brace.  */
12813       cp_lexer_consume_token (parser->lexer);
12814       /* Skip the initializer.  */
12815       cp_parser_skip_to_closing_brace (parser);
12816       /* Look for the trailing `}'.  */
12817       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12818       
12819       return error_mark_node;
12820     }
12821
12822   return cp_parser_constant_expression (parser, 
12823                                         /*allow_non_constant=*/false,
12824                                         NULL);
12825 }
12826
12827 /* Derived classes [gram.class.derived] */
12828
12829 /* Parse a base-clause.
12830
12831    base-clause:
12832      : base-specifier-list  
12833
12834    base-specifier-list:
12835      base-specifier
12836      base-specifier-list , base-specifier
12837
12838    Returns a TREE_LIST representing the base-classes, in the order in
12839    which they were declared.  The representation of each node is as
12840    described by cp_parser_base_specifier.  
12841
12842    In the case that no bases are specified, this function will return
12843    NULL_TREE, not ERROR_MARK_NODE.  */
12844
12845 static tree
12846 cp_parser_base_clause (cp_parser* parser)
12847 {
12848   tree bases = NULL_TREE;
12849
12850   /* Look for the `:' that begins the list.  */
12851   cp_parser_require (parser, CPP_COLON, "`:'");
12852
12853   /* Scan the base-specifier-list.  */
12854   while (true)
12855     {
12856       cp_token *token;
12857       tree base;
12858
12859       /* Look for the base-specifier.  */
12860       base = cp_parser_base_specifier (parser);
12861       /* Add BASE to the front of the list.  */
12862       if (base != error_mark_node)
12863         {
12864           TREE_CHAIN (base) = bases;
12865           bases = base;
12866         }
12867       /* Peek at the next token.  */
12868       token = cp_lexer_peek_token (parser->lexer);
12869       /* If it's not a comma, then the list is complete.  */
12870       if (token->type != CPP_COMMA)
12871         break;
12872       /* Consume the `,'.  */
12873       cp_lexer_consume_token (parser->lexer);
12874     }
12875
12876   /* PARSER->SCOPE may still be non-NULL at this point, if the last
12877      base class had a qualified name.  However, the next name that
12878      appears is certainly not qualified.  */
12879   parser->scope = NULL_TREE;
12880   parser->qualifying_scope = NULL_TREE;
12881   parser->object_scope = NULL_TREE;
12882
12883   return nreverse (bases);
12884 }
12885
12886 /* Parse a base-specifier.
12887
12888    base-specifier:
12889      :: [opt] nested-name-specifier [opt] class-name
12890      virtual access-specifier [opt] :: [opt] nested-name-specifier
12891        [opt] class-name
12892      access-specifier virtual [opt] :: [opt] nested-name-specifier
12893        [opt] class-name
12894
12895    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
12896    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12897    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
12898    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
12899        
12900 static tree
12901 cp_parser_base_specifier (cp_parser* parser)
12902 {
12903   cp_token *token;
12904   bool done = false;
12905   bool virtual_p = false;
12906   bool duplicate_virtual_error_issued_p = false;
12907   bool duplicate_access_error_issued_p = false;
12908   bool class_scope_p, template_p;
12909   tree access = access_default_node;
12910   tree type;
12911
12912   /* Process the optional `virtual' and `access-specifier'.  */
12913   while (!done)
12914     {
12915       /* Peek at the next token.  */
12916       token = cp_lexer_peek_token (parser->lexer);
12917       /* Process `virtual'.  */
12918       switch (token->keyword)
12919         {
12920         case RID_VIRTUAL:
12921           /* If `virtual' appears more than once, issue an error.  */
12922           if (virtual_p && !duplicate_virtual_error_issued_p)
12923             {
12924               cp_parser_error (parser,
12925                                "`virtual' specified more than once in base-specified");
12926               duplicate_virtual_error_issued_p = true;
12927             }
12928
12929           virtual_p = true;
12930
12931           /* Consume the `virtual' token.  */
12932           cp_lexer_consume_token (parser->lexer);
12933
12934           break;
12935
12936         case RID_PUBLIC:
12937         case RID_PROTECTED:
12938         case RID_PRIVATE:
12939           /* If more than one access specifier appears, issue an
12940              error.  */
12941           if (access != access_default_node
12942               && !duplicate_access_error_issued_p)
12943             {
12944               cp_parser_error (parser,
12945                                "more than one access specifier in base-specified");
12946               duplicate_access_error_issued_p = true;
12947             }
12948
12949           access = ridpointers[(int) token->keyword];
12950
12951           /* Consume the access-specifier.  */
12952           cp_lexer_consume_token (parser->lexer);
12953
12954           break;
12955
12956         default:
12957           done = true;
12958           break;
12959         }
12960     }
12961   /* It is not uncommon to see programs mechanically, errouneously, use
12962      the 'typename' keyword to denote (dependent) qualified types
12963      as base classes.  */
12964   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12965     {
12966       if (!processing_template_decl)
12967         error ("keyword `typename' not allowed outside of templates");
12968       else
12969         error ("keyword `typename' not allowed in this context "
12970                "(the base class is implicitly a type)");
12971       cp_lexer_consume_token (parser->lexer);
12972     }
12973
12974   /* Look for the optional `::' operator.  */
12975   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12976   /* Look for the nested-name-specifier.  The simplest way to
12977      implement:
12978
12979        [temp.res]
12980
12981        The keyword `typename' is not permitted in a base-specifier or
12982        mem-initializer; in these contexts a qualified name that
12983        depends on a template-parameter is implicitly assumed to be a
12984        type name.
12985
12986      is to pretend that we have seen the `typename' keyword at this
12987      point.  */ 
12988   cp_parser_nested_name_specifier_opt (parser,
12989                                        /*typename_keyword_p=*/true,
12990                                        /*check_dependency_p=*/true,
12991                                        /*type_p=*/true,
12992                                        /*is_declaration=*/true);
12993   /* If the base class is given by a qualified name, assume that names
12994      we see are type names or templates, as appropriate.  */
12995   class_scope_p = (parser->scope && TYPE_P (parser->scope));
12996   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12997   
12998   /* Finally, look for the class-name.  */
12999   type = cp_parser_class_name (parser, 
13000                                class_scope_p,
13001                                template_p,
13002                                /*type_p=*/true,
13003                                /*check_dependency_p=*/true,
13004                                /*class_head_p=*/false,
13005                                /*is_declaration=*/true);
13006
13007   if (type == error_mark_node)
13008     return error_mark_node;
13009
13010   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13011 }
13012
13013 /* Exception handling [gram.exception] */
13014
13015 /* Parse an (optional) exception-specification.
13016
13017    exception-specification:
13018      throw ( type-id-list [opt] )
13019
13020    Returns a TREE_LIST representing the exception-specification.  The
13021    TREE_VALUE of each node is a type.  */
13022
13023 static tree
13024 cp_parser_exception_specification_opt (cp_parser* parser)
13025 {
13026   cp_token *token;
13027   tree type_id_list;
13028
13029   /* Peek at the next token.  */
13030   token = cp_lexer_peek_token (parser->lexer);
13031   /* If it's not `throw', then there's no exception-specification.  */
13032   if (!cp_parser_is_keyword (token, RID_THROW))
13033     return NULL_TREE;
13034
13035   /* Consume the `throw'.  */
13036   cp_lexer_consume_token (parser->lexer);
13037
13038   /* Look for the `('.  */
13039   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13040
13041   /* Peek at the next token.  */
13042   token = cp_lexer_peek_token (parser->lexer);
13043   /* If it's not a `)', then there is a type-id-list.  */
13044   if (token->type != CPP_CLOSE_PAREN)
13045     {
13046       const char *saved_message;
13047
13048       /* Types may not be defined in an exception-specification.  */
13049       saved_message = parser->type_definition_forbidden_message;
13050       parser->type_definition_forbidden_message
13051         = "types may not be defined in an exception-specification";
13052       /* Parse the type-id-list.  */
13053       type_id_list = cp_parser_type_id_list (parser);
13054       /* Restore the saved message.  */
13055       parser->type_definition_forbidden_message = saved_message;
13056     }
13057   else
13058     type_id_list = empty_except_spec;
13059
13060   /* Look for the `)'.  */
13061   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13062
13063   return type_id_list;
13064 }
13065
13066 /* Parse an (optional) type-id-list.
13067
13068    type-id-list:
13069      type-id
13070      type-id-list , type-id
13071
13072    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13073    in the order that the types were presented.  */
13074
13075 static tree
13076 cp_parser_type_id_list (cp_parser* parser)
13077 {
13078   tree types = NULL_TREE;
13079
13080   while (true)
13081     {
13082       cp_token *token;
13083       tree type;
13084
13085       /* Get the next type-id.  */
13086       type = cp_parser_type_id (parser);
13087       /* Add it to the list.  */
13088       types = add_exception_specifier (types, type, /*complain=*/1);
13089       /* Peek at the next token.  */
13090       token = cp_lexer_peek_token (parser->lexer);
13091       /* If it is not a `,', we are done.  */
13092       if (token->type != CPP_COMMA)
13093         break;
13094       /* Consume the `,'.  */
13095       cp_lexer_consume_token (parser->lexer);
13096     }
13097
13098   return nreverse (types);
13099 }
13100
13101 /* Parse a try-block.
13102
13103    try-block:
13104      try compound-statement handler-seq  */
13105
13106 static tree
13107 cp_parser_try_block (cp_parser* parser)
13108 {
13109   tree try_block;
13110
13111   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13112   try_block = begin_try_block ();
13113   cp_parser_compound_statement (parser, false);
13114   finish_try_block (try_block);
13115   cp_parser_handler_seq (parser);
13116   finish_handler_sequence (try_block);
13117
13118   return try_block;
13119 }
13120
13121 /* Parse a function-try-block.
13122
13123    function-try-block:
13124      try ctor-initializer [opt] function-body handler-seq  */
13125
13126 static bool
13127 cp_parser_function_try_block (cp_parser* parser)
13128 {
13129   tree try_block;
13130   bool ctor_initializer_p;
13131
13132   /* Look for the `try' keyword.  */
13133   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13134     return false;
13135   /* Let the rest of the front-end know where we are.  */
13136   try_block = begin_function_try_block ();
13137   /* Parse the function-body.  */
13138   ctor_initializer_p 
13139     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13140   /* We're done with the `try' part.  */
13141   finish_function_try_block (try_block);
13142   /* Parse the handlers.  */
13143   cp_parser_handler_seq (parser);
13144   /* We're done with the handlers.  */
13145   finish_function_handler_sequence (try_block);
13146
13147   return ctor_initializer_p;
13148 }
13149
13150 /* Parse a handler-seq.
13151
13152    handler-seq:
13153      handler handler-seq [opt]  */
13154
13155 static void
13156 cp_parser_handler_seq (cp_parser* parser)
13157 {
13158   while (true)
13159     {
13160       cp_token *token;
13161
13162       /* Parse the handler.  */
13163       cp_parser_handler (parser);
13164       /* Peek at the next token.  */
13165       token = cp_lexer_peek_token (parser->lexer);
13166       /* If it's not `catch' then there are no more handlers.  */
13167       if (!cp_parser_is_keyword (token, RID_CATCH))
13168         break;
13169     }
13170 }
13171
13172 /* Parse a handler.
13173
13174    handler:
13175      catch ( exception-declaration ) compound-statement  */
13176
13177 static void
13178 cp_parser_handler (cp_parser* parser)
13179 {
13180   tree handler;
13181   tree declaration;
13182
13183   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13184   handler = begin_handler ();
13185   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13186   declaration = cp_parser_exception_declaration (parser);
13187   finish_handler_parms (declaration, handler);
13188   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13189   cp_parser_compound_statement (parser, false);
13190   finish_handler (handler);
13191 }
13192
13193 /* Parse an exception-declaration.
13194
13195    exception-declaration:
13196      type-specifier-seq declarator
13197      type-specifier-seq abstract-declarator
13198      type-specifier-seq
13199      ...  
13200
13201    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13202    ellipsis variant is used.  */
13203
13204 static tree
13205 cp_parser_exception_declaration (cp_parser* parser)
13206 {
13207   tree type_specifiers;
13208   tree declarator;
13209   const char *saved_message;
13210
13211   /* If it's an ellipsis, it's easy to handle.  */
13212   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13213     {
13214       /* Consume the `...' token.  */
13215       cp_lexer_consume_token (parser->lexer);
13216       return NULL_TREE;
13217     }
13218
13219   /* Types may not be defined in exception-declarations.  */
13220   saved_message = parser->type_definition_forbidden_message;
13221   parser->type_definition_forbidden_message
13222     = "types may not be defined in exception-declarations";
13223
13224   /* Parse the type-specifier-seq.  */
13225   type_specifiers = cp_parser_type_specifier_seq (parser);
13226   /* If it's a `)', then there is no declarator.  */
13227   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13228     declarator = NULL_TREE;
13229   else
13230     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13231                                        /*ctor_dtor_or_conv_p=*/NULL,
13232                                        /*parenthesized_p=*/NULL,
13233                                        /*member_p=*/false);
13234
13235   /* Restore the saved message.  */
13236   parser->type_definition_forbidden_message = saved_message;
13237
13238   return start_handler_parms (type_specifiers, declarator);
13239 }
13240
13241 /* Parse a throw-expression. 
13242
13243    throw-expression:
13244      throw assignment-expression [opt]
13245
13246    Returns a THROW_EXPR representing the throw-expression.  */
13247
13248 static tree
13249 cp_parser_throw_expression (cp_parser* parser)
13250 {
13251   tree expression;
13252   cp_token* token;
13253
13254   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13255   token = cp_lexer_peek_token (parser->lexer);
13256   /* Figure out whether or not there is an assignment-expression
13257      following the "throw" keyword.  */
13258   if (token->type == CPP_COMMA
13259       || token->type == CPP_SEMICOLON
13260       || token->type == CPP_CLOSE_PAREN
13261       || token->type == CPP_CLOSE_SQUARE
13262       || token->type == CPP_CLOSE_BRACE
13263       || token->type == CPP_COLON)
13264     expression = NULL_TREE;
13265   else
13266     expression = cp_parser_assignment_expression (parser);
13267
13268   return build_throw (expression);
13269 }
13270
13271 /* GNU Extensions */
13272
13273 /* Parse an (optional) asm-specification.
13274
13275    asm-specification:
13276      asm ( string-literal )
13277
13278    If the asm-specification is present, returns a STRING_CST
13279    corresponding to the string-literal.  Otherwise, returns
13280    NULL_TREE.  */
13281
13282 static tree
13283 cp_parser_asm_specification_opt (cp_parser* parser)
13284 {
13285   cp_token *token;
13286   tree asm_specification;
13287
13288   /* Peek at the next token.  */
13289   token = cp_lexer_peek_token (parser->lexer);
13290   /* If the next token isn't the `asm' keyword, then there's no 
13291      asm-specification.  */
13292   if (!cp_parser_is_keyword (token, RID_ASM))
13293     return NULL_TREE;
13294
13295   /* Consume the `asm' token.  */
13296   cp_lexer_consume_token (parser->lexer);
13297   /* Look for the `('.  */
13298   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13299
13300   /* Look for the string-literal.  */
13301   token = cp_parser_require (parser, CPP_STRING, "string-literal");
13302   if (token)
13303     asm_specification = token->value;
13304   else
13305     asm_specification = NULL_TREE;
13306
13307   /* Look for the `)'.  */
13308   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13309
13310   return asm_specification;
13311 }
13312
13313 /* Parse an asm-operand-list.  
13314
13315    asm-operand-list:
13316      asm-operand
13317      asm-operand-list , asm-operand
13318      
13319    asm-operand:
13320      string-literal ( expression )  
13321      [ string-literal ] string-literal ( expression )
13322
13323    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13324    each node is the expression.  The TREE_PURPOSE is itself a
13325    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13326    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13327    is a STRING_CST for the string literal before the parenthesis.  */
13328
13329 static tree
13330 cp_parser_asm_operand_list (cp_parser* parser)
13331 {
13332   tree asm_operands = NULL_TREE;
13333
13334   while (true)
13335     {
13336       tree string_literal;
13337       tree expression;
13338       tree name;
13339       cp_token *token;
13340       
13341       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 
13342         {
13343           /* Consume the `[' token.  */
13344           cp_lexer_consume_token (parser->lexer);
13345           /* Read the operand name.  */
13346           name = cp_parser_identifier (parser);
13347           if (name != error_mark_node) 
13348             name = build_string (IDENTIFIER_LENGTH (name),
13349                                  IDENTIFIER_POINTER (name));
13350           /* Look for the closing `]'.  */
13351           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13352         }
13353       else
13354         name = NULL_TREE;
13355       /* Look for the string-literal.  */
13356       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13357       string_literal = token ? token->value : error_mark_node;
13358       /* Look for the `('.  */
13359       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13360       /* Parse the expression.  */
13361       expression = cp_parser_expression (parser);
13362       /* Look for the `)'.  */
13363       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13364       /* Add this operand to the list.  */
13365       asm_operands = tree_cons (build_tree_list (name, string_literal),
13366                                 expression, 
13367                                 asm_operands);
13368       /* If the next token is not a `,', there are no more 
13369          operands.  */
13370       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13371         break;
13372       /* Consume the `,'.  */
13373       cp_lexer_consume_token (parser->lexer);
13374     }
13375
13376   return nreverse (asm_operands);
13377 }
13378
13379 /* Parse an asm-clobber-list.  
13380
13381    asm-clobber-list:
13382      string-literal
13383      asm-clobber-list , string-literal  
13384
13385    Returns a TREE_LIST, indicating the clobbers in the order that they
13386    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13387
13388 static tree
13389 cp_parser_asm_clobber_list (cp_parser* parser)
13390 {
13391   tree clobbers = NULL_TREE;
13392
13393   while (true)
13394     {
13395       cp_token *token;
13396       tree string_literal;
13397
13398       /* Look for the string literal.  */
13399       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13400       string_literal = token ? token->value : error_mark_node;
13401       /* Add it to the list.  */
13402       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13403       /* If the next token is not a `,', then the list is 
13404          complete.  */
13405       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13406         break;
13407       /* Consume the `,' token.  */
13408       cp_lexer_consume_token (parser->lexer);
13409     }
13410
13411   return clobbers;
13412 }
13413
13414 /* Parse an (optional) series of attributes.
13415
13416    attributes:
13417      attributes attribute
13418
13419    attribute:
13420      __attribute__ (( attribute-list [opt] ))  
13421
13422    The return value is as for cp_parser_attribute_list.  */
13423      
13424 static tree
13425 cp_parser_attributes_opt (cp_parser* parser)
13426 {
13427   tree attributes = NULL_TREE;
13428
13429   while (true)
13430     {
13431       cp_token *token;
13432       tree attribute_list;
13433
13434       /* Peek at the next token.  */
13435       token = cp_lexer_peek_token (parser->lexer);
13436       /* If it's not `__attribute__', then we're done.  */
13437       if (token->keyword != RID_ATTRIBUTE)
13438         break;
13439
13440       /* Consume the `__attribute__' keyword.  */
13441       cp_lexer_consume_token (parser->lexer);
13442       /* Look for the two `(' tokens.  */
13443       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13444       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13445
13446       /* Peek at the next token.  */
13447       token = cp_lexer_peek_token (parser->lexer);
13448       if (token->type != CPP_CLOSE_PAREN)
13449         /* Parse the attribute-list.  */
13450         attribute_list = cp_parser_attribute_list (parser);
13451       else
13452         /* If the next token is a `)', then there is no attribute
13453            list.  */
13454         attribute_list = NULL;
13455
13456       /* Look for the two `)' tokens.  */
13457       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13458       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13459
13460       /* Add these new attributes to the list.  */
13461       attributes = chainon (attributes, attribute_list);
13462     }
13463
13464   return attributes;
13465 }
13466
13467 /* Parse an attribute-list.  
13468
13469    attribute-list:  
13470      attribute 
13471      attribute-list , attribute
13472
13473    attribute:
13474      identifier     
13475      identifier ( identifier )
13476      identifier ( identifier , expression-list )
13477      identifier ( expression-list ) 
13478
13479    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13480    TREE_PURPOSE of each node is the identifier indicating which
13481    attribute is in use.  The TREE_VALUE represents the arguments, if
13482    any.  */
13483
13484 static tree
13485 cp_parser_attribute_list (cp_parser* parser)
13486 {
13487   tree attribute_list = NULL_TREE;
13488
13489   while (true)
13490     {
13491       cp_token *token;
13492       tree identifier;
13493       tree attribute;
13494
13495       /* Look for the identifier.  We also allow keywords here; for
13496          example `__attribute__ ((const))' is legal.  */
13497       token = cp_lexer_peek_token (parser->lexer);
13498       if (token->type != CPP_NAME 
13499           && token->type != CPP_KEYWORD)
13500         return error_mark_node;
13501       /* Consume the token.  */
13502       token = cp_lexer_consume_token (parser->lexer);
13503       
13504       /* Save away the identifier that indicates which attribute this is.  */
13505       identifier = token->value;
13506       attribute = build_tree_list (identifier, NULL_TREE);
13507
13508       /* Peek at the next token.  */
13509       token = cp_lexer_peek_token (parser->lexer);
13510       /* If it's an `(', then parse the attribute arguments.  */
13511       if (token->type == CPP_OPEN_PAREN)
13512         {
13513           tree arguments;
13514
13515           arguments = (cp_parser_parenthesized_expression_list 
13516                        (parser, true, /*non_constant_p=*/NULL));
13517           /* Save the identifier and arguments away.  */
13518           TREE_VALUE (attribute) = arguments;
13519         }
13520
13521       /* Add this attribute to the list.  */
13522       TREE_CHAIN (attribute) = attribute_list;
13523       attribute_list = attribute;
13524
13525       /* Now, look for more attributes.  */
13526       token = cp_lexer_peek_token (parser->lexer);
13527       /* If the next token isn't a `,', we're done.  */
13528       if (token->type != CPP_COMMA)
13529         break;
13530
13531       /* Consume the comma and keep going.  */
13532       cp_lexer_consume_token (parser->lexer);
13533     }
13534
13535   /* We built up the list in reverse order.  */
13536   return nreverse (attribute_list);
13537 }
13538
13539 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13540    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13541    current value of the PEDANTIC flag, regardless of whether or not
13542    the `__extension__' keyword is present.  The caller is responsible
13543    for restoring the value of the PEDANTIC flag.  */
13544
13545 static bool
13546 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13547 {
13548   /* Save the old value of the PEDANTIC flag.  */
13549   *saved_pedantic = pedantic;
13550
13551   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13552     {
13553       /* Consume the `__extension__' token.  */
13554       cp_lexer_consume_token (parser->lexer);
13555       /* We're not being pedantic while the `__extension__' keyword is
13556          in effect.  */
13557       pedantic = 0;
13558
13559       return true;
13560     }
13561
13562   return false;
13563 }
13564
13565 /* Parse a label declaration.
13566
13567    label-declaration:
13568      __label__ label-declarator-seq ;
13569
13570    label-declarator-seq:
13571      identifier , label-declarator-seq
13572      identifier  */
13573
13574 static void
13575 cp_parser_label_declaration (cp_parser* parser)
13576 {
13577   /* Look for the `__label__' keyword.  */
13578   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13579
13580   while (true)
13581     {
13582       tree identifier;
13583
13584       /* Look for an identifier.  */
13585       identifier = cp_parser_identifier (parser);
13586       /* Declare it as a lobel.  */
13587       finish_label_decl (identifier);
13588       /* If the next token is a `;', stop.  */
13589       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13590         break;
13591       /* Look for the `,' separating the label declarations.  */
13592       cp_parser_require (parser, CPP_COMMA, "`,'");
13593     }
13594
13595   /* Look for the final `;'.  */
13596   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13597 }
13598
13599 /* Support Functions */
13600
13601 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13602    NAME should have one of the representations used for an
13603    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13604    is returned.  If PARSER->SCOPE is a dependent type, then a
13605    SCOPE_REF is returned.
13606
13607    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13608    returned; the name was already resolved when the TEMPLATE_ID_EXPR
13609    was formed.  Abstractly, such entities should not be passed to this
13610    function, because they do not need to be looked up, but it is
13611    simpler to check for this special case here, rather than at the
13612    call-sites.
13613
13614    In cases not explicitly covered above, this function returns a
13615    DECL, OVERLOAD, or baselink representing the result of the lookup.
13616    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13617    is returned.
13618
13619    If IS_TYPE is TRUE, bindings that do not refer to types are
13620    ignored.
13621
13622    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13623    ignored.
13624
13625    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13626    are ignored.
13627
13628    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13629    types.  */
13630
13631 static tree
13632 cp_parser_lookup_name (cp_parser *parser, tree name, 
13633                        bool is_type, bool is_template, bool is_namespace,
13634                        bool check_dependency)
13635 {
13636   tree decl;
13637   tree object_type = parser->context->object_type;
13638
13639   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13640      no longer valid.  Note that if we are parsing tentatively, and
13641      the parse fails, OBJECT_TYPE will be automatically restored.  */
13642   parser->context->object_type = NULL_TREE;
13643
13644   if (name == error_mark_node)
13645     return error_mark_node;
13646
13647   /* A template-id has already been resolved; there is no lookup to
13648      do.  */
13649   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13650     return name;
13651   if (BASELINK_P (name))
13652     {
13653       my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13654                            == TEMPLATE_ID_EXPR),
13655                           20020909);
13656       return name;
13657     }
13658
13659   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
13660      it should already have been checked to make sure that the name
13661      used matches the type being destroyed.  */
13662   if (TREE_CODE (name) == BIT_NOT_EXPR)
13663     {
13664       tree type;
13665
13666       /* Figure out to which type this destructor applies.  */
13667       if (parser->scope)
13668         type = parser->scope;
13669       else if (object_type)
13670         type = object_type;
13671       else
13672         type = current_class_type;
13673       /* If that's not a class type, there is no destructor.  */
13674       if (!type || !CLASS_TYPE_P (type))
13675         return error_mark_node;
13676       if (!CLASSTYPE_DESTRUCTORS (type))
13677           return error_mark_node;
13678       /* If it was a class type, return the destructor.  */
13679       return CLASSTYPE_DESTRUCTORS (type);
13680     }
13681
13682   /* By this point, the NAME should be an ordinary identifier.  If
13683      the id-expression was a qualified name, the qualifying scope is
13684      stored in PARSER->SCOPE at this point.  */
13685   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13686                       20000619);
13687   
13688   /* Perform the lookup.  */
13689   if (parser->scope)
13690     { 
13691       bool dependent_p;
13692
13693       if (parser->scope == error_mark_node)
13694         return error_mark_node;
13695
13696       /* If the SCOPE is dependent, the lookup must be deferred until
13697          the template is instantiated -- unless we are explicitly
13698          looking up names in uninstantiated templates.  Even then, we
13699          cannot look up the name if the scope is not a class type; it
13700          might, for example, be a template type parameter.  */
13701       dependent_p = (TYPE_P (parser->scope)
13702                      && !(parser->in_declarator_p
13703                           && currently_open_class (parser->scope))
13704                      && dependent_type_p (parser->scope));
13705       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13706            && dependent_p)
13707         {
13708           if (is_type)
13709             /* The resolution to Core Issue 180 says that `struct A::B'
13710                should be considered a type-name, even if `A' is
13711                dependent.  */
13712             decl = TYPE_NAME (make_typename_type (parser->scope,
13713                                                   name,
13714                                                   /*complain=*/1));
13715           else if (is_template)
13716             decl = make_unbound_class_template (parser->scope,
13717                                                 name,
13718                                                 /*complain=*/1);
13719           else
13720             decl = build_nt (SCOPE_REF, parser->scope, name);
13721         }
13722       else
13723         {
13724           bool pop_p = false;
13725
13726           /* If PARSER->SCOPE is a dependent type, then it must be a
13727              class type, and we must not be checking dependencies;
13728              otherwise, we would have processed this lookup above.  So
13729              that PARSER->SCOPE is not considered a dependent base by
13730              lookup_member, we must enter the scope here.  */
13731           if (dependent_p)
13732             pop_p = push_scope (parser->scope);
13733           /* If the PARSER->SCOPE is a a template specialization, it
13734              may be instantiated during name lookup.  In that case,
13735              errors may be issued.  Even if we rollback the current
13736              tentative parse, those errors are valid.  */
13737           decl = lookup_qualified_name (parser->scope, name, is_type,
13738                                         /*complain=*/true);
13739           if (pop_p)
13740             pop_scope (parser->scope);
13741         }
13742       parser->qualifying_scope = parser->scope;
13743       parser->object_scope = NULL_TREE;
13744     }
13745   else if (object_type)
13746     {
13747       tree object_decl = NULL_TREE;
13748       /* Look up the name in the scope of the OBJECT_TYPE, unless the
13749          OBJECT_TYPE is not a class.  */
13750       if (CLASS_TYPE_P (object_type))
13751         /* If the OBJECT_TYPE is a template specialization, it may
13752            be instantiated during name lookup.  In that case, errors
13753            may be issued.  Even if we rollback the current tentative
13754            parse, those errors are valid.  */
13755         object_decl = lookup_member (object_type,
13756                                      name,
13757                                      /*protect=*/0, is_type);
13758       /* Look it up in the enclosing context, too.  */
13759       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13760                                is_namespace,
13761                                /*flags=*/0);
13762       parser->object_scope = object_type;
13763       parser->qualifying_scope = NULL_TREE;
13764       if (object_decl)
13765         decl = object_decl;
13766     }
13767   else
13768     {
13769       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13770                                is_namespace,
13771                                /*flags=*/0);
13772       parser->qualifying_scope = NULL_TREE;
13773       parser->object_scope = NULL_TREE;
13774     }
13775
13776   /* If the lookup failed, let our caller know.  */
13777   if (!decl 
13778       || decl == error_mark_node
13779       || (TREE_CODE (decl) == FUNCTION_DECL 
13780           && DECL_ANTICIPATED (decl)))
13781     return error_mark_node;
13782
13783   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
13784   if (TREE_CODE (decl) == TREE_LIST)
13785     {
13786       /* The error message we have to print is too complicated for
13787          cp_parser_error, so we incorporate its actions directly.  */
13788       if (!cp_parser_simulate_error (parser))
13789         {
13790           error ("reference to `%D' is ambiguous", name);
13791           print_candidates (decl);
13792         }
13793       return error_mark_node;
13794     }
13795
13796   my_friendly_assert (DECL_P (decl) 
13797                       || TREE_CODE (decl) == OVERLOAD
13798                       || TREE_CODE (decl) == SCOPE_REF
13799                       || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13800                       || BASELINK_P (decl),
13801                       20000619);
13802
13803   /* If we have resolved the name of a member declaration, check to
13804      see if the declaration is accessible.  When the name resolves to
13805      set of overloaded functions, accessibility is checked when
13806      overload resolution is done.  
13807
13808      During an explicit instantiation, access is not checked at all,
13809      as per [temp.explicit].  */
13810   if (DECL_P (decl))
13811     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13812
13813   return decl;
13814 }
13815
13816 /* Like cp_parser_lookup_name, but for use in the typical case where
13817    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13818    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
13819
13820 static tree
13821 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13822 {
13823   return cp_parser_lookup_name (parser, name, 
13824                                 /*is_type=*/false,
13825                                 /*is_template=*/false,
13826                                 /*is_namespace=*/false,
13827                                 /*check_dependency=*/true);
13828 }
13829
13830 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13831    the current context, return the TYPE_DECL.  If TAG_NAME_P is
13832    true, the DECL indicates the class being defined in a class-head,
13833    or declared in an elaborated-type-specifier.
13834
13835    Otherwise, return DECL.  */
13836
13837 static tree
13838 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13839 {
13840   /* If the TEMPLATE_DECL is being declared as part of a class-head,
13841      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13842
13843        struct A { 
13844          template <typename T> struct B;
13845        };
13846
13847        template <typename T> struct A::B {}; 
13848    
13849      Similarly, in a elaborated-type-specifier:
13850
13851        namespace N { struct X{}; }
13852
13853        struct A {
13854          template <typename T> friend struct N::X;
13855        };
13856
13857      However, if the DECL refers to a class type, and we are in
13858      the scope of the class, then the name lookup automatically
13859      finds the TYPE_DECL created by build_self_reference rather
13860      than a TEMPLATE_DECL.  For example, in:
13861
13862        template <class T> struct S {
13863          S s;
13864        };
13865
13866      there is no need to handle such case.  */
13867
13868   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13869     return DECL_TEMPLATE_RESULT (decl);
13870
13871   return decl;
13872 }
13873
13874 /* If too many, or too few, template-parameter lists apply to the
13875    declarator, issue an error message.  Returns TRUE if all went well,
13876    and FALSE otherwise.  */
13877
13878 static bool
13879 cp_parser_check_declarator_template_parameters (cp_parser* parser, 
13880                                                 tree declarator)
13881 {
13882   unsigned num_templates;
13883
13884   /* We haven't seen any classes that involve template parameters yet.  */
13885   num_templates = 0;
13886
13887   switch (TREE_CODE (declarator))
13888     {
13889     case CALL_EXPR:
13890     case ARRAY_REF:
13891     case INDIRECT_REF:
13892     case ADDR_EXPR:
13893       {
13894         tree main_declarator = TREE_OPERAND (declarator, 0);
13895         return
13896           cp_parser_check_declarator_template_parameters (parser, 
13897                                                           main_declarator);
13898       }
13899
13900     case SCOPE_REF:
13901       {
13902         tree scope;
13903         tree member;
13904
13905         scope = TREE_OPERAND (declarator, 0);
13906         member = TREE_OPERAND (declarator, 1);
13907
13908         /* If this is a pointer-to-member, then we are not interested
13909            in the SCOPE, because it does not qualify the thing that is
13910            being declared.  */
13911         if (TREE_CODE (member) == INDIRECT_REF)
13912           return (cp_parser_check_declarator_template_parameters
13913                   (parser, member));
13914
13915         while (scope && CLASS_TYPE_P (scope))
13916           {
13917             /* You're supposed to have one `template <...>'
13918                for every template class, but you don't need one
13919                for a full specialization.  For example:
13920                
13921                template <class T> struct S{};
13922                template <> struct S<int> { void f(); };
13923                void S<int>::f () {}
13924                
13925                is correct; there shouldn't be a `template <>' for
13926                the definition of `S<int>::f'.  */
13927             if (CLASSTYPE_TEMPLATE_INFO (scope)
13928                 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13929                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13930                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13931               ++num_templates;
13932
13933             scope = TYPE_CONTEXT (scope);
13934           }
13935       }
13936
13937       /* Fall through.  */
13938
13939     default:
13940       /* If the DECLARATOR has the form `X<y>' then it uses one
13941          additional level of template parameters.  */
13942       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13943         ++num_templates;
13944
13945       return cp_parser_check_template_parameters (parser, 
13946                                                   num_templates);
13947     }
13948 }
13949
13950 /* NUM_TEMPLATES were used in the current declaration.  If that is
13951    invalid, return FALSE and issue an error messages.  Otherwise,
13952    return TRUE.  */
13953
13954 static bool
13955 cp_parser_check_template_parameters (cp_parser* parser,
13956                                      unsigned num_templates)
13957 {
13958   /* If there are more template classes than parameter lists, we have
13959      something like:
13960      
13961        template <class T> void S<T>::R<T>::f ();  */
13962   if (parser->num_template_parameter_lists < num_templates)
13963     {
13964       error ("too few template-parameter-lists");
13965       return false;
13966     }
13967   /* If there are the same number of template classes and parameter
13968      lists, that's OK.  */
13969   if (parser->num_template_parameter_lists == num_templates)
13970     return true;
13971   /* If there are more, but only one more, then we are referring to a
13972      member template.  That's OK too.  */
13973   if (parser->num_template_parameter_lists == num_templates + 1)
13974       return true;
13975   /* Otherwise, there are too many template parameter lists.  We have
13976      something like:
13977
13978      template <class T> template <class U> void S::f();  */
13979   error ("too many template-parameter-lists");
13980   return false;
13981 }
13982
13983 /* Parse a binary-expression of the general form:
13984
13985    binary-expression:
13986      <expr>
13987      binary-expression <token> <expr>
13988
13989    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
13990    to parser the <expr>s.  If the first production is used, then the
13991    value returned by FN is returned directly.  Otherwise, a node with
13992    the indicated EXPR_TYPE is returned, with operands corresponding to
13993    the two sub-expressions.  */
13994
13995 static tree
13996 cp_parser_binary_expression (cp_parser* parser, 
13997                              const cp_parser_token_tree_map token_tree_map, 
13998                              cp_parser_expression_fn fn)
13999 {
14000   tree lhs;
14001
14002   /* Parse the first expression.  */
14003   lhs = (*fn) (parser);
14004   /* Now, look for more expressions.  */
14005   while (true)
14006     {
14007       cp_token *token;
14008       const cp_parser_token_tree_map_node *map_node;
14009       tree rhs;
14010
14011       /* Peek at the next token.  */
14012       token = cp_lexer_peek_token (parser->lexer);
14013       /* If the token is `>', and that's not an operator at the
14014          moment, then we're done.  */
14015       if (token->type == CPP_GREATER
14016           && !parser->greater_than_is_operator_p)
14017         break;
14018       /* If we find one of the tokens we want, build the corresponding
14019          tree representation.  */
14020       for (map_node = token_tree_map; 
14021            map_node->token_type != CPP_EOF;
14022            ++map_node)
14023         if (map_node->token_type == token->type)
14024           {
14025             /* Assume that an overloaded operator will not be used.  */
14026             bool overloaded_p = false;
14027
14028             /* Consume the operator token.  */
14029             cp_lexer_consume_token (parser->lexer);
14030             /* Parse the right-hand side of the expression.  */
14031             rhs = (*fn) (parser);
14032             /* Build the binary tree node.  */
14033             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs, 
14034                                      &overloaded_p);
14035             /* If the binary operator required the use of an
14036                overloaded operator, then this expression cannot be an
14037                integral constant-expression.  An overloaded operator
14038                can be used even if both operands are otherwise
14039                permissible in an integral constant-expression if at
14040                least one of the operands is of enumeration type.  */
14041             if (overloaded_p
14042                 && (cp_parser_non_integral_constant_expression 
14043                     (parser, "calls to overloaded operators")))
14044               lhs = error_mark_node;
14045             break;
14046           }
14047
14048       /* If the token wasn't one of the ones we want, we're done.  */
14049       if (map_node->token_type == CPP_EOF)
14050         break;
14051     }
14052
14053   return lhs;
14054 }
14055
14056 /* Parse an optional `::' token indicating that the following name is
14057    from the global namespace.  If so, PARSER->SCOPE is set to the
14058    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14059    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14060    Returns the new value of PARSER->SCOPE, if the `::' token is
14061    present, and NULL_TREE otherwise.  */
14062
14063 static tree
14064 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14065 {
14066   cp_token *token;
14067
14068   /* Peek at the next token.  */
14069   token = cp_lexer_peek_token (parser->lexer);
14070   /* If we're looking at a `::' token then we're starting from the
14071      global namespace, not our current location.  */
14072   if (token->type == CPP_SCOPE)
14073     {
14074       /* Consume the `::' token.  */
14075       cp_lexer_consume_token (parser->lexer);
14076       /* Set the SCOPE so that we know where to start the lookup.  */
14077       parser->scope = global_namespace;
14078       parser->qualifying_scope = global_namespace;
14079       parser->object_scope = NULL_TREE;
14080
14081       return parser->scope;
14082     }
14083   else if (!current_scope_valid_p)
14084     {
14085       parser->scope = NULL_TREE;
14086       parser->qualifying_scope = NULL_TREE;
14087       parser->object_scope = NULL_TREE;
14088     }
14089
14090   return NULL_TREE;
14091 }
14092
14093 /* Returns TRUE if the upcoming token sequence is the start of a
14094    constructor declarator.  If FRIEND_P is true, the declarator is
14095    preceded by the `friend' specifier.  */
14096
14097 static bool
14098 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14099 {
14100   bool constructor_p;
14101   tree type_decl = NULL_TREE;
14102   bool nested_name_p;
14103   cp_token *next_token;
14104
14105   /* The common case is that this is not a constructor declarator, so
14106      try to avoid doing lots of work if at all possible.  It's not
14107      valid declare a constructor at function scope.  */
14108   if (at_function_scope_p ())
14109     return false;
14110   /* And only certain tokens can begin a constructor declarator.  */
14111   next_token = cp_lexer_peek_token (parser->lexer);
14112   if (next_token->type != CPP_NAME
14113       && next_token->type != CPP_SCOPE
14114       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14115       && next_token->type != CPP_TEMPLATE_ID)
14116     return false;
14117
14118   /* Parse tentatively; we are going to roll back all of the tokens
14119      consumed here.  */
14120   cp_parser_parse_tentatively (parser);
14121   /* Assume that we are looking at a constructor declarator.  */
14122   constructor_p = true;
14123
14124   /* Look for the optional `::' operator.  */
14125   cp_parser_global_scope_opt (parser,
14126                               /*current_scope_valid_p=*/false);
14127   /* Look for the nested-name-specifier.  */
14128   nested_name_p 
14129     = (cp_parser_nested_name_specifier_opt (parser,
14130                                             /*typename_keyword_p=*/false,
14131                                             /*check_dependency_p=*/false,
14132                                             /*type_p=*/false,
14133                                             /*is_declaration=*/false)
14134        != NULL_TREE);
14135   /* Outside of a class-specifier, there must be a
14136      nested-name-specifier.  */
14137   if (!nested_name_p && 
14138       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14139        || friend_p))
14140     constructor_p = false;
14141   /* If we still think that this might be a constructor-declarator,
14142      look for a class-name.  */
14143   if (constructor_p)
14144     {
14145       /* If we have:
14146
14147            template <typename T> struct S { S(); };
14148            template <typename T> S<T>::S ();
14149
14150          we must recognize that the nested `S' names a class.
14151          Similarly, for:
14152
14153            template <typename T> S<T>::S<T> ();
14154
14155          we must recognize that the nested `S' names a template.  */
14156       type_decl = cp_parser_class_name (parser,
14157                                         /*typename_keyword_p=*/false,
14158                                         /*template_keyword_p=*/false,
14159                                         /*type_p=*/false,
14160                                         /*check_dependency_p=*/false,
14161                                         /*class_head_p=*/false,
14162                                         /*is_declaration=*/false);
14163       /* If there was no class-name, then this is not a constructor.  */
14164       constructor_p = !cp_parser_error_occurred (parser);
14165     }
14166
14167   /* If we're still considering a constructor, we have to see a `(',
14168      to begin the parameter-declaration-clause, followed by either a
14169      `)', an `...', or a decl-specifier.  We need to check for a
14170      type-specifier to avoid being fooled into thinking that:
14171
14172        S::S (f) (int);
14173
14174      is a constructor.  (It is actually a function named `f' that
14175      takes one parameter (of type `int') and returns a value of type
14176      `S::S'.  */
14177   if (constructor_p 
14178       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14179     {
14180       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14181           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14182           /* A parameter declaration begins with a decl-specifier,
14183              which is either the "attribute" keyword, a storage class
14184              specifier, or (usually) a type-specifier.  */
14185           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14186           && !cp_parser_storage_class_specifier_opt (parser))
14187         {
14188           tree type;
14189           bool pop_p = false;
14190           unsigned saved_num_template_parameter_lists;
14191
14192           /* Names appearing in the type-specifier should be looked up
14193              in the scope of the class.  */
14194           if (current_class_type)
14195             type = NULL_TREE;
14196           else
14197             {
14198               type = TREE_TYPE (type_decl);
14199               if (TREE_CODE (type) == TYPENAME_TYPE)
14200                 {
14201                   type = resolve_typename_type (type, 
14202                                                 /*only_current_p=*/false);
14203                   if (type == error_mark_node)
14204                     {
14205                       cp_parser_abort_tentative_parse (parser);
14206                       return false;
14207                     }
14208                 }
14209               pop_p = push_scope (type);
14210             }
14211
14212           /* Inside the constructor parameter list, surrounding
14213              template-parameter-lists do not apply.  */
14214           saved_num_template_parameter_lists
14215             = parser->num_template_parameter_lists;
14216           parser->num_template_parameter_lists = 0;
14217
14218           /* Look for the type-specifier.  */
14219           cp_parser_type_specifier (parser,
14220                                     CP_PARSER_FLAGS_NONE,
14221                                     /*is_friend=*/false,
14222                                     /*is_declarator=*/true,
14223                                     /*declares_class_or_enum=*/NULL,
14224                                     /*is_cv_qualifier=*/NULL);
14225
14226           parser->num_template_parameter_lists
14227             = saved_num_template_parameter_lists;
14228
14229           /* Leave the scope of the class.  */
14230           if (pop_p)
14231             pop_scope (type);
14232
14233           constructor_p = !cp_parser_error_occurred (parser);
14234         }
14235     }
14236   else
14237     constructor_p = false;
14238   /* We did not really want to consume any tokens.  */
14239   cp_parser_abort_tentative_parse (parser);
14240
14241   return constructor_p;
14242 }
14243
14244 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14245    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14246    they must be performed once we are in the scope of the function.
14247
14248    Returns the function defined.  */
14249
14250 static tree
14251 cp_parser_function_definition_from_specifiers_and_declarator
14252   (cp_parser* parser,
14253    tree decl_specifiers,
14254    tree attributes,
14255    tree declarator)
14256 {
14257   tree fn;
14258   bool success_p;
14259
14260   /* Begin the function-definition.  */
14261   success_p = begin_function_definition (decl_specifiers, 
14262                                          attributes, 
14263                                          declarator);
14264
14265   /* If there were names looked up in the decl-specifier-seq that we
14266      did not check, check them now.  We must wait until we are in the
14267      scope of the function to perform the checks, since the function
14268      might be a friend.  */
14269   perform_deferred_access_checks ();
14270
14271   if (!success_p)
14272     {
14273       /* If begin_function_definition didn't like the definition, skip
14274          the entire function.  */
14275       error ("invalid function declaration");
14276       cp_parser_skip_to_end_of_block_or_statement (parser);
14277       fn = error_mark_node;
14278     }
14279   else
14280     fn = cp_parser_function_definition_after_declarator (parser,
14281                                                          /*inline_p=*/false);
14282
14283   return fn;
14284 }
14285
14286 /* Parse the part of a function-definition that follows the
14287    declarator.  INLINE_P is TRUE iff this function is an inline
14288    function defined with a class-specifier.
14289
14290    Returns the function defined.  */
14291
14292 static tree 
14293 cp_parser_function_definition_after_declarator (cp_parser* parser, 
14294                                                 bool inline_p)
14295 {
14296   tree fn;
14297   bool ctor_initializer_p = false;
14298   bool saved_in_unbraced_linkage_specification_p;
14299   unsigned saved_num_template_parameter_lists;
14300
14301   /* If the next token is `return', then the code may be trying to
14302      make use of the "named return value" extension that G++ used to
14303      support.  */
14304   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14305     {
14306       /* Consume the `return' keyword.  */
14307       cp_lexer_consume_token (parser->lexer);
14308       /* Look for the identifier that indicates what value is to be
14309          returned.  */
14310       cp_parser_identifier (parser);
14311       /* Issue an error message.  */
14312       error ("named return values are no longer supported");
14313       /* Skip tokens until we reach the start of the function body.  */
14314       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14315              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14316         cp_lexer_consume_token (parser->lexer);
14317     }
14318   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14319      anything declared inside `f'.  */
14320   saved_in_unbraced_linkage_specification_p 
14321     = parser->in_unbraced_linkage_specification_p;
14322   parser->in_unbraced_linkage_specification_p = false;
14323   /* Inside the function, surrounding template-parameter-lists do not
14324      apply.  */
14325   saved_num_template_parameter_lists 
14326     = parser->num_template_parameter_lists; 
14327   parser->num_template_parameter_lists = 0;
14328   /* If the next token is `try', then we are looking at a
14329      function-try-block.  */
14330   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14331     ctor_initializer_p = cp_parser_function_try_block (parser);
14332   /* A function-try-block includes the function-body, so we only do
14333      this next part if we're not processing a function-try-block.  */
14334   else
14335     ctor_initializer_p 
14336       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14337
14338   /* Finish the function.  */
14339   fn = finish_function ((ctor_initializer_p ? 1 : 0) | 
14340                         (inline_p ? 2 : 0));
14341   /* Generate code for it, if necessary.  */
14342   expand_or_defer_fn (fn);
14343   /* Restore the saved values.  */
14344   parser->in_unbraced_linkage_specification_p 
14345     = saved_in_unbraced_linkage_specification_p;
14346   parser->num_template_parameter_lists 
14347     = saved_num_template_parameter_lists;
14348
14349   return fn;
14350 }
14351
14352 /* Parse a template-declaration, assuming that the `export' (and
14353    `extern') keywords, if present, has already been scanned.  MEMBER_P
14354    is as for cp_parser_template_declaration.  */
14355
14356 static void
14357 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14358 {
14359   tree decl = NULL_TREE;
14360   tree parameter_list;
14361   bool friend_p = false;
14362
14363   /* Look for the `template' keyword.  */
14364   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14365     return;
14366       
14367   /* And the `<'.  */
14368   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14369     return;
14370       
14371   /* If the next token is `>', then we have an invalid
14372      specialization.  Rather than complain about an invalid template
14373      parameter, issue an error message here.  */
14374   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14375     {
14376       cp_parser_error (parser, "invalid explicit specialization");
14377       begin_specialization ();
14378       parameter_list = NULL_TREE;
14379     }
14380   else
14381     {
14382       /* Parse the template parameters.  */
14383       begin_template_parm_list ();
14384       parameter_list = cp_parser_template_parameter_list (parser);
14385       parameter_list = end_template_parm_list (parameter_list);
14386     }
14387
14388   /* Look for the `>'.  */
14389   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14390   /* We just processed one more parameter list.  */
14391   ++parser->num_template_parameter_lists;
14392   /* If the next token is `template', there are more template
14393      parameters.  */
14394   if (cp_lexer_next_token_is_keyword (parser->lexer, 
14395                                       RID_TEMPLATE))
14396     cp_parser_template_declaration_after_export (parser, member_p);
14397   else
14398     {
14399       decl = cp_parser_single_declaration (parser,
14400                                            member_p,
14401                                            &friend_p);
14402
14403       /* If this is a member template declaration, let the front
14404          end know.  */
14405       if (member_p && !friend_p && decl)
14406         {
14407           if (TREE_CODE (decl) == TYPE_DECL)
14408             cp_parser_check_access_in_redeclaration (decl);
14409
14410           decl = finish_member_template_decl (decl);
14411         }
14412       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14413         make_friend_class (current_class_type, TREE_TYPE (decl),
14414                            /*complain=*/true);
14415     }
14416   /* We are done with the current parameter list.  */
14417   --parser->num_template_parameter_lists;
14418
14419   /* Finish up.  */
14420   finish_template_decl (parameter_list);
14421
14422   /* Register member declarations.  */
14423   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14424     finish_member_declaration (decl);
14425
14426   /* If DECL is a function template, we must return to parse it later.
14427      (Even though there is no definition, there might be default
14428      arguments that need handling.)  */
14429   if (member_p && decl 
14430       && (TREE_CODE (decl) == FUNCTION_DECL
14431           || DECL_FUNCTION_TEMPLATE_P (decl)))
14432     TREE_VALUE (parser->unparsed_functions_queues)
14433       = tree_cons (NULL_TREE, decl, 
14434                    TREE_VALUE (parser->unparsed_functions_queues));
14435 }
14436
14437 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14438    `function-definition' sequence.  MEMBER_P is true, this declaration
14439    appears in a class scope.
14440
14441    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14442    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14443
14444 static tree
14445 cp_parser_single_declaration (cp_parser* parser, 
14446                               bool member_p,
14447                               bool* friend_p)
14448 {
14449   int declares_class_or_enum;
14450   tree decl = NULL_TREE;
14451   tree decl_specifiers;
14452   tree attributes;
14453   bool function_definition_p = false;
14454
14455   /* Defer access checks until we know what is being declared.  */
14456   push_deferring_access_checks (dk_deferred);
14457
14458   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14459      alternative.  */
14460   decl_specifiers 
14461     = cp_parser_decl_specifier_seq (parser,
14462                                     CP_PARSER_FLAGS_OPTIONAL,
14463                                     &attributes,
14464                                     &declares_class_or_enum);
14465   if (friend_p)
14466     *friend_p = cp_parser_friend_p (decl_specifiers);
14467   /* Gather up the access checks that occurred the
14468      decl-specifier-seq.  */
14469   stop_deferring_access_checks ();
14470
14471   /* Check for the declaration of a template class.  */
14472   if (declares_class_or_enum)
14473     {
14474       if (cp_parser_declares_only_class_p (parser))
14475         {
14476           decl = shadow_tag (decl_specifiers);
14477           if (decl)
14478             decl = TYPE_NAME (decl);
14479           else
14480             decl = error_mark_node;
14481         }
14482     }
14483   else
14484     decl = NULL_TREE;
14485   /* If it's not a template class, try for a template function.  If
14486      the next token is a `;', then this declaration does not declare
14487      anything.  But, if there were errors in the decl-specifiers, then
14488      the error might well have come from an attempted class-specifier.
14489      In that case, there's no need to warn about a missing declarator.  */
14490   if (!decl
14491       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14492           || !value_member (error_mark_node, decl_specifiers)))
14493     decl = cp_parser_init_declarator (parser, 
14494                                       decl_specifiers,
14495                                       attributes,
14496                                       /*function_definition_allowed_p=*/true,
14497                                       member_p,
14498                                       declares_class_or_enum,
14499                                       &function_definition_p);
14500
14501   pop_deferring_access_checks ();
14502
14503   /* Clear any current qualification; whatever comes next is the start
14504      of something new.  */
14505   parser->scope = NULL_TREE;
14506   parser->qualifying_scope = NULL_TREE;
14507   parser->object_scope = NULL_TREE;
14508   /* Look for a trailing `;' after the declaration.  */
14509   if (!function_definition_p
14510       && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14511     cp_parser_skip_to_end_of_block_or_statement (parser);
14512
14513   return decl;
14514 }
14515
14516 /* Parse a cast-expression that is not the operand of a unary "&".  */
14517
14518 static tree
14519 cp_parser_simple_cast_expression (cp_parser *parser)
14520 {
14521   return cp_parser_cast_expression (parser, /*address_p=*/false);
14522 }
14523
14524 /* Parse a functional cast to TYPE.  Returns an expression
14525    representing the cast.  */
14526
14527 static tree
14528 cp_parser_functional_cast (cp_parser* parser, tree type)
14529 {
14530   tree expression_list;
14531   tree cast;
14532
14533   expression_list
14534     = cp_parser_parenthesized_expression_list (parser, false,
14535                                                /*non_constant_p=*/NULL);
14536
14537   cast = build_functional_cast (type, expression_list);
14538   /* [expr.const]/1: In an integral constant expression "only type
14539      conversions to integral or enumeration type can be used".  */
14540   if (cast != error_mark_node && !type_dependent_expression_p (type) 
14541       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14542     {
14543       if (cp_parser_non_integral_constant_expression 
14544           (parser, "a call to a constructor"))
14545         return error_mark_node;
14546     }
14547   return cast;
14548 }
14549
14550 /* Save the tokens that make up the body of a member function defined
14551    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
14552    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
14553    specifiers applied to the declaration.  Returns the FUNCTION_DECL
14554    for the member function.  */
14555
14556 static tree
14557 cp_parser_save_member_function_body (cp_parser* parser,
14558                                      tree decl_specifiers,
14559                                      tree declarator,
14560                                      tree attributes)
14561 {
14562   cp_token_cache *cache;
14563   tree fn;
14564
14565   /* Create the function-declaration.  */
14566   fn = start_method (decl_specifiers, declarator, attributes);
14567   /* If something went badly wrong, bail out now.  */
14568   if (fn == error_mark_node)
14569     {
14570       /* If there's a function-body, skip it.  */
14571       if (cp_parser_token_starts_function_definition_p 
14572           (cp_lexer_peek_token (parser->lexer)))
14573         cp_parser_skip_to_end_of_block_or_statement (parser);
14574       return error_mark_node;
14575     }
14576
14577   /* Remember it, if there default args to post process.  */
14578   cp_parser_save_default_args (parser, fn);
14579
14580   /* Create a token cache.  */
14581   cache = cp_token_cache_new ();
14582   /* Save away the tokens that make up the body of the 
14583      function.  */
14584   cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14585   /* Handle function try blocks.  */
14586   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14587     cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14588
14589   /* Save away the inline definition; we will process it when the
14590      class is complete.  */
14591   DECL_PENDING_INLINE_INFO (fn) = cache;
14592   DECL_PENDING_INLINE_P (fn) = 1;
14593
14594   /* We need to know that this was defined in the class, so that
14595      friend templates are handled correctly.  */
14596   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14597
14598   /* We're done with the inline definition.  */
14599   finish_method (fn);
14600
14601   /* Add FN to the queue of functions to be parsed later.  */
14602   TREE_VALUE (parser->unparsed_functions_queues)
14603     = tree_cons (NULL_TREE, fn, 
14604                  TREE_VALUE (parser->unparsed_functions_queues));
14605
14606   return fn;
14607 }
14608
14609 /* Parse a template-argument-list, as well as the trailing ">" (but
14610    not the opening ">").  See cp_parser_template_argument_list for the
14611    return value.  */
14612
14613 static tree
14614 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14615 {
14616   tree arguments;
14617   tree saved_scope;
14618   tree saved_qualifying_scope;
14619   tree saved_object_scope;
14620   bool saved_greater_than_is_operator_p;
14621
14622   /* [temp.names]
14623
14624      When parsing a template-id, the first non-nested `>' is taken as
14625      the end of the template-argument-list rather than a greater-than
14626      operator.  */
14627   saved_greater_than_is_operator_p 
14628     = parser->greater_than_is_operator_p;
14629   parser->greater_than_is_operator_p = false;
14630   /* Parsing the argument list may modify SCOPE, so we save it
14631      here.  */
14632   saved_scope = parser->scope;
14633   saved_qualifying_scope = parser->qualifying_scope;
14634   saved_object_scope = parser->object_scope;
14635   /* Parse the template-argument-list itself.  */
14636   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14637     arguments = NULL_TREE;
14638   else
14639     arguments = cp_parser_template_argument_list (parser);
14640   /* Look for the `>' that ends the template-argument-list. If we find
14641      a '>>' instead, it's probably just a typo.  */
14642   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14643     {
14644       if (!saved_greater_than_is_operator_p)
14645         {
14646           /* If we're in a nested template argument list, the '>>' has to be
14647             a typo for '> >'. We emit the error message, but we continue
14648             parsing and we push a '>' as next token, so that the argument
14649             list will be parsed correctly..  */
14650           cp_token* token;
14651           error ("`>>' should be `> >' within a nested template argument list");
14652           token = cp_lexer_peek_token (parser->lexer);
14653           token->type = CPP_GREATER;
14654         }
14655       else
14656         {
14657           /* If this is not a nested template argument list, the '>>' is
14658             a typo for '>'. Emit an error message and continue.  */
14659           error ("spurious `>>', use `>' to terminate a template argument list");
14660           cp_lexer_consume_token (parser->lexer);
14661         }
14662     }
14663   else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14664     error ("missing `>' to terminate the template argument list");
14665   /* The `>' token might be a greater-than operator again now.  */
14666   parser->greater_than_is_operator_p 
14667     = saved_greater_than_is_operator_p;
14668   /* Restore the SAVED_SCOPE.  */
14669   parser->scope = saved_scope;
14670   parser->qualifying_scope = saved_qualifying_scope;
14671   parser->object_scope = saved_object_scope;
14672
14673   return arguments;
14674 }
14675
14676 /* MEMBER_FUNCTION is a member function, or a friend.  If default
14677    arguments, or the body of the function have not yet been parsed,
14678    parse them now.  */
14679
14680 static void
14681 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14682 {
14683   cp_lexer *saved_lexer;
14684
14685   /* If this member is a template, get the underlying
14686      FUNCTION_DECL.  */
14687   if (DECL_FUNCTION_TEMPLATE_P (member_function))
14688     member_function = DECL_TEMPLATE_RESULT (member_function);
14689
14690   /* There should not be any class definitions in progress at this
14691      point; the bodies of members are only parsed outside of all class
14692      definitions.  */
14693   my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14694   /* While we're parsing the member functions we might encounter more
14695      classes.  We want to handle them right away, but we don't want
14696      them getting mixed up with functions that are currently in the
14697      queue.  */
14698   parser->unparsed_functions_queues
14699     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14700
14701   /* Make sure that any template parameters are in scope.  */
14702   maybe_begin_member_template_processing (member_function);
14703
14704   /* If the body of the function has not yet been parsed, parse it
14705      now.  */
14706   if (DECL_PENDING_INLINE_P (member_function))
14707     {
14708       tree function_scope;
14709       cp_token_cache *tokens;
14710
14711       /* The function is no longer pending; we are processing it.  */
14712       tokens = DECL_PENDING_INLINE_INFO (member_function);
14713       DECL_PENDING_INLINE_INFO (member_function) = NULL;
14714       DECL_PENDING_INLINE_P (member_function) = 0;
14715       /* If this was an inline function in a local class, enter the scope
14716          of the containing function.  */
14717       function_scope = decl_function_context (member_function);
14718       if (function_scope)
14719         push_function_context_to (function_scope);
14720       
14721       /* Save away the current lexer.  */
14722       saved_lexer = parser->lexer;
14723       /* Make a new lexer to feed us the tokens saved for this function.  */
14724       parser->lexer = cp_lexer_new_from_tokens (tokens);
14725       parser->lexer->next = saved_lexer;
14726       
14727       /* Set the current source position to be the location of the first
14728          token in the saved inline body.  */
14729       cp_lexer_peek_token (parser->lexer);
14730       
14731       /* Let the front end know that we going to be defining this
14732          function.  */
14733       start_function (NULL_TREE, member_function, NULL_TREE,
14734                       SF_PRE_PARSED | SF_INCLASS_INLINE);
14735       
14736       /* Now, parse the body of the function.  */
14737       cp_parser_function_definition_after_declarator (parser,
14738                                                       /*inline_p=*/true);
14739       
14740       /* Leave the scope of the containing function.  */
14741       if (function_scope)
14742         pop_function_context_from (function_scope);
14743       /* Restore the lexer.  */
14744       parser->lexer = saved_lexer;
14745     }
14746
14747   /* Remove any template parameters from the symbol table.  */
14748   maybe_end_member_template_processing ();
14749
14750   /* Restore the queue.  */
14751   parser->unparsed_functions_queues 
14752     = TREE_CHAIN (parser->unparsed_functions_queues);
14753 }
14754
14755 /* If DECL contains any default args, remember it on the unparsed
14756    functions queue.  */
14757
14758 static void
14759 cp_parser_save_default_args (cp_parser* parser, tree decl)
14760 {
14761   tree probe;
14762
14763   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14764        probe;
14765        probe = TREE_CHAIN (probe))
14766     if (TREE_PURPOSE (probe))
14767       {
14768         TREE_PURPOSE (parser->unparsed_functions_queues)
14769           = tree_cons (NULL_TREE, decl, 
14770                        TREE_PURPOSE (parser->unparsed_functions_queues));
14771         break;
14772       }
14773   return;
14774 }
14775
14776 /* FN is a FUNCTION_DECL which may contains a parameter with an
14777    unparsed DEFAULT_ARG.  Parse the default args now.  */
14778
14779 static void
14780 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14781 {
14782   cp_lexer *saved_lexer;
14783   cp_token_cache *tokens;
14784   bool saved_local_variables_forbidden_p;
14785   tree parameters;
14786
14787   /* While we're parsing the default args, we might (due to the
14788      statement expression extension) encounter more classes.  We want
14789      to handle them right away, but we don't want them getting mixed
14790      up with default args that are currently in the queue.  */
14791   parser->unparsed_functions_queues
14792     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14793
14794   for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14795        parameters;
14796        parameters = TREE_CHAIN (parameters))
14797     {
14798       if (!TREE_PURPOSE (parameters)
14799           || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14800         continue;
14801   
14802        /* Save away the current lexer.  */
14803       saved_lexer = parser->lexer;
14804        /* Create a new one, using the tokens we have saved.  */
14805       tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
14806       parser->lexer = cp_lexer_new_from_tokens (tokens);
14807
14808        /* Set the current source position to be the location of the
14809           first token in the default argument.  */
14810       cp_lexer_peek_token (parser->lexer);
14811
14812        /* Local variable names (and the `this' keyword) may not appear
14813           in a default argument.  */
14814       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14815       parser->local_variables_forbidden_p = true;
14816        /* Parse the assignment-expression.  */
14817       if (DECL_CLASS_SCOPE_P (fn))
14818         push_nested_class (DECL_CONTEXT (fn));
14819       TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14820       if (DECL_CLASS_SCOPE_P (fn))
14821         pop_nested_class ();
14822
14823       /* If the token stream has not been completely used up, then
14824          there was extra junk after the end of the default
14825          argument.  */
14826       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14827         cp_parser_error (parser, "expected `,'");
14828
14829        /* Restore saved state.  */
14830       parser->lexer = saved_lexer;
14831       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14832     }
14833
14834   /* Restore the queue.  */
14835   parser->unparsed_functions_queues 
14836     = TREE_CHAIN (parser->unparsed_functions_queues);
14837 }
14838
14839 /* Parse the operand of `sizeof' (or a similar operator).  Returns
14840    either a TYPE or an expression, depending on the form of the
14841    input.  The KEYWORD indicates which kind of expression we have
14842    encountered.  */
14843
14844 static tree
14845 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14846 {
14847   static const char *format;
14848   tree expr = NULL_TREE;
14849   const char *saved_message;
14850   bool saved_integral_constant_expression_p;
14851
14852   /* Initialize FORMAT the first time we get here.  */
14853   if (!format)
14854     format = "types may not be defined in `%s' expressions";
14855
14856   /* Types cannot be defined in a `sizeof' expression.  Save away the
14857      old message.  */
14858   saved_message = parser->type_definition_forbidden_message;
14859   /* And create the new one.  */
14860   parser->type_definition_forbidden_message 
14861     = xmalloc (strlen (format) 
14862                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14863                + 1 /* `\0' */);
14864   sprintf ((char *) parser->type_definition_forbidden_message,
14865            format, IDENTIFIER_POINTER (ridpointers[keyword]));
14866
14867   /* The restrictions on constant-expressions do not apply inside
14868      sizeof expressions.  */
14869   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14870   parser->integral_constant_expression_p = false;
14871
14872   /* Do not actually evaluate the expression.  */
14873   ++skip_evaluation;
14874   /* If it's a `(', then we might be looking at the type-id
14875      construction.  */
14876   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14877     {
14878       tree type;
14879       bool saved_in_type_id_in_expr_p;
14880
14881       /* We can't be sure yet whether we're looking at a type-id or an
14882          expression.  */
14883       cp_parser_parse_tentatively (parser);
14884       /* Consume the `('.  */
14885       cp_lexer_consume_token (parser->lexer);
14886       /* Parse the type-id.  */
14887       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14888       parser->in_type_id_in_expr_p = true;
14889       type = cp_parser_type_id (parser);
14890       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14891       /* Now, look for the trailing `)'.  */
14892       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14893       /* If all went well, then we're done.  */
14894       if (cp_parser_parse_definitely (parser))
14895         {
14896           /* Build a list of decl-specifiers; right now, we have only
14897              a single type-specifier.  */
14898           type = build_tree_list (NULL_TREE,
14899                                   type);
14900
14901           /* Call grokdeclarator to figure out what type this is.  */
14902           expr = grokdeclarator (NULL_TREE,
14903                                  type,
14904                                  TYPENAME,
14905                                  /*initialized=*/0,
14906                                  /*attrlist=*/NULL);
14907         }
14908     }
14909
14910   /* If the type-id production did not work out, then we must be
14911      looking at the unary-expression production.  */
14912   if (!expr)
14913     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14914   /* Go back to evaluating expressions.  */
14915   --skip_evaluation;
14916
14917   /* Free the message we created.  */
14918   free ((char *) parser->type_definition_forbidden_message);
14919   /* And restore the old one.  */
14920   parser->type_definition_forbidden_message = saved_message;
14921   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14922
14923   return expr;
14924 }
14925
14926 /* If the current declaration has no declarator, return true.  */
14927
14928 static bool
14929 cp_parser_declares_only_class_p (cp_parser *parser)
14930 {
14931   /* If the next token is a `;' or a `,' then there is no 
14932      declarator.  */
14933   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14934           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14935 }
14936
14937 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14938    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
14939
14940 static bool
14941 cp_parser_friend_p (tree decl_specifiers)
14942 {
14943   while (decl_specifiers)
14944     {
14945       /* See if this decl-specifier is `friend'.  */
14946       if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14947           && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14948         return true;
14949
14950       /* Go on to the next decl-specifier.  */
14951       decl_specifiers = TREE_CHAIN (decl_specifiers);
14952     }
14953
14954   return false;
14955 }
14956
14957 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
14958    issue an error message indicating that TOKEN_DESC was expected.
14959    
14960    Returns the token consumed, if the token had the appropriate type.
14961    Otherwise, returns NULL.  */
14962
14963 static cp_token *
14964 cp_parser_require (cp_parser* parser,
14965                    enum cpp_ttype type,
14966                    const char* token_desc)
14967 {
14968   if (cp_lexer_next_token_is (parser->lexer, type))
14969     return cp_lexer_consume_token (parser->lexer);
14970   else
14971     {
14972       /* Output the MESSAGE -- unless we're parsing tentatively.  */
14973       if (!cp_parser_simulate_error (parser))
14974         {
14975           char *message = concat ("expected ", token_desc, NULL);
14976           cp_parser_error (parser, message);
14977           free (message);
14978         }
14979       return NULL;
14980     }
14981 }
14982
14983 /* Like cp_parser_require, except that tokens will be skipped until
14984    the desired token is found.  An error message is still produced if
14985    the next token is not as expected.  */
14986
14987 static void
14988 cp_parser_skip_until_found (cp_parser* parser, 
14989                             enum cpp_ttype type, 
14990                             const char* token_desc)
14991 {
14992   cp_token *token;
14993   unsigned nesting_depth = 0;
14994
14995   if (cp_parser_require (parser, type, token_desc))
14996     return;
14997
14998   /* Skip tokens until the desired token is found.  */
14999   while (true)
15000     {
15001       /* Peek at the next token.  */
15002       token = cp_lexer_peek_token (parser->lexer);
15003       /* If we've reached the token we want, consume it and 
15004          stop.  */
15005       if (token->type == type && !nesting_depth)
15006         {
15007           cp_lexer_consume_token (parser->lexer);
15008           return;
15009         }
15010       /* If we've run out of tokens, stop.  */
15011       if (token->type == CPP_EOF)
15012         return;
15013       if (token->type == CPP_OPEN_BRACE 
15014           || token->type == CPP_OPEN_PAREN
15015           || token->type == CPP_OPEN_SQUARE)
15016         ++nesting_depth;
15017       else if (token->type == CPP_CLOSE_BRACE 
15018                || token->type == CPP_CLOSE_PAREN
15019                || token->type == CPP_CLOSE_SQUARE)
15020         {
15021           if (nesting_depth-- == 0)
15022             return;
15023         }
15024       /* Consume this token.  */
15025       cp_lexer_consume_token (parser->lexer);
15026     }
15027 }
15028
15029 /* If the next token is the indicated keyword, consume it.  Otherwise,
15030    issue an error message indicating that TOKEN_DESC was expected.
15031    
15032    Returns the token consumed, if the token had the appropriate type.
15033    Otherwise, returns NULL.  */
15034
15035 static cp_token *
15036 cp_parser_require_keyword (cp_parser* parser,
15037                            enum rid keyword,
15038                            const char* token_desc)
15039 {
15040   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15041
15042   if (token && token->keyword != keyword)
15043     {
15044       dyn_string_t error_msg;
15045
15046       /* Format the error message.  */
15047       error_msg = dyn_string_new (0);
15048       dyn_string_append_cstr (error_msg, "expected ");
15049       dyn_string_append_cstr (error_msg, token_desc);
15050       cp_parser_error (parser, error_msg->s);
15051       dyn_string_delete (error_msg);
15052       return NULL;
15053     }
15054
15055   return token;
15056 }
15057
15058 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15059    function-definition.  */
15060
15061 static bool 
15062 cp_parser_token_starts_function_definition_p (cp_token* token)
15063 {
15064   return (/* An ordinary function-body begins with an `{'.  */
15065           token->type == CPP_OPEN_BRACE
15066           /* A ctor-initializer begins with a `:'.  */
15067           || token->type == CPP_COLON
15068           /* A function-try-block begins with `try'.  */
15069           || token->keyword == RID_TRY
15070           /* The named return value extension begins with `return'.  */
15071           || token->keyword == RID_RETURN);
15072 }
15073
15074 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15075    definition.  */
15076
15077 static bool
15078 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15079 {
15080   cp_token *token;
15081
15082   token = cp_lexer_peek_token (parser->lexer);
15083   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15084 }
15085
15086 /* Returns TRUE iff the next token is the "," or ">" ending a
15087    template-argument.   */
15088
15089 static bool
15090 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15091 {
15092   cp_token *token;
15093
15094   token = cp_lexer_peek_token (parser->lexer);
15095   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15096 }
15097
15098 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15099    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15100
15101 static bool
15102 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser, 
15103                                                      size_t n)
15104 {
15105   cp_token *token;
15106
15107   token = cp_lexer_peek_nth_token (parser->lexer, n);
15108   if (token->type == CPP_LESS)
15109     return true;
15110   /* Check for the sequence `<::' in the original code. It would be lexed as
15111      `[:', where `[' is a digraph, and there is no whitespace before
15112      `:'.  */
15113   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15114     {
15115       cp_token *token2;
15116       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15117       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15118         return true;
15119     }
15120   return false;
15121 }
15122  
15123 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15124    or none_type otherwise.  */
15125
15126 static enum tag_types
15127 cp_parser_token_is_class_key (cp_token* token)
15128 {
15129   switch (token->keyword)
15130     {
15131     case RID_CLASS:
15132       return class_type;
15133     case RID_STRUCT:
15134       return record_type;
15135     case RID_UNION:
15136       return union_type;
15137       
15138     default:
15139       return none_type;
15140     }
15141 }
15142
15143 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15144
15145 static void
15146 cp_parser_check_class_key (enum tag_types class_key, tree type)
15147 {
15148   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15149     pedwarn ("`%s' tag used in naming `%#T'",
15150             class_key == union_type ? "union"
15151              : class_key == record_type ? "struct" : "class", 
15152              type);
15153 }
15154                            
15155 /* Issue an error message if DECL is redeclared with different
15156    access than its original declaration [class.access.spec/3].
15157    This applies to nested classes and nested class templates.
15158    [class.mem/1].  */
15159
15160 static void cp_parser_check_access_in_redeclaration (tree decl)
15161 {
15162   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15163     return;
15164
15165   if ((TREE_PRIVATE (decl)
15166        != (current_access_specifier == access_private_node))
15167       || (TREE_PROTECTED (decl)
15168           != (current_access_specifier == access_protected_node)))
15169     error ("%D redeclared with different access", decl);
15170 }
15171
15172 /* Look for the `template' keyword, as a syntactic disambiguator.
15173    Return TRUE iff it is present, in which case it will be 
15174    consumed.  */
15175
15176 static bool
15177 cp_parser_optional_template_keyword (cp_parser *parser)
15178 {
15179   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15180     {
15181       /* The `template' keyword can only be used within templates;
15182          outside templates the parser can always figure out what is a
15183          template and what is not.  */
15184       if (!processing_template_decl)
15185         {
15186           error ("`template' (as a disambiguator) is only allowed "
15187                  "within templates");
15188           /* If this part of the token stream is rescanned, the same
15189              error message would be generated.  So, we purge the token
15190              from the stream.  */
15191           cp_lexer_purge_token (parser->lexer);
15192           return false;
15193         }
15194       else
15195         {
15196           /* Consume the `template' keyword.  */
15197           cp_lexer_consume_token (parser->lexer);
15198           return true;
15199         }
15200     }
15201
15202   return false;
15203 }
15204
15205 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15206    set PARSER->SCOPE, and perform other related actions.  */
15207
15208 static void
15209 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15210 {
15211   tree value;
15212   tree check;
15213
15214   /* Get the stored value.  */
15215   value = cp_lexer_consume_token (parser->lexer)->value;
15216   /* Perform any access checks that were deferred.  */
15217   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15218     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15219   /* Set the scope from the stored value.  */
15220   parser->scope = TREE_VALUE (value);
15221   parser->qualifying_scope = TREE_TYPE (value);
15222   parser->object_scope = NULL_TREE;
15223 }
15224
15225 /* Add tokens to CACHE until an non-nested END token appears.  */
15226
15227 static void
15228 cp_parser_cache_group (cp_parser *parser, 
15229                        cp_token_cache *cache,
15230                        enum cpp_ttype end,
15231                        unsigned depth)
15232 {
15233   while (true)
15234     {
15235       cp_token *token;
15236
15237       /* Abort a parenthesized expression if we encounter a brace.  */
15238       if ((end == CPP_CLOSE_PAREN || depth == 0)
15239           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15240         return;
15241       /* If we've reached the end of the file, stop.  */
15242       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15243         return;
15244       /* Consume the next token.  */
15245       token = cp_lexer_consume_token (parser->lexer);
15246       /* Add this token to the tokens we are saving.  */
15247       cp_token_cache_push_token (cache, token);
15248       /* See if it starts a new group.  */
15249       if (token->type == CPP_OPEN_BRACE)
15250         {
15251           cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15252           if (depth == 0)
15253             return;
15254         }
15255       else if (token->type == CPP_OPEN_PAREN)
15256         cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15257       else if (token->type == end)
15258         return;
15259     }
15260 }
15261
15262 /* Begin parsing tentatively.  We always save tokens while parsing
15263    tentatively so that if the tentative parsing fails we can restore the
15264    tokens.  */
15265
15266 static void
15267 cp_parser_parse_tentatively (cp_parser* parser)
15268 {
15269   /* Enter a new parsing context.  */
15270   parser->context = cp_parser_context_new (parser->context);
15271   /* Begin saving tokens.  */
15272   cp_lexer_save_tokens (parser->lexer);
15273   /* In order to avoid repetitive access control error messages,
15274      access checks are queued up until we are no longer parsing
15275      tentatively.  */
15276   push_deferring_access_checks (dk_deferred);
15277 }
15278
15279 /* Commit to the currently active tentative parse.  */
15280
15281 static void
15282 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15283 {
15284   cp_parser_context *context;
15285   cp_lexer *lexer;
15286
15287   /* Mark all of the levels as committed.  */
15288   lexer = parser->lexer;
15289   for (context = parser->context; context->next; context = context->next)
15290     {
15291       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15292         break;
15293       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15294       while (!cp_lexer_saving_tokens (lexer))
15295         lexer = lexer->next;
15296       cp_lexer_commit_tokens (lexer);
15297     }
15298 }
15299
15300 /* Abort the currently active tentative parse.  All consumed tokens
15301    will be rolled back, and no diagnostics will be issued.  */
15302
15303 static void
15304 cp_parser_abort_tentative_parse (cp_parser* parser)
15305 {
15306   cp_parser_simulate_error (parser);
15307   /* Now, pretend that we want to see if the construct was
15308      successfully parsed.  */
15309   cp_parser_parse_definitely (parser);
15310 }
15311
15312 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15313    token stream.  Otherwise, commit to the tokens we have consumed.
15314    Returns true if no error occurred; false otherwise.  */
15315
15316 static bool
15317 cp_parser_parse_definitely (cp_parser* parser)
15318 {
15319   bool error_occurred;
15320   cp_parser_context *context;
15321
15322   /* Remember whether or not an error occurred, since we are about to
15323      destroy that information.  */
15324   error_occurred = cp_parser_error_occurred (parser);
15325   /* Remove the topmost context from the stack.  */
15326   context = parser->context;
15327   parser->context = context->next;
15328   /* If no parse errors occurred, commit to the tentative parse.  */
15329   if (!error_occurred)
15330     {
15331       /* Commit to the tokens read tentatively, unless that was
15332          already done.  */
15333       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15334         cp_lexer_commit_tokens (parser->lexer);
15335
15336       pop_to_parent_deferring_access_checks ();
15337     }
15338   /* Otherwise, if errors occurred, roll back our state so that things
15339      are just as they were before we began the tentative parse.  */
15340   else
15341     {
15342       cp_lexer_rollback_tokens (parser->lexer);
15343       pop_deferring_access_checks ();
15344     }
15345   /* Add the context to the front of the free list.  */
15346   context->next = cp_parser_context_free_list;
15347   cp_parser_context_free_list = context;
15348
15349   return !error_occurred;
15350 }
15351
15352 /* Returns true if we are parsing tentatively -- but have decided that
15353    we will stick with this tentative parse, even if errors occur.  */
15354
15355 static bool
15356 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15357 {
15358   return (cp_parser_parsing_tentatively (parser)
15359           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15360 }
15361
15362 /* Returns nonzero iff an error has occurred during the most recent
15363    tentative parse.  */
15364    
15365 static bool
15366 cp_parser_error_occurred (cp_parser* parser)
15367 {
15368   return (cp_parser_parsing_tentatively (parser)
15369           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15370 }
15371
15372 /* Returns nonzero if GNU extensions are allowed.  */
15373
15374 static bool
15375 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15376 {
15377   return parser->allow_gnu_extensions_p;
15378 }
15379
15380 \f
15381
15382 /* The parser.  */
15383
15384 static GTY (()) cp_parser *the_parser;
15385
15386 /* External interface.  */
15387
15388 /* Parse one entire translation unit.  */
15389
15390 void
15391 c_parse_file (void)
15392 {
15393   bool error_occurred;
15394
15395   the_parser = cp_parser_new ();
15396   push_deferring_access_checks (flag_access_control
15397                                 ? dk_no_deferred : dk_no_check);
15398   error_occurred = cp_parser_translation_unit (the_parser);
15399   the_parser = NULL;
15400 }
15401
15402 /* This variable must be provided by every front end.  */
15403
15404 int yydebug;
15405
15406 #include "gt-cp-parser.h"