OSDN Git Service

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