OSDN Git Service

b2a111314634e15d1191b27d11e63ca25dae85b8
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>.
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GCC is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37
38 \f
39 /* The lexer.  */
40
41 /* Overview
42    --------
43
44    A cp_lexer represents a stream of cp_tokens.  It allows arbitrary
45    look-ahead.
46
47    Methodology
48    -----------
49
50    We use a circular buffer to store incoming tokens.
51
52    Some artifacts of the C++ language (such as the
53    expression/declaration ambiguity) require arbitrary look-ahead.
54    The strategy we adopt for dealing with these problems is to attempt
55    to parse one construct (e.g., the declaration) and fall back to the
56    other (e.g., the expression) if that attempt does not succeed.
57    Therefore, we must sometimes store an arbitrary number of tokens.
58
59    The parser routinely peeks at the next token, and then consumes it
60    later.  That also requires a buffer in which to store the tokens.
61      
62    In order to easily permit adding tokens to the end of the buffer,
63    while removing them from the beginning of the buffer, we use a
64    circular buffer.  */
65
66 /* A C++ token.  */
67
68 typedef struct cp_token GTY (())
69 {
70   /* The kind of token.  */
71   enum cpp_ttype type;
72   /* The value associated with this token, if any.  */
73   tree value;
74   /* If this token is a keyword, this value indicates which keyword.
75      Otherwise, this value is RID_MAX.  */
76   enum rid keyword;
77   /* The location at which this token was found.  */
78   location_t location;
79 } cp_token;
80
81 /* The number of tokens in a single token block.  */
82
83 #define CP_TOKEN_BLOCK_NUM_TOKENS 32
84
85 /* A group of tokens.  These groups are chained together to store
86    large numbers of tokens.  (For example, a token block is created
87    when the body of an inline member function is first encountered;
88    the tokens are processed later after the class definition is
89    complete.)  
90
91    This somewhat ungainly data structure (as opposed to, say, a
92    variable-length array), is used due to constraints imposed by the
93    current garbage-collection methodology.  If it is made more
94    flexible, we could perhaps simplify the data structures involved.  */
95
96 typedef struct cp_token_block GTY (())
97 {
98   /* The tokens.  */
99   cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
100   /* The number of tokens in this block.  */
101   size_t num_tokens;
102   /* The next token block in the chain.  */
103   struct cp_token_block *next;
104   /* The previous block in the chain.  */
105   struct cp_token_block *prev;
106 } cp_token_block;
107
108 typedef struct cp_token_cache GTY (())
109 {
110   /* The first block in the cache.  NULL if there are no tokens in the
111      cache.  */
112   cp_token_block *first;
113   /* The last block in the cache.  NULL If there are no tokens in the
114      cache.  */
115   cp_token_block *last;
116 } cp_token_cache;
117
118 /* Prototypes.  */
119
120 static cp_token_cache *cp_token_cache_new 
121   (void);
122 static void cp_token_cache_push_token
123   (cp_token_cache *, cp_token *);
124
125 /* Create a new cp_token_cache.  */
126
127 static cp_token_cache *
128 cp_token_cache_new ()
129 {
130   return ggc_alloc_cleared (sizeof (cp_token_cache));
131 }
132
133 /* Add *TOKEN to *CACHE.  */
134
135 static void
136 cp_token_cache_push_token (cp_token_cache *cache,
137                            cp_token *token)
138 {
139   cp_token_block *b = cache->last;
140
141   /* See if we need to allocate a new token block.  */
142   if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
143     {
144       b = ggc_alloc_cleared (sizeof (cp_token_block));
145       b->prev = cache->last;
146       if (cache->last)
147         {
148           cache->last->next = b;
149           cache->last = b;
150         }
151       else
152         cache->first = cache->last = b;
153     }
154   /* Add this token to the current token block.  */
155   b->tokens[b->num_tokens++] = *token;
156 }
157
158 /* The cp_lexer structure represents the C++ lexer.  It is responsible
159    for managing the token stream from the preprocessor and supplying
160    it to the parser.  */
161
162 typedef struct cp_lexer GTY (())
163 {
164   /* The memory allocated for the buffer.  Never NULL.  */
165   cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
166   /* A pointer just past the end of the memory allocated for the buffer.  */
167   cp_token * GTY ((skip (""))) buffer_end;
168   /* The first valid token in the buffer, or NULL if none.  */
169   cp_token * GTY ((skip (""))) first_token;
170   /* The next available token.  If NEXT_TOKEN is NULL, then there are
171      no more available tokens.  */
172   cp_token * GTY ((skip (""))) next_token;
173   /* A pointer just past the last available token.  If FIRST_TOKEN is
174      NULL, however, there are no available tokens, and then this
175      location is simply the place in which the next token read will be
176      placed.  If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
177      When the LAST_TOKEN == BUFFER, then the last token is at the
178      highest memory address in the BUFFER.  */
179   cp_token * GTY ((skip (""))) last_token;
180
181   /* A stack indicating positions at which cp_lexer_save_tokens was
182      called.  The top entry is the most recent position at which we
183      began saving tokens.  The entries are differences in token
184      position between FIRST_TOKEN and the first saved token.
185
186      If the stack is non-empty, we are saving tokens.  When a token is
187      consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
188      pointer will not.  The token stream will be preserved so that it
189      can be reexamined later.
190
191      If the stack is empty, then we are not saving tokens.  Whenever a
192      token is consumed, the FIRST_TOKEN pointer will be moved, and the
193      consumed token will be gone forever.  */
194   varray_type saved_tokens;
195
196   /* The STRING_CST tokens encountered while processing the current
197      string literal.  */
198   varray_type string_tokens;
199
200   /* True if we should obtain more tokens from the preprocessor; false
201      if we are processing a saved token cache.  */
202   bool main_lexer_p;
203
204   /* True if we should output debugging information.  */
205   bool debugging_p;
206
207   /* The next lexer in a linked list of lexers.  */
208   struct cp_lexer *next;
209 } cp_lexer;
210
211 /* Prototypes.  */
212
213 static cp_lexer *cp_lexer_new_main
214   (void);
215 static cp_lexer *cp_lexer_new_from_tokens
216   (struct cp_token_cache *);
217 static int cp_lexer_saving_tokens
218   (const cp_lexer *);
219 static cp_token *cp_lexer_next_token
220   (cp_lexer *, cp_token *);
221 static ptrdiff_t cp_lexer_token_difference 
222   (cp_lexer *, cp_token *, cp_token *);
223 static cp_token *cp_lexer_read_token
224   (cp_lexer *);
225 static void cp_lexer_maybe_grow_buffer
226   (cp_lexer *);
227 static void cp_lexer_get_preprocessor_token
228   (cp_lexer *, cp_token *);
229 static cp_token *cp_lexer_peek_token
230   (cp_lexer *);
231 static cp_token *cp_lexer_peek_nth_token
232   (cp_lexer *, size_t);
233 static inline bool cp_lexer_next_token_is
234   (cp_lexer *, enum cpp_ttype);
235 static bool cp_lexer_next_token_is_not
236   (cp_lexer *, enum cpp_ttype);
237 static bool cp_lexer_next_token_is_keyword
238   (cp_lexer *, enum rid);
239 static cp_token *cp_lexer_consume_token 
240   (cp_lexer *);
241 static void cp_lexer_purge_token
242   (cp_lexer *);
243 static void cp_lexer_purge_tokens_after
244   (cp_lexer *, cp_token *);
245 static void cp_lexer_save_tokens
246   (cp_lexer *);
247 static void cp_lexer_commit_tokens
248   (cp_lexer *);
249 static void cp_lexer_rollback_tokens
250   (cp_lexer *);
251 static inline void cp_lexer_set_source_position_from_token 
252   (cp_lexer *, const cp_token *);
253 static void cp_lexer_print_token
254   (FILE *, cp_token *);
255 static inline bool cp_lexer_debugging_p 
256   (cp_lexer *);
257 static void cp_lexer_start_debugging
258   (cp_lexer *) ATTRIBUTE_UNUSED;
259 static void cp_lexer_stop_debugging
260   (cp_lexer *) ATTRIBUTE_UNUSED;
261
262 /* Manifest constants.  */
263
264 #define CP_TOKEN_BUFFER_SIZE 5
265 #define CP_SAVED_TOKENS_SIZE 5
266
267 /* A token type for keywords, as opposed to ordinary identifiers.  */
268 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
269
270 /* A token type for template-ids.  If a template-id is processed while
271    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
272    the value of the CPP_TEMPLATE_ID is whatever was returned by
273    cp_parser_template_id.  */
274 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
275
276 /* A token type for nested-name-specifiers.  If a
277    nested-name-specifier is processed while parsing tentatively, it is
278    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
279    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
280    cp_parser_nested_name_specifier_opt.  */
281 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
282
283 /* A token type for tokens that are not tokens at all; these are used
284    to mark the end of a token block.  */
285 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
286
287 /* Variables.  */
288
289 /* The stream to which debugging output should be written.  */
290 static FILE *cp_lexer_debug_stream;
291
292 /* Create a new main C++ lexer, the lexer that gets tokens from the
293    preprocessor.  */
294
295 static cp_lexer *
296 cp_lexer_new_main (void)
297 {
298   cp_lexer *lexer;
299   cp_token first_token;
300
301   /* It's possible that lexing the first token will load a PCH file,
302      which is a GC collection point.  So we have to grab the first
303      token before allocating any memory.  */
304   cp_lexer_get_preprocessor_token (NULL, &first_token);
305   c_common_no_more_pch ();
306
307   /* Allocate the memory.  */
308   lexer = ggc_alloc_cleared (sizeof (cp_lexer));
309
310   /* Create the circular buffer.  */
311   lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
312   lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
313
314   /* There is one token in the buffer.  */
315   lexer->last_token = lexer->buffer + 1;
316   lexer->first_token = lexer->buffer;
317   lexer->next_token = lexer->buffer;
318   memcpy (lexer->buffer, &first_token, sizeof (cp_token));
319
320   /* This lexer obtains more tokens by calling c_lex.  */
321   lexer->main_lexer_p = true;
322
323   /* Create the SAVED_TOKENS stack.  */
324   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
325   
326   /* Create the STRINGS array.  */
327   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
328
329   /* Assume we are not debugging.  */
330   lexer->debugging_p = false;
331
332   return lexer;
333 }
334
335 /* Create a new lexer whose token stream is primed with the TOKENS.
336    When these tokens are exhausted, no new tokens will be read.  */
337
338 static cp_lexer *
339 cp_lexer_new_from_tokens (cp_token_cache *tokens)
340 {
341   cp_lexer *lexer;
342   cp_token *token;
343   cp_token_block *block;
344   ptrdiff_t num_tokens;
345
346   /* Allocate the memory.  */
347   lexer = ggc_alloc_cleared (sizeof (cp_lexer));
348
349   /* Create a new buffer, appropriately sized.  */
350   num_tokens = 0;
351   for (block = tokens->first; block != NULL; block = block->next)
352     num_tokens += block->num_tokens;
353   lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
354   lexer->buffer_end = lexer->buffer + num_tokens;
355   
356   /* Install the tokens.  */
357   token = lexer->buffer;
358   for (block = tokens->first; block != NULL; block = block->next)
359     {
360       memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
361       token += block->num_tokens;
362     }
363
364   /* The FIRST_TOKEN is the beginning of the buffer.  */
365   lexer->first_token = lexer->buffer;
366   /* The next available token is also at the beginning of the buffer.  */
367   lexer->next_token = lexer->buffer;
368   /* The buffer is full.  */
369   lexer->last_token = lexer->first_token;
370
371   /* This lexer doesn't obtain more tokens.  */
372   lexer->main_lexer_p = false;
373
374   /* Create the SAVED_TOKENS stack.  */
375   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
376   
377   /* Create the STRINGS array.  */
378   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
379
380   /* Assume we are not debugging.  */
381   lexer->debugging_p = false;
382
383   return lexer;
384 }
385
386 /* Returns nonzero if debugging information should be output.  */
387
388 static inline bool
389 cp_lexer_debugging_p (cp_lexer *lexer)
390 {
391   return lexer->debugging_p;
392 }
393
394 /* Set the current source position from the information stored in
395    TOKEN.  */
396
397 static inline void
398 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
399                                          const cp_token *token)
400 {
401   /* Ideally, the source position information would not be a global
402      variable, but it is.  */
403
404   /* Update the line number.  */
405   if (token->type != CPP_EOF)
406     input_location = token->location;
407 }
408
409 /* TOKEN points into the circular token buffer.  Return a pointer to
410    the next token in the buffer.  */
411
412 static inline cp_token *
413 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
414 {
415   token++;
416   if (token == lexer->buffer_end)
417     token = lexer->buffer;
418   return token;
419 }
420
421 /* nonzero if we are presently saving tokens.  */
422
423 static int
424 cp_lexer_saving_tokens (const cp_lexer* lexer)
425 {
426   return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
427 }
428
429 /* Return a pointer to the token that is N tokens beyond TOKEN in the
430    buffer.  */
431
432 static cp_token *
433 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
434 {
435   token += n;
436   if (token >= lexer->buffer_end)
437     token = lexer->buffer + (token - lexer->buffer_end);
438   return token;
439 }
440
441 /* Returns the number of times that START would have to be incremented
442    to reach FINISH.  If START and FINISH are the same, returns zero.  */
443
444 static ptrdiff_t
445 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
446 {
447   if (finish >= start)
448     return finish - start;
449   else
450     return ((lexer->buffer_end - lexer->buffer)
451             - (start - finish));
452 }
453
454 /* Obtain another token from the C preprocessor and add it to the
455    token buffer.  Returns the newly read token.  */
456
457 static cp_token *
458 cp_lexer_read_token (cp_lexer* lexer)
459 {
460   cp_token *token;
461
462   /* Make sure there is room in the buffer.  */
463   cp_lexer_maybe_grow_buffer (lexer);
464
465   /* If there weren't any tokens, then this one will be the first.  */
466   if (!lexer->first_token)
467     lexer->first_token = lexer->last_token;
468   /* Similarly, if there were no available tokens, there is one now.  */
469   if (!lexer->next_token)
470     lexer->next_token = lexer->last_token;
471
472   /* Figure out where we're going to store the new token.  */
473   token = lexer->last_token;
474
475   /* Get a new token from the preprocessor.  */
476   cp_lexer_get_preprocessor_token (lexer, token);
477
478   /* Increment LAST_TOKEN.  */
479   lexer->last_token = cp_lexer_next_token (lexer, token);
480
481   /* Strings should have type `const char []'.  Right now, we will
482      have an ARRAY_TYPE that is constant rather than an array of
483      constant elements.
484      FIXME: Make fix_string_type get this right in the first place.  */
485   if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
486       && flag_const_strings)
487     {
488       tree type;
489
490       /* Get the current type.  It will be an ARRAY_TYPE.  */
491       type = TREE_TYPE (token->value);
492       /* Use build_cplus_array_type to rebuild the array, thereby
493          getting the right type.  */
494       type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
495       /* Reset the type of the token.  */
496       TREE_TYPE (token->value) = type;
497     }
498
499   return token;
500 }
501
502 /* If the circular buffer is full, make it bigger.  */
503
504 static void
505 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
506 {
507   /* If the buffer is full, enlarge it.  */
508   if (lexer->last_token == lexer->first_token)
509     {
510       cp_token *new_buffer;
511       cp_token *old_buffer;
512       cp_token *new_first_token;
513       ptrdiff_t buffer_length;
514       size_t num_tokens_to_copy;
515
516       /* Remember the current buffer pointer.  It will become invalid,
517          but we will need to do pointer arithmetic involving this
518          value.  */
519       old_buffer = lexer->buffer;
520       /* Compute the current buffer size.  */
521       buffer_length = lexer->buffer_end - lexer->buffer;
522       /* Allocate a buffer twice as big.  */
523       new_buffer = ggc_realloc (lexer->buffer, 
524                                 2 * buffer_length * sizeof (cp_token));
525       
526       /* Because the buffer is circular, logically consecutive tokens
527          are not necessarily placed consecutively in memory.
528          Therefore, we must keep move the tokens that were before
529          FIRST_TOKEN to the second half of the newly allocated
530          buffer.  */
531       num_tokens_to_copy = (lexer->first_token - old_buffer);
532       memcpy (new_buffer + buffer_length,
533               new_buffer,
534               num_tokens_to_copy * sizeof (cp_token));
535       /* Clear the rest of the buffer.  We never look at this storage,
536          but the garbage collector may.  */
537       memset (new_buffer + buffer_length + num_tokens_to_copy, 0, 
538               (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
539
540       /* Now recompute all of the buffer pointers.  */
541       new_first_token 
542         = new_buffer + (lexer->first_token - old_buffer);
543       if (lexer->next_token != NULL)
544         {
545           ptrdiff_t next_token_delta;
546
547           if (lexer->next_token > lexer->first_token)
548             next_token_delta = lexer->next_token - lexer->first_token;
549           else
550             next_token_delta = 
551               buffer_length - (lexer->first_token - lexer->next_token);
552           lexer->next_token = new_first_token + next_token_delta;
553         }
554       lexer->last_token = new_first_token + buffer_length;
555       lexer->buffer = new_buffer;
556       lexer->buffer_end = new_buffer + buffer_length * 2;
557       lexer->first_token = new_first_token;
558     }
559 }
560
561 /* Store the next token from the preprocessor in *TOKEN.  */
562
563 static void 
564 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
565                                  cp_token *token)
566 {
567   bool done;
568
569   /* If this not the main lexer, return a terminating CPP_EOF token.  */
570   if (lexer != NULL && !lexer->main_lexer_p)
571     {
572       token->type = CPP_EOF;
573       token->location.line = 0;
574       token->location.file = NULL;
575       token->value = NULL_TREE;
576       token->keyword = RID_MAX;
577
578       return;
579     }
580
581   done = false;
582   /* Keep going until we get a token we like.  */
583   while (!done)
584     {
585       /* Get a new token from the preprocessor.  */
586       token->type = c_lex (&token->value);
587       /* Issue messages about tokens we cannot process.  */
588       switch (token->type)
589         {
590         case CPP_ATSIGN:
591         case CPP_HASH:
592         case CPP_PASTE:
593           error ("invalid token");
594           break;
595
596         default:
597           /* This is a good token, so we exit the loop.  */
598           done = true;
599           break;
600         }
601     }
602   /* Now we've got our token.  */
603   token->location = input_location;
604
605   /* Check to see if this token is a keyword.  */
606   if (token->type == CPP_NAME 
607       && C_IS_RESERVED_WORD (token->value))
608     {
609       /* Mark this token as a keyword.  */
610       token->type = CPP_KEYWORD;
611       /* Record which keyword.  */
612       token->keyword = C_RID_CODE (token->value);
613       /* Update the value.  Some keywords are mapped to particular
614          entities, rather than simply having the value of the
615          corresponding IDENTIFIER_NODE.  For example, `__const' is
616          mapped to `const'.  */
617       token->value = ridpointers[token->keyword];
618     }
619   else
620     token->keyword = RID_MAX;
621 }
622
623 /* Return a pointer to the next token in the token stream, but do not
624    consume it.  */
625
626 static cp_token *
627 cp_lexer_peek_token (cp_lexer* lexer)
628 {
629   cp_token *token;
630
631   /* If there are no tokens, read one now.  */
632   if (!lexer->next_token)
633     cp_lexer_read_token (lexer);
634
635   /* Provide debugging output.  */
636   if (cp_lexer_debugging_p (lexer))
637     {
638       fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
639       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
640       fprintf (cp_lexer_debug_stream, "\n");
641     }
642
643   token = lexer->next_token;
644   cp_lexer_set_source_position_from_token (lexer, token);
645   return token;
646 }
647
648 /* Return true if the next token has the indicated TYPE.  */
649
650 static bool
651 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
652 {
653   cp_token *token;
654
655   /* Peek at the next token.  */
656   token = cp_lexer_peek_token (lexer);
657   /* Check to see if it has the indicated TYPE.  */
658   return token->type == type;
659 }
660
661 /* Return true if the next token does not have the indicated TYPE.  */
662
663 static bool
664 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
665 {
666   return !cp_lexer_next_token_is (lexer, type);
667 }
668
669 /* Return true if the next token is the indicated KEYWORD.  */
670
671 static bool
672 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
673 {
674   cp_token *token;
675
676   /* Peek at the next token.  */
677   token = cp_lexer_peek_token (lexer);
678   /* Check to see if it is the indicated keyword.  */
679   return token->keyword == keyword;
680 }
681
682 /* Return a pointer to the Nth token in the token stream.  If N is 1,
683    then this is precisely equivalent to cp_lexer_peek_token.  */
684
685 static cp_token *
686 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
687 {
688   cp_token *token;
689
690   /* N is 1-based, not zero-based.  */
691   my_friendly_assert (n > 0, 20000224);
692
693   /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary.  */
694   token = lexer->next_token;
695   /* If there are no tokens in the buffer, get one now.  */
696   if (!token)
697     {
698       cp_lexer_read_token (lexer);
699       token = lexer->next_token;
700     }
701
702   /* Now, read tokens until we have enough.  */
703   while (--n > 0)
704     {
705       /* Advance to the next token.  */
706       token = cp_lexer_next_token (lexer, token);
707       /* If that's all the tokens we have, read a new one.  */
708       if (token == lexer->last_token)
709         token = cp_lexer_read_token (lexer);
710     }
711
712   return token;
713 }
714
715 /* Consume the next token.  The pointer returned is valid only until
716    another token is read.  Callers should preserve copy the token
717    explicitly if they will need its value for a longer period of
718    time.  */
719
720 static cp_token *
721 cp_lexer_consume_token (cp_lexer* lexer)
722 {
723   cp_token *token;
724
725   /* If there are no tokens, read one now.  */
726   if (!lexer->next_token)
727     cp_lexer_read_token (lexer);
728
729   /* Remember the token we'll be returning.  */
730   token = lexer->next_token;
731
732   /* Increment NEXT_TOKEN.  */
733   lexer->next_token = cp_lexer_next_token (lexer, 
734                                            lexer->next_token);
735   /* Check to see if we're all out of tokens.  */
736   if (lexer->next_token == lexer->last_token)
737     lexer->next_token = NULL;
738
739   /* If we're not saving tokens, then move FIRST_TOKEN too.  */
740   if (!cp_lexer_saving_tokens (lexer))
741     {
742       /* If there are no tokens available, set FIRST_TOKEN to NULL.  */
743       if (!lexer->next_token)
744         lexer->first_token = NULL;
745       else
746         lexer->first_token = lexer->next_token;
747     }
748
749   /* Provide debugging output.  */
750   if (cp_lexer_debugging_p (lexer))
751     {
752       fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
753       cp_lexer_print_token (cp_lexer_debug_stream, token);
754       fprintf (cp_lexer_debug_stream, "\n");
755     }
756
757   return token;
758 }
759
760 /* Permanently remove the next token from the token stream.  There
761    must be a valid next token already; this token never reads
762    additional tokens from the preprocessor.  */
763
764 static void
765 cp_lexer_purge_token (cp_lexer *lexer)
766 {
767   cp_token *token;
768   cp_token *next_token;
769
770   token = lexer->next_token;
771   while (true) 
772     {
773       next_token = cp_lexer_next_token (lexer, token);
774       if (next_token == lexer->last_token)
775         break;
776       *token = *next_token;
777       token = next_token;
778     }
779
780   lexer->last_token = token;
781   /* The token purged may have been the only token remaining; if so,
782      clear NEXT_TOKEN.  */
783   if (lexer->next_token == token)
784     lexer->next_token = NULL;
785 }
786
787 /* Permanently remove all tokens after TOKEN, up to, but not
788    including, the token that will be returned next by
789    cp_lexer_peek_token.  */
790
791 static void
792 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
793 {
794   cp_token *peek;
795   cp_token *t1;
796   cp_token *t2;
797
798   if (lexer->next_token)
799     {
800       /* Copy the tokens that have not yet been read to the location
801          immediately following TOKEN.  */
802       t1 = cp_lexer_next_token (lexer, token);
803       t2 = peek = cp_lexer_peek_token (lexer);
804       /* Move tokens into the vacant area between TOKEN and PEEK.  */
805       while (t2 != lexer->last_token)
806         {
807           *t1 = *t2;
808           t1 = cp_lexer_next_token (lexer, t1);
809           t2 = cp_lexer_next_token (lexer, t2);
810         }
811       /* Now, the next available token is right after TOKEN.  */
812       lexer->next_token = cp_lexer_next_token (lexer, token);
813       /* And the last token is wherever we ended up.  */
814       lexer->last_token = t1;
815     }
816   else
817     {
818       /* There are no tokens in the buffer, so there is nothing to
819          copy.  The last token in the buffer is TOKEN itself.  */
820       lexer->last_token = cp_lexer_next_token (lexer, token);
821     }
822 }
823
824 /* Begin saving tokens.  All tokens consumed after this point will be
825    preserved.  */
826
827 static void
828 cp_lexer_save_tokens (cp_lexer* lexer)
829 {
830   /* Provide debugging output.  */
831   if (cp_lexer_debugging_p (lexer))
832     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
833
834   /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
835      restore the tokens if required.  */
836   if (!lexer->next_token)
837     cp_lexer_read_token (lexer);
838
839   VARRAY_PUSH_INT (lexer->saved_tokens,
840                    cp_lexer_token_difference (lexer,
841                                               lexer->first_token,
842                                               lexer->next_token));
843 }
844
845 /* Commit to the portion of the token stream most recently saved.  */
846
847 static void
848 cp_lexer_commit_tokens (cp_lexer* lexer)
849 {
850   /* Provide debugging output.  */
851   if (cp_lexer_debugging_p (lexer))
852     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
853
854   VARRAY_POP (lexer->saved_tokens);
855 }
856
857 /* Return all tokens saved since the last call to cp_lexer_save_tokens
858    to the token stream.  Stop saving tokens.  */
859
860 static void
861 cp_lexer_rollback_tokens (cp_lexer* lexer)
862 {
863   size_t delta;
864
865   /* Provide debugging output.  */
866   if (cp_lexer_debugging_p (lexer))
867     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
868
869   /* Find the token that was the NEXT_TOKEN when we started saving
870      tokens.  */
871   delta = VARRAY_TOP_INT(lexer->saved_tokens);
872   /* Make it the next token again now.  */
873   lexer->next_token = cp_lexer_advance_token (lexer,
874                                               lexer->first_token, 
875                                               delta);
876   /* It might be the case that there were no tokens when we started
877      saving tokens, but that there are some tokens now.  */
878   if (!lexer->next_token && lexer->first_token)
879     lexer->next_token = lexer->first_token;
880
881   /* Stop saving tokens.  */
882   VARRAY_POP (lexer->saved_tokens);
883 }
884
885 /* Print a representation of the TOKEN on the STREAM.  */
886
887 static void
888 cp_lexer_print_token (FILE * stream, cp_token* token)
889 {
890   const char *token_type = NULL;
891
892   /* Figure out what kind of token this is.  */
893   switch (token->type)
894     {
895     case CPP_EQ:
896       token_type = "EQ";
897       break;
898
899     case CPP_COMMA:
900       token_type = "COMMA";
901       break;
902
903     case CPP_OPEN_PAREN:
904       token_type = "OPEN_PAREN";
905       break;
906
907     case CPP_CLOSE_PAREN:
908       token_type = "CLOSE_PAREN";
909       break;
910
911     case CPP_OPEN_BRACE:
912       token_type = "OPEN_BRACE";
913       break;
914
915     case CPP_CLOSE_BRACE:
916       token_type = "CLOSE_BRACE";
917       break;
918
919     case CPP_SEMICOLON:
920       token_type = "SEMICOLON";
921       break;
922
923     case CPP_NAME:
924       token_type = "NAME";
925       break;
926
927     case CPP_EOF:
928       token_type = "EOF";
929       break;
930
931     case CPP_KEYWORD:
932       token_type = "keyword";
933       break;
934
935       /* This is not a token that we know how to handle yet.  */
936     default:
937       break;
938     }
939
940   /* If we have a name for the token, print it out.  Otherwise, we
941      simply give the numeric code.  */
942   if (token_type)
943     fprintf (stream, "%s", token_type);
944   else
945     fprintf (stream, "%d", token->type);
946   /* And, for an identifier, print the identifier name.  */
947   if (token->type == CPP_NAME 
948       /* Some keywords have a value that is not an IDENTIFIER_NODE.
949          For example, `struct' is mapped to an INTEGER_CST.  */
950       || (token->type == CPP_KEYWORD 
951           && TREE_CODE (token->value) == IDENTIFIER_NODE))
952     fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
953 }
954
955 /* Start emitting debugging information.  */
956
957 static void
958 cp_lexer_start_debugging (cp_lexer* lexer)
959 {
960   ++lexer->debugging_p;
961 }
962   
963 /* Stop emitting debugging information.  */
964
965 static void
966 cp_lexer_stop_debugging (cp_lexer* lexer)
967 {
968   --lexer->debugging_p;
969 }
970
971 \f
972 /* The parser.  */
973
974 /* Overview
975    --------
976
977    A cp_parser parses the token stream as specified by the C++
978    grammar.  Its job is purely parsing, not semantic analysis.  For
979    example, the parser breaks the token stream into declarators,
980    expressions, statements, and other similar syntactic constructs.
981    It does not check that the types of the expressions on either side
982    of an assignment-statement are compatible, or that a function is
983    not declared with a parameter of type `void'.
984
985    The parser invokes routines elsewhere in the compiler to perform
986    semantic analysis and to build up the abstract syntax tree for the
987    code processed.  
988
989    The parser (and the template instantiation code, which is, in a
990    way, a close relative of parsing) are the only parts of the
991    compiler that should be calling push_scope and pop_scope, or
992    related functions.  The parser (and template instantiation code)
993    keeps track of what scope is presently active; everything else
994    should simply honor that.  (The code that generates static
995    initializers may also need to set the scope, in order to check
996    access control correctly when emitting the initializers.)
997
998    Methodology
999    -----------
1000    
1001    The parser is of the standard recursive-descent variety.  Upcoming
1002    tokens in the token stream are examined in order to determine which
1003    production to use when parsing a non-terminal.  Some C++ constructs
1004    require arbitrary look ahead to disambiguate.  For example, it is
1005    impossible, in the general case, to tell whether a statement is an
1006    expression or declaration without scanning the entire statement.
1007    Therefore, the parser is capable of "parsing tentatively."  When the
1008    parser is not sure what construct comes next, it enters this mode.
1009    Then, while we attempt to parse the construct, the parser queues up
1010    error messages, rather than issuing them immediately, and saves the
1011    tokens it consumes.  If the construct is parsed successfully, the
1012    parser "commits", i.e., it issues any queued error messages and
1013    the tokens that were being preserved are permanently discarded.
1014    If, however, the construct is not parsed successfully, the parser
1015    rolls back its state completely so that it can resume parsing using
1016    a different alternative.
1017
1018    Future Improvements
1019    -------------------
1020    
1021    The performance of the parser could probably be improved
1022    substantially.  Some possible improvements include:
1023
1024      - The expression parser recurses through the various levels of
1025        precedence as specified in the grammar, rather than using an
1026        operator-precedence technique.  Therefore, parsing a simple
1027        identifier requires multiple recursive calls.
1028
1029      - We could often eliminate the need to parse tentatively by
1030        looking ahead a little bit.  In some places, this approach
1031        might not entirely eliminate the need to parse tentatively, but
1032        it might still speed up the average case.  */
1033
1034 /* Flags that are passed to some parsing functions.  These values can
1035    be bitwise-ored together.  */
1036
1037 typedef enum cp_parser_flags
1038 {
1039   /* No flags.  */
1040   CP_PARSER_FLAGS_NONE = 0x0,
1041   /* The construct is optional.  If it is not present, then no error
1042      should be issued.  */
1043   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1044   /* When parsing a type-specifier, do not allow user-defined types.  */
1045   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1046 } cp_parser_flags;
1047
1048 /* The different kinds of declarators we want to parse.  */
1049
1050 typedef enum cp_parser_declarator_kind
1051 {
1052   /* We want an abstract declartor.  */
1053   CP_PARSER_DECLARATOR_ABSTRACT,
1054   /* We want a named declarator.  */
1055   CP_PARSER_DECLARATOR_NAMED,
1056   /* We don't mind, but the name must be an unqualified-id  */
1057   CP_PARSER_DECLARATOR_EITHER
1058 } cp_parser_declarator_kind;
1059
1060 /* A mapping from a token type to a corresponding tree node type.  */
1061
1062 typedef struct cp_parser_token_tree_map_node
1063 {
1064   /* The token type.  */
1065   enum cpp_ttype token_type;
1066   /* The corresponding tree code.  */
1067   enum tree_code tree_type;
1068 } cp_parser_token_tree_map_node;
1069
1070 /* A complete map consists of several ordinary entries, followed by a
1071    terminator.  The terminating entry has a token_type of CPP_EOF.  */
1072
1073 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1074
1075 /* The status of a tentative parse.  */
1076
1077 typedef enum cp_parser_status_kind
1078 {
1079   /* No errors have occurred.  */
1080   CP_PARSER_STATUS_KIND_NO_ERROR,
1081   /* An error has occurred.  */
1082   CP_PARSER_STATUS_KIND_ERROR,
1083   /* We are committed to this tentative parse, whether or not an error
1084      has occurred.  */
1085   CP_PARSER_STATUS_KIND_COMMITTED
1086 } cp_parser_status_kind;
1087
1088 /* Context that is saved and restored when parsing tentatively.  */
1089
1090 typedef struct cp_parser_context GTY (())
1091 {
1092   /* If this is a tentative parsing context, the status of the
1093      tentative parse.  */
1094   enum cp_parser_status_kind status;
1095   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1096      that are looked up in this context must be looked up both in the
1097      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1098      the context of the containing expression.  */
1099   tree object_type;
1100   /* The next parsing context in the stack.  */
1101   struct cp_parser_context *next;
1102 } cp_parser_context;
1103
1104 /* Prototypes.  */
1105
1106 /* Constructors and destructors.  */
1107
1108 static cp_parser_context *cp_parser_context_new
1109   (cp_parser_context *);
1110
1111 /* Class variables.  */
1112
1113 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1114
1115 /* Constructors and destructors.  */
1116
1117 /* Construct a new context.  The context below this one on the stack
1118    is given by NEXT.  */
1119
1120 static cp_parser_context *
1121 cp_parser_context_new (cp_parser_context* next)
1122 {
1123   cp_parser_context *context;
1124
1125   /* Allocate the storage.  */
1126   if (cp_parser_context_free_list != NULL)
1127     {
1128       /* Pull the first entry from the free list.  */
1129       context = cp_parser_context_free_list;
1130       cp_parser_context_free_list = context->next;
1131       memset (context, 0, sizeof (*context));
1132     }
1133   else
1134     context = ggc_alloc_cleared (sizeof (cp_parser_context));
1135   /* No errors have occurred yet in this context.  */
1136   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1137   /* If this is not the bottomost context, copy information that we
1138      need from the previous context.  */
1139   if (next)
1140     {
1141       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1142          expression, then we are parsing one in this context, too.  */
1143       context->object_type = next->object_type;
1144       /* Thread the stack.  */
1145       context->next = next;
1146     }
1147
1148   return context;
1149 }
1150
1151 /* The cp_parser structure represents the C++ parser.  */
1152
1153 typedef struct cp_parser GTY(())
1154 {
1155   /* The lexer from which we are obtaining tokens.  */
1156   cp_lexer *lexer;
1157
1158   /* The scope in which names should be looked up.  If NULL_TREE, then
1159      we look up names in the scope that is currently open in the
1160      source program.  If non-NULL, this is either a TYPE or
1161      NAMESPACE_DECL for the scope in which we should look.  
1162
1163      This value is not cleared automatically after a name is looked
1164      up, so we must be careful to clear it before starting a new look
1165      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1166      will look up `Z' in the scope of `X', rather than the current
1167      scope.)  Unfortunately, it is difficult to tell when name lookup
1168      is complete, because we sometimes peek at a token, look it up,
1169      and then decide not to consume it.  */
1170   tree scope;
1171
1172   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1173      last lookup took place.  OBJECT_SCOPE is used if an expression
1174      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1175      respectively.  QUALIFYING_SCOPE is used for an expression of the 
1176      form "X::Y"; it refers to X.  */
1177   tree object_scope;
1178   tree qualifying_scope;
1179
1180   /* A stack of parsing contexts.  All but the bottom entry on the
1181      stack will be tentative contexts.
1182
1183      We parse tentatively in order to determine which construct is in
1184      use in some situations.  For example, in order to determine
1185      whether a statement is an expression-statement or a
1186      declaration-statement we parse it tentatively as a
1187      declaration-statement.  If that fails, we then reparse the same
1188      token stream as an expression-statement.  */
1189   cp_parser_context *context;
1190
1191   /* True if we are parsing GNU C++.  If this flag is not set, then
1192      GNU extensions are not recognized.  */
1193   bool allow_gnu_extensions_p;
1194
1195   /* TRUE if the `>' token should be interpreted as the greater-than
1196      operator.  FALSE if it is the end of a template-id or
1197      template-parameter-list.  */
1198   bool greater_than_is_operator_p;
1199
1200   /* TRUE if default arguments are allowed within a parameter list
1201      that starts at this point. FALSE if only a gnu extension makes
1202      them permissable.  */
1203   bool default_arg_ok_p;
1204   
1205   /* TRUE if we are parsing an integral constant-expression.  See
1206      [expr.const] for a precise definition.  */
1207   bool constant_expression_p;
1208
1209   /* TRUE if we are parsing an integral constant-expression -- but a
1210      non-constant expression should be permitted as well.  This flag
1211      is used when parsing an array bound so that GNU variable-length
1212      arrays are tolerated.  */
1213   bool allow_non_constant_expression_p;
1214
1215   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1216      been seen that makes the expression non-constant.  */
1217   bool non_constant_expression_p;
1218
1219   /* TRUE if local variable names and `this' are forbidden in the
1220      current context.  */
1221   bool local_variables_forbidden_p;
1222
1223   /* TRUE if the declaration we are parsing is part of a
1224      linkage-specification of the form `extern string-literal
1225      declaration'.  */
1226   bool in_unbraced_linkage_specification_p;
1227
1228   /* TRUE if we are presently parsing a declarator, after the
1229      direct-declarator.  */
1230   bool in_declarator_p;
1231
1232   /* If non-NULL, then we are parsing a construct where new type
1233      definitions are not permitted.  The string stored here will be
1234      issued as an error message if a type is defined.  */
1235   const char *type_definition_forbidden_message;
1236
1237   /* A list of lists. The outer list is a stack, used for member
1238      functions of local classes. At each level there are two sub-list,
1239      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1240      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1241      TREE_VALUE's. The functions are chained in reverse declaration
1242      order.
1243
1244      The TREE_PURPOSE sublist contains those functions with default
1245      arguments that need post processing, and the TREE_VALUE sublist
1246      contains those functions with definitions that need post
1247      processing.
1248
1249      These lists can only be processed once the outermost class being
1250      defined is complete.  */
1251   tree unparsed_functions_queues;
1252
1253   /* The number of classes whose definitions are currently in
1254      progress.  */
1255   unsigned num_classes_being_defined;
1256
1257   /* The number of template parameter lists that apply directly to the
1258      current declaration.  */
1259   unsigned num_template_parameter_lists;
1260 } cp_parser;
1261
1262 /* The type of a function that parses some kind of expression  */
1263 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1264
1265 /* Prototypes.  */
1266
1267 /* Constructors and destructors.  */
1268
1269 static cp_parser *cp_parser_new
1270   (void);
1271
1272 /* Routines to parse various constructs.  
1273
1274    Those that return `tree' will return the error_mark_node (rather
1275    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1276    Sometimes, they will return an ordinary node if error-recovery was
1277    attempted, even though a parse error occurred.  So, to check
1278    whether or not a parse error occurred, you should always use
1279    cp_parser_error_occurred.  If the construct is optional (indicated
1280    either by an `_opt' in the name of the function that does the
1281    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1282    the construct is not present.  */
1283
1284 /* Lexical conventions [gram.lex]  */
1285
1286 static tree cp_parser_identifier
1287   (cp_parser *);
1288
1289 /* Basic concepts [gram.basic]  */
1290
1291 static bool cp_parser_translation_unit
1292   (cp_parser *);
1293
1294 /* Expressions [gram.expr]  */
1295
1296 static tree cp_parser_primary_expression
1297   (cp_parser *, cp_id_kind *, tree *);
1298 static tree cp_parser_id_expression
1299   (cp_parser *, bool, bool, bool *);
1300 static tree cp_parser_unqualified_id
1301   (cp_parser *, bool, bool);
1302 static tree cp_parser_nested_name_specifier_opt
1303   (cp_parser *, bool, bool, bool);
1304 static tree cp_parser_nested_name_specifier
1305   (cp_parser *, bool, bool, bool);
1306 static tree cp_parser_class_or_namespace_name
1307   (cp_parser *, bool, bool, bool, bool);
1308 static tree cp_parser_postfix_expression
1309   (cp_parser *, bool);
1310 static tree cp_parser_parenthesized_expression_list
1311   (cp_parser *, bool, bool *);
1312 static void cp_parser_pseudo_destructor_name
1313   (cp_parser *, tree *, tree *);
1314 static tree cp_parser_unary_expression
1315   (cp_parser *, bool);
1316 static enum tree_code cp_parser_unary_operator
1317   (cp_token *);
1318 static tree cp_parser_new_expression
1319   (cp_parser *);
1320 static tree cp_parser_new_placement
1321   (cp_parser *);
1322 static tree cp_parser_new_type_id
1323   (cp_parser *);
1324 static tree cp_parser_new_declarator_opt
1325   (cp_parser *);
1326 static tree cp_parser_direct_new_declarator
1327   (cp_parser *);
1328 static tree cp_parser_new_initializer
1329   (cp_parser *);
1330 static tree cp_parser_delete_expression
1331   (cp_parser *);
1332 static tree cp_parser_cast_expression 
1333   (cp_parser *, bool);
1334 static tree cp_parser_pm_expression
1335   (cp_parser *);
1336 static tree cp_parser_multiplicative_expression
1337   (cp_parser *);
1338 static tree cp_parser_additive_expression
1339   (cp_parser *);
1340 static tree cp_parser_shift_expression
1341   (cp_parser *);
1342 static tree cp_parser_relational_expression
1343   (cp_parser *);
1344 static tree cp_parser_equality_expression
1345   (cp_parser *);
1346 static tree cp_parser_and_expression
1347   (cp_parser *);
1348 static tree cp_parser_exclusive_or_expression
1349   (cp_parser *);
1350 static tree cp_parser_inclusive_or_expression
1351   (cp_parser *);
1352 static tree cp_parser_logical_and_expression
1353   (cp_parser *);
1354 static tree cp_parser_logical_or_expression 
1355   (cp_parser *);
1356 static tree cp_parser_question_colon_clause
1357   (cp_parser *, tree);
1358 static tree cp_parser_assignment_expression
1359   (cp_parser *);
1360 static enum tree_code cp_parser_assignment_operator_opt
1361   (cp_parser *);
1362 static tree cp_parser_expression
1363   (cp_parser *);
1364 static tree cp_parser_constant_expression
1365   (cp_parser *, bool, bool *);
1366
1367 /* Statements [gram.stmt.stmt]  */
1368
1369 static void cp_parser_statement
1370   (cp_parser *, bool);
1371 static tree cp_parser_labeled_statement
1372   (cp_parser *, bool);
1373 static tree cp_parser_expression_statement
1374   (cp_parser *, bool);
1375 static tree cp_parser_compound_statement
1376   (cp_parser *, bool);
1377 static void cp_parser_statement_seq_opt
1378   (cp_parser *, bool);
1379 static tree cp_parser_selection_statement
1380   (cp_parser *);
1381 static tree cp_parser_condition
1382   (cp_parser *);
1383 static tree cp_parser_iteration_statement
1384   (cp_parser *);
1385 static void cp_parser_for_init_statement
1386   (cp_parser *);
1387 static tree cp_parser_jump_statement
1388   (cp_parser *);
1389 static void cp_parser_declaration_statement
1390   (cp_parser *);
1391
1392 static tree cp_parser_implicitly_scoped_statement
1393   (cp_parser *);
1394 static void cp_parser_already_scoped_statement
1395   (cp_parser *);
1396
1397 /* Declarations [gram.dcl.dcl] */
1398
1399 static void cp_parser_declaration_seq_opt
1400   (cp_parser *);
1401 static void cp_parser_declaration
1402   (cp_parser *);
1403 static void cp_parser_block_declaration
1404   (cp_parser *, bool);
1405 static void cp_parser_simple_declaration
1406   (cp_parser *, bool);
1407 static tree cp_parser_decl_specifier_seq 
1408   (cp_parser *, cp_parser_flags, tree *, int *);
1409 static tree cp_parser_storage_class_specifier_opt
1410   (cp_parser *);
1411 static tree cp_parser_function_specifier_opt
1412   (cp_parser *);
1413 static tree cp_parser_type_specifier
1414   (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1415 static tree cp_parser_simple_type_specifier
1416   (cp_parser *, cp_parser_flags, bool);
1417 static tree cp_parser_type_name
1418   (cp_parser *);
1419 static tree cp_parser_elaborated_type_specifier
1420   (cp_parser *, bool, bool);
1421 static tree cp_parser_enum_specifier
1422   (cp_parser *);
1423 static void cp_parser_enumerator_list
1424   (cp_parser *, tree);
1425 static void cp_parser_enumerator_definition 
1426   (cp_parser *, tree);
1427 static tree cp_parser_namespace_name
1428   (cp_parser *);
1429 static void cp_parser_namespace_definition
1430   (cp_parser *);
1431 static void cp_parser_namespace_body
1432   (cp_parser *);
1433 static tree cp_parser_qualified_namespace_specifier
1434   (cp_parser *);
1435 static void cp_parser_namespace_alias_definition
1436   (cp_parser *);
1437 static void cp_parser_using_declaration
1438   (cp_parser *);
1439 static void cp_parser_using_directive
1440   (cp_parser *);
1441 static void cp_parser_asm_definition
1442   (cp_parser *);
1443 static void cp_parser_linkage_specification
1444   (cp_parser *);
1445
1446 /* Declarators [gram.dcl.decl] */
1447
1448 static tree cp_parser_init_declarator
1449   (cp_parser *, tree, tree, bool, bool, int, bool *);
1450 static tree cp_parser_declarator
1451   (cp_parser *, cp_parser_declarator_kind, int *);
1452 static tree cp_parser_direct_declarator
1453   (cp_parser *, cp_parser_declarator_kind, int *);
1454 static enum tree_code cp_parser_ptr_operator
1455   (cp_parser *, tree *, tree *);
1456 static tree cp_parser_cv_qualifier_seq_opt
1457   (cp_parser *);
1458 static tree cp_parser_cv_qualifier_opt
1459   (cp_parser *);
1460 static tree cp_parser_declarator_id
1461   (cp_parser *);
1462 static tree cp_parser_type_id
1463   (cp_parser *);
1464 static tree cp_parser_type_specifier_seq
1465   (cp_parser *);
1466 static tree cp_parser_parameter_declaration_clause
1467   (cp_parser *);
1468 static tree cp_parser_parameter_declaration_list
1469   (cp_parser *);
1470 static tree cp_parser_parameter_declaration
1471   (cp_parser *, bool);
1472 static tree cp_parser_function_definition
1473   (cp_parser *, bool *);
1474 static void cp_parser_function_body
1475   (cp_parser *);
1476 static tree cp_parser_initializer
1477   (cp_parser *, bool *, bool *);
1478 static tree cp_parser_initializer_clause
1479   (cp_parser *, bool *);
1480 static tree cp_parser_initializer_list
1481   (cp_parser *, bool *);
1482
1483 static bool cp_parser_ctor_initializer_opt_and_function_body
1484   (cp_parser *);
1485
1486 /* Classes [gram.class] */
1487
1488 static tree cp_parser_class_name
1489   (cp_parser *, bool, bool, bool, bool, bool);
1490 static tree cp_parser_class_specifier
1491   (cp_parser *);
1492 static tree cp_parser_class_head
1493   (cp_parser *, bool *);
1494 static enum tag_types cp_parser_class_key
1495   (cp_parser *);
1496 static void cp_parser_member_specification_opt
1497   (cp_parser *);
1498 static void cp_parser_member_declaration
1499   (cp_parser *);
1500 static tree cp_parser_pure_specifier
1501   (cp_parser *);
1502 static tree cp_parser_constant_initializer
1503   (cp_parser *);
1504
1505 /* Derived classes [gram.class.derived] */
1506
1507 static tree cp_parser_base_clause
1508   (cp_parser *);
1509 static tree cp_parser_base_specifier
1510   (cp_parser *);
1511
1512 /* Special member functions [gram.special] */
1513
1514 static tree cp_parser_conversion_function_id
1515   (cp_parser *);
1516 static tree cp_parser_conversion_type_id
1517   (cp_parser *);
1518 static tree cp_parser_conversion_declarator_opt
1519   (cp_parser *);
1520 static bool cp_parser_ctor_initializer_opt
1521   (cp_parser *);
1522 static void cp_parser_mem_initializer_list
1523   (cp_parser *);
1524 static tree cp_parser_mem_initializer
1525   (cp_parser *);
1526 static tree cp_parser_mem_initializer_id
1527   (cp_parser *);
1528
1529 /* Overloading [gram.over] */
1530
1531 static tree cp_parser_operator_function_id
1532   (cp_parser *);
1533 static tree cp_parser_operator
1534   (cp_parser *);
1535
1536 /* Templates [gram.temp] */
1537
1538 static void cp_parser_template_declaration
1539   (cp_parser *, bool);
1540 static tree cp_parser_template_parameter_list
1541   (cp_parser *);
1542 static tree cp_parser_template_parameter
1543   (cp_parser *);
1544 static tree cp_parser_type_parameter
1545   (cp_parser *);
1546 static tree cp_parser_template_id
1547   (cp_parser *, bool, bool);
1548 static tree cp_parser_template_name
1549   (cp_parser *, bool, bool);
1550 static tree cp_parser_template_argument_list
1551   (cp_parser *);
1552 static tree cp_parser_template_argument
1553   (cp_parser *);
1554 static void cp_parser_explicit_instantiation
1555   (cp_parser *);
1556 static void cp_parser_explicit_specialization
1557   (cp_parser *);
1558
1559 /* Exception handling [gram.exception] */
1560
1561 static tree cp_parser_try_block 
1562   (cp_parser *);
1563 static bool cp_parser_function_try_block
1564   (cp_parser *);
1565 static void cp_parser_handler_seq
1566   (cp_parser *);
1567 static void cp_parser_handler
1568   (cp_parser *);
1569 static tree cp_parser_exception_declaration
1570   (cp_parser *);
1571 static tree cp_parser_throw_expression
1572   (cp_parser *);
1573 static tree cp_parser_exception_specification_opt
1574   (cp_parser *);
1575 static tree cp_parser_type_id_list
1576   (cp_parser *);
1577
1578 /* GNU Extensions */
1579
1580 static tree cp_parser_asm_specification_opt
1581   (cp_parser *);
1582 static tree cp_parser_asm_operand_list
1583   (cp_parser *);
1584 static tree cp_parser_asm_clobber_list
1585   (cp_parser *);
1586 static tree cp_parser_attributes_opt
1587   (cp_parser *);
1588 static tree cp_parser_attribute_list
1589   (cp_parser *);
1590 static bool cp_parser_extension_opt
1591   (cp_parser *, int *);
1592 static void cp_parser_label_declaration
1593   (cp_parser *);
1594
1595 /* Utility Routines */
1596
1597 static tree cp_parser_lookup_name
1598   (cp_parser *, tree, bool, bool, bool);
1599 static tree cp_parser_lookup_name_simple
1600   (cp_parser *, tree);
1601 static tree cp_parser_maybe_treat_template_as_class
1602   (tree, bool);
1603 static bool cp_parser_check_declarator_template_parameters
1604   (cp_parser *, tree);
1605 static bool cp_parser_check_template_parameters
1606   (cp_parser *, unsigned);
1607 static tree cp_parser_simple_cast_expression
1608   (cp_parser *);
1609 static tree cp_parser_binary_expression
1610   (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1611 static tree cp_parser_global_scope_opt
1612   (cp_parser *, bool);
1613 static bool cp_parser_constructor_declarator_p
1614   (cp_parser *, bool);
1615 static tree cp_parser_function_definition_from_specifiers_and_declarator
1616   (cp_parser *, tree, tree, tree);
1617 static tree cp_parser_function_definition_after_declarator
1618   (cp_parser *, bool);
1619 static void cp_parser_template_declaration_after_export
1620   (cp_parser *, bool);
1621 static tree cp_parser_single_declaration
1622   (cp_parser *, bool, bool *);
1623 static tree cp_parser_functional_cast
1624   (cp_parser *, tree);
1625 static void cp_parser_save_default_args
1626   (cp_parser *, tree);
1627 static void cp_parser_late_parsing_for_member
1628   (cp_parser *, tree);
1629 static void cp_parser_late_parsing_default_args
1630   (cp_parser *, tree);
1631 static tree cp_parser_sizeof_operand
1632   (cp_parser *, enum rid);
1633 static bool cp_parser_declares_only_class_p
1634   (cp_parser *);
1635 static tree cp_parser_fold_non_dependent_expr
1636   (tree);
1637 static bool cp_parser_friend_p
1638   (tree);
1639 static cp_token *cp_parser_require
1640   (cp_parser *, enum cpp_ttype, const char *);
1641 static cp_token *cp_parser_require_keyword
1642   (cp_parser *, enum rid, const char *);
1643 static bool cp_parser_token_starts_function_definition_p 
1644   (cp_token *);
1645 static bool cp_parser_next_token_starts_class_definition_p
1646   (cp_parser *);
1647 static bool cp_parser_next_token_ends_template_argument_p
1648   (cp_parser *);
1649 static enum tag_types cp_parser_token_is_class_key
1650   (cp_token *);
1651 static void cp_parser_check_class_key
1652   (enum tag_types, tree type);
1653 static bool cp_parser_optional_template_keyword
1654   (cp_parser *);
1655 static void cp_parser_pre_parsed_nested_name_specifier 
1656   (cp_parser *);
1657 static void cp_parser_cache_group
1658   (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1659 static void cp_parser_parse_tentatively 
1660   (cp_parser *);
1661 static void cp_parser_commit_to_tentative_parse
1662   (cp_parser *);
1663 static void cp_parser_abort_tentative_parse
1664   (cp_parser *);
1665 static bool cp_parser_parse_definitely
1666   (cp_parser *);
1667 static inline bool cp_parser_parsing_tentatively
1668   (cp_parser *);
1669 static bool cp_parser_committed_to_tentative_parse
1670   (cp_parser *);
1671 static void cp_parser_error
1672   (cp_parser *, const char *);
1673 static bool cp_parser_simulate_error
1674   (cp_parser *);
1675 static void cp_parser_check_type_definition
1676   (cp_parser *);
1677 static void cp_parser_check_for_definition_in_return_type
1678   (tree, int);
1679 static tree cp_parser_non_constant_expression
1680   (const char *);
1681 static bool cp_parser_diagnose_invalid_type_name
1682   (cp_parser *);
1683 static int cp_parser_skip_to_closing_parenthesis
1684   (cp_parser *, bool, bool);
1685 static void cp_parser_skip_to_end_of_statement
1686   (cp_parser *);
1687 static void cp_parser_consume_semicolon_at_end_of_statement
1688   (cp_parser *);
1689 static void cp_parser_skip_to_end_of_block_or_statement
1690   (cp_parser *);
1691 static void cp_parser_skip_to_closing_brace
1692   (cp_parser *);
1693 static void cp_parser_skip_until_found
1694   (cp_parser *, enum cpp_ttype, const char *);
1695 static bool cp_parser_error_occurred
1696   (cp_parser *);
1697 static bool cp_parser_allow_gnu_extensions_p
1698   (cp_parser *);
1699 static bool cp_parser_is_string_literal
1700   (cp_token *);
1701 static bool cp_parser_is_keyword 
1702   (cp_token *, enum rid);
1703
1704 /* Returns nonzero if we are parsing tentatively.  */
1705
1706 static inline bool
1707 cp_parser_parsing_tentatively (cp_parser* parser)
1708 {
1709   return parser->context->next != NULL;
1710 }
1711
1712 /* Returns nonzero if TOKEN is a string literal.  */
1713
1714 static bool
1715 cp_parser_is_string_literal (cp_token* token)
1716 {
1717   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1718 }
1719
1720 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1721
1722 static bool
1723 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1724 {
1725   return token->keyword == keyword;
1726 }
1727
1728 /* Issue the indicated error MESSAGE.  */
1729
1730 static void
1731 cp_parser_error (cp_parser* parser, const char* message)
1732 {
1733   /* Output the MESSAGE -- unless we're parsing tentatively.  */
1734   if (!cp_parser_simulate_error (parser))
1735     error (message);
1736 }
1737
1738 /* If we are parsing tentatively, remember that an error has occurred
1739    during this tentative parse.  Returns true if the error was
1740    simulated; false if a messgae should be issued by the caller.  */
1741
1742 static bool
1743 cp_parser_simulate_error (cp_parser* parser)
1744 {
1745   if (cp_parser_parsing_tentatively (parser)
1746       && !cp_parser_committed_to_tentative_parse (parser))
1747     {
1748       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1749       return true;
1750     }
1751   return false;
1752 }
1753
1754 /* This function is called when a type is defined.  If type
1755    definitions are forbidden at this point, an error message is
1756    issued.  */
1757
1758 static void
1759 cp_parser_check_type_definition (cp_parser* parser)
1760 {
1761   /* If types are forbidden here, issue a message.  */
1762   if (parser->type_definition_forbidden_message)
1763     /* Use `%s' to print the string in case there are any escape
1764        characters in the message.  */
1765     error ("%s", parser->type_definition_forbidden_message);
1766 }
1767
1768 /* This function is called when a declaration is parsed.  If
1769    DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1770    indicates that a type was defined in the decl-specifiers for DECL,
1771    then an error is issued.  */
1772
1773 static void
1774 cp_parser_check_for_definition_in_return_type (tree declarator, 
1775                                                int declares_class_or_enum)
1776 {
1777   /* [dcl.fct] forbids type definitions in return types.
1778      Unfortunately, it's not easy to know whether or not we are
1779      processing a return type until after the fact.  */
1780   while (declarator
1781          && (TREE_CODE (declarator) == INDIRECT_REF
1782              || TREE_CODE (declarator) == ADDR_EXPR))
1783     declarator = TREE_OPERAND (declarator, 0);
1784   if (declarator
1785       && TREE_CODE (declarator) == CALL_EXPR 
1786       && declares_class_or_enum & 2)
1787     error ("new types may not be defined in a return type");
1788 }
1789
1790 /* Issue an eror message about the fact that THING appeared in a
1791    constant-expression.  Returns ERROR_MARK_NODE.  */
1792
1793 static tree
1794 cp_parser_non_constant_expression (const char *thing)
1795 {
1796   error ("%s cannot appear in a constant-expression", thing);
1797   return error_mark_node;
1798 }
1799
1800 /* Check for a common situation where a type-name should be present,
1801    but is not, and issue a sensible error message.  Returns true if an
1802    invalid type-name was detected.  */
1803
1804 static bool
1805 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1806 {
1807   /* If the next two tokens are both identifiers, the code is
1808      erroneous. The usual cause of this situation is code like:
1809
1810        T t;
1811
1812      where "T" should name a type -- but does not.  */
1813   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1814       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1815     {
1816       tree name;
1817
1818       /* If parsing tentatively, we should commit; we really are
1819          looking at a declaration.  */
1820       /* Consume the first identifier.  */
1821       name = cp_lexer_consume_token (parser->lexer)->value;
1822       /* Issue an error message.  */
1823       error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1824       /* If we're in a template class, it's possible that the user was
1825          referring to a type from a base class.  For example:
1826
1827            template <typename T> struct A { typedef T X; };
1828            template <typename T> struct B : public A<T> { X x; };
1829
1830          The user should have said "typename A<T>::X".  */
1831       if (processing_template_decl && current_class_type)
1832         {
1833           tree b;
1834
1835           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1836                b;
1837                b = TREE_CHAIN (b))
1838             {
1839               tree base_type = BINFO_TYPE (b);
1840               if (CLASS_TYPE_P (base_type) 
1841                   && dependent_type_p (base_type))
1842                 {
1843                   tree field;
1844                   /* Go from a particular instantiation of the
1845                      template (which will have an empty TYPE_FIELDs),
1846                      to the main version.  */
1847                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1848                   for (field = TYPE_FIELDS (base_type);
1849                        field;
1850                        field = TREE_CHAIN (field))
1851                     if (TREE_CODE (field) == TYPE_DECL
1852                         && DECL_NAME (field) == name)
1853                       {
1854                         error ("(perhaps `typename %T::%s' was intended)",
1855                                BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1856                         break;
1857                       }
1858                   if (field)
1859                     break;
1860                 }
1861             }
1862         }
1863       /* Skip to the end of the declaration; there's no point in
1864          trying to process it.  */
1865       cp_parser_skip_to_end_of_statement (parser);
1866       
1867       return true;
1868     }
1869
1870   return false;
1871 }
1872
1873 /* Consume tokens up to, and including, the next non-nested closing `)'. 
1874    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
1875    are doing error recovery. Returns -1 if OR_COMMA is true and we
1876    found an unnested comma.  */
1877
1878 static int
1879 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
1880                                        bool recovering, bool or_comma)
1881 {
1882   unsigned paren_depth = 0;
1883   unsigned brace_depth = 0;
1884
1885   if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
1886       && !cp_parser_committed_to_tentative_parse (parser))
1887     return 0;
1888   
1889   while (true)
1890     {
1891       cp_token *token;
1892       
1893       /* If we've run out of tokens, then there is no closing `)'.  */
1894       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
1895         return 0;
1896
1897       if (recovering)
1898         {
1899           token = cp_lexer_peek_token (parser->lexer);
1900
1901           /* This matches the processing in skip_to_end_of_statement */
1902           if (token->type == CPP_SEMICOLON && !brace_depth)
1903             return 0;
1904           if (token->type == CPP_OPEN_BRACE)
1905             ++brace_depth;
1906           if (token->type == CPP_CLOSE_BRACE)
1907             {
1908               if (!brace_depth--)
1909                 return 0;
1910             }
1911           if (or_comma && token->type == CPP_COMMA
1912               && !brace_depth && !paren_depth)
1913             return -1;
1914         }
1915       
1916       /* Consume the token.  */
1917       token = cp_lexer_consume_token (parser->lexer);
1918
1919       if (!brace_depth)
1920         {
1921           /* If it is an `(', we have entered another level of nesting.  */
1922           if (token->type == CPP_OPEN_PAREN)
1923             ++paren_depth;
1924           /* If it is a `)', then we might be done.  */
1925           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
1926             return 1;
1927         }
1928     }
1929 }
1930
1931 /* Consume tokens until we reach the end of the current statement.
1932    Normally, that will be just before consuming a `;'.  However, if a
1933    non-nested `}' comes first, then we stop before consuming that.  */
1934
1935 static void
1936 cp_parser_skip_to_end_of_statement (cp_parser* parser)
1937 {
1938   unsigned nesting_depth = 0;
1939
1940   while (true)
1941     {
1942       cp_token *token;
1943
1944       /* Peek at the next token.  */
1945       token = cp_lexer_peek_token (parser->lexer);
1946       /* If we've run out of tokens, stop.  */
1947       if (token->type == CPP_EOF)
1948         break;
1949       /* If the next token is a `;', we have reached the end of the
1950          statement.  */
1951       if (token->type == CPP_SEMICOLON && !nesting_depth)
1952         break;
1953       /* If the next token is a non-nested `}', then we have reached
1954          the end of the current block.  */
1955       if (token->type == CPP_CLOSE_BRACE)
1956         {
1957           /* If this is a non-nested `}', stop before consuming it.
1958              That way, when confronted with something like:
1959
1960                { 3 + } 
1961
1962              we stop before consuming the closing `}', even though we
1963              have not yet reached a `;'.  */
1964           if (nesting_depth == 0)
1965             break;
1966           /* If it is the closing `}' for a block that we have
1967              scanned, stop -- but only after consuming the token.
1968              That way given:
1969
1970                 void f g () { ... }
1971                 typedef int I;
1972
1973              we will stop after the body of the erroneously declared
1974              function, but before consuming the following `typedef'
1975              declaration.  */
1976           if (--nesting_depth == 0)
1977             {
1978               cp_lexer_consume_token (parser->lexer);
1979               break;
1980             }
1981         }
1982       /* If it the next token is a `{', then we are entering a new
1983          block.  Consume the entire block.  */
1984       else if (token->type == CPP_OPEN_BRACE)
1985         ++nesting_depth;
1986       /* Consume the token.  */
1987       cp_lexer_consume_token (parser->lexer);
1988     }
1989 }
1990
1991 /* This function is called at the end of a statement or declaration.
1992    If the next token is a semicolon, it is consumed; otherwise, error
1993    recovery is attempted.  */
1994
1995 static void
1996 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
1997 {
1998   /* Look for the trailing `;'.  */
1999   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2000     {
2001       /* If there is additional (erroneous) input, skip to the end of
2002          the statement.  */
2003       cp_parser_skip_to_end_of_statement (parser);
2004       /* If the next token is now a `;', consume it.  */
2005       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2006         cp_lexer_consume_token (parser->lexer);
2007     }
2008 }
2009
2010 /* Skip tokens until we have consumed an entire block, or until we
2011    have consumed a non-nested `;'.  */
2012
2013 static void
2014 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2015 {
2016   unsigned nesting_depth = 0;
2017
2018   while (true)
2019     {
2020       cp_token *token;
2021
2022       /* Peek at the next token.  */
2023       token = cp_lexer_peek_token (parser->lexer);
2024       /* If we've run out of tokens, stop.  */
2025       if (token->type == CPP_EOF)
2026         break;
2027       /* If the next token is a `;', we have reached the end of the
2028          statement.  */
2029       if (token->type == CPP_SEMICOLON && !nesting_depth)
2030         {
2031           /* Consume the `;'.  */
2032           cp_lexer_consume_token (parser->lexer);
2033           break;
2034         }
2035       /* Consume the token.  */
2036       token = cp_lexer_consume_token (parser->lexer);
2037       /* If the next token is a non-nested `}', then we have reached
2038          the end of the current block.  */
2039       if (token->type == CPP_CLOSE_BRACE 
2040           && (nesting_depth == 0 || --nesting_depth == 0))
2041         break;
2042       /* If it the next token is a `{', then we are entering a new
2043          block.  Consume the entire block.  */
2044       if (token->type == CPP_OPEN_BRACE)
2045         ++nesting_depth;
2046     }
2047 }
2048
2049 /* Skip tokens until a non-nested closing curly brace is the next
2050    token.  */
2051
2052 static void
2053 cp_parser_skip_to_closing_brace (cp_parser *parser)
2054 {
2055   unsigned nesting_depth = 0;
2056
2057   while (true)
2058     {
2059       cp_token *token;
2060
2061       /* Peek at the next token.  */
2062       token = cp_lexer_peek_token (parser->lexer);
2063       /* If we've run out of tokens, stop.  */
2064       if (token->type == CPP_EOF)
2065         break;
2066       /* If the next token is a non-nested `}', then we have reached
2067          the end of the current block.  */
2068       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2069         break;
2070       /* If it the next token is a `{', then we are entering a new
2071          block.  Consume the entire block.  */
2072       else if (token->type == CPP_OPEN_BRACE)
2073         ++nesting_depth;
2074       /* Consume the token.  */
2075       cp_lexer_consume_token (parser->lexer);
2076     }
2077 }
2078
2079 /* Create a new C++ parser.  */
2080
2081 static cp_parser *
2082 cp_parser_new (void)
2083 {
2084   cp_parser *parser;
2085   cp_lexer *lexer;
2086
2087   /* cp_lexer_new_main is called before calling ggc_alloc because
2088      cp_lexer_new_main might load a PCH file.  */
2089   lexer = cp_lexer_new_main ();
2090
2091   parser = ggc_alloc_cleared (sizeof (cp_parser));
2092   parser->lexer = lexer;
2093   parser->context = cp_parser_context_new (NULL);
2094
2095   /* For now, we always accept GNU extensions.  */
2096   parser->allow_gnu_extensions_p = 1;
2097
2098   /* The `>' token is a greater-than operator, not the end of a
2099      template-id.  */
2100   parser->greater_than_is_operator_p = true;
2101
2102   parser->default_arg_ok_p = true;
2103   
2104   /* We are not parsing a constant-expression.  */
2105   parser->constant_expression_p = false;
2106   parser->allow_non_constant_expression_p = false;
2107   parser->non_constant_expression_p = false;
2108
2109   /* Local variable names are not forbidden.  */
2110   parser->local_variables_forbidden_p = false;
2111
2112   /* We are not processing an `extern "C"' declaration.  */
2113   parser->in_unbraced_linkage_specification_p = false;
2114
2115   /* We are not processing a declarator.  */
2116   parser->in_declarator_p = false;
2117
2118   /* The unparsed function queue is empty.  */
2119   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2120
2121   /* There are no classes being defined.  */
2122   parser->num_classes_being_defined = 0;
2123
2124   /* No template parameters apply.  */
2125   parser->num_template_parameter_lists = 0;
2126
2127   return parser;
2128 }
2129
2130 /* Lexical conventions [gram.lex]  */
2131
2132 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2133    identifier.  */
2134
2135 static tree 
2136 cp_parser_identifier (cp_parser* parser)
2137 {
2138   cp_token *token;
2139
2140   /* Look for the identifier.  */
2141   token = cp_parser_require (parser, CPP_NAME, "identifier");
2142   /* Return the value.  */
2143   return token ? token->value : error_mark_node;
2144 }
2145
2146 /* Basic concepts [gram.basic]  */
2147
2148 /* Parse a translation-unit.
2149
2150    translation-unit:
2151      declaration-seq [opt]  
2152
2153    Returns TRUE if all went well.  */
2154
2155 static bool
2156 cp_parser_translation_unit (cp_parser* parser)
2157 {
2158   while (true)
2159     {
2160       cp_parser_declaration_seq_opt (parser);
2161
2162       /* If there are no tokens left then all went well.  */
2163       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2164         break;
2165       
2166       /* Otherwise, issue an error message.  */
2167       cp_parser_error (parser, "expected declaration");
2168       return false;
2169     }
2170
2171   /* Consume the EOF token.  */
2172   cp_parser_require (parser, CPP_EOF, "end-of-file");
2173   
2174   /* Finish up.  */
2175   finish_translation_unit ();
2176
2177   /* All went well.  */
2178   return true;
2179 }
2180
2181 /* Expressions [gram.expr] */
2182
2183 /* Parse a primary-expression.
2184
2185    primary-expression:
2186      literal
2187      this
2188      ( expression )
2189      id-expression
2190
2191    GNU Extensions:
2192
2193    primary-expression:
2194      ( compound-statement )
2195      __builtin_va_arg ( assignment-expression , type-id )
2196
2197    literal:
2198      __null
2199
2200    Returns a representation of the expression.  
2201
2202    *IDK indicates what kind of id-expression (if any) was present.  
2203
2204    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2205    used as the operand of a pointer-to-member.  In that case,
2206    *QUALIFYING_CLASS gives the class that is used as the qualifying
2207    class in the pointer-to-member.  */
2208
2209 static tree
2210 cp_parser_primary_expression (cp_parser *parser, 
2211                               cp_id_kind *idk,
2212                               tree *qualifying_class)
2213 {
2214   cp_token *token;
2215
2216   /* Assume the primary expression is not an id-expression.  */
2217   *idk = CP_ID_KIND_NONE;
2218   /* And that it cannot be used as pointer-to-member.  */
2219   *qualifying_class = NULL_TREE;
2220
2221   /* Peek at the next token.  */
2222   token = cp_lexer_peek_token (parser->lexer);
2223   switch (token->type)
2224     {
2225       /* literal:
2226            integer-literal
2227            character-literal
2228            floating-literal
2229            string-literal
2230            boolean-literal  */
2231     case CPP_CHAR:
2232     case CPP_WCHAR:
2233     case CPP_STRING:
2234     case CPP_WSTRING:
2235     case CPP_NUMBER:
2236       token = cp_lexer_consume_token (parser->lexer);
2237       return token->value;
2238
2239     case CPP_OPEN_PAREN:
2240       {
2241         tree expr;
2242         bool saved_greater_than_is_operator_p;
2243
2244         /* Consume the `('.  */
2245         cp_lexer_consume_token (parser->lexer);
2246         /* Within a parenthesized expression, a `>' token is always
2247            the greater-than operator.  */
2248         saved_greater_than_is_operator_p 
2249           = parser->greater_than_is_operator_p;
2250         parser->greater_than_is_operator_p = true;
2251         /* If we see `( { ' then we are looking at the beginning of
2252            a GNU statement-expression.  */
2253         if (cp_parser_allow_gnu_extensions_p (parser)
2254             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2255           {
2256             /* Statement-expressions are not allowed by the standard.  */
2257             if (pedantic)
2258               pedwarn ("ISO C++ forbids braced-groups within expressions");  
2259             
2260             /* And they're not allowed outside of a function-body; you
2261                cannot, for example, write:
2262                
2263                  int i = ({ int j = 3; j + 1; });
2264                
2265                at class or namespace scope.  */
2266             if (!at_function_scope_p ())
2267               error ("statement-expressions are allowed only inside functions");
2268             /* Start the statement-expression.  */
2269             expr = begin_stmt_expr ();
2270             /* Parse the compound-statement.  */
2271             cp_parser_compound_statement (parser, true);
2272             /* Finish up.  */
2273             expr = finish_stmt_expr (expr, false);
2274           }
2275         else
2276           {
2277             /* Parse the parenthesized expression.  */
2278             expr = cp_parser_expression (parser);
2279             /* Let the front end know that this expression was
2280                enclosed in parentheses. This matters in case, for
2281                example, the expression is of the form `A::B', since
2282                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2283                not.  */
2284             finish_parenthesized_expr (expr);
2285           }
2286         /* The `>' token might be the end of a template-id or
2287            template-parameter-list now.  */
2288         parser->greater_than_is_operator_p 
2289           = saved_greater_than_is_operator_p;
2290         /* Consume the `)'.  */
2291         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2292           cp_parser_skip_to_end_of_statement (parser);
2293
2294         return expr;
2295       }
2296
2297     case CPP_KEYWORD:
2298       switch (token->keyword)
2299         {
2300           /* These two are the boolean literals.  */
2301         case RID_TRUE:
2302           cp_lexer_consume_token (parser->lexer);
2303           return boolean_true_node;
2304         case RID_FALSE:
2305           cp_lexer_consume_token (parser->lexer);
2306           return boolean_false_node;
2307           
2308           /* The `__null' literal.  */
2309         case RID_NULL:
2310           cp_lexer_consume_token (parser->lexer);
2311           return null_node;
2312
2313           /* Recognize the `this' keyword.  */
2314         case RID_THIS:
2315           cp_lexer_consume_token (parser->lexer);
2316           if (parser->local_variables_forbidden_p)
2317             {
2318               error ("`this' may not be used in this context");
2319               return error_mark_node;
2320             }
2321           /* Pointers cannot appear in constant-expressions.  */
2322           if (parser->constant_expression_p)
2323             {
2324               if (!parser->allow_non_constant_expression_p)
2325                 return cp_parser_non_constant_expression ("`this'");
2326               parser->non_constant_expression_p = true;
2327             }
2328           return finish_this_expr ();
2329
2330           /* The `operator' keyword can be the beginning of an
2331              id-expression.  */
2332         case RID_OPERATOR:
2333           goto id_expression;
2334
2335         case RID_FUNCTION_NAME:
2336         case RID_PRETTY_FUNCTION_NAME:
2337         case RID_C99_FUNCTION_NAME:
2338           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2339              __func__ are the names of variables -- but they are
2340              treated specially.  Therefore, they are handled here,
2341              rather than relying on the generic id-expression logic
2342              below.  Grammatically, these names are id-expressions.  
2343
2344              Consume the token.  */
2345           token = cp_lexer_consume_token (parser->lexer);
2346           /* Look up the name.  */
2347           return finish_fname (token->value);
2348
2349         case RID_VA_ARG:
2350           {
2351             tree expression;
2352             tree type;
2353
2354             /* The `__builtin_va_arg' construct is used to handle
2355                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2356             cp_lexer_consume_token (parser->lexer);
2357             /* Look for the opening `('.  */
2358             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2359             /* Now, parse the assignment-expression.  */
2360             expression = cp_parser_assignment_expression (parser);
2361             /* Look for the `,'.  */
2362             cp_parser_require (parser, CPP_COMMA, "`,'");
2363             /* Parse the type-id.  */
2364             type = cp_parser_type_id (parser);
2365             /* Look for the closing `)'.  */
2366             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2367             /* Using `va_arg' in a constant-expression is not
2368                allowed.  */
2369             if (parser->constant_expression_p)
2370               {
2371                 if (!parser->allow_non_constant_expression_p)
2372                   return cp_parser_non_constant_expression ("`va_arg'");
2373                 parser->non_constant_expression_p = true;
2374               }
2375             return build_x_va_arg (expression, type);
2376           }
2377
2378         default:
2379           cp_parser_error (parser, "expected primary-expression");
2380           return error_mark_node;
2381         }
2382
2383       /* An id-expression can start with either an identifier, a
2384          `::' as the beginning of a qualified-id, or the "operator"
2385          keyword.  */
2386     case CPP_NAME:
2387     case CPP_SCOPE:
2388     case CPP_TEMPLATE_ID:
2389     case CPP_NESTED_NAME_SPECIFIER:
2390       {
2391         tree id_expression;
2392         tree decl;
2393         const char *error_msg;
2394
2395       id_expression:
2396         /* Parse the id-expression.  */
2397         id_expression 
2398           = cp_parser_id_expression (parser, 
2399                                      /*template_keyword_p=*/false,
2400                                      /*check_dependency_p=*/true,
2401                                      /*template_p=*/NULL);
2402         if (id_expression == error_mark_node)
2403           return error_mark_node;
2404         /* If we have a template-id, then no further lookup is
2405            required.  If the template-id was for a template-class, we
2406            will sometimes have a TYPE_DECL at this point.  */
2407         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2408             || TREE_CODE (id_expression) == TYPE_DECL)
2409           decl = id_expression;
2410         /* Look up the name.  */
2411         else 
2412           {
2413             decl = cp_parser_lookup_name_simple (parser, id_expression);
2414             /* If name lookup gives us a SCOPE_REF, then the
2415                qualifying scope was dependent.  Just propagate the
2416                name.  */
2417             if (TREE_CODE (decl) == SCOPE_REF)
2418               {
2419                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2420                   *qualifying_class = TREE_OPERAND (decl, 0);
2421                 return decl;
2422               }
2423             /* Check to see if DECL is a local variable in a context
2424                where that is forbidden.  */
2425             if (parser->local_variables_forbidden_p
2426                 && local_variable_p (decl))
2427               {
2428                 /* It might be that we only found DECL because we are
2429                    trying to be generous with pre-ISO scoping rules.
2430                    For example, consider:
2431
2432                      int i;
2433                      void g() {
2434                        for (int i = 0; i < 10; ++i) {}
2435                        extern void f(int j = i);
2436                      }
2437
2438                    Here, name look up will originally find the out 
2439                    of scope `i'.  We need to issue a warning message,
2440                    but then use the global `i'.  */
2441                 decl = check_for_out_of_scope_variable (decl);
2442                 if (local_variable_p (decl))
2443                   {
2444                     error ("local variable `%D' may not appear in this context",
2445                            decl);
2446                     return error_mark_node;
2447                   }
2448               }
2449           }
2450         
2451         decl = finish_id_expression (id_expression, decl, parser->scope, 
2452                                      idk, qualifying_class,
2453                                      parser->constant_expression_p,
2454                                      parser->allow_non_constant_expression_p,
2455                                      &parser->non_constant_expression_p,
2456                                      &error_msg);
2457         if (error_msg)
2458           cp_parser_error (parser, error_msg);
2459         return decl;
2460       }
2461
2462       /* Anything else is an error.  */
2463     default:
2464       cp_parser_error (parser, "expected primary-expression");
2465       return error_mark_node;
2466     }
2467 }
2468
2469 /* Parse an id-expression.
2470
2471    id-expression:
2472      unqualified-id
2473      qualified-id
2474
2475    qualified-id:
2476      :: [opt] nested-name-specifier template [opt] unqualified-id
2477      :: identifier
2478      :: operator-function-id
2479      :: template-id
2480
2481    Return a representation of the unqualified portion of the
2482    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2483    a `::' or nested-name-specifier.
2484
2485    Often, if the id-expression was a qualified-id, the caller will
2486    want to make a SCOPE_REF to represent the qualified-id.  This
2487    function does not do this in order to avoid wastefully creating
2488    SCOPE_REFs when they are not required.
2489
2490    If TEMPLATE_KEYWORD_P is true, then we have just seen the
2491    `template' keyword.
2492
2493    If CHECK_DEPENDENCY_P is false, then names are looked up inside
2494    uninstantiated templates.  
2495
2496    If *TEMPLATE_P is non-NULL, it is set to true iff the
2497    `template' keyword is used to explicitly indicate that the entity
2498    named is a template.  */
2499
2500 static tree
2501 cp_parser_id_expression (cp_parser *parser,
2502                          bool template_keyword_p,
2503                          bool check_dependency_p,
2504                          bool *template_p)
2505 {
2506   bool global_scope_p;
2507   bool nested_name_specifier_p;
2508
2509   /* Assume the `template' keyword was not used.  */
2510   if (template_p)
2511     *template_p = false;
2512
2513   /* Look for the optional `::' operator.  */
2514   global_scope_p 
2515     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) 
2516        != NULL_TREE);
2517   /* Look for the optional nested-name-specifier.  */
2518   nested_name_specifier_p 
2519     = (cp_parser_nested_name_specifier_opt (parser,
2520                                             /*typename_keyword_p=*/false,
2521                                             check_dependency_p,
2522                                             /*type_p=*/false)
2523        != NULL_TREE);
2524   /* If there is a nested-name-specifier, then we are looking at
2525      the first qualified-id production.  */
2526   if (nested_name_specifier_p)
2527     {
2528       tree saved_scope;
2529       tree saved_object_scope;
2530       tree saved_qualifying_scope;
2531       tree unqualified_id;
2532       bool is_template;
2533
2534       /* See if the next token is the `template' keyword.  */
2535       if (!template_p)
2536         template_p = &is_template;
2537       *template_p = cp_parser_optional_template_keyword (parser);
2538       /* Name lookup we do during the processing of the
2539          unqualified-id might obliterate SCOPE.  */
2540       saved_scope = parser->scope;
2541       saved_object_scope = parser->object_scope;
2542       saved_qualifying_scope = parser->qualifying_scope;
2543       /* Process the final unqualified-id.  */
2544       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2545                                                  check_dependency_p);
2546       /* Restore the SAVED_SCOPE for our caller.  */
2547       parser->scope = saved_scope;
2548       parser->object_scope = saved_object_scope;
2549       parser->qualifying_scope = saved_qualifying_scope;
2550
2551       return unqualified_id;
2552     }
2553   /* Otherwise, if we are in global scope, then we are looking at one
2554      of the other qualified-id productions.  */
2555   else if (global_scope_p)
2556     {
2557       cp_token *token;
2558       tree id;
2559
2560       /* Peek at the next token.  */
2561       token = cp_lexer_peek_token (parser->lexer);
2562
2563       /* If it's an identifier, and the next token is not a "<", then
2564          we can avoid the template-id case.  This is an optimization
2565          for this common case.  */
2566       if (token->type == CPP_NAME 
2567           && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2568         return cp_parser_identifier (parser);
2569
2570       cp_parser_parse_tentatively (parser);
2571       /* Try a template-id.  */
2572       id = cp_parser_template_id (parser, 
2573                                   /*template_keyword_p=*/false,
2574                                   /*check_dependency_p=*/true);
2575       /* If that worked, we're done.  */
2576       if (cp_parser_parse_definitely (parser))
2577         return id;
2578
2579       /* Peek at the next token.  (Changes in the token buffer may
2580          have invalidated the pointer obtained above.)  */
2581       token = cp_lexer_peek_token (parser->lexer);
2582
2583       switch (token->type)
2584         {
2585         case CPP_NAME:
2586           return cp_parser_identifier (parser);
2587
2588         case CPP_KEYWORD:
2589           if (token->keyword == RID_OPERATOR)
2590             return cp_parser_operator_function_id (parser);
2591           /* Fall through.  */
2592           
2593         default:
2594           cp_parser_error (parser, "expected id-expression");
2595           return error_mark_node;
2596         }
2597     }
2598   else
2599     return cp_parser_unqualified_id (parser, template_keyword_p,
2600                                      /*check_dependency_p=*/true);
2601 }
2602
2603 /* Parse an unqualified-id.
2604
2605    unqualified-id:
2606      identifier
2607      operator-function-id
2608      conversion-function-id
2609      ~ class-name
2610      template-id
2611
2612    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2613    keyword, in a construct like `A::template ...'.
2614
2615    Returns a representation of unqualified-id.  For the `identifier'
2616    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
2617    production a BIT_NOT_EXPR is returned; the operand of the
2618    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
2619    other productions, see the documentation accompanying the
2620    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
2621    names are looked up in uninstantiated templates.  */
2622
2623 static tree
2624 cp_parser_unqualified_id (cp_parser* parser, 
2625                           bool template_keyword_p,
2626                           bool check_dependency_p)
2627 {
2628   cp_token *token;
2629
2630   /* Peek at the next token.  */
2631   token = cp_lexer_peek_token (parser->lexer);
2632   
2633   switch (token->type)
2634     {
2635     case CPP_NAME:
2636       {
2637         tree id;
2638
2639         /* We don't know yet whether or not this will be a
2640            template-id.  */
2641         cp_parser_parse_tentatively (parser);
2642         /* Try a template-id.  */
2643         id = cp_parser_template_id (parser, template_keyword_p,
2644                                     check_dependency_p);
2645         /* If it worked, we're done.  */
2646         if (cp_parser_parse_definitely (parser))
2647           return id;
2648         /* Otherwise, it's an ordinary identifier.  */
2649         return cp_parser_identifier (parser);
2650       }
2651
2652     case CPP_TEMPLATE_ID:
2653       return cp_parser_template_id (parser, template_keyword_p,
2654                                     check_dependency_p);
2655
2656     case CPP_COMPL:
2657       {
2658         tree type_decl;
2659         tree qualifying_scope;
2660         tree object_scope;
2661         tree scope;
2662
2663         /* Consume the `~' token.  */
2664         cp_lexer_consume_token (parser->lexer);
2665         /* Parse the class-name.  The standard, as written, seems to
2666            say that:
2667
2668              template <typename T> struct S { ~S (); };
2669              template <typename T> S<T>::~S() {}
2670
2671            is invalid, since `~' must be followed by a class-name, but
2672            `S<T>' is dependent, and so not known to be a class.
2673            That's not right; we need to look in uninstantiated
2674            templates.  A further complication arises from:
2675
2676              template <typename T> void f(T t) {
2677                t.T::~T();
2678              } 
2679
2680            Here, it is not possible to look up `T' in the scope of `T'
2681            itself.  We must look in both the current scope, and the
2682            scope of the containing complete expression.  
2683
2684            Yet another issue is:
2685
2686              struct S {
2687                int S;
2688                ~S();
2689              };
2690
2691              S::~S() {}
2692
2693            The standard does not seem to say that the `S' in `~S'
2694            should refer to the type `S' and not the data member
2695            `S::S'.  */
2696
2697         /* DR 244 says that we look up the name after the "~" in the
2698            same scope as we looked up the qualifying name.  That idea
2699            isn't fully worked out; it's more complicated than that.  */
2700         scope = parser->scope;
2701         object_scope = parser->object_scope;
2702         qualifying_scope = parser->qualifying_scope;
2703
2704         /* If the name is of the form "X::~X" it's OK.  */
2705         if (scope && TYPE_P (scope)
2706             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2707             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
2708                 == CPP_OPEN_PAREN)
2709             && (cp_lexer_peek_token (parser->lexer)->value 
2710                 == TYPE_IDENTIFIER (scope)))
2711           {
2712             cp_lexer_consume_token (parser->lexer);
2713             return build_nt (BIT_NOT_EXPR, scope);
2714           }
2715
2716         /* If there was an explicit qualification (S::~T), first look
2717            in the scope given by the qualification (i.e., S).  */
2718         if (scope)
2719           {
2720             cp_parser_parse_tentatively (parser);
2721             type_decl = cp_parser_class_name (parser, 
2722                                               /*typename_keyword_p=*/false,
2723                                               /*template_keyword_p=*/false,
2724                                               /*type_p=*/false,
2725                                               /*check_dependency=*/false,
2726                                               /*class_head_p=*/false);
2727             if (cp_parser_parse_definitely (parser))
2728               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2729           }
2730         /* In "N::S::~S", look in "N" as well.  */
2731         if (scope && qualifying_scope)
2732           {
2733             cp_parser_parse_tentatively (parser);
2734             parser->scope = qualifying_scope;
2735             parser->object_scope = NULL_TREE;
2736             parser->qualifying_scope = NULL_TREE;
2737             type_decl 
2738               = cp_parser_class_name (parser, 
2739                                       /*typename_keyword_p=*/false,
2740                                       /*template_keyword_p=*/false,
2741                                       /*type_p=*/false,
2742                                       /*check_dependency=*/false,
2743                                       /*class_head_p=*/false);
2744             if (cp_parser_parse_definitely (parser))
2745               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2746           }
2747         /* In "p->S::~T", look in the scope given by "*p" as well.  */
2748         else if (object_scope)
2749           {
2750             cp_parser_parse_tentatively (parser);
2751             parser->scope = object_scope;
2752             parser->object_scope = NULL_TREE;
2753             parser->qualifying_scope = NULL_TREE;
2754             type_decl 
2755               = cp_parser_class_name (parser, 
2756                                       /*typename_keyword_p=*/false,
2757                                       /*template_keyword_p=*/false,
2758                                       /*type_p=*/false,
2759                                       /*check_dependency=*/false,
2760                                       /*class_head_p=*/false);
2761             if (cp_parser_parse_definitely (parser))
2762               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2763           }
2764         /* Look in the surrounding context.  */
2765         parser->scope = NULL_TREE;
2766         parser->object_scope = NULL_TREE;
2767         parser->qualifying_scope = NULL_TREE;
2768         type_decl 
2769           = cp_parser_class_name (parser, 
2770                                   /*typename_keyword_p=*/false,
2771                                   /*template_keyword_p=*/false,
2772                                   /*type_p=*/false,
2773                                   /*check_dependency=*/false,
2774                                   /*class_head_p=*/false);
2775         /* If an error occurred, assume that the name of the
2776            destructor is the same as the name of the qualifying
2777            class.  That allows us to keep parsing after running
2778            into ill-formed destructor names.  */
2779         if (type_decl == error_mark_node && scope && TYPE_P (scope))
2780           return build_nt (BIT_NOT_EXPR, scope);
2781         else if (type_decl == error_mark_node)
2782           return error_mark_node;
2783
2784         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2785       }
2786
2787     case CPP_KEYWORD:
2788       if (token->keyword == RID_OPERATOR)
2789         {
2790           tree id;
2791
2792           /* This could be a template-id, so we try that first.  */
2793           cp_parser_parse_tentatively (parser);
2794           /* Try a template-id.  */
2795           id = cp_parser_template_id (parser, template_keyword_p,
2796                                       /*check_dependency_p=*/true);
2797           /* If that worked, we're done.  */
2798           if (cp_parser_parse_definitely (parser))
2799             return id;
2800           /* We still don't know whether we're looking at an
2801              operator-function-id or a conversion-function-id.  */
2802           cp_parser_parse_tentatively (parser);
2803           /* Try an operator-function-id.  */
2804           id = cp_parser_operator_function_id (parser);
2805           /* If that didn't work, try a conversion-function-id.  */
2806           if (!cp_parser_parse_definitely (parser))
2807             id = cp_parser_conversion_function_id (parser);
2808
2809           return id;
2810         }
2811       /* Fall through.  */
2812
2813     default:
2814       cp_parser_error (parser, "expected unqualified-id");
2815       return error_mark_node;
2816     }
2817 }
2818
2819 /* Parse an (optional) nested-name-specifier.
2820
2821    nested-name-specifier:
2822      class-or-namespace-name :: nested-name-specifier [opt]
2823      class-or-namespace-name :: template nested-name-specifier [opt]
2824
2825    PARSER->SCOPE should be set appropriately before this function is
2826    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
2827    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
2828    in name lookups.
2829
2830    Sets PARSER->SCOPE to the class (TYPE) or namespace
2831    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
2832    it unchanged if there is no nested-name-specifier.  Returns the new
2833    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.  */
2834
2835 static tree
2836 cp_parser_nested_name_specifier_opt (cp_parser *parser, 
2837                                      bool typename_keyword_p, 
2838                                      bool check_dependency_p,
2839                                      bool type_p)
2840 {
2841   bool success = false;
2842   tree access_check = NULL_TREE;
2843   ptrdiff_t start;
2844   cp_token* token;
2845
2846   /* If the next token corresponds to a nested name specifier, there
2847      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
2848      false, it may have been true before, in which case something 
2849      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
2850      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
2851      CHECK_DEPENDENCY_P is false, we have to fall through into the
2852      main loop.  */
2853   if (check_dependency_p
2854       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
2855     {
2856       cp_parser_pre_parsed_nested_name_specifier (parser);
2857       return parser->scope;
2858     }
2859
2860   /* Remember where the nested-name-specifier starts.  */
2861   if (cp_parser_parsing_tentatively (parser)
2862       && !cp_parser_committed_to_tentative_parse (parser))
2863     {
2864       token = cp_lexer_peek_token (parser->lexer);
2865       start = cp_lexer_token_difference (parser->lexer,
2866                                          parser->lexer->first_token,
2867                                          token);
2868     }
2869   else
2870     start = -1;
2871
2872   push_deferring_access_checks (dk_deferred);
2873
2874   while (true)
2875     {
2876       tree new_scope;
2877       tree old_scope;
2878       tree saved_qualifying_scope;
2879       bool template_keyword_p;
2880
2881       /* Spot cases that cannot be the beginning of a
2882          nested-name-specifier.  */
2883       token = cp_lexer_peek_token (parser->lexer);
2884
2885       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
2886          the already parsed nested-name-specifier.  */
2887       if (token->type == CPP_NESTED_NAME_SPECIFIER)
2888         {
2889           /* Grab the nested-name-specifier and continue the loop.  */
2890           cp_parser_pre_parsed_nested_name_specifier (parser);
2891           success = true;
2892           continue;
2893         }
2894
2895       /* Spot cases that cannot be the beginning of a
2896          nested-name-specifier.  On the second and subsequent times
2897          through the loop, we look for the `template' keyword.  */
2898       if (success && token->keyword == RID_TEMPLATE)
2899         ;
2900       /* A template-id can start a nested-name-specifier.  */
2901       else if (token->type == CPP_TEMPLATE_ID)
2902         ;
2903       else
2904         {
2905           /* If the next token is not an identifier, then it is
2906              definitely not a class-or-namespace-name.  */
2907           if (token->type != CPP_NAME)
2908             break;
2909           /* If the following token is neither a `<' (to begin a
2910              template-id), nor a `::', then we are not looking at a
2911              nested-name-specifier.  */
2912           token = cp_lexer_peek_nth_token (parser->lexer, 2);
2913           if (token->type != CPP_LESS && token->type != CPP_SCOPE)
2914             break;
2915         }
2916
2917       /* The nested-name-specifier is optional, so we parse
2918          tentatively.  */
2919       cp_parser_parse_tentatively (parser);
2920
2921       /* Look for the optional `template' keyword, if this isn't the
2922          first time through the loop.  */
2923       if (success)
2924         template_keyword_p = cp_parser_optional_template_keyword (parser);
2925       else
2926         template_keyword_p = false;
2927
2928       /* Save the old scope since the name lookup we are about to do
2929          might destroy it.  */
2930       old_scope = parser->scope;
2931       saved_qualifying_scope = parser->qualifying_scope;
2932       /* Parse the qualifying entity.  */
2933       new_scope 
2934         = cp_parser_class_or_namespace_name (parser,
2935                                              typename_keyword_p,
2936                                              template_keyword_p,
2937                                              check_dependency_p,
2938                                              type_p);
2939       /* Look for the `::' token.  */
2940       cp_parser_require (parser, CPP_SCOPE, "`::'");
2941
2942       /* If we found what we wanted, we keep going; otherwise, we're
2943          done.  */
2944       if (!cp_parser_parse_definitely (parser))
2945         {
2946           bool error_p = false;
2947
2948           /* Restore the OLD_SCOPE since it was valid before the
2949              failed attempt at finding the last
2950              class-or-namespace-name.  */
2951           parser->scope = old_scope;
2952           parser->qualifying_scope = saved_qualifying_scope;
2953           /* If the next token is an identifier, and the one after
2954              that is a `::', then any valid interpretation would have
2955              found a class-or-namespace-name.  */
2956           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2957                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
2958                      == CPP_SCOPE)
2959                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
2960                      != CPP_COMPL))
2961             {
2962               token = cp_lexer_consume_token (parser->lexer);
2963               if (!error_p) 
2964                 {
2965                   tree decl;
2966
2967                   decl = cp_parser_lookup_name_simple (parser, token->value);
2968                   if (TREE_CODE (decl) == TEMPLATE_DECL)
2969                     error ("`%D' used without template parameters",
2970                            decl);
2971                   else if (parser->scope)
2972                     {
2973                       if (TYPE_P (parser->scope))
2974                         error ("`%T::%D' is not a class-name or "
2975                                "namespace-name",
2976                                parser->scope, token->value);
2977                       else
2978                         error ("`%D::%D' is not a class-name or "
2979                                "namespace-name",
2980                                parser->scope, token->value);
2981                     }
2982                   else
2983                     error ("`%D' is not a class-name or namespace-name",
2984                            token->value);
2985                   parser->scope = NULL_TREE;
2986                   error_p = true;
2987                   /* Treat this as a successful nested-name-specifier
2988                      due to:
2989
2990                      [basic.lookup.qual]
2991
2992                      If the name found is not a class-name (clause
2993                      _class_) or namespace-name (_namespace.def_), the
2994                      program is ill-formed.  */
2995                   success = true;
2996                 }
2997               cp_lexer_consume_token (parser->lexer);
2998             }
2999           break;
3000         }
3001
3002       /* We've found one valid nested-name-specifier.  */
3003       success = true;
3004       /* Make sure we look in the right scope the next time through
3005          the loop.  */
3006       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL 
3007                        ? TREE_TYPE (new_scope)
3008                        : new_scope);
3009       /* If it is a class scope, try to complete it; we are about to
3010          be looking up names inside the class.  */
3011       if (TYPE_P (parser->scope)
3012           /* Since checking types for dependency can be expensive,
3013              avoid doing it if the type is already complete.  */
3014           && !COMPLETE_TYPE_P (parser->scope)
3015           /* Do not try to complete dependent types.  */
3016           && !dependent_type_p (parser->scope))
3017         complete_type (parser->scope);
3018     }
3019
3020   /* Retrieve any deferred checks.  Do not pop this access checks yet
3021      so the memory will not be reclaimed during token replacing below.  */
3022   access_check = get_deferred_access_checks ();
3023
3024   /* If parsing tentatively, replace the sequence of tokens that makes
3025      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3026      token.  That way, should we re-parse the token stream, we will
3027      not have to repeat the effort required to do the parse, nor will
3028      we issue duplicate error messages.  */
3029   if (success && start >= 0)
3030     {
3031       /* Find the token that corresponds to the start of the
3032          template-id.  */
3033       token = cp_lexer_advance_token (parser->lexer, 
3034                                       parser->lexer->first_token,
3035                                       start);
3036
3037       /* Reset the contents of the START token.  */
3038       token->type = CPP_NESTED_NAME_SPECIFIER;
3039       token->value = build_tree_list (access_check, parser->scope);
3040       TREE_TYPE (token->value) = parser->qualifying_scope;
3041       token->keyword = RID_MAX;
3042       /* Purge all subsequent tokens.  */
3043       cp_lexer_purge_tokens_after (parser->lexer, token);
3044     }
3045
3046   pop_deferring_access_checks ();
3047   return success ? parser->scope : NULL_TREE;
3048 }
3049
3050 /* Parse a nested-name-specifier.  See
3051    cp_parser_nested_name_specifier_opt for details.  This function
3052    behaves identically, except that it will an issue an error if no
3053    nested-name-specifier is present, and it will return
3054    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3055    is present.  */
3056
3057 static tree
3058 cp_parser_nested_name_specifier (cp_parser *parser, 
3059                                  bool typename_keyword_p, 
3060                                  bool check_dependency_p,
3061                                  bool type_p)
3062 {
3063   tree scope;
3064
3065   /* Look for the nested-name-specifier.  */
3066   scope = cp_parser_nested_name_specifier_opt (parser,
3067                                                typename_keyword_p,
3068                                                check_dependency_p,
3069                                                type_p);
3070   /* If it was not present, issue an error message.  */
3071   if (!scope)
3072     {
3073       cp_parser_error (parser, "expected nested-name-specifier");
3074       parser->scope = NULL_TREE;
3075       return error_mark_node;
3076     }
3077
3078   return scope;
3079 }
3080
3081 /* Parse a class-or-namespace-name.
3082
3083    class-or-namespace-name:
3084      class-name
3085      namespace-name
3086
3087    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3088    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3089    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3090    TYPE_P is TRUE iff the next name should be taken as a class-name,
3091    even the same name is declared to be another entity in the same
3092    scope.
3093
3094    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3095    specified by the class-or-namespace-name.  If neither is found the
3096    ERROR_MARK_NODE is returned.  */
3097
3098 static tree
3099 cp_parser_class_or_namespace_name (cp_parser *parser, 
3100                                    bool typename_keyword_p,
3101                                    bool template_keyword_p,
3102                                    bool check_dependency_p,
3103                                    bool type_p)
3104 {
3105   tree saved_scope;
3106   tree saved_qualifying_scope;
3107   tree saved_object_scope;
3108   tree scope;
3109   bool only_class_p;
3110
3111   /* Before we try to parse the class-name, we must save away the
3112      current PARSER->SCOPE since cp_parser_class_name will destroy
3113      it.  */
3114   saved_scope = parser->scope;
3115   saved_qualifying_scope = parser->qualifying_scope;
3116   saved_object_scope = parser->object_scope;
3117   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3118      there is no need to look for a namespace-name.  */
3119   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3120   if (!only_class_p)
3121     cp_parser_parse_tentatively (parser);
3122   scope = cp_parser_class_name (parser, 
3123                                 typename_keyword_p,
3124                                 template_keyword_p,
3125                                 type_p,
3126                                 check_dependency_p,
3127                                 /*class_head_p=*/false);
3128   /* If that didn't work, try for a namespace-name.  */
3129   if (!only_class_p && !cp_parser_parse_definitely (parser))
3130     {
3131       /* Restore the saved scope.  */
3132       parser->scope = saved_scope;
3133       parser->qualifying_scope = saved_qualifying_scope;
3134       parser->object_scope = saved_object_scope;
3135       /* If we are not looking at an identifier followed by the scope
3136          resolution operator, then this is not part of a
3137          nested-name-specifier.  (Note that this function is only used
3138          to parse the components of a nested-name-specifier.)  */
3139       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3140           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3141         return error_mark_node;
3142       scope = cp_parser_namespace_name (parser);
3143     }
3144
3145   return scope;
3146 }
3147
3148 /* Parse a postfix-expression.
3149
3150    postfix-expression:
3151      primary-expression
3152      postfix-expression [ expression ]
3153      postfix-expression ( expression-list [opt] )
3154      simple-type-specifier ( expression-list [opt] )
3155      typename :: [opt] nested-name-specifier identifier 
3156        ( expression-list [opt] )
3157      typename :: [opt] nested-name-specifier template [opt] template-id
3158        ( expression-list [opt] )
3159      postfix-expression . template [opt] id-expression
3160      postfix-expression -> template [opt] id-expression
3161      postfix-expression . pseudo-destructor-name
3162      postfix-expression -> pseudo-destructor-name
3163      postfix-expression ++
3164      postfix-expression --
3165      dynamic_cast < type-id > ( expression )
3166      static_cast < type-id > ( expression )
3167      reinterpret_cast < type-id > ( expression )
3168      const_cast < type-id > ( expression )
3169      typeid ( expression )
3170      typeid ( type-id )
3171
3172    GNU Extension:
3173      
3174    postfix-expression:
3175      ( type-id ) { initializer-list , [opt] }
3176
3177    This extension is a GNU version of the C99 compound-literal
3178    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3179    but they are essentially the same concept.)
3180
3181    If ADDRESS_P is true, the postfix expression is the operand of the
3182    `&' operator.
3183
3184    Returns a representation of the expression.  */
3185
3186 static tree
3187 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3188 {
3189   cp_token *token;
3190   enum rid keyword;
3191   cp_id_kind idk = CP_ID_KIND_NONE;
3192   tree postfix_expression = NULL_TREE;
3193   /* Non-NULL only if the current postfix-expression can be used to
3194      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3195      class used to qualify the member.  */
3196   tree qualifying_class = NULL_TREE;
3197
3198   /* Peek at the next token.  */
3199   token = cp_lexer_peek_token (parser->lexer);
3200   /* Some of the productions are determined by keywords.  */
3201   keyword = token->keyword;
3202   switch (keyword)
3203     {
3204     case RID_DYNCAST:
3205     case RID_STATCAST:
3206     case RID_REINTCAST:
3207     case RID_CONSTCAST:
3208       {
3209         tree type;
3210         tree expression;
3211         const char *saved_message;
3212
3213         /* All of these can be handled in the same way from the point
3214            of view of parsing.  Begin by consuming the token
3215            identifying the cast.  */
3216         cp_lexer_consume_token (parser->lexer);
3217         
3218         /* New types cannot be defined in the cast.  */
3219         saved_message = parser->type_definition_forbidden_message;
3220         parser->type_definition_forbidden_message
3221           = "types may not be defined in casts";
3222
3223         /* Look for the opening `<'.  */
3224         cp_parser_require (parser, CPP_LESS, "`<'");
3225         /* Parse the type to which we are casting.  */
3226         type = cp_parser_type_id (parser);
3227         /* Look for the closing `>'.  */
3228         cp_parser_require (parser, CPP_GREATER, "`>'");
3229         /* Restore the old message.  */
3230         parser->type_definition_forbidden_message = saved_message;
3231
3232         /* And the expression which is being cast.  */
3233         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3234         expression = cp_parser_expression (parser);
3235         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3236
3237         /* Only type conversions to integral or enumeration types
3238            can be used in constant-expressions.  */
3239         if (parser->constant_expression_p
3240             && !dependent_type_p (type)
3241             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3242           {
3243             if (!parser->allow_non_constant_expression_p)
3244               return (cp_parser_non_constant_expression 
3245                       ("a cast to a type other than an integral or "
3246                        "enumeration type"));
3247             parser->non_constant_expression_p = true;
3248           }
3249
3250         switch (keyword)
3251           {
3252           case RID_DYNCAST:
3253             postfix_expression
3254               = build_dynamic_cast (type, expression);
3255             break;
3256           case RID_STATCAST:
3257             postfix_expression
3258               = build_static_cast (type, expression);
3259             break;
3260           case RID_REINTCAST:
3261             postfix_expression
3262               = build_reinterpret_cast (type, expression);
3263             break;
3264           case RID_CONSTCAST:
3265             postfix_expression
3266               = build_const_cast (type, expression);
3267             break;
3268           default:
3269             abort ();
3270           }
3271       }
3272       break;
3273
3274     case RID_TYPEID:
3275       {
3276         tree type;
3277         const char *saved_message;
3278
3279         /* Consume the `typeid' token.  */
3280         cp_lexer_consume_token (parser->lexer);
3281         /* Look for the `(' token.  */
3282         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3283         /* Types cannot be defined in a `typeid' expression.  */
3284         saved_message = parser->type_definition_forbidden_message;
3285         parser->type_definition_forbidden_message
3286           = "types may not be defined in a `typeid\' expression";
3287         /* We can't be sure yet whether we're looking at a type-id or an
3288            expression.  */
3289         cp_parser_parse_tentatively (parser);
3290         /* Try a type-id first.  */
3291         type = cp_parser_type_id (parser);
3292         /* Look for the `)' token.  Otherwise, we can't be sure that
3293            we're not looking at an expression: consider `typeid (int
3294            (3))', for example.  */
3295         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3296         /* If all went well, simply lookup the type-id.  */
3297         if (cp_parser_parse_definitely (parser))
3298           postfix_expression = get_typeid (type);
3299         /* Otherwise, fall back to the expression variant.  */
3300         else
3301           {
3302             tree expression;
3303
3304             /* Look for an expression.  */
3305             expression = cp_parser_expression (parser);
3306             /* Compute its typeid.  */
3307             postfix_expression = build_typeid (expression);
3308             /* Look for the `)' token.  */
3309             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3310           }
3311
3312         /* Restore the saved message.  */
3313         parser->type_definition_forbidden_message = saved_message;
3314       }
3315       break;
3316       
3317     case RID_TYPENAME:
3318       {
3319         bool template_p = false;
3320         tree id;
3321         tree type;
3322
3323         /* Consume the `typename' token.  */
3324         cp_lexer_consume_token (parser->lexer);
3325         /* Look for the optional `::' operator.  */
3326         cp_parser_global_scope_opt (parser, 
3327                                     /*current_scope_valid_p=*/false);
3328         /* Look for the nested-name-specifier.  */
3329         cp_parser_nested_name_specifier (parser,
3330                                          /*typename_keyword_p=*/true,
3331                                          /*check_dependency_p=*/true,
3332                                          /*type_p=*/true);
3333         /* Look for the optional `template' keyword.  */
3334         template_p = cp_parser_optional_template_keyword (parser);
3335         /* We don't know whether we're looking at a template-id or an
3336            identifier.  */
3337         cp_parser_parse_tentatively (parser);
3338         /* Try a template-id.  */
3339         id = cp_parser_template_id (parser, template_p,
3340                                     /*check_dependency_p=*/true);
3341         /* If that didn't work, try an identifier.  */
3342         if (!cp_parser_parse_definitely (parser))
3343           id = cp_parser_identifier (parser);
3344         /* Create a TYPENAME_TYPE to represent the type to which the
3345            functional cast is being performed.  */
3346         type = make_typename_type (parser->scope, id, 
3347                                    /*complain=*/1);
3348
3349         postfix_expression = cp_parser_functional_cast (parser, type);
3350       }
3351       break;
3352
3353     default:
3354       {
3355         tree type;
3356
3357         /* If the next thing is a simple-type-specifier, we may be
3358            looking at a functional cast.  We could also be looking at
3359            an id-expression.  So, we try the functional cast, and if
3360            that doesn't work we fall back to the primary-expression.  */
3361         cp_parser_parse_tentatively (parser);
3362         /* Look for the simple-type-specifier.  */
3363         type = cp_parser_simple_type_specifier (parser, 
3364                                                 CP_PARSER_FLAGS_NONE,
3365                                                 /*identifier_p=*/false);
3366         /* Parse the cast itself.  */
3367         if (!cp_parser_error_occurred (parser))
3368           postfix_expression 
3369             = cp_parser_functional_cast (parser, type);
3370         /* If that worked, we're done.  */
3371         if (cp_parser_parse_definitely (parser))
3372           break;
3373
3374         /* If the functional-cast didn't work out, try a
3375            compound-literal.  */
3376         if (cp_parser_allow_gnu_extensions_p (parser)
3377             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3378           {
3379             tree initializer_list = NULL_TREE;
3380
3381             cp_parser_parse_tentatively (parser);
3382             /* Consume the `('.  */
3383             cp_lexer_consume_token (parser->lexer);
3384             /* Parse the type.  */
3385             type = cp_parser_type_id (parser);
3386             /* Look for the `)'.  */
3387             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3388             /* Look for the `{'.  */
3389             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3390             /* If things aren't going well, there's no need to
3391                keep going.  */
3392             if (!cp_parser_error_occurred (parser))
3393               {
3394                 bool non_constant_p;
3395                 /* Parse the initializer-list.  */
3396                 initializer_list 
3397                   = cp_parser_initializer_list (parser, &non_constant_p);
3398                 /* Allow a trailing `,'.  */
3399                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3400                   cp_lexer_consume_token (parser->lexer);
3401                 /* Look for the final `}'.  */
3402                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3403               }
3404             /* If that worked, we're definitely looking at a
3405                compound-literal expression.  */
3406             if (cp_parser_parse_definitely (parser))
3407               {
3408                 /* Warn the user that a compound literal is not
3409                    allowed in standard C++.  */
3410                 if (pedantic)
3411                   pedwarn ("ISO C++ forbids compound-literals");
3412                 /* Form the representation of the compound-literal.  */
3413                 postfix_expression 
3414                   = finish_compound_literal (type, initializer_list);
3415                 break;
3416               }
3417           }
3418
3419         /* It must be a primary-expression.  */
3420         postfix_expression = cp_parser_primary_expression (parser, 
3421                                                            &idk,
3422                                                            &qualifying_class);
3423       }
3424       break;
3425     }
3426
3427   /* If we were avoiding committing to the processing of a
3428      qualified-id until we knew whether or not we had a
3429      pointer-to-member, we now know.  */
3430   if (qualifying_class)
3431     {
3432       bool done;
3433
3434       /* Peek at the next token.  */
3435       token = cp_lexer_peek_token (parser->lexer);
3436       done = (token->type != CPP_OPEN_SQUARE
3437               && token->type != CPP_OPEN_PAREN
3438               && token->type != CPP_DOT
3439               && token->type != CPP_DEREF
3440               && token->type != CPP_PLUS_PLUS
3441               && token->type != CPP_MINUS_MINUS);
3442
3443       postfix_expression = finish_qualified_id_expr (qualifying_class,
3444                                                      postfix_expression,
3445                                                      done,
3446                                                      address_p);
3447       if (done)
3448         return postfix_expression;
3449     }
3450
3451   /* Keep looping until the postfix-expression is complete.  */
3452   while (true)
3453     {
3454       if (idk == CP_ID_KIND_UNQUALIFIED
3455           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3456           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3457         /* It is not a Koenig lookup function call.  */
3458         postfix_expression 
3459           = unqualified_name_lookup_error (postfix_expression);
3460       
3461       /* Peek at the next token.  */
3462       token = cp_lexer_peek_token (parser->lexer);
3463
3464       switch (token->type)
3465         {
3466         case CPP_OPEN_SQUARE:
3467           /* postfix-expression [ expression ] */
3468           {
3469             tree index;
3470
3471             /* Consume the `[' token.  */
3472             cp_lexer_consume_token (parser->lexer);
3473             /* Parse the index expression.  */
3474             index = cp_parser_expression (parser);
3475             /* Look for the closing `]'.  */
3476             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3477
3478             /* Build the ARRAY_REF.  */
3479             postfix_expression 
3480               = grok_array_decl (postfix_expression, index);
3481             idk = CP_ID_KIND_NONE;
3482           }
3483           break;
3484
3485         case CPP_OPEN_PAREN:
3486           /* postfix-expression ( expression-list [opt] ) */
3487           {
3488             tree args = (cp_parser_parenthesized_expression_list 
3489                          (parser, false, /*non_constant_p=*/NULL));
3490
3491             if (args == error_mark_node)
3492               {
3493                 postfix_expression = error_mark_node;
3494                 break;
3495               }
3496             
3497             /* Function calls are not permitted in
3498                constant-expressions.  */
3499             if (parser->constant_expression_p)
3500               {
3501                 if (!parser->allow_non_constant_expression_p)
3502                   return cp_parser_non_constant_expression ("a function call");
3503                 parser->non_constant_expression_p = true;
3504               }
3505
3506             if (idk == CP_ID_KIND_UNQUALIFIED)
3507               {
3508                 if (args
3509                     && (is_overloaded_fn (postfix_expression)
3510                         || DECL_P (postfix_expression)
3511                         || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3512                   postfix_expression 
3513                     = perform_koenig_lookup (postfix_expression, args);
3514                 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3515                   postfix_expression
3516                     = unqualified_fn_lookup_error (postfix_expression);
3517               }
3518           
3519             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3520               {
3521                 tree instance = TREE_OPERAND (postfix_expression, 0);
3522                 tree fn = TREE_OPERAND (postfix_expression, 1);
3523
3524                 if (processing_template_decl
3525                     && (type_dependent_expression_p (instance)
3526                         || (!BASELINK_P (fn)
3527                             && TREE_CODE (fn) != FIELD_DECL)
3528                         || type_dependent_expression_p (fn)
3529                         || any_type_dependent_arguments_p (args)))
3530                   {
3531                     postfix_expression
3532                       = build_min_nt (CALL_EXPR, postfix_expression, args);
3533                     break;
3534                   }
3535                   
3536                 postfix_expression
3537                   = (build_new_method_call 
3538                      (instance, fn, args, NULL_TREE, 
3539                       (idk == CP_ID_KIND_QUALIFIED 
3540                        ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3541               }
3542             else if (TREE_CODE (postfix_expression) == OFFSET_REF
3543                      || TREE_CODE (postfix_expression) == MEMBER_REF
3544                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3545               postfix_expression = (build_offset_ref_call_from_tree
3546                                     (postfix_expression, args));
3547             else if (idk == CP_ID_KIND_QUALIFIED)
3548               /* A call to a static class member, or a namespace-scope
3549                  function.  */
3550               postfix_expression
3551                 = finish_call_expr (postfix_expression, args,
3552                                     /*disallow_virtual=*/true);
3553             else
3554               /* All other function calls.  */
3555               postfix_expression 
3556                 = finish_call_expr (postfix_expression, args, 
3557                                     /*disallow_virtual=*/false);
3558
3559             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
3560             idk = CP_ID_KIND_NONE;
3561           }
3562           break;
3563           
3564         case CPP_DOT:
3565         case CPP_DEREF:
3566           /* postfix-expression . template [opt] id-expression  
3567              postfix-expression . pseudo-destructor-name 
3568              postfix-expression -> template [opt] id-expression
3569              postfix-expression -> pseudo-destructor-name */
3570           {
3571             tree name;
3572             bool dependent_p;
3573             bool template_p;
3574             tree scope = NULL_TREE;
3575
3576             /* If this is a `->' operator, dereference the pointer.  */
3577             if (token->type == CPP_DEREF)
3578               postfix_expression = build_x_arrow (postfix_expression);
3579             /* Check to see whether or not the expression is
3580                type-dependent.  */
3581             dependent_p = type_dependent_expression_p (postfix_expression);
3582             /* The identifier following the `->' or `.' is not
3583                qualified.  */
3584             parser->scope = NULL_TREE;
3585             parser->qualifying_scope = NULL_TREE;
3586             parser->object_scope = NULL_TREE;
3587             idk = CP_ID_KIND_NONE;
3588             /* Enter the scope corresponding to the type of the object
3589                given by the POSTFIX_EXPRESSION.  */
3590             if (!dependent_p 
3591                 && TREE_TYPE (postfix_expression) != NULL_TREE)
3592               {
3593                 scope = TREE_TYPE (postfix_expression);
3594                 /* According to the standard, no expression should
3595                    ever have reference type.  Unfortunately, we do not
3596                    currently match the standard in this respect in
3597                    that our internal representation of an expression
3598                    may have reference type even when the standard says
3599                    it does not.  Therefore, we have to manually obtain
3600                    the underlying type here.  */
3601                 scope = non_reference (scope);
3602                 /* The type of the POSTFIX_EXPRESSION must be
3603                    complete.  */
3604                 scope = complete_type_or_else (scope, NULL_TREE);
3605                 /* Let the name lookup machinery know that we are
3606                    processing a class member access expression.  */
3607                 parser->context->object_type = scope;
3608                 /* If something went wrong, we want to be able to
3609                    discern that case, as opposed to the case where
3610                    there was no SCOPE due to the type of expression
3611                    being dependent.  */
3612                 if (!scope)
3613                   scope = error_mark_node;
3614               }
3615
3616             /* Consume the `.' or `->' operator.  */
3617             cp_lexer_consume_token (parser->lexer);
3618             /* If the SCOPE is not a scalar type, we are looking at an
3619                ordinary class member access expression, rather than a
3620                pseudo-destructor-name.  */
3621             if (!scope || !SCALAR_TYPE_P (scope))
3622               {
3623                 template_p = cp_parser_optional_template_keyword (parser);
3624                 /* Parse the id-expression.  */
3625                 name = cp_parser_id_expression (parser,
3626                                                 template_p,
3627                                                 /*check_dependency_p=*/true,
3628                                                 /*template_p=*/NULL);
3629                 /* In general, build a SCOPE_REF if the member name is
3630                    qualified.  However, if the name was not dependent
3631                    and has already been resolved; there is no need to
3632                    build the SCOPE_REF.  For example;
3633
3634                      struct X { void f(); };
3635                      template <typename T> void f(T* t) { t->X::f(); }
3636  
3637                    Even though "t" is dependent, "X::f" is not and has
3638                    been resolved to a BASELINK; there is no need to
3639                    include scope information.  */
3640
3641                 /* But we do need to remember that there was an explicit
3642                    scope for virtual function calls.  */
3643                 if (parser->scope)
3644                   idk = CP_ID_KIND_QUALIFIED;
3645
3646                 if (name != error_mark_node 
3647                     && !BASELINK_P (name)
3648                     && parser->scope)
3649                   {
3650                     name = build_nt (SCOPE_REF, parser->scope, name);
3651                     parser->scope = NULL_TREE;
3652                     parser->qualifying_scope = NULL_TREE;
3653                     parser->object_scope = NULL_TREE;
3654                   }
3655                 postfix_expression 
3656                   = finish_class_member_access_expr (postfix_expression, name);
3657               }
3658             /* Otherwise, try the pseudo-destructor-name production.  */
3659             else
3660               {
3661                 tree s;
3662                 tree type;
3663
3664                 /* Parse the pseudo-destructor-name.  */
3665                 cp_parser_pseudo_destructor_name (parser, &s, &type);
3666                 /* Form the call.  */
3667                 postfix_expression 
3668                   = finish_pseudo_destructor_expr (postfix_expression,
3669                                                    s, TREE_TYPE (type));
3670               }
3671
3672             /* We no longer need to look up names in the scope of the
3673                object on the left-hand side of the `.' or `->'
3674                operator.  */
3675             parser->context->object_type = NULL_TREE;
3676           }
3677           break;
3678
3679         case CPP_PLUS_PLUS:
3680           /* postfix-expression ++  */
3681           /* Consume the `++' token.  */
3682           cp_lexer_consume_token (parser->lexer);
3683           /* Increments may not appear in constant-expressions.  */
3684           if (parser->constant_expression_p)
3685             {
3686               if (!parser->allow_non_constant_expression_p)
3687                 return cp_parser_non_constant_expression ("an increment");
3688               parser->non_constant_expression_p = true;
3689             }
3690           /* Generate a representation for the complete expression.  */
3691           postfix_expression 
3692             = finish_increment_expr (postfix_expression, 
3693                                      POSTINCREMENT_EXPR);
3694           idk = CP_ID_KIND_NONE;
3695           break;
3696
3697         case CPP_MINUS_MINUS:
3698           /* postfix-expression -- */
3699           /* Consume the `--' token.  */
3700           cp_lexer_consume_token (parser->lexer);
3701           /* Decrements may not appear in constant-expressions.  */
3702           if (parser->constant_expression_p)
3703             {
3704               if (!parser->allow_non_constant_expression_p)
3705                 return cp_parser_non_constant_expression ("a decrement");
3706               parser->non_constant_expression_p = true;
3707             }
3708           /* Generate a representation for the complete expression.  */
3709           postfix_expression 
3710             = finish_increment_expr (postfix_expression, 
3711                                      POSTDECREMENT_EXPR);
3712           idk = CP_ID_KIND_NONE;
3713           break;
3714
3715         default:
3716           return postfix_expression;
3717         }
3718     }
3719
3720   /* We should never get here.  */
3721   abort ();
3722   return error_mark_node;
3723 }
3724
3725 /* Parse a parenthesized expression-list.
3726
3727    expression-list:
3728      assignment-expression
3729      expression-list, assignment-expression
3730
3731    attribute-list:
3732      expression-list
3733      identifier
3734      identifier, expression-list
3735
3736    Returns a TREE_LIST.  The TREE_VALUE of each node is a
3737    representation of an assignment-expression.  Note that a TREE_LIST
3738    is returned even if there is only a single expression in the list.
3739    error_mark_node is returned if the ( and or ) are
3740    missing. NULL_TREE is returned on no expressions. The parentheses
3741    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
3742    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3743    indicates whether or not all of the expressions in the list were
3744    constant.  */
3745
3746 static tree
3747 cp_parser_parenthesized_expression_list (cp_parser* parser, 
3748                                          bool is_attribute_list,
3749                                          bool *non_constant_p)
3750 {
3751   tree expression_list = NULL_TREE;
3752   tree identifier = NULL_TREE;
3753
3754   /* Assume all the expressions will be constant.  */
3755   if (non_constant_p)
3756     *non_constant_p = false;
3757
3758   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3759     return error_mark_node;
3760   
3761   /* Consume expressions until there are no more.  */
3762   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
3763     while (true)
3764       {
3765         tree expr;
3766         
3767         /* At the beginning of attribute lists, check to see if the
3768            next token is an identifier.  */
3769         if (is_attribute_list
3770             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
3771           {
3772             cp_token *token;
3773             
3774             /* Consume the identifier.  */
3775             token = cp_lexer_consume_token (parser->lexer);
3776             /* Save the identifier.  */
3777             identifier = token->value;
3778           }
3779         else
3780           {
3781             /* Parse the next assignment-expression.  */
3782             if (non_constant_p)
3783               {
3784                 bool expr_non_constant_p;
3785                 expr = (cp_parser_constant_expression 
3786                         (parser, /*allow_non_constant_p=*/true,
3787                          &expr_non_constant_p));
3788                 if (expr_non_constant_p)
3789                   *non_constant_p = true;
3790               }
3791             else
3792               expr = cp_parser_assignment_expression (parser);
3793
3794              /* Add it to the list.  We add error_mark_node
3795                 expressions to the list, so that we can still tell if
3796                 the correct form for a parenthesized expression-list
3797                 is found. That gives better errors.  */
3798             expression_list = tree_cons (NULL_TREE, expr, expression_list);
3799
3800             if (expr == error_mark_node)
3801               goto skip_comma;
3802           }
3803
3804         /* After the first item, attribute lists look the same as
3805            expression lists.  */
3806         is_attribute_list = false;
3807         
3808       get_comma:;
3809         /* If the next token isn't a `,', then we are done.  */
3810         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
3811           break;
3812
3813         /* Otherwise, consume the `,' and keep going.  */
3814         cp_lexer_consume_token (parser->lexer);
3815       }
3816   
3817   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3818     {
3819       int ending;
3820       
3821     skip_comma:;
3822       /* We try and resync to an unnested comma, as that will give the
3823          user better diagnostics.  */
3824       ending = cp_parser_skip_to_closing_parenthesis (parser, true, true);
3825       if (ending < 0)
3826         goto get_comma;
3827       if (!ending)
3828         return error_mark_node;
3829     }
3830
3831   /* We built up the list in reverse order so we must reverse it now.  */
3832   expression_list = nreverse (expression_list);
3833   if (identifier)
3834     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
3835   
3836   return expression_list;
3837 }
3838
3839 /* Parse a pseudo-destructor-name.
3840
3841    pseudo-destructor-name:
3842      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
3843      :: [opt] nested-name-specifier template template-id :: ~ type-name
3844      :: [opt] nested-name-specifier [opt] ~ type-name
3845
3846    If either of the first two productions is used, sets *SCOPE to the
3847    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
3848    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
3849    or ERROR_MARK_NODE if no type-name is present.  */
3850
3851 static void
3852 cp_parser_pseudo_destructor_name (cp_parser* parser, 
3853                                   tree* scope, 
3854                                   tree* type)
3855 {
3856   bool nested_name_specifier_p;
3857
3858   /* Look for the optional `::' operator.  */
3859   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
3860   /* Look for the optional nested-name-specifier.  */
3861   nested_name_specifier_p 
3862     = (cp_parser_nested_name_specifier_opt (parser,
3863                                             /*typename_keyword_p=*/false,
3864                                             /*check_dependency_p=*/true,
3865                                             /*type_p=*/false) 
3866        != NULL_TREE);
3867   /* Now, if we saw a nested-name-specifier, we might be doing the
3868      second production.  */
3869   if (nested_name_specifier_p 
3870       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3871     {
3872       /* Consume the `template' keyword.  */
3873       cp_lexer_consume_token (parser->lexer);
3874       /* Parse the template-id.  */
3875       cp_parser_template_id (parser, 
3876                              /*template_keyword_p=*/true,
3877                              /*check_dependency_p=*/false);
3878       /* Look for the `::' token.  */
3879       cp_parser_require (parser, CPP_SCOPE, "`::'");
3880     }
3881   /* If the next token is not a `~', then there might be some
3882      additional qualification.  */
3883   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
3884     {
3885       /* Look for the type-name.  */
3886       *scope = TREE_TYPE (cp_parser_type_name (parser));
3887       /* Look for the `::' token.  */
3888       cp_parser_require (parser, CPP_SCOPE, "`::'");
3889     }
3890   else
3891     *scope = NULL_TREE;
3892
3893   /* Look for the `~'.  */
3894   cp_parser_require (parser, CPP_COMPL, "`~'");
3895   /* Look for the type-name again.  We are not responsible for
3896      checking that it matches the first type-name.  */
3897   *type = cp_parser_type_name (parser);
3898 }
3899
3900 /* Parse a unary-expression.
3901
3902    unary-expression:
3903      postfix-expression
3904      ++ cast-expression
3905      -- cast-expression
3906      unary-operator cast-expression
3907      sizeof unary-expression
3908      sizeof ( type-id )
3909      new-expression
3910      delete-expression
3911
3912    GNU Extensions:
3913
3914    unary-expression:
3915      __extension__ cast-expression
3916      __alignof__ unary-expression
3917      __alignof__ ( type-id )
3918      __real__ cast-expression
3919      __imag__ cast-expression
3920      && identifier
3921
3922    ADDRESS_P is true iff the unary-expression is appearing as the
3923    operand of the `&' operator.
3924
3925    Returns a representation of the expression.  */
3926
3927 static tree
3928 cp_parser_unary_expression (cp_parser *parser, bool address_p)
3929 {
3930   cp_token *token;
3931   enum tree_code unary_operator;
3932
3933   /* Peek at the next token.  */
3934   token = cp_lexer_peek_token (parser->lexer);
3935   /* Some keywords give away the kind of expression.  */
3936   if (token->type == CPP_KEYWORD)
3937     {
3938       enum rid keyword = token->keyword;
3939
3940       switch (keyword)
3941         {
3942         case RID_ALIGNOF:
3943           {
3944             /* Consume the `alignof' token.  */
3945             cp_lexer_consume_token (parser->lexer);
3946             /* Parse the operand.  */
3947             return finish_alignof (cp_parser_sizeof_operand 
3948                                    (parser, keyword));
3949           }
3950
3951         case RID_SIZEOF:
3952           {
3953             tree operand;
3954             
3955             /* Consume the `sizeof' token.  */
3956             cp_lexer_consume_token (parser->lexer);
3957             /* Parse the operand.  */
3958             operand = cp_parser_sizeof_operand (parser, keyword);
3959
3960             /* If the type of the operand cannot be determined build a
3961                SIZEOF_EXPR.  */
3962             if (TYPE_P (operand)
3963                 ? dependent_type_p (operand)
3964                 : type_dependent_expression_p (operand))
3965               return build_min (SIZEOF_EXPR, size_type_node, operand);
3966             /* Otherwise, compute the constant value.  */
3967             else
3968               return finish_sizeof (operand);
3969           }
3970
3971         case RID_NEW:
3972           return cp_parser_new_expression (parser);
3973
3974         case RID_DELETE:
3975           return cp_parser_delete_expression (parser);
3976           
3977         case RID_EXTENSION:
3978           {
3979             /* The saved value of the PEDANTIC flag.  */
3980             int saved_pedantic;
3981             tree expr;
3982
3983             /* Save away the PEDANTIC flag.  */
3984             cp_parser_extension_opt (parser, &saved_pedantic);
3985             /* Parse the cast-expression.  */
3986             expr = cp_parser_simple_cast_expression (parser);
3987             /* Restore the PEDANTIC flag.  */
3988             pedantic = saved_pedantic;
3989
3990             return expr;
3991           }
3992
3993         case RID_REALPART:
3994         case RID_IMAGPART:
3995           {
3996             tree expression;
3997
3998             /* Consume the `__real__' or `__imag__' token.  */
3999             cp_lexer_consume_token (parser->lexer);
4000             /* Parse the cast-expression.  */
4001             expression = cp_parser_simple_cast_expression (parser);
4002             /* Create the complete representation.  */
4003             return build_x_unary_op ((keyword == RID_REALPART
4004                                       ? REALPART_EXPR : IMAGPART_EXPR),
4005                                      expression);
4006           }
4007           break;
4008
4009         default:
4010           break;
4011         }
4012     }
4013
4014   /* Look for the `:: new' and `:: delete', which also signal the
4015      beginning of a new-expression, or delete-expression,
4016      respectively.  If the next token is `::', then it might be one of
4017      these.  */
4018   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4019     {
4020       enum rid keyword;
4021
4022       /* See if the token after the `::' is one of the keywords in
4023          which we're interested.  */
4024       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4025       /* If it's `new', we have a new-expression.  */
4026       if (keyword == RID_NEW)
4027         return cp_parser_new_expression (parser);
4028       /* Similarly, for `delete'.  */
4029       else if (keyword == RID_DELETE)
4030         return cp_parser_delete_expression (parser);
4031     }
4032
4033   /* Look for a unary operator.  */
4034   unary_operator = cp_parser_unary_operator (token);
4035   /* The `++' and `--' operators can be handled similarly, even though
4036      they are not technically unary-operators in the grammar.  */
4037   if (unary_operator == ERROR_MARK)
4038     {
4039       if (token->type == CPP_PLUS_PLUS)
4040         unary_operator = PREINCREMENT_EXPR;
4041       else if (token->type == CPP_MINUS_MINUS)
4042         unary_operator = PREDECREMENT_EXPR;
4043       /* Handle the GNU address-of-label extension.  */
4044       else if (cp_parser_allow_gnu_extensions_p (parser)
4045                && token->type == CPP_AND_AND)
4046         {
4047           tree identifier;
4048
4049           /* Consume the '&&' token.  */
4050           cp_lexer_consume_token (parser->lexer);
4051           /* Look for the identifier.  */
4052           identifier = cp_parser_identifier (parser);
4053           /* Create an expression representing the address.  */
4054           return finish_label_address_expr (identifier);
4055         }
4056     }
4057   if (unary_operator != ERROR_MARK)
4058     {
4059       tree cast_expression;
4060
4061       /* Consume the operator token.  */
4062       token = cp_lexer_consume_token (parser->lexer);
4063       /* Parse the cast-expression.  */
4064       cast_expression 
4065         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4066       /* Now, build an appropriate representation.  */
4067       switch (unary_operator)
4068         {
4069         case INDIRECT_REF:
4070           return build_x_indirect_ref (cast_expression, "unary *");
4071           
4072         case ADDR_EXPR:
4073         case BIT_NOT_EXPR:
4074           return build_x_unary_op (unary_operator, cast_expression);
4075           
4076         case PREINCREMENT_EXPR:
4077         case PREDECREMENT_EXPR:
4078           if (parser->constant_expression_p)
4079             {
4080               if (!parser->allow_non_constant_expression_p)
4081                 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4082                                                           ? "an increment"
4083                                                           : "a decrement");
4084               parser->non_constant_expression_p = true;
4085             }
4086           /* Fall through.  */
4087         case CONVERT_EXPR:
4088         case NEGATE_EXPR:
4089         case TRUTH_NOT_EXPR:
4090           return finish_unary_op_expr (unary_operator, cast_expression);
4091
4092         default:
4093           abort ();
4094           return error_mark_node;
4095         }
4096     }
4097
4098   return cp_parser_postfix_expression (parser, address_p);
4099 }
4100
4101 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4102    unary-operator, the corresponding tree code is returned.  */
4103
4104 static enum tree_code
4105 cp_parser_unary_operator (cp_token* token)
4106 {
4107   switch (token->type)
4108     {
4109     case CPP_MULT:
4110       return INDIRECT_REF;
4111
4112     case CPP_AND:
4113       return ADDR_EXPR;
4114
4115     case CPP_PLUS:
4116       return CONVERT_EXPR;
4117
4118     case CPP_MINUS:
4119       return NEGATE_EXPR;
4120
4121     case CPP_NOT:
4122       return TRUTH_NOT_EXPR;
4123       
4124     case CPP_COMPL:
4125       return BIT_NOT_EXPR;
4126
4127     default:
4128       return ERROR_MARK;
4129     }
4130 }
4131
4132 /* Parse a new-expression.
4133
4134    new-expression:
4135      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4136      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4137
4138    Returns a representation of the expression.  */
4139
4140 static tree
4141 cp_parser_new_expression (cp_parser* parser)
4142 {
4143   bool global_scope_p;
4144   tree placement;
4145   tree type;
4146   tree initializer;
4147
4148   /* Look for the optional `::' operator.  */
4149   global_scope_p 
4150     = (cp_parser_global_scope_opt (parser,
4151                                    /*current_scope_valid_p=*/false)
4152        != NULL_TREE);
4153   /* Look for the `new' operator.  */
4154   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4155   /* There's no easy way to tell a new-placement from the
4156      `( type-id )' construct.  */
4157   cp_parser_parse_tentatively (parser);
4158   /* Look for a new-placement.  */
4159   placement = cp_parser_new_placement (parser);
4160   /* If that didn't work out, there's no new-placement.  */
4161   if (!cp_parser_parse_definitely (parser))
4162     placement = NULL_TREE;
4163
4164   /* If the next token is a `(', then we have a parenthesized
4165      type-id.  */
4166   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4167     {
4168       /* Consume the `('.  */
4169       cp_lexer_consume_token (parser->lexer);
4170       /* Parse the type-id.  */
4171       type = cp_parser_type_id (parser);
4172       /* Look for the closing `)'.  */
4173       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4174     }
4175   /* Otherwise, there must be a new-type-id.  */
4176   else
4177     type = cp_parser_new_type_id (parser);
4178
4179   /* If the next token is a `(', then we have a new-initializer.  */
4180   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4181     initializer = cp_parser_new_initializer (parser);
4182   else
4183     initializer = NULL_TREE;
4184
4185   /* Create a representation of the new-expression.  */
4186   return build_new (placement, type, initializer, global_scope_p);
4187 }
4188
4189 /* Parse a new-placement.
4190
4191    new-placement:
4192      ( expression-list )
4193
4194    Returns the same representation as for an expression-list.  */
4195
4196 static tree
4197 cp_parser_new_placement (cp_parser* parser)
4198 {
4199   tree expression_list;
4200
4201   /* Parse the expression-list.  */
4202   expression_list = (cp_parser_parenthesized_expression_list 
4203                      (parser, false, /*non_constant_p=*/NULL));
4204
4205   return expression_list;
4206 }
4207
4208 /* Parse a new-type-id.
4209
4210    new-type-id:
4211      type-specifier-seq new-declarator [opt]
4212
4213    Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4214    and whose TREE_VALUE is the new-declarator.  */
4215
4216 static tree
4217 cp_parser_new_type_id (cp_parser* parser)
4218 {
4219   tree type_specifier_seq;
4220   tree declarator;
4221   const char *saved_message;
4222
4223   /* The type-specifier sequence must not contain type definitions.
4224      (It cannot contain declarations of new types either, but if they
4225      are not definitions we will catch that because they are not
4226      complete.)  */
4227   saved_message = parser->type_definition_forbidden_message;
4228   parser->type_definition_forbidden_message
4229     = "types may not be defined in a new-type-id";
4230   /* Parse the type-specifier-seq.  */
4231   type_specifier_seq = cp_parser_type_specifier_seq (parser);
4232   /* Restore the old message.  */
4233   parser->type_definition_forbidden_message = saved_message;
4234   /* Parse the new-declarator.  */
4235   declarator = cp_parser_new_declarator_opt (parser);
4236
4237   return build_tree_list (type_specifier_seq, declarator);
4238 }
4239
4240 /* Parse an (optional) new-declarator.
4241
4242    new-declarator:
4243      ptr-operator new-declarator [opt]
4244      direct-new-declarator
4245
4246    Returns a representation of the declarator.  See
4247    cp_parser_declarator for the representations used.  */
4248
4249 static tree
4250 cp_parser_new_declarator_opt (cp_parser* parser)
4251 {
4252   enum tree_code code;
4253   tree type;
4254   tree cv_qualifier_seq;
4255
4256   /* We don't know if there's a ptr-operator next, or not.  */
4257   cp_parser_parse_tentatively (parser);
4258   /* Look for a ptr-operator.  */
4259   code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4260   /* If that worked, look for more new-declarators.  */
4261   if (cp_parser_parse_definitely (parser))
4262     {
4263       tree declarator;
4264
4265       /* Parse another optional declarator.  */
4266       declarator = cp_parser_new_declarator_opt (parser);
4267
4268       /* Create the representation of the declarator.  */
4269       if (code == INDIRECT_REF)
4270         declarator = make_pointer_declarator (cv_qualifier_seq,
4271                                               declarator);
4272       else
4273         declarator = make_reference_declarator (cv_qualifier_seq,
4274                                                 declarator);
4275
4276      /* Handle the pointer-to-member case.  */
4277      if (type)
4278        declarator = build_nt (SCOPE_REF, type, declarator);
4279
4280       return declarator;
4281     }
4282
4283   /* If the next token is a `[', there is a direct-new-declarator.  */
4284   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4285     return cp_parser_direct_new_declarator (parser);
4286
4287   return NULL_TREE;
4288 }
4289
4290 /* Parse a direct-new-declarator.
4291
4292    direct-new-declarator:
4293      [ expression ]
4294      direct-new-declarator [constant-expression]  
4295
4296    Returns an ARRAY_REF, following the same conventions as are
4297    documented for cp_parser_direct_declarator.  */
4298
4299 static tree
4300 cp_parser_direct_new_declarator (cp_parser* parser)
4301 {
4302   tree declarator = NULL_TREE;
4303
4304   while (true)
4305     {
4306       tree expression;
4307
4308       /* Look for the opening `['.  */
4309       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4310       /* The first expression is not required to be constant.  */
4311       if (!declarator)
4312         {
4313           expression = cp_parser_expression (parser);
4314           /* The standard requires that the expression have integral
4315              type.  DR 74 adds enumeration types.  We believe that the
4316              real intent is that these expressions be handled like the
4317              expression in a `switch' condition, which also allows
4318              classes with a single conversion to integral or
4319              enumeration type.  */
4320           if (!processing_template_decl)
4321             {
4322               expression 
4323                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4324                                               expression,
4325                                               /*complain=*/true);
4326               if (!expression)
4327                 {
4328                   error ("expression in new-declarator must have integral or enumeration type");
4329                   expression = error_mark_node;
4330                 }
4331             }
4332         }
4333       /* But all the other expressions must be.  */
4334       else
4335         expression 
4336           = cp_parser_constant_expression (parser, 
4337                                            /*allow_non_constant=*/false,
4338                                            NULL);
4339       /* Look for the closing `]'.  */
4340       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4341
4342       /* Add this bound to the declarator.  */
4343       declarator = build_nt (ARRAY_REF, declarator, expression);
4344
4345       /* If the next token is not a `[', then there are no more
4346          bounds.  */
4347       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4348         break;
4349     }
4350
4351   return declarator;
4352 }
4353
4354 /* Parse a new-initializer.
4355
4356    new-initializer:
4357      ( expression-list [opt] )
4358
4359    Returns a representation of the expression-list.  If there is no
4360    expression-list, VOID_ZERO_NODE is returned.  */
4361
4362 static tree
4363 cp_parser_new_initializer (cp_parser* parser)
4364 {
4365   tree expression_list;
4366
4367   expression_list = (cp_parser_parenthesized_expression_list 
4368                      (parser, false, /*non_constant_p=*/NULL));
4369   if (!expression_list)
4370     expression_list = void_zero_node;
4371
4372   return expression_list;
4373 }
4374
4375 /* Parse a delete-expression.
4376
4377    delete-expression:
4378      :: [opt] delete cast-expression
4379      :: [opt] delete [ ] cast-expression
4380
4381    Returns a representation of the expression.  */
4382
4383 static tree
4384 cp_parser_delete_expression (cp_parser* parser)
4385 {
4386   bool global_scope_p;
4387   bool array_p;
4388   tree expression;
4389
4390   /* Look for the optional `::' operator.  */
4391   global_scope_p
4392     = (cp_parser_global_scope_opt (parser,
4393                                    /*current_scope_valid_p=*/false)
4394        != NULL_TREE);
4395   /* Look for the `delete' keyword.  */
4396   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4397   /* See if the array syntax is in use.  */
4398   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4399     {
4400       /* Consume the `[' token.  */
4401       cp_lexer_consume_token (parser->lexer);
4402       /* Look for the `]' token.  */
4403       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4404       /* Remember that this is the `[]' construct.  */
4405       array_p = true;
4406     }
4407   else
4408     array_p = false;
4409
4410   /* Parse the cast-expression.  */
4411   expression = cp_parser_simple_cast_expression (parser);
4412
4413   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4414 }
4415
4416 /* Parse a cast-expression.
4417
4418    cast-expression:
4419      unary-expression
4420      ( type-id ) cast-expression
4421
4422    Returns a representation of the expression.  */
4423
4424 static tree
4425 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4426 {
4427   /* If it's a `(', then we might be looking at a cast.  */
4428   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4429     {
4430       tree type = NULL_TREE;
4431       tree expr = NULL_TREE;
4432       bool compound_literal_p;
4433       const char *saved_message;
4434
4435       /* There's no way to know yet whether or not this is a cast.
4436          For example, `(int (3))' is a unary-expression, while `(int)
4437          3' is a cast.  So, we resort to parsing tentatively.  */
4438       cp_parser_parse_tentatively (parser);
4439       /* Types may not be defined in a cast.  */
4440       saved_message = parser->type_definition_forbidden_message;
4441       parser->type_definition_forbidden_message
4442         = "types may not be defined in casts";
4443       /* Consume the `('.  */
4444       cp_lexer_consume_token (parser->lexer);
4445       /* A very tricky bit is that `(struct S) { 3 }' is a
4446          compound-literal (which we permit in C++ as an extension).
4447          But, that construct is not a cast-expression -- it is a
4448          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
4449          is legal; if the compound-literal were a cast-expression,
4450          you'd need an extra set of parentheses.)  But, if we parse
4451          the type-id, and it happens to be a class-specifier, then we
4452          will commit to the parse at that point, because we cannot
4453          undo the action that is done when creating a new class.  So,
4454          then we cannot back up and do a postfix-expression.  
4455
4456          Therefore, we scan ahead to the closing `)', and check to see
4457          if the token after the `)' is a `{'.  If so, we are not
4458          looking at a cast-expression.  
4459
4460          Save tokens so that we can put them back.  */
4461       cp_lexer_save_tokens (parser->lexer);
4462       /* Skip tokens until the next token is a closing parenthesis.
4463          If we find the closing `)', and the next token is a `{', then
4464          we are looking at a compound-literal.  */
4465       compound_literal_p 
4466         = (cp_parser_skip_to_closing_parenthesis (parser, false, false)
4467            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4468       /* Roll back the tokens we skipped.  */
4469       cp_lexer_rollback_tokens (parser->lexer);
4470       /* If we were looking at a compound-literal, simulate an error
4471          so that the call to cp_parser_parse_definitely below will
4472          fail.  */
4473       if (compound_literal_p)
4474         cp_parser_simulate_error (parser);
4475       else
4476         {
4477           /* Look for the type-id.  */
4478           type = cp_parser_type_id (parser);
4479           /* Look for the closing `)'.  */
4480           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4481         }
4482
4483       /* Restore the saved message.  */
4484       parser->type_definition_forbidden_message = saved_message;
4485
4486       /* If ok so far, parse the dependent expression. We cannot be
4487          sure it is a cast. Consider `(T ())'.  It is a parenthesized
4488          ctor of T, but looks like a cast to function returning T
4489          without a dependent expression.  */
4490       if (!cp_parser_error_occurred (parser))
4491         expr = cp_parser_simple_cast_expression (parser);
4492
4493       if (cp_parser_parse_definitely (parser))
4494         {
4495           /* Warn about old-style casts, if so requested.  */
4496           if (warn_old_style_cast 
4497               && !in_system_header 
4498               && !VOID_TYPE_P (type) 
4499               && current_lang_name != lang_name_c)
4500             warning ("use of old-style cast");
4501
4502           /* Only type conversions to integral or enumeration types
4503              can be used in constant-expressions.  */
4504           if (parser->constant_expression_p
4505               && !dependent_type_p (type)
4506               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4507             {
4508               if (!parser->allow_non_constant_expression_p)
4509                 return (cp_parser_non_constant_expression 
4510                         ("a casts to a type other than an integral or "
4511                          "enumeration type"));
4512               parser->non_constant_expression_p = true;
4513             }
4514           /* Perform the cast.  */
4515           expr = build_c_cast (type, expr);
4516           return expr;
4517         }
4518     }
4519
4520   /* If we get here, then it's not a cast, so it must be a
4521      unary-expression.  */
4522   return cp_parser_unary_expression (parser, address_p);
4523 }
4524
4525 /* Parse a pm-expression.
4526
4527    pm-expression:
4528      cast-expression
4529      pm-expression .* cast-expression
4530      pm-expression ->* cast-expression
4531
4532      Returns a representation of the expression.  */
4533
4534 static tree
4535 cp_parser_pm_expression (cp_parser* parser)
4536 {
4537   static const cp_parser_token_tree_map map = {
4538     { CPP_DEREF_STAR, MEMBER_REF },
4539     { CPP_DOT_STAR, DOTSTAR_EXPR },
4540     { CPP_EOF, ERROR_MARK }
4541   };
4542
4543   return cp_parser_binary_expression (parser, map, 
4544                                       cp_parser_simple_cast_expression);
4545 }
4546
4547 /* Parse a multiplicative-expression.
4548
4549    mulitplicative-expression:
4550      pm-expression
4551      multiplicative-expression * pm-expression
4552      multiplicative-expression / pm-expression
4553      multiplicative-expression % pm-expression
4554
4555    Returns a representation of the expression.  */
4556
4557 static tree
4558 cp_parser_multiplicative_expression (cp_parser* parser)
4559 {
4560   static const cp_parser_token_tree_map map = {
4561     { CPP_MULT, MULT_EXPR },
4562     { CPP_DIV, TRUNC_DIV_EXPR },
4563     { CPP_MOD, TRUNC_MOD_EXPR },
4564     { CPP_EOF, ERROR_MARK }
4565   };
4566
4567   return cp_parser_binary_expression (parser,
4568                                       map,
4569                                       cp_parser_pm_expression);
4570 }
4571
4572 /* Parse an additive-expression.
4573
4574    additive-expression:
4575      multiplicative-expression
4576      additive-expression + multiplicative-expression
4577      additive-expression - multiplicative-expression
4578
4579    Returns a representation of the expression.  */
4580
4581 static tree
4582 cp_parser_additive_expression (cp_parser* parser)
4583 {
4584   static const cp_parser_token_tree_map map = {
4585     { CPP_PLUS, PLUS_EXPR },
4586     { CPP_MINUS, MINUS_EXPR },
4587     { CPP_EOF, ERROR_MARK }
4588   };
4589
4590   return cp_parser_binary_expression (parser,
4591                                       map,
4592                                       cp_parser_multiplicative_expression);
4593 }
4594
4595 /* Parse a shift-expression.
4596
4597    shift-expression:
4598      additive-expression
4599      shift-expression << additive-expression
4600      shift-expression >> additive-expression
4601
4602    Returns a representation of the expression.  */
4603
4604 static tree
4605 cp_parser_shift_expression (cp_parser* parser)
4606 {
4607   static const cp_parser_token_tree_map map = {
4608     { CPP_LSHIFT, LSHIFT_EXPR },
4609     { CPP_RSHIFT, RSHIFT_EXPR },
4610     { CPP_EOF, ERROR_MARK }
4611   };
4612
4613   return cp_parser_binary_expression (parser,
4614                                       map,
4615                                       cp_parser_additive_expression);
4616 }
4617
4618 /* Parse a relational-expression.
4619
4620    relational-expression:
4621      shift-expression
4622      relational-expression < shift-expression
4623      relational-expression > shift-expression
4624      relational-expression <= shift-expression
4625      relational-expression >= shift-expression
4626
4627    GNU Extension:
4628
4629    relational-expression:
4630      relational-expression <? shift-expression
4631      relational-expression >? shift-expression
4632
4633    Returns a representation of the expression.  */
4634
4635 static tree
4636 cp_parser_relational_expression (cp_parser* parser)
4637 {
4638   static const cp_parser_token_tree_map map = {
4639     { CPP_LESS, LT_EXPR },
4640     { CPP_GREATER, GT_EXPR },
4641     { CPP_LESS_EQ, LE_EXPR },
4642     { CPP_GREATER_EQ, GE_EXPR },
4643     { CPP_MIN, MIN_EXPR },
4644     { CPP_MAX, MAX_EXPR },
4645     { CPP_EOF, ERROR_MARK }
4646   };
4647
4648   return cp_parser_binary_expression (parser,
4649                                       map,
4650                                       cp_parser_shift_expression);
4651 }
4652
4653 /* Parse an equality-expression.
4654
4655    equality-expression:
4656      relational-expression
4657      equality-expression == relational-expression
4658      equality-expression != relational-expression
4659
4660    Returns a representation of the expression.  */
4661
4662 static tree
4663 cp_parser_equality_expression (cp_parser* parser)
4664 {
4665   static const cp_parser_token_tree_map map = {
4666     { CPP_EQ_EQ, EQ_EXPR },
4667     { CPP_NOT_EQ, NE_EXPR },
4668     { CPP_EOF, ERROR_MARK }
4669   };
4670
4671   return cp_parser_binary_expression (parser,
4672                                       map,
4673                                       cp_parser_relational_expression);
4674 }
4675
4676 /* Parse an and-expression.
4677
4678    and-expression:
4679      equality-expression
4680      and-expression & equality-expression
4681
4682    Returns a representation of the expression.  */
4683
4684 static tree
4685 cp_parser_and_expression (cp_parser* parser)
4686 {
4687   static const cp_parser_token_tree_map map = {
4688     { CPP_AND, BIT_AND_EXPR },
4689     { CPP_EOF, ERROR_MARK }
4690   };
4691
4692   return cp_parser_binary_expression (parser,
4693                                       map,
4694                                       cp_parser_equality_expression);
4695 }
4696
4697 /* Parse an exclusive-or-expression.
4698
4699    exclusive-or-expression:
4700      and-expression
4701      exclusive-or-expression ^ and-expression
4702
4703    Returns a representation of the expression.  */
4704
4705 static tree
4706 cp_parser_exclusive_or_expression (cp_parser* parser)
4707 {
4708   static const cp_parser_token_tree_map map = {
4709     { CPP_XOR, BIT_XOR_EXPR },
4710     { CPP_EOF, ERROR_MARK }
4711   };
4712
4713   return cp_parser_binary_expression (parser,
4714                                       map,
4715                                       cp_parser_and_expression);
4716 }
4717
4718
4719 /* Parse an inclusive-or-expression.
4720
4721    inclusive-or-expression:
4722      exclusive-or-expression
4723      inclusive-or-expression | exclusive-or-expression
4724
4725    Returns a representation of the expression.  */
4726
4727 static tree
4728 cp_parser_inclusive_or_expression (cp_parser* parser)
4729 {
4730   static const cp_parser_token_tree_map map = {
4731     { CPP_OR, BIT_IOR_EXPR },
4732     { CPP_EOF, ERROR_MARK }
4733   };
4734
4735   return cp_parser_binary_expression (parser,
4736                                       map,
4737                                       cp_parser_exclusive_or_expression);
4738 }
4739
4740 /* Parse a logical-and-expression.
4741
4742    logical-and-expression:
4743      inclusive-or-expression
4744      logical-and-expression && inclusive-or-expression
4745
4746    Returns a representation of the expression.  */
4747
4748 static tree
4749 cp_parser_logical_and_expression (cp_parser* parser)
4750 {
4751   static const cp_parser_token_tree_map map = {
4752     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
4753     { CPP_EOF, ERROR_MARK }
4754   };
4755
4756   return cp_parser_binary_expression (parser,
4757                                       map,
4758                                       cp_parser_inclusive_or_expression);
4759 }
4760
4761 /* Parse a logical-or-expression.
4762
4763    logical-or-expression:
4764      logical-and-expression
4765      logical-or-expression || logical-and-expression
4766
4767    Returns a representation of the expression.  */
4768
4769 static tree
4770 cp_parser_logical_or_expression (cp_parser* parser)
4771 {
4772   static const cp_parser_token_tree_map map = {
4773     { CPP_OR_OR, TRUTH_ORIF_EXPR },
4774     { CPP_EOF, ERROR_MARK }
4775   };
4776
4777   return cp_parser_binary_expression (parser,
4778                                       map,
4779                                       cp_parser_logical_and_expression);
4780 }
4781
4782 /* Parse the `? expression : assignment-expression' part of a
4783    conditional-expression.  The LOGICAL_OR_EXPR is the
4784    logical-or-expression that started the conditional-expression.
4785    Returns a representation of the entire conditional-expression.
4786
4787    This routine is used by cp_parser_assignment_expression.
4788
4789      ? expression : assignment-expression
4790    
4791    GNU Extensions:
4792    
4793      ? : assignment-expression */
4794
4795 static tree
4796 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
4797 {
4798   tree expr;
4799   tree assignment_expr;
4800
4801   /* Consume the `?' token.  */
4802   cp_lexer_consume_token (parser->lexer);
4803   if (cp_parser_allow_gnu_extensions_p (parser)
4804       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
4805     /* Implicit true clause.  */
4806     expr = NULL_TREE;
4807   else
4808     /* Parse the expression.  */
4809     expr = cp_parser_expression (parser);
4810   
4811   /* The next token should be a `:'.  */
4812   cp_parser_require (parser, CPP_COLON, "`:'");
4813   /* Parse the assignment-expression.  */
4814   assignment_expr = cp_parser_assignment_expression (parser);
4815
4816   /* Build the conditional-expression.  */
4817   return build_x_conditional_expr (logical_or_expr,
4818                                    expr,
4819                                    assignment_expr);
4820 }
4821
4822 /* Parse an assignment-expression.
4823
4824    assignment-expression:
4825      conditional-expression
4826      logical-or-expression assignment-operator assignment_expression
4827      throw-expression
4828
4829    Returns a representation for the expression.  */
4830
4831 static tree
4832 cp_parser_assignment_expression (cp_parser* parser)
4833 {
4834   tree expr;
4835
4836   /* If the next token is the `throw' keyword, then we're looking at
4837      a throw-expression.  */
4838   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
4839     expr = cp_parser_throw_expression (parser);
4840   /* Otherwise, it must be that we are looking at a
4841      logical-or-expression.  */
4842   else
4843     {
4844       /* Parse the logical-or-expression.  */
4845       expr = cp_parser_logical_or_expression (parser);
4846       /* If the next token is a `?' then we're actually looking at a
4847          conditional-expression.  */
4848       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
4849         return cp_parser_question_colon_clause (parser, expr);
4850       else 
4851         {
4852           enum tree_code assignment_operator;
4853
4854           /* If it's an assignment-operator, we're using the second
4855              production.  */
4856           assignment_operator 
4857             = cp_parser_assignment_operator_opt (parser);
4858           if (assignment_operator != ERROR_MARK)
4859             {
4860               tree rhs;
4861
4862               /* Parse the right-hand side of the assignment.  */
4863               rhs = cp_parser_assignment_expression (parser);
4864               /* An assignment may not appear in a
4865                  constant-expression.  */
4866               if (parser->constant_expression_p)
4867                 {
4868                   if (!parser->allow_non_constant_expression_p)
4869                     return cp_parser_non_constant_expression ("an assignment");
4870                   parser->non_constant_expression_p = true;
4871                 }
4872               /* Build the assignment expression.  */
4873               expr = build_x_modify_expr (expr, 
4874                                           assignment_operator, 
4875                                           rhs);
4876             }
4877         }
4878     }
4879
4880   return expr;
4881 }
4882
4883 /* Parse an (optional) assignment-operator.
4884
4885    assignment-operator: one of 
4886      = *= /= %= += -= >>= <<= &= ^= |=  
4887
4888    GNU Extension:
4889    
4890    assignment-operator: one of
4891      <?= >?=
4892
4893    If the next token is an assignment operator, the corresponding tree
4894    code is returned, and the token is consumed.  For example, for
4895    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
4896    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
4897    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
4898    operator, ERROR_MARK is returned.  */
4899
4900 static enum tree_code
4901 cp_parser_assignment_operator_opt (cp_parser* parser)
4902 {
4903   enum tree_code op;
4904   cp_token *token;
4905
4906   /* Peek at the next toen.  */
4907   token = cp_lexer_peek_token (parser->lexer);
4908
4909   switch (token->type)
4910     {
4911     case CPP_EQ:
4912       op = NOP_EXPR;
4913       break;
4914
4915     case CPP_MULT_EQ:
4916       op = MULT_EXPR;
4917       break;
4918
4919     case CPP_DIV_EQ:
4920       op = TRUNC_DIV_EXPR;
4921       break;
4922
4923     case CPP_MOD_EQ:
4924       op = TRUNC_MOD_EXPR;
4925       break;
4926
4927     case CPP_PLUS_EQ:
4928       op = PLUS_EXPR;
4929       break;
4930
4931     case CPP_MINUS_EQ:
4932       op = MINUS_EXPR;
4933       break;
4934
4935     case CPP_RSHIFT_EQ:
4936       op = RSHIFT_EXPR;
4937       break;
4938
4939     case CPP_LSHIFT_EQ:
4940       op = LSHIFT_EXPR;
4941       break;
4942
4943     case CPP_AND_EQ:
4944       op = BIT_AND_EXPR;
4945       break;
4946
4947     case CPP_XOR_EQ:
4948       op = BIT_XOR_EXPR;
4949       break;
4950
4951     case CPP_OR_EQ:
4952       op = BIT_IOR_EXPR;
4953       break;
4954
4955     case CPP_MIN_EQ:
4956       op = MIN_EXPR;
4957       break;
4958
4959     case CPP_MAX_EQ:
4960       op = MAX_EXPR;
4961       break;
4962
4963     default: 
4964       /* Nothing else is an assignment operator.  */
4965       op = ERROR_MARK;
4966     }
4967
4968   /* If it was an assignment operator, consume it.  */
4969   if (op != ERROR_MARK)
4970     cp_lexer_consume_token (parser->lexer);
4971
4972   return op;
4973 }
4974
4975 /* Parse an expression.
4976
4977    expression:
4978      assignment-expression
4979      expression , assignment-expression
4980
4981    Returns a representation of the expression.  */
4982
4983 static tree
4984 cp_parser_expression (cp_parser* parser)
4985 {
4986   tree expression = NULL_TREE;
4987
4988   while (true)
4989     {
4990       tree assignment_expression;
4991
4992       /* Parse the next assignment-expression.  */
4993       assignment_expression 
4994         = cp_parser_assignment_expression (parser);
4995       /* If this is the first assignment-expression, we can just
4996          save it away.  */
4997       if (!expression)
4998         expression = assignment_expression;
4999       else
5000         expression = build_x_compound_expr (expression,
5001                                             assignment_expression);
5002       /* If the next token is not a comma, then we are done with the
5003          expression.  */
5004       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5005         break;
5006       /* Consume the `,'.  */
5007       cp_lexer_consume_token (parser->lexer);
5008       /* A comma operator cannot appear in a constant-expression.  */
5009       if (parser->constant_expression_p)
5010         {
5011           if (!parser->allow_non_constant_expression_p)
5012             expression 
5013               = cp_parser_non_constant_expression ("a comma operator");
5014           parser->non_constant_expression_p = true;
5015         }
5016     }
5017
5018   return expression;
5019 }
5020
5021 /* Parse a constant-expression. 
5022
5023    constant-expression:
5024      conditional-expression  
5025
5026   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5027   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5028   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5029   is false, NON_CONSTANT_P should be NULL.  */
5030
5031 static tree
5032 cp_parser_constant_expression (cp_parser* parser, 
5033                                bool allow_non_constant_p,
5034                                bool *non_constant_p)
5035 {
5036   bool saved_constant_expression_p;
5037   bool saved_allow_non_constant_expression_p;
5038   bool saved_non_constant_expression_p;
5039   tree expression;
5040
5041   /* It might seem that we could simply parse the
5042      conditional-expression, and then check to see if it were
5043      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5044      one that the compiler can figure out is constant, possibly after
5045      doing some simplifications or optimizations.  The standard has a
5046      precise definition of constant-expression, and we must honor
5047      that, even though it is somewhat more restrictive.
5048
5049      For example:
5050
5051        int i[(2, 3)];
5052
5053      is not a legal declaration, because `(2, 3)' is not a
5054      constant-expression.  The `,' operator is forbidden in a
5055      constant-expression.  However, GCC's constant-folding machinery
5056      will fold this operation to an INTEGER_CST for `3'.  */
5057
5058   /* Save the old settings.  */
5059   saved_constant_expression_p = parser->constant_expression_p;
5060   saved_allow_non_constant_expression_p 
5061     = parser->allow_non_constant_expression_p;
5062   saved_non_constant_expression_p = parser->non_constant_expression_p;
5063   /* We are now parsing a constant-expression.  */
5064   parser->constant_expression_p = true;
5065   parser->allow_non_constant_expression_p = allow_non_constant_p;
5066   parser->non_constant_expression_p = false;
5067   /* Although the grammar says "conditional-expression", we parse an
5068      "assignment-expression", which also permits "throw-expression"
5069      and the use of assignment operators.  In the case that
5070      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5071      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5072      actually essential that we look for an assignment-expression.
5073      For example, cp_parser_initializer_clauses uses this function to
5074      determine whether a particular assignment-expression is in fact
5075      constant.  */
5076   expression = cp_parser_assignment_expression (parser);
5077   /* Restore the old settings.  */
5078   parser->constant_expression_p = saved_constant_expression_p;
5079   parser->allow_non_constant_expression_p 
5080     = saved_allow_non_constant_expression_p;
5081   if (allow_non_constant_p)
5082     *non_constant_p = parser->non_constant_expression_p;
5083   parser->non_constant_expression_p = saved_non_constant_expression_p;
5084
5085   return expression;
5086 }
5087
5088 /* Statements [gram.stmt.stmt]  */
5089
5090 /* Parse a statement.  
5091
5092    statement:
5093      labeled-statement
5094      expression-statement
5095      compound-statement
5096      selection-statement
5097      iteration-statement
5098      jump-statement
5099      declaration-statement
5100      try-block  */
5101
5102 static void
5103 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5104 {
5105   tree statement;
5106   cp_token *token;
5107   int statement_line_number;
5108
5109   /* There is no statement yet.  */
5110   statement = NULL_TREE;
5111   /* Peek at the next token.  */
5112   token = cp_lexer_peek_token (parser->lexer);
5113   /* Remember the line number of the first token in the statement.  */
5114   statement_line_number = token->location.line;
5115   /* If this is a keyword, then that will often determine what kind of
5116      statement we have.  */
5117   if (token->type == CPP_KEYWORD)
5118     {
5119       enum rid keyword = token->keyword;
5120
5121       switch (keyword)
5122         {
5123         case RID_CASE:
5124         case RID_DEFAULT:
5125           statement = cp_parser_labeled_statement (parser,
5126                                                    in_statement_expr_p);
5127           break;
5128
5129         case RID_IF:
5130         case RID_SWITCH:
5131           statement = cp_parser_selection_statement (parser);
5132           break;
5133
5134         case RID_WHILE:
5135         case RID_DO:
5136         case RID_FOR:
5137           statement = cp_parser_iteration_statement (parser);
5138           break;
5139
5140         case RID_BREAK:
5141         case RID_CONTINUE:
5142         case RID_RETURN:
5143         case RID_GOTO:
5144           statement = cp_parser_jump_statement (parser);
5145           break;
5146
5147         case RID_TRY:
5148           statement = cp_parser_try_block (parser);
5149           break;
5150
5151         default:
5152           /* It might be a keyword like `int' that can start a
5153              declaration-statement.  */
5154           break;
5155         }
5156     }
5157   else if (token->type == CPP_NAME)
5158     {
5159       /* If the next token is a `:', then we are looking at a
5160          labeled-statement.  */
5161       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5162       if (token->type == CPP_COLON)
5163         statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5164     }
5165   /* Anything that starts with a `{' must be a compound-statement.  */
5166   else if (token->type == CPP_OPEN_BRACE)
5167     statement = cp_parser_compound_statement (parser, false);
5168
5169   /* Everything else must be a declaration-statement or an
5170      expression-statement.  Try for the declaration-statement 
5171      first, unless we are looking at a `;', in which case we know that
5172      we have an expression-statement.  */
5173   if (!statement)
5174     {
5175       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5176         {
5177           cp_parser_parse_tentatively (parser);
5178           /* Try to parse the declaration-statement.  */
5179           cp_parser_declaration_statement (parser);
5180           /* If that worked, we're done.  */
5181           if (cp_parser_parse_definitely (parser))
5182             return;
5183         }
5184       /* Look for an expression-statement instead.  */
5185       statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5186     }
5187
5188   /* Set the line number for the statement.  */
5189   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5190     STMT_LINENO (statement) = statement_line_number;
5191 }
5192
5193 /* Parse a labeled-statement.
5194
5195    labeled-statement:
5196      identifier : statement
5197      case constant-expression : statement
5198      default : statement  
5199
5200    Returns the new CASE_LABEL, for a `case' or `default' label.  For
5201    an ordinary label, returns a LABEL_STMT.  */
5202
5203 static tree
5204 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5205 {
5206   cp_token *token;
5207   tree statement = NULL_TREE;
5208
5209   /* The next token should be an identifier.  */
5210   token = cp_lexer_peek_token (parser->lexer);
5211   if (token->type != CPP_NAME
5212       && token->type != CPP_KEYWORD)
5213     {
5214       cp_parser_error (parser, "expected labeled-statement");
5215       return error_mark_node;
5216     }
5217
5218   switch (token->keyword)
5219     {
5220     case RID_CASE:
5221       {
5222         tree expr;
5223
5224         /* Consume the `case' token.  */
5225         cp_lexer_consume_token (parser->lexer);
5226         /* Parse the constant-expression.  */
5227         expr = cp_parser_constant_expression (parser, 
5228                                               /*allow_non_constant_p=*/false,
5229                                               NULL);
5230         /* Create the label.  */
5231         statement = finish_case_label (expr, NULL_TREE);
5232       }
5233       break;
5234
5235     case RID_DEFAULT:
5236       /* Consume the `default' token.  */
5237       cp_lexer_consume_token (parser->lexer);
5238       /* Create the label.  */
5239       statement = finish_case_label (NULL_TREE, NULL_TREE);
5240       break;
5241
5242     default:
5243       /* Anything else must be an ordinary label.  */
5244       statement = finish_label_stmt (cp_parser_identifier (parser));
5245       break;
5246     }
5247
5248   /* Require the `:' token.  */
5249   cp_parser_require (parser, CPP_COLON, "`:'");
5250   /* Parse the labeled statement.  */
5251   cp_parser_statement (parser, in_statement_expr_p);
5252
5253   /* Return the label, in the case of a `case' or `default' label.  */
5254   return statement;
5255 }
5256
5257 /* Parse an expression-statement.
5258
5259    expression-statement:
5260      expression [opt] ;
5261
5262    Returns the new EXPR_STMT -- or NULL_TREE if the expression
5263    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5264    indicates whether this expression-statement is part of an
5265    expression statement.  */
5266
5267 static tree
5268 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5269 {
5270   tree statement = NULL_TREE;
5271
5272   /* If the next token is a ';', then there is no expression
5273      statement. */
5274   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5275     statement = cp_parser_expression (parser);
5276   
5277   /* Consume the final `;'.  */
5278   cp_parser_consume_semicolon_at_end_of_statement (parser);
5279
5280   if (in_statement_expr_p
5281       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5282     {
5283       /* This is the final expression statement of a statement
5284          expression.  */
5285       statement = finish_stmt_expr_expr (statement);
5286     }
5287   else if (statement)
5288     statement = finish_expr_stmt (statement);
5289   else
5290     finish_stmt ();
5291   
5292   return statement;
5293 }
5294
5295 /* Parse a compound-statement.
5296
5297    compound-statement:
5298      { statement-seq [opt] }
5299      
5300    Returns a COMPOUND_STMT representing the statement.  */
5301
5302 static tree
5303 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5304 {
5305   tree compound_stmt;
5306
5307   /* Consume the `{'.  */
5308   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5309     return error_mark_node;
5310   /* Begin the compound-statement.  */
5311   compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5312   /* Parse an (optional) statement-seq.  */
5313   cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5314   /* Finish the compound-statement.  */
5315   finish_compound_stmt (compound_stmt);
5316   /* Consume the `}'.  */
5317   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5318
5319   return compound_stmt;
5320 }
5321
5322 /* Parse an (optional) statement-seq.
5323
5324    statement-seq:
5325      statement
5326      statement-seq [opt] statement  */
5327
5328 static void
5329 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5330 {
5331   /* Scan statements until there aren't any more.  */
5332   while (true)
5333     {
5334       /* If we're looking at a `}', then we've run out of statements.  */
5335       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5336           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5337         break;
5338
5339       /* Parse the statement.  */
5340       cp_parser_statement (parser, in_statement_expr_p);
5341     }
5342 }
5343
5344 /* Parse a selection-statement.
5345
5346    selection-statement:
5347      if ( condition ) statement
5348      if ( condition ) statement else statement
5349      switch ( condition ) statement  
5350
5351    Returns the new IF_STMT or SWITCH_STMT.  */
5352
5353 static tree
5354 cp_parser_selection_statement (cp_parser* parser)
5355 {
5356   cp_token *token;
5357   enum rid keyword;
5358
5359   /* Peek at the next token.  */
5360   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5361
5362   /* See what kind of keyword it is.  */
5363   keyword = token->keyword;
5364   switch (keyword)
5365     {
5366     case RID_IF:
5367     case RID_SWITCH:
5368       {
5369         tree statement;
5370         tree condition;
5371
5372         /* Look for the `('.  */
5373         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5374           {
5375             cp_parser_skip_to_end_of_statement (parser);
5376             return error_mark_node;
5377           }
5378
5379         /* Begin the selection-statement.  */
5380         if (keyword == RID_IF)
5381           statement = begin_if_stmt ();
5382         else
5383           statement = begin_switch_stmt ();
5384
5385         /* Parse the condition.  */
5386         condition = cp_parser_condition (parser);
5387         /* Look for the `)'.  */
5388         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5389           cp_parser_skip_to_closing_parenthesis (parser, true, false);
5390
5391         if (keyword == RID_IF)
5392           {
5393             tree then_stmt;
5394
5395             /* Add the condition.  */
5396             finish_if_stmt_cond (condition, statement);
5397
5398             /* Parse the then-clause.  */
5399             then_stmt = cp_parser_implicitly_scoped_statement (parser);
5400             finish_then_clause (statement);
5401
5402             /* If the next token is `else', parse the else-clause.  */
5403             if (cp_lexer_next_token_is_keyword (parser->lexer,
5404                                                 RID_ELSE))
5405               {
5406                 tree else_stmt;
5407
5408                 /* Consume the `else' keyword.  */
5409                 cp_lexer_consume_token (parser->lexer);
5410                 /* Parse the else-clause.  */
5411                 else_stmt 
5412                   = cp_parser_implicitly_scoped_statement (parser);
5413                 finish_else_clause (statement);
5414               }
5415
5416             /* Now we're all done with the if-statement.  */
5417             finish_if_stmt ();
5418           }
5419         else
5420           {
5421             tree body;
5422
5423             /* Add the condition.  */
5424             finish_switch_cond (condition, statement);
5425
5426             /* Parse the body of the switch-statement.  */
5427             body = cp_parser_implicitly_scoped_statement (parser);
5428
5429             /* Now we're all done with the switch-statement.  */
5430             finish_switch_stmt (statement);
5431           }
5432
5433         return statement;
5434       }
5435       break;
5436
5437     default:
5438       cp_parser_error (parser, "expected selection-statement");
5439       return error_mark_node;
5440     }
5441 }
5442
5443 /* Parse a condition. 
5444
5445    condition:
5446      expression
5447      type-specifier-seq declarator = assignment-expression  
5448
5449    GNU Extension:
5450    
5451    condition:
5452      type-specifier-seq declarator asm-specification [opt] 
5453        attributes [opt] = assignment-expression
5454  
5455    Returns the expression that should be tested.  */
5456
5457 static tree
5458 cp_parser_condition (cp_parser* parser)
5459 {
5460   tree type_specifiers;
5461   const char *saved_message;
5462
5463   /* Try the declaration first.  */
5464   cp_parser_parse_tentatively (parser);
5465   /* New types are not allowed in the type-specifier-seq for a
5466      condition.  */
5467   saved_message = parser->type_definition_forbidden_message;
5468   parser->type_definition_forbidden_message
5469     = "types may not be defined in conditions";
5470   /* Parse the type-specifier-seq.  */
5471   type_specifiers = cp_parser_type_specifier_seq (parser);
5472   /* Restore the saved message.  */
5473   parser->type_definition_forbidden_message = saved_message;
5474   /* If all is well, we might be looking at a declaration.  */
5475   if (!cp_parser_error_occurred (parser))
5476     {
5477       tree decl;
5478       tree asm_specification;
5479       tree attributes;
5480       tree declarator;
5481       tree initializer = NULL_TREE;
5482       
5483       /* Parse the declarator.  */
5484       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5485                                          /*ctor_dtor_or_conv_p=*/NULL);
5486       /* Parse the attributes.  */
5487       attributes = cp_parser_attributes_opt (parser);
5488       /* Parse the asm-specification.  */
5489       asm_specification = cp_parser_asm_specification_opt (parser);
5490       /* If the next token is not an `=', then we might still be
5491          looking at an expression.  For example:
5492          
5493            if (A(a).x)
5494           
5495          looks like a decl-specifier-seq and a declarator -- but then
5496          there is no `=', so this is an expression.  */
5497       cp_parser_require (parser, CPP_EQ, "`='");
5498       /* If we did see an `=', then we are looking at a declaration
5499          for sure.  */
5500       if (cp_parser_parse_definitely (parser))
5501         {
5502           /* Create the declaration.  */
5503           decl = start_decl (declarator, type_specifiers, 
5504                              /*initialized_p=*/true,
5505                              attributes, /*prefix_attributes=*/NULL_TREE);
5506           /* Parse the assignment-expression.  */
5507           initializer = cp_parser_assignment_expression (parser);
5508           
5509           /* Process the initializer.  */
5510           cp_finish_decl (decl, 
5511                           initializer, 
5512                           asm_specification, 
5513                           LOOKUP_ONLYCONVERTING);
5514           
5515           return convert_from_reference (decl);
5516         }
5517     }
5518   /* If we didn't even get past the declarator successfully, we are
5519      definitely not looking at a declaration.  */
5520   else
5521     cp_parser_abort_tentative_parse (parser);
5522
5523   /* Otherwise, we are looking at an expression.  */
5524   return cp_parser_expression (parser);
5525 }
5526
5527 /* Parse an iteration-statement.
5528
5529    iteration-statement:
5530      while ( condition ) statement
5531      do statement while ( expression ) ;
5532      for ( for-init-statement condition [opt] ; expression [opt] )
5533        statement
5534
5535    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
5536
5537 static tree
5538 cp_parser_iteration_statement (cp_parser* parser)
5539 {
5540   cp_token *token;
5541   enum rid keyword;
5542   tree statement;
5543
5544   /* Peek at the next token.  */
5545   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5546   if (!token)
5547     return error_mark_node;
5548
5549   /* See what kind of keyword it is.  */
5550   keyword = token->keyword;
5551   switch (keyword)
5552     {
5553     case RID_WHILE:
5554       {
5555         tree condition;
5556
5557         /* Begin the while-statement.  */
5558         statement = begin_while_stmt ();
5559         /* Look for the `('.  */
5560         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5561         /* Parse the condition.  */
5562         condition = cp_parser_condition (parser);
5563         finish_while_stmt_cond (condition, statement);
5564         /* Look for the `)'.  */
5565         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5566         /* Parse the dependent statement.  */
5567         cp_parser_already_scoped_statement (parser);
5568         /* We're done with the while-statement.  */
5569         finish_while_stmt (statement);
5570       }
5571       break;
5572
5573     case RID_DO:
5574       {
5575         tree expression;
5576
5577         /* Begin the do-statement.  */
5578         statement = begin_do_stmt ();
5579         /* Parse the body of the do-statement.  */
5580         cp_parser_implicitly_scoped_statement (parser);
5581         finish_do_body (statement);
5582         /* Look for the `while' keyword.  */
5583         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5584         /* Look for the `('.  */
5585         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5586         /* Parse the expression.  */
5587         expression = cp_parser_expression (parser);
5588         /* We're done with the do-statement.  */
5589         finish_do_stmt (expression, statement);
5590         /* Look for the `)'.  */
5591         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5592         /* Look for the `;'.  */
5593         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5594       }
5595       break;
5596
5597     case RID_FOR:
5598       {
5599         tree condition = NULL_TREE;
5600         tree expression = NULL_TREE;
5601
5602         /* Begin the for-statement.  */
5603         statement = begin_for_stmt ();
5604         /* Look for the `('.  */
5605         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5606         /* Parse the initialization.  */
5607         cp_parser_for_init_statement (parser);
5608         finish_for_init_stmt (statement);
5609
5610         /* If there's a condition, process it.  */
5611         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5612           condition = cp_parser_condition (parser);
5613         finish_for_cond (condition, statement);
5614         /* Look for the `;'.  */
5615         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5616
5617         /* If there's an expression, process it.  */
5618         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5619           expression = cp_parser_expression (parser);
5620         finish_for_expr (expression, statement);
5621         /* Look for the `)'.  */
5622         cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5623
5624         /* Parse the body of the for-statement.  */
5625         cp_parser_already_scoped_statement (parser);
5626
5627         /* We're done with the for-statement.  */
5628         finish_for_stmt (statement);
5629       }
5630       break;
5631
5632     default:
5633       cp_parser_error (parser, "expected iteration-statement");
5634       statement = error_mark_node;
5635       break;
5636     }
5637
5638   return statement;
5639 }
5640
5641 /* Parse a for-init-statement.
5642
5643    for-init-statement:
5644      expression-statement
5645      simple-declaration  */
5646
5647 static void
5648 cp_parser_for_init_statement (cp_parser* parser)
5649 {
5650   /* If the next token is a `;', then we have an empty
5651      expression-statement.  Grammatically, this is also a
5652      simple-declaration, but an invalid one, because it does not
5653      declare anything.  Therefore, if we did not handle this case
5654      specially, we would issue an error message about an invalid
5655      declaration.  */
5656   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5657     {
5658       /* We're going to speculatively look for a declaration, falling back
5659          to an expression, if necessary.  */
5660       cp_parser_parse_tentatively (parser);
5661       /* Parse the declaration.  */
5662       cp_parser_simple_declaration (parser,
5663                                     /*function_definition_allowed_p=*/false);
5664       /* If the tentative parse failed, then we shall need to look for an
5665          expression-statement.  */
5666       if (cp_parser_parse_definitely (parser))
5667         return;
5668     }
5669
5670   cp_parser_expression_statement (parser, false);
5671 }
5672
5673 /* Parse a jump-statement.
5674
5675    jump-statement:
5676      break ;
5677      continue ;
5678      return expression [opt] ;
5679      goto identifier ;  
5680
5681    GNU extension:
5682
5683    jump-statement:
5684      goto * expression ;
5685
5686    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5687    GOTO_STMT.  */
5688
5689 static tree
5690 cp_parser_jump_statement (cp_parser* parser)
5691 {
5692   tree statement = error_mark_node;
5693   cp_token *token;
5694   enum rid keyword;
5695
5696   /* Peek at the next token.  */
5697   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
5698   if (!token)
5699     return error_mark_node;
5700
5701   /* See what kind of keyword it is.  */
5702   keyword = token->keyword;
5703   switch (keyword)
5704     {
5705     case RID_BREAK:
5706       statement = finish_break_stmt ();
5707       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5708       break;
5709
5710     case RID_CONTINUE:
5711       statement = finish_continue_stmt ();
5712       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5713       break;
5714
5715     case RID_RETURN:
5716       {
5717         tree expr;
5718
5719         /* If the next token is a `;', then there is no 
5720            expression.  */
5721         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5722           expr = cp_parser_expression (parser);
5723         else
5724           expr = NULL_TREE;
5725         /* Build the return-statement.  */
5726         statement = finish_return_stmt (expr);
5727         /* Look for the final `;'.  */
5728         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5729       }
5730       break;
5731
5732     case RID_GOTO:
5733       /* Create the goto-statement.  */
5734       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
5735         {
5736           /* Issue a warning about this use of a GNU extension.  */
5737           if (pedantic)
5738             pedwarn ("ISO C++ forbids computed gotos");
5739           /* Consume the '*' token.  */
5740           cp_lexer_consume_token (parser->lexer);
5741           /* Parse the dependent expression.  */
5742           finish_goto_stmt (cp_parser_expression (parser));
5743         }
5744       else
5745         finish_goto_stmt (cp_parser_identifier (parser));
5746       /* Look for the final `;'.  */
5747       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5748       break;
5749
5750     default:
5751       cp_parser_error (parser, "expected jump-statement");
5752       break;
5753     }
5754
5755   return statement;
5756 }
5757
5758 /* Parse a declaration-statement.
5759
5760    declaration-statement:
5761      block-declaration  */
5762
5763 static void
5764 cp_parser_declaration_statement (cp_parser* parser)
5765 {
5766   /* Parse the block-declaration.  */
5767   cp_parser_block_declaration (parser, /*statement_p=*/true);
5768
5769   /* Finish off the statement.  */
5770   finish_stmt ();
5771 }
5772
5773 /* Some dependent statements (like `if (cond) statement'), are
5774    implicitly in their own scope.  In other words, if the statement is
5775    a single statement (as opposed to a compound-statement), it is
5776    none-the-less treated as if it were enclosed in braces.  Any
5777    declarations appearing in the dependent statement are out of scope
5778    after control passes that point.  This function parses a statement,
5779    but ensures that is in its own scope, even if it is not a
5780    compound-statement.  
5781
5782    Returns the new statement.  */
5783
5784 static tree
5785 cp_parser_implicitly_scoped_statement (cp_parser* parser)
5786 {
5787   tree statement;
5788
5789   /* If the token is not a `{', then we must take special action.  */
5790   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
5791     {
5792       /* Create a compound-statement.  */
5793       statement = begin_compound_stmt (/*has_no_scope=*/false);
5794       /* Parse the dependent-statement.  */
5795       cp_parser_statement (parser, false);
5796       /* Finish the dummy compound-statement.  */
5797       finish_compound_stmt (statement);
5798     }
5799   /* Otherwise, we simply parse the statement directly.  */
5800   else
5801     statement = cp_parser_compound_statement (parser, false);
5802
5803   /* Return the statement.  */
5804   return statement;
5805 }
5806
5807 /* For some dependent statements (like `while (cond) statement'), we
5808    have already created a scope.  Therefore, even if the dependent
5809    statement is a compound-statement, we do not want to create another
5810    scope.  */
5811
5812 static void
5813 cp_parser_already_scoped_statement (cp_parser* parser)
5814 {
5815   /* If the token is not a `{', then we must take special action.  */
5816   if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
5817     {
5818       tree statement;
5819
5820       /* Create a compound-statement.  */
5821       statement = begin_compound_stmt (/*has_no_scope=*/true);
5822       /* Parse the dependent-statement.  */
5823       cp_parser_statement (parser, false);
5824       /* Finish the dummy compound-statement.  */
5825       finish_compound_stmt (statement);
5826     }
5827   /* Otherwise, we simply parse the statement directly.  */
5828   else
5829     cp_parser_statement (parser, false);
5830 }
5831
5832 /* Declarations [gram.dcl.dcl] */
5833
5834 /* Parse an optional declaration-sequence.
5835
5836    declaration-seq:
5837      declaration
5838      declaration-seq declaration  */
5839
5840 static void
5841 cp_parser_declaration_seq_opt (cp_parser* parser)
5842 {
5843   while (true)
5844     {
5845       cp_token *token;
5846
5847       token = cp_lexer_peek_token (parser->lexer);
5848
5849       if (token->type == CPP_CLOSE_BRACE
5850           || token->type == CPP_EOF)
5851         break;
5852
5853       if (token->type == CPP_SEMICOLON) 
5854         {
5855           /* A declaration consisting of a single semicolon is
5856              invalid.  Allow it unless we're being pedantic.  */
5857           if (pedantic)
5858             pedwarn ("extra `;'");
5859           cp_lexer_consume_token (parser->lexer);
5860           continue;
5861         }
5862
5863       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
5864          parser to enter or exit implicit `extern "C"' blocks.  */
5865       while (pending_lang_change > 0)
5866         {
5867           push_lang_context (lang_name_c);
5868           --pending_lang_change;
5869         }
5870       while (pending_lang_change < 0)
5871         {
5872           pop_lang_context ();
5873           ++pending_lang_change;
5874         }
5875
5876       /* Parse the declaration itself.  */
5877       cp_parser_declaration (parser);
5878     }
5879 }
5880
5881 /* Parse a declaration.
5882
5883    declaration:
5884      block-declaration
5885      function-definition
5886      template-declaration
5887      explicit-instantiation
5888      explicit-specialization
5889      linkage-specification
5890      namespace-definition    
5891
5892    GNU extension:
5893
5894    declaration:
5895       __extension__ declaration */
5896
5897 static void
5898 cp_parser_declaration (cp_parser* parser)
5899 {
5900   cp_token token1;
5901   cp_token token2;
5902   int saved_pedantic;
5903
5904   /* Check for the `__extension__' keyword.  */
5905   if (cp_parser_extension_opt (parser, &saved_pedantic))
5906     {
5907       /* Parse the qualified declaration.  */
5908       cp_parser_declaration (parser);
5909       /* Restore the PEDANTIC flag.  */
5910       pedantic = saved_pedantic;
5911
5912       return;
5913     }
5914
5915   /* Try to figure out what kind of declaration is present.  */
5916   token1 = *cp_lexer_peek_token (parser->lexer);
5917   if (token1.type != CPP_EOF)
5918     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
5919
5920   /* If the next token is `extern' and the following token is a string
5921      literal, then we have a linkage specification.  */
5922   if (token1.keyword == RID_EXTERN
5923       && cp_parser_is_string_literal (&token2))
5924     cp_parser_linkage_specification (parser);
5925   /* If the next token is `template', then we have either a template
5926      declaration, an explicit instantiation, or an explicit
5927      specialization.  */
5928   else if (token1.keyword == RID_TEMPLATE)
5929     {
5930       /* `template <>' indicates a template specialization.  */
5931       if (token2.type == CPP_LESS
5932           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
5933         cp_parser_explicit_specialization (parser);
5934       /* `template <' indicates a template declaration.  */
5935       else if (token2.type == CPP_LESS)
5936         cp_parser_template_declaration (parser, /*member_p=*/false);
5937       /* Anything else must be an explicit instantiation.  */
5938       else
5939         cp_parser_explicit_instantiation (parser);
5940     }
5941   /* If the next token is `export', then we have a template
5942      declaration.  */
5943   else if (token1.keyword == RID_EXPORT)
5944     cp_parser_template_declaration (parser, /*member_p=*/false);
5945   /* If the next token is `extern', 'static' or 'inline' and the one
5946      after that is `template', we have a GNU extended explicit
5947      instantiation directive.  */
5948   else if (cp_parser_allow_gnu_extensions_p (parser)
5949            && (token1.keyword == RID_EXTERN
5950                || token1.keyword == RID_STATIC
5951                || token1.keyword == RID_INLINE)
5952            && token2.keyword == RID_TEMPLATE)
5953     cp_parser_explicit_instantiation (parser);
5954   /* If the next token is `namespace', check for a named or unnamed
5955      namespace definition.  */
5956   else if (token1.keyword == RID_NAMESPACE
5957            && (/* A named namespace definition.  */
5958                (token2.type == CPP_NAME
5959                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
5960                     == CPP_OPEN_BRACE))
5961                /* An unnamed namespace definition.  */
5962                || token2.type == CPP_OPEN_BRACE))
5963     cp_parser_namespace_definition (parser);
5964   /* We must have either a block declaration or a function
5965      definition.  */
5966   else
5967     /* Try to parse a block-declaration, or a function-definition.  */
5968     cp_parser_block_declaration (parser, /*statement_p=*/false);
5969 }
5970
5971 /* Parse a block-declaration.  
5972
5973    block-declaration:
5974      simple-declaration
5975      asm-definition
5976      namespace-alias-definition
5977      using-declaration
5978      using-directive  
5979
5980    GNU Extension:
5981
5982    block-declaration:
5983      __extension__ block-declaration 
5984      label-declaration
5985
5986    If STATEMENT_P is TRUE, then this block-declaration is occurring as
5987    part of a declaration-statement.  */
5988
5989 static void
5990 cp_parser_block_declaration (cp_parser *parser, 
5991                              bool      statement_p)
5992 {
5993   cp_token *token1;
5994   int saved_pedantic;
5995
5996   /* Check for the `__extension__' keyword.  */
5997   if (cp_parser_extension_opt (parser, &saved_pedantic))
5998     {
5999       /* Parse the qualified declaration.  */
6000       cp_parser_block_declaration (parser, statement_p);
6001       /* Restore the PEDANTIC flag.  */
6002       pedantic = saved_pedantic;
6003
6004       return;
6005     }
6006
6007   /* Peek at the next token to figure out which kind of declaration is
6008      present.  */
6009   token1 = cp_lexer_peek_token (parser->lexer);
6010
6011   /* If the next keyword is `asm', we have an asm-definition.  */
6012   if (token1->keyword == RID_ASM)
6013     {
6014       if (statement_p)
6015         cp_parser_commit_to_tentative_parse (parser);
6016       cp_parser_asm_definition (parser);
6017     }
6018   /* If the next keyword is `namespace', we have a
6019      namespace-alias-definition.  */
6020   else if (token1->keyword == RID_NAMESPACE)
6021     cp_parser_namespace_alias_definition (parser);
6022   /* If the next keyword is `using', we have either a
6023      using-declaration or a using-directive.  */
6024   else if (token1->keyword == RID_USING)
6025     {
6026       cp_token *token2;
6027
6028       if (statement_p)
6029         cp_parser_commit_to_tentative_parse (parser);
6030       /* If the token after `using' is `namespace', then we have a
6031          using-directive.  */
6032       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6033       if (token2->keyword == RID_NAMESPACE)
6034         cp_parser_using_directive (parser);
6035       /* Otherwise, it's a using-declaration.  */
6036       else
6037         cp_parser_using_declaration (parser);
6038     }
6039   /* If the next keyword is `__label__' we have a label declaration.  */
6040   else if (token1->keyword == RID_LABEL)
6041     {
6042       if (statement_p)
6043         cp_parser_commit_to_tentative_parse (parser);
6044       cp_parser_label_declaration (parser);
6045     }
6046   /* Anything else must be a simple-declaration.  */
6047   else
6048     cp_parser_simple_declaration (parser, !statement_p);
6049 }
6050
6051 /* Parse a simple-declaration.
6052
6053    simple-declaration:
6054      decl-specifier-seq [opt] init-declarator-list [opt] ;  
6055
6056    init-declarator-list:
6057      init-declarator
6058      init-declarator-list , init-declarator 
6059
6060    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6061    function-definition as a simple-declaration.  */
6062
6063 static void
6064 cp_parser_simple_declaration (cp_parser* parser, 
6065                               bool function_definition_allowed_p)
6066 {
6067   tree decl_specifiers;
6068   tree attributes;
6069   int declares_class_or_enum;
6070   bool saw_declarator;
6071
6072   /* Defer access checks until we know what is being declared; the
6073      checks for names appearing in the decl-specifier-seq should be
6074      done as if we were in the scope of the thing being declared.  */
6075   push_deferring_access_checks (dk_deferred);
6076
6077   /* Parse the decl-specifier-seq.  We have to keep track of whether
6078      or not the decl-specifier-seq declares a named class or
6079      enumeration type, since that is the only case in which the
6080      init-declarator-list is allowed to be empty.  
6081
6082      [dcl.dcl]
6083
6084      In a simple-declaration, the optional init-declarator-list can be
6085      omitted only when declaring a class or enumeration, that is when
6086      the decl-specifier-seq contains either a class-specifier, an
6087      elaborated-type-specifier, or an enum-specifier.  */
6088   decl_specifiers
6089     = cp_parser_decl_specifier_seq (parser, 
6090                                     CP_PARSER_FLAGS_OPTIONAL,
6091                                     &attributes,
6092                                     &declares_class_or_enum);
6093   /* We no longer need to defer access checks.  */
6094   stop_deferring_access_checks ();
6095
6096   /* In a block scope, a valid declaration must always have a
6097      decl-specifier-seq.  By not trying to parse declarators, we can
6098      resolve the declaration/expression ambiguity more quickly.  */
6099   if (!function_definition_allowed_p && !decl_specifiers)
6100     {
6101       cp_parser_error (parser, "expected declaration");
6102       goto done;
6103     }
6104
6105   /* If the next two tokens are both identifiers, the code is
6106      erroneous. The usual cause of this situation is code like:
6107
6108        T t;
6109
6110      where "T" should name a type -- but does not.  */
6111   if (cp_parser_diagnose_invalid_type_name (parser))
6112     {
6113       /* If parsing tentatively, we should commit; we really are
6114          looking at a declaration.  */
6115       cp_parser_commit_to_tentative_parse (parser);
6116       /* Give up.  */
6117       goto done;
6118     }
6119
6120   /* Keep going until we hit the `;' at the end of the simple
6121      declaration.  */
6122   saw_declarator = false;
6123   while (cp_lexer_next_token_is_not (parser->lexer, 
6124                                      CPP_SEMICOLON))
6125     {
6126       cp_token *token;
6127       bool function_definition_p;
6128       tree decl;
6129
6130       saw_declarator = true;
6131       /* Parse the init-declarator.  */
6132       decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6133                                         function_definition_allowed_p,
6134                                         /*member_p=*/false,
6135                                         declares_class_or_enum,
6136                                         &function_definition_p);
6137       /* If an error occurred while parsing tentatively, exit quickly.
6138          (That usually happens when in the body of a function; each
6139          statement is treated as a declaration-statement until proven
6140          otherwise.)  */
6141       if (cp_parser_error_occurred (parser))
6142         goto done;
6143       /* Handle function definitions specially.  */
6144       if (function_definition_p)
6145         {
6146           /* If the next token is a `,', then we are probably
6147              processing something like:
6148
6149                void f() {}, *p;
6150
6151              which is erroneous.  */
6152           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6153             error ("mixing declarations and function-definitions is forbidden");
6154           /* Otherwise, we're done with the list of declarators.  */
6155           else
6156             {
6157               pop_deferring_access_checks ();
6158               return;
6159             }
6160         }
6161       /* The next token should be either a `,' or a `;'.  */
6162       token = cp_lexer_peek_token (parser->lexer);
6163       /* If it's a `,', there are more declarators to come.  */
6164       if (token->type == CPP_COMMA)
6165         cp_lexer_consume_token (parser->lexer);
6166       /* If it's a `;', we are done.  */
6167       else if (token->type == CPP_SEMICOLON)
6168         break;
6169       /* Anything else is an error.  */
6170       else
6171         {
6172           cp_parser_error (parser, "expected `,' or `;'");
6173           /* Skip tokens until we reach the end of the statement.  */
6174           cp_parser_skip_to_end_of_statement (parser);
6175           goto done;
6176         }
6177       /* After the first time around, a function-definition is not
6178          allowed -- even if it was OK at first.  For example:
6179
6180            int i, f() {}
6181
6182          is not valid.  */
6183       function_definition_allowed_p = false;
6184     }
6185
6186   /* Issue an error message if no declarators are present, and the
6187      decl-specifier-seq does not itself declare a class or
6188      enumeration.  */
6189   if (!saw_declarator)
6190     {
6191       if (cp_parser_declares_only_class_p (parser))
6192         shadow_tag (decl_specifiers);
6193       /* Perform any deferred access checks.  */
6194       perform_deferred_access_checks ();
6195     }
6196
6197   /* Consume the `;'.  */
6198   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6199
6200  done:
6201   pop_deferring_access_checks ();
6202 }
6203
6204 /* Parse a decl-specifier-seq.
6205
6206    decl-specifier-seq:
6207      decl-specifier-seq [opt] decl-specifier
6208
6209    decl-specifier:
6210      storage-class-specifier
6211      type-specifier
6212      function-specifier
6213      friend
6214      typedef  
6215
6216    GNU Extension:
6217
6218    decl-specifier-seq:
6219      decl-specifier-seq [opt] attributes
6220
6221    Returns a TREE_LIST, giving the decl-specifiers in the order they
6222    appear in the source code.  The TREE_VALUE of each node is the
6223    decl-specifier.  For a keyword (such as `auto' or `friend'), the
6224    TREE_VALUE is simply the corresponding TREE_IDENTIFIER.  For the
6225    representation of a type-specifier, see cp_parser_type_specifier.  
6226
6227    If there are attributes, they will be stored in *ATTRIBUTES,
6228    represented as described above cp_parser_attributes.  
6229
6230    If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6231    appears, and the entity that will be a friend is not going to be a
6232    class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
6233    even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6234    friendship is granted might not be a class.  
6235
6236    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6237    *flags:
6238
6239      1: one of the decl-specifiers is an elaborated-type-specifier
6240      2: one of the decl-specifiers is an enum-specifier or a
6241         class-specifier
6242
6243    */
6244
6245 static tree
6246 cp_parser_decl_specifier_seq (cp_parser* parser, 
6247                               cp_parser_flags flags, 
6248                               tree* attributes,
6249                               int* declares_class_or_enum)
6250 {
6251   tree decl_specs = NULL_TREE;
6252   bool friend_p = false;
6253   bool constructor_possible_p = !parser->in_declarator_p;
6254   
6255   /* Assume no class or enumeration type is declared.  */
6256   *declares_class_or_enum = 0;
6257
6258   /* Assume there are no attributes.  */
6259   *attributes = NULL_TREE;
6260
6261   /* Keep reading specifiers until there are no more to read.  */
6262   while (true)
6263     {
6264       tree decl_spec = NULL_TREE;
6265       bool constructor_p;
6266       cp_token *token;
6267
6268       /* Peek at the next token.  */
6269       token = cp_lexer_peek_token (parser->lexer);
6270       /* Handle attributes.  */
6271       if (token->keyword == RID_ATTRIBUTE)
6272         {
6273           /* Parse the attributes.  */
6274           decl_spec = cp_parser_attributes_opt (parser);
6275           /* Add them to the list.  */
6276           *attributes = chainon (*attributes, decl_spec);
6277           continue;
6278         }
6279       /* If the next token is an appropriate keyword, we can simply
6280          add it to the list.  */
6281       switch (token->keyword)
6282         {
6283         case RID_FRIEND:
6284           /* decl-specifier:
6285                friend  */
6286           friend_p = true;
6287           /* The representation of the specifier is simply the
6288              appropriate TREE_IDENTIFIER node.  */
6289           decl_spec = token->value;
6290           /* Consume the token.  */
6291           cp_lexer_consume_token (parser->lexer);
6292           break;
6293
6294           /* function-specifier:
6295                inline
6296                virtual
6297                explicit  */
6298         case RID_INLINE:
6299         case RID_VIRTUAL:
6300         case RID_EXPLICIT:
6301           decl_spec = cp_parser_function_specifier_opt (parser);
6302           break;
6303           
6304           /* decl-specifier:
6305                typedef  */
6306         case RID_TYPEDEF:
6307           /* The representation of the specifier is simply the
6308              appropriate TREE_IDENTIFIER node.  */
6309           decl_spec = token->value;
6310           /* Consume the token.  */
6311           cp_lexer_consume_token (parser->lexer);
6312           /* A constructor declarator cannot appear in a typedef.  */
6313           constructor_possible_p = false;
6314           /* The "typedef" keyword can only occur in a declaration; we
6315              may as well commit at this point.  */
6316           cp_parser_commit_to_tentative_parse (parser);
6317           break;
6318
6319           /* storage-class-specifier:
6320                auto
6321                register
6322                static
6323                extern
6324                mutable  
6325
6326              GNU Extension:
6327                thread  */
6328         case RID_AUTO:
6329         case RID_REGISTER:
6330         case RID_STATIC:
6331         case RID_EXTERN:
6332         case RID_MUTABLE:
6333         case RID_THREAD:
6334           decl_spec = cp_parser_storage_class_specifier_opt (parser);
6335           break;
6336           
6337         default:
6338           break;
6339         }
6340
6341       /* Constructors are a special case.  The `S' in `S()' is not a
6342          decl-specifier; it is the beginning of the declarator.  */
6343       constructor_p = (!decl_spec 
6344                        && constructor_possible_p
6345                        && cp_parser_constructor_declarator_p (parser,
6346                                                               friend_p));
6347
6348       /* If we don't have a DECL_SPEC yet, then we must be looking at
6349          a type-specifier.  */
6350       if (!decl_spec && !constructor_p)
6351         {
6352           int decl_spec_declares_class_or_enum;
6353           bool is_cv_qualifier;
6354
6355           decl_spec
6356             = cp_parser_type_specifier (parser, flags,
6357                                         friend_p,
6358                                         /*is_declaration=*/true,
6359                                         &decl_spec_declares_class_or_enum,
6360                                         &is_cv_qualifier);
6361
6362           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6363
6364           /* If this type-specifier referenced a user-defined type
6365              (a typedef, class-name, etc.), then we can't allow any
6366              more such type-specifiers henceforth.
6367
6368              [dcl.spec]
6369
6370              The longest sequence of decl-specifiers that could
6371              possibly be a type name is taken as the
6372              decl-specifier-seq of a declaration.  The sequence shall
6373              be self-consistent as described below.
6374
6375              [dcl.type]
6376
6377              As a general rule, at most one type-specifier is allowed
6378              in the complete decl-specifier-seq of a declaration.  The
6379              only exceptions are the following:
6380
6381              -- const or volatile can be combined with any other
6382                 type-specifier. 
6383
6384              -- signed or unsigned can be combined with char, long,
6385                 short, or int.
6386
6387              -- ..
6388
6389              Example:
6390
6391                typedef char* Pc;
6392                void g (const int Pc);
6393
6394              Here, Pc is *not* part of the decl-specifier seq; it's
6395              the declarator.  Therefore, once we see a type-specifier
6396              (other than a cv-qualifier), we forbid any additional
6397              user-defined types.  We *do* still allow things like `int
6398              int' to be considered a decl-specifier-seq, and issue the
6399              error message later.  */
6400           if (decl_spec && !is_cv_qualifier)
6401             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6402           /* A constructor declarator cannot follow a type-specifier.  */
6403           if (decl_spec)
6404             constructor_possible_p = false;
6405         }
6406
6407       /* If we still do not have a DECL_SPEC, then there are no more
6408          decl-specifiers.  */
6409       if (!decl_spec)
6410         {
6411           /* Issue an error message, unless the entire construct was
6412              optional.  */
6413           if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6414             {
6415               cp_parser_error (parser, "expected decl specifier");
6416               return error_mark_node;
6417             }
6418
6419           break;
6420         }
6421
6422       /* Add the DECL_SPEC to the list of specifiers.  */
6423       decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6424
6425       /* After we see one decl-specifier, further decl-specifiers are
6426          always optional.  */
6427       flags |= CP_PARSER_FLAGS_OPTIONAL;
6428     }
6429
6430   /* We have built up the DECL_SPECS in reverse order.  Return them in
6431      the correct order.  */
6432   return nreverse (decl_specs);
6433 }
6434
6435 /* Parse an (optional) storage-class-specifier. 
6436
6437    storage-class-specifier:
6438      auto
6439      register
6440      static
6441      extern
6442      mutable  
6443
6444    GNU Extension:
6445
6446    storage-class-specifier:
6447      thread
6448
6449    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6450    
6451 static tree
6452 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6453 {
6454   switch (cp_lexer_peek_token (parser->lexer)->keyword)
6455     {
6456     case RID_AUTO:
6457     case RID_REGISTER:
6458     case RID_STATIC:
6459     case RID_EXTERN:
6460     case RID_MUTABLE:
6461     case RID_THREAD:
6462       /* Consume the token.  */
6463       return cp_lexer_consume_token (parser->lexer)->value;
6464
6465     default:
6466       return NULL_TREE;
6467     }
6468 }
6469
6470 /* Parse an (optional) function-specifier. 
6471
6472    function-specifier:
6473      inline
6474      virtual
6475      explicit
6476
6477    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6478    
6479 static tree
6480 cp_parser_function_specifier_opt (cp_parser* parser)
6481 {
6482   switch (cp_lexer_peek_token (parser->lexer)->keyword)
6483     {
6484     case RID_INLINE:
6485     case RID_VIRTUAL:
6486     case RID_EXPLICIT:
6487       /* Consume the token.  */
6488       return cp_lexer_consume_token (parser->lexer)->value;
6489
6490     default:
6491       return NULL_TREE;
6492     }
6493 }
6494
6495 /* Parse a linkage-specification.
6496
6497    linkage-specification:
6498      extern string-literal { declaration-seq [opt] }
6499      extern string-literal declaration  */
6500
6501 static void
6502 cp_parser_linkage_specification (cp_parser* parser)
6503 {
6504   cp_token *token;
6505   tree linkage;
6506
6507   /* Look for the `extern' keyword.  */
6508   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6509
6510   /* Peek at the next token.  */
6511   token = cp_lexer_peek_token (parser->lexer);
6512   /* If it's not a string-literal, then there's a problem.  */
6513   if (!cp_parser_is_string_literal (token))
6514     {
6515       cp_parser_error (parser, "expected language-name");
6516       return;
6517     }
6518   /* Consume the token.  */
6519   cp_lexer_consume_token (parser->lexer);
6520
6521   /* Transform the literal into an identifier.  If the literal is a
6522      wide-character string, or contains embedded NULs, then we can't
6523      handle it as the user wants.  */
6524   if (token->type == CPP_WSTRING
6525       || (strlen (TREE_STRING_POINTER (token->value))
6526           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6527     {
6528       cp_parser_error (parser, "invalid linkage-specification");
6529       /* Assume C++ linkage.  */
6530       linkage = get_identifier ("c++");
6531     }
6532   /* If it's a simple string constant, things are easier.  */
6533   else
6534     linkage = get_identifier (TREE_STRING_POINTER (token->value));
6535
6536   /* We're now using the new linkage.  */
6537   push_lang_context (linkage);
6538
6539   /* If the next token is a `{', then we're using the first
6540      production.  */
6541   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6542     {
6543       /* Consume the `{' token.  */
6544       cp_lexer_consume_token (parser->lexer);
6545       /* Parse the declarations.  */
6546       cp_parser_declaration_seq_opt (parser);
6547       /* Look for the closing `}'.  */
6548       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6549     }
6550   /* Otherwise, there's just one declaration.  */
6551   else
6552     {
6553       bool saved_in_unbraced_linkage_specification_p;
6554
6555       saved_in_unbraced_linkage_specification_p 
6556         = parser->in_unbraced_linkage_specification_p;
6557       parser->in_unbraced_linkage_specification_p = true;
6558       have_extern_spec = true;
6559       cp_parser_declaration (parser);
6560       have_extern_spec = false;
6561       parser->in_unbraced_linkage_specification_p 
6562         = saved_in_unbraced_linkage_specification_p;
6563     }
6564
6565   /* We're done with the linkage-specification.  */
6566   pop_lang_context ();
6567 }
6568
6569 /* Special member functions [gram.special] */
6570
6571 /* Parse a conversion-function-id.
6572
6573    conversion-function-id:
6574      operator conversion-type-id  
6575
6576    Returns an IDENTIFIER_NODE representing the operator.  */
6577
6578 static tree 
6579 cp_parser_conversion_function_id (cp_parser* parser)
6580 {
6581   tree type;
6582   tree saved_scope;
6583   tree saved_qualifying_scope;
6584   tree saved_object_scope;
6585
6586   /* Look for the `operator' token.  */
6587   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6588     return error_mark_node;
6589   /* When we parse the conversion-type-id, the current scope will be
6590      reset.  However, we need that information in able to look up the
6591      conversion function later, so we save it here.  */
6592   saved_scope = parser->scope;
6593   saved_qualifying_scope = parser->qualifying_scope;
6594   saved_object_scope = parser->object_scope;
6595   /* We must enter the scope of the class so that the names of
6596      entities declared within the class are available in the
6597      conversion-type-id.  For example, consider:
6598
6599        struct S { 
6600          typedef int I;
6601          operator I();
6602        };
6603
6604        S::operator I() { ... }
6605
6606      In order to see that `I' is a type-name in the definition, we
6607      must be in the scope of `S'.  */
6608   if (saved_scope)
6609     push_scope (saved_scope);
6610   /* Parse the conversion-type-id.  */
6611   type = cp_parser_conversion_type_id (parser);
6612   /* Leave the scope of the class, if any.  */
6613   if (saved_scope)
6614     pop_scope (saved_scope);
6615   /* Restore the saved scope.  */
6616   parser->scope = saved_scope;
6617   parser->qualifying_scope = saved_qualifying_scope;
6618   parser->object_scope = saved_object_scope;
6619   /* If the TYPE is invalid, indicate failure.  */
6620   if (type == error_mark_node)
6621     return error_mark_node;
6622   return mangle_conv_op_name_for_type (type);
6623 }
6624
6625 /* Parse a conversion-type-id:
6626
6627    conversion-type-id:
6628      type-specifier-seq conversion-declarator [opt]
6629
6630    Returns the TYPE specified.  */
6631
6632 static tree
6633 cp_parser_conversion_type_id (cp_parser* parser)
6634 {
6635   tree attributes;
6636   tree type_specifiers;
6637   tree declarator;
6638
6639   /* Parse the attributes.  */
6640   attributes = cp_parser_attributes_opt (parser);
6641   /* Parse the type-specifiers.  */
6642   type_specifiers = cp_parser_type_specifier_seq (parser);
6643   /* If that didn't work, stop.  */
6644   if (type_specifiers == error_mark_node)
6645     return error_mark_node;
6646   /* Parse the conversion-declarator.  */
6647   declarator = cp_parser_conversion_declarator_opt (parser);
6648
6649   return grokdeclarator (declarator, type_specifiers, TYPENAME,
6650                          /*initialized=*/0, &attributes);
6651 }
6652
6653 /* Parse an (optional) conversion-declarator.
6654
6655    conversion-declarator:
6656      ptr-operator conversion-declarator [opt]  
6657
6658    Returns a representation of the declarator.  See
6659    cp_parser_declarator for details.  */
6660
6661 static tree
6662 cp_parser_conversion_declarator_opt (cp_parser* parser)
6663 {
6664   enum tree_code code;
6665   tree class_type;
6666   tree cv_qualifier_seq;
6667
6668   /* We don't know if there's a ptr-operator next, or not.  */
6669   cp_parser_parse_tentatively (parser);
6670   /* Try the ptr-operator.  */
6671   code = cp_parser_ptr_operator (parser, &class_type, 
6672                                  &cv_qualifier_seq);
6673   /* If it worked, look for more conversion-declarators.  */
6674   if (cp_parser_parse_definitely (parser))
6675     {
6676      tree declarator;
6677
6678      /* Parse another optional declarator.  */
6679      declarator = cp_parser_conversion_declarator_opt (parser);
6680
6681      /* Create the representation of the declarator.  */
6682      if (code == INDIRECT_REF)
6683        declarator = make_pointer_declarator (cv_qualifier_seq,
6684                                              declarator);
6685      else
6686        declarator =  make_reference_declarator (cv_qualifier_seq,
6687                                                 declarator);
6688
6689      /* Handle the pointer-to-member case.  */
6690      if (class_type)
6691        declarator = build_nt (SCOPE_REF, class_type, declarator);
6692
6693      return declarator;
6694    }
6695
6696   return NULL_TREE;
6697 }
6698
6699 /* Parse an (optional) ctor-initializer.
6700
6701    ctor-initializer:
6702      : mem-initializer-list  
6703
6704    Returns TRUE iff the ctor-initializer was actually present.  */
6705
6706 static bool
6707 cp_parser_ctor_initializer_opt (cp_parser* parser)
6708 {
6709   /* If the next token is not a `:', then there is no
6710      ctor-initializer.  */
6711   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
6712     {
6713       /* Do default initialization of any bases and members.  */
6714       if (DECL_CONSTRUCTOR_P (current_function_decl))
6715         finish_mem_initializers (NULL_TREE);
6716
6717       return false;
6718     }
6719
6720   /* Consume the `:' token.  */
6721   cp_lexer_consume_token (parser->lexer);
6722   /* And the mem-initializer-list.  */
6723   cp_parser_mem_initializer_list (parser);
6724
6725   return true;
6726 }
6727
6728 /* Parse a mem-initializer-list.
6729
6730    mem-initializer-list:
6731      mem-initializer
6732      mem-initializer , mem-initializer-list  */
6733
6734 static void
6735 cp_parser_mem_initializer_list (cp_parser* parser)
6736 {
6737   tree mem_initializer_list = NULL_TREE;
6738
6739   /* Let the semantic analysis code know that we are starting the
6740      mem-initializer-list.  */
6741   if (!DECL_CONSTRUCTOR_P (current_function_decl))
6742     error ("only constructors take base initializers");
6743
6744   /* Loop through the list.  */
6745   while (true)
6746     {
6747       tree mem_initializer;
6748
6749       /* Parse the mem-initializer.  */
6750       mem_initializer = cp_parser_mem_initializer (parser);
6751       /* Add it to the list, unless it was erroneous.  */
6752       if (mem_initializer)
6753         {
6754           TREE_CHAIN (mem_initializer) = mem_initializer_list;
6755           mem_initializer_list = mem_initializer;
6756         }
6757       /* If the next token is not a `,', we're done.  */
6758       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6759         break;
6760       /* Consume the `,' token.  */
6761       cp_lexer_consume_token (parser->lexer);
6762     }
6763
6764   /* Perform semantic analysis.  */
6765   if (DECL_CONSTRUCTOR_P (current_function_decl))
6766     finish_mem_initializers (mem_initializer_list);
6767 }
6768
6769 /* Parse a mem-initializer.
6770
6771    mem-initializer:
6772      mem-initializer-id ( expression-list [opt] )  
6773
6774    GNU extension:
6775   
6776    mem-initializer:
6777      ( expression-list [opt] )
6778
6779    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
6780    class) or FIELD_DECL (for a non-static data member) to initialize;
6781    the TREE_VALUE is the expression-list.  */
6782
6783 static tree
6784 cp_parser_mem_initializer (cp_parser* parser)
6785 {
6786   tree mem_initializer_id;
6787   tree expression_list;
6788   tree member;
6789   
6790   /* Find out what is being initialized.  */
6791   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6792     {
6793       pedwarn ("anachronistic old-style base class initializer");
6794       mem_initializer_id = NULL_TREE;
6795     }
6796   else
6797     mem_initializer_id = cp_parser_mem_initializer_id (parser);
6798   member = expand_member_init (mem_initializer_id);
6799   if (member && !DECL_P (member))
6800     in_base_initializer = 1;
6801
6802   expression_list 
6803     = cp_parser_parenthesized_expression_list (parser, false,
6804                                                /*non_constant_p=*/NULL);
6805   if (!expression_list)
6806     expression_list = void_type_node;
6807
6808   in_base_initializer = 0;
6809   
6810   return member ? build_tree_list (member, expression_list) : NULL_TREE;
6811 }
6812
6813 /* Parse a mem-initializer-id.
6814
6815    mem-initializer-id:
6816      :: [opt] nested-name-specifier [opt] class-name
6817      identifier  
6818
6819    Returns a TYPE indicating the class to be initializer for the first
6820    production.  Returns an IDENTIFIER_NODE indicating the data member
6821    to be initialized for the second production.  */
6822
6823 static tree
6824 cp_parser_mem_initializer_id (cp_parser* parser)
6825 {
6826   bool global_scope_p;
6827   bool nested_name_specifier_p;
6828   tree id;
6829
6830   /* Look for the optional `::' operator.  */
6831   global_scope_p 
6832     = (cp_parser_global_scope_opt (parser, 
6833                                    /*current_scope_valid_p=*/false) 
6834        != NULL_TREE);
6835   /* Look for the optional nested-name-specifier.  The simplest way to
6836      implement:
6837
6838        [temp.res]
6839
6840        The keyword `typename' is not permitted in a base-specifier or
6841        mem-initializer; in these contexts a qualified name that
6842        depends on a template-parameter is implicitly assumed to be a
6843        type name.
6844
6845      is to assume that we have seen the `typename' keyword at this
6846      point.  */
6847   nested_name_specifier_p 
6848     = (cp_parser_nested_name_specifier_opt (parser,
6849                                             /*typename_keyword_p=*/true,
6850                                             /*check_dependency_p=*/true,
6851                                             /*type_p=*/true)
6852        != NULL_TREE);
6853   /* If there is a `::' operator or a nested-name-specifier, then we
6854      are definitely looking for a class-name.  */
6855   if (global_scope_p || nested_name_specifier_p)
6856     return cp_parser_class_name (parser,
6857                                  /*typename_keyword_p=*/true,
6858                                  /*template_keyword_p=*/false,
6859                                  /*type_p=*/false,
6860                                  /*check_dependency_p=*/true,
6861                                  /*class_head_p=*/false);
6862   /* Otherwise, we could also be looking for an ordinary identifier.  */
6863   cp_parser_parse_tentatively (parser);
6864   /* Try a class-name.  */
6865   id = cp_parser_class_name (parser, 
6866                              /*typename_keyword_p=*/true,
6867                              /*template_keyword_p=*/false,
6868                              /*type_p=*/false,
6869                              /*check_dependency_p=*/true,
6870                              /*class_head_p=*/false);
6871   /* If we found one, we're done.  */
6872   if (cp_parser_parse_definitely (parser))
6873     return id;
6874   /* Otherwise, look for an ordinary identifier.  */
6875   return cp_parser_identifier (parser);
6876 }
6877
6878 /* Overloading [gram.over] */
6879
6880 /* Parse an operator-function-id.
6881
6882    operator-function-id:
6883      operator operator  
6884
6885    Returns an IDENTIFIER_NODE for the operator which is a
6886    human-readable spelling of the identifier, e.g., `operator +'.  */
6887
6888 static tree 
6889 cp_parser_operator_function_id (cp_parser* parser)
6890 {
6891   /* Look for the `operator' keyword.  */
6892   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6893     return error_mark_node;
6894   /* And then the name of the operator itself.  */
6895   return cp_parser_operator (parser);
6896 }
6897
6898 /* Parse an operator.
6899
6900    operator:
6901      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
6902      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
6903      || ++ -- , ->* -> () []
6904
6905    GNU Extensions:
6906    
6907    operator:
6908      <? >? <?= >?=
6909
6910    Returns an IDENTIFIER_NODE for the operator which is a
6911    human-readable spelling of the identifier, e.g., `operator +'.  */
6912    
6913 static tree
6914 cp_parser_operator (cp_parser* parser)
6915 {
6916   tree id = NULL_TREE;
6917   cp_token *token;
6918
6919   /* Peek at the next token.  */
6920   token = cp_lexer_peek_token (parser->lexer);
6921   /* Figure out which operator we have.  */
6922   switch (token->type)
6923     {
6924     case CPP_KEYWORD:
6925       {
6926         enum tree_code op;
6927
6928         /* The keyword should be either `new' or `delete'.  */
6929         if (token->keyword == RID_NEW)
6930           op = NEW_EXPR;
6931         else if (token->keyword == RID_DELETE)
6932           op = DELETE_EXPR;
6933         else
6934           break;
6935
6936         /* Consume the `new' or `delete' token.  */
6937         cp_lexer_consume_token (parser->lexer);
6938
6939         /* Peek at the next token.  */
6940         token = cp_lexer_peek_token (parser->lexer);
6941         /* If it's a `[' token then this is the array variant of the
6942            operator.  */
6943         if (token->type == CPP_OPEN_SQUARE)
6944           {
6945             /* Consume the `[' token.  */
6946             cp_lexer_consume_token (parser->lexer);
6947             /* Look for the `]' token.  */
6948             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
6949             id = ansi_opname (op == NEW_EXPR 
6950                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
6951           }
6952         /* Otherwise, we have the non-array variant.  */
6953         else
6954           id = ansi_opname (op);
6955
6956         return id;
6957       }
6958
6959     case CPP_PLUS:
6960       id = ansi_opname (PLUS_EXPR);
6961       break;
6962
6963     case CPP_MINUS:
6964       id = ansi_opname (MINUS_EXPR);
6965       break;
6966
6967     case CPP_MULT:
6968       id = ansi_opname (MULT_EXPR);
6969       break;
6970
6971     case CPP_DIV:
6972       id = ansi_opname (TRUNC_DIV_EXPR);
6973       break;
6974
6975     case CPP_MOD:
6976       id = ansi_opname (TRUNC_MOD_EXPR);
6977       break;
6978
6979     case CPP_XOR:
6980       id = ansi_opname (BIT_XOR_EXPR);
6981       break;
6982
6983     case CPP_AND:
6984       id = ansi_opname (BIT_AND_EXPR);
6985       break;
6986
6987     case CPP_OR:
6988       id = ansi_opname (BIT_IOR_EXPR);
6989       break;
6990
6991     case CPP_COMPL:
6992       id = ansi_opname (BIT_NOT_EXPR);
6993       break;
6994       
6995     case CPP_NOT:
6996       id = ansi_opname (TRUTH_NOT_EXPR);
6997       break;
6998
6999     case CPP_EQ:
7000       id = ansi_assopname (NOP_EXPR);
7001       break;
7002
7003     case CPP_LESS:
7004       id = ansi_opname (LT_EXPR);
7005       break;
7006
7007     case CPP_GREATER:
7008       id = ansi_opname (GT_EXPR);
7009       break;
7010
7011     case CPP_PLUS_EQ:
7012       id = ansi_assopname (PLUS_EXPR);
7013       break;
7014
7015     case CPP_MINUS_EQ:
7016       id = ansi_assopname (MINUS_EXPR);
7017       break;
7018
7019     case CPP_MULT_EQ:
7020       id = ansi_assopname (MULT_EXPR);
7021       break;
7022
7023     case CPP_DIV_EQ:
7024       id = ansi_assopname (TRUNC_DIV_EXPR);
7025       break;
7026
7027     case CPP_MOD_EQ:
7028       id = ansi_assopname (TRUNC_MOD_EXPR);
7029       break;
7030
7031     case CPP_XOR_EQ:
7032       id = ansi_assopname (BIT_XOR_EXPR);
7033       break;
7034
7035     case CPP_AND_EQ:
7036       id = ansi_assopname (BIT_AND_EXPR);
7037       break;
7038
7039     case CPP_OR_EQ:
7040       id = ansi_assopname (BIT_IOR_EXPR);
7041       break;
7042
7043     case CPP_LSHIFT:
7044       id = ansi_opname (LSHIFT_EXPR);
7045       break;
7046
7047     case CPP_RSHIFT:
7048       id = ansi_opname (RSHIFT_EXPR);
7049       break;
7050
7051     case CPP_LSHIFT_EQ:
7052       id = ansi_assopname (LSHIFT_EXPR);
7053       break;
7054
7055     case CPP_RSHIFT_EQ:
7056       id = ansi_assopname (RSHIFT_EXPR);
7057       break;
7058
7059     case CPP_EQ_EQ:
7060       id = ansi_opname (EQ_EXPR);
7061       break;
7062
7063     case CPP_NOT_EQ:
7064       id = ansi_opname (NE_EXPR);
7065       break;
7066
7067     case CPP_LESS_EQ:
7068       id = ansi_opname (LE_EXPR);
7069       break;
7070
7071     case CPP_GREATER_EQ:
7072       id = ansi_opname (GE_EXPR);
7073       break;
7074
7075     case CPP_AND_AND:
7076       id = ansi_opname (TRUTH_ANDIF_EXPR);
7077       break;
7078
7079     case CPP_OR_OR:
7080       id = ansi_opname (TRUTH_ORIF_EXPR);
7081       break;
7082       
7083     case CPP_PLUS_PLUS:
7084       id = ansi_opname (POSTINCREMENT_EXPR);
7085       break;
7086
7087     case CPP_MINUS_MINUS:
7088       id = ansi_opname (PREDECREMENT_EXPR);
7089       break;
7090
7091     case CPP_COMMA:
7092       id = ansi_opname (COMPOUND_EXPR);
7093       break;
7094
7095     case CPP_DEREF_STAR:
7096       id = ansi_opname (MEMBER_REF);
7097       break;
7098
7099     case CPP_DEREF:
7100       id = ansi_opname (COMPONENT_REF);
7101       break;
7102
7103     case CPP_OPEN_PAREN:
7104       /* Consume the `('.  */
7105       cp_lexer_consume_token (parser->lexer);
7106       /* Look for the matching `)'.  */
7107       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7108       return ansi_opname (CALL_EXPR);
7109
7110     case CPP_OPEN_SQUARE:
7111       /* Consume the `['.  */
7112       cp_lexer_consume_token (parser->lexer);
7113       /* Look for the matching `]'.  */
7114       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7115       return ansi_opname (ARRAY_REF);
7116
7117       /* Extensions.  */
7118     case CPP_MIN:
7119       id = ansi_opname (MIN_EXPR);
7120       break;
7121
7122     case CPP_MAX:
7123       id = ansi_opname (MAX_EXPR);
7124       break;
7125
7126     case CPP_MIN_EQ:
7127       id = ansi_assopname (MIN_EXPR);
7128       break;
7129
7130     case CPP_MAX_EQ:
7131       id = ansi_assopname (MAX_EXPR);
7132       break;
7133
7134     default:
7135       /* Anything else is an error.  */
7136       break;
7137     }
7138
7139   /* If we have selected an identifier, we need to consume the
7140      operator token.  */
7141   if (id)
7142     cp_lexer_consume_token (parser->lexer);
7143   /* Otherwise, no valid operator name was present.  */
7144   else
7145     {
7146       cp_parser_error (parser, "expected operator");
7147       id = error_mark_node;
7148     }
7149
7150   return id;
7151 }
7152
7153 /* Parse a template-declaration.
7154
7155    template-declaration:
7156      export [opt] template < template-parameter-list > declaration  
7157
7158    If MEMBER_P is TRUE, this template-declaration occurs within a
7159    class-specifier.  
7160
7161    The grammar rule given by the standard isn't correct.  What
7162    is really meant is:
7163
7164    template-declaration:
7165      export [opt] template-parameter-list-seq 
7166        decl-specifier-seq [opt] init-declarator [opt] ;
7167      export [opt] template-parameter-list-seq 
7168        function-definition
7169
7170    template-parameter-list-seq:
7171      template-parameter-list-seq [opt]
7172      template < template-parameter-list >  */
7173
7174 static void
7175 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7176 {
7177   /* Check for `export'.  */
7178   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7179     {
7180       /* Consume the `export' token.  */
7181       cp_lexer_consume_token (parser->lexer);
7182       /* Warn that we do not support `export'.  */
7183       warning ("keyword `export' not implemented, and will be ignored");
7184     }
7185
7186   cp_parser_template_declaration_after_export (parser, member_p);
7187 }
7188
7189 /* Parse a template-parameter-list.
7190
7191    template-parameter-list:
7192      template-parameter
7193      template-parameter-list , template-parameter
7194
7195    Returns a TREE_LIST.  Each node represents a template parameter.
7196    The nodes are connected via their TREE_CHAINs.  */
7197
7198 static tree
7199 cp_parser_template_parameter_list (cp_parser* parser)
7200 {
7201   tree parameter_list = NULL_TREE;
7202
7203   while (true)
7204     {
7205       tree parameter;
7206       cp_token *token;
7207
7208       /* Parse the template-parameter.  */
7209       parameter = cp_parser_template_parameter (parser);
7210       /* Add it to the list.  */
7211       parameter_list = process_template_parm (parameter_list,
7212                                               parameter);
7213
7214       /* Peek at the next token.  */
7215       token = cp_lexer_peek_token (parser->lexer);
7216       /* If it's not a `,', we're done.  */
7217       if (token->type != CPP_COMMA)
7218         break;
7219       /* Otherwise, consume the `,' token.  */
7220       cp_lexer_consume_token (parser->lexer);
7221     }
7222
7223   return parameter_list;
7224 }
7225
7226 /* Parse a template-parameter.
7227
7228    template-parameter:
7229      type-parameter
7230      parameter-declaration
7231
7232    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
7233    TREE_PURPOSE is the default value, if any.  */
7234
7235 static tree
7236 cp_parser_template_parameter (cp_parser* parser)
7237 {
7238   cp_token *token;
7239
7240   /* Peek at the next token.  */
7241   token = cp_lexer_peek_token (parser->lexer);
7242   /* If it is `class' or `template', we have a type-parameter.  */
7243   if (token->keyword == RID_TEMPLATE)
7244     return cp_parser_type_parameter (parser);
7245   /* If it is `class' or `typename' we do not know yet whether it is a
7246      type parameter or a non-type parameter.  Consider:
7247
7248        template <typename T, typename T::X X> ...
7249
7250      or:
7251      
7252        template <class C, class D*> ...
7253
7254      Here, the first parameter is a type parameter, and the second is
7255      a non-type parameter.  We can tell by looking at the token after
7256      the identifier -- if it is a `,', `=', or `>' then we have a type
7257      parameter.  */
7258   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7259     {
7260       /* Peek at the token after `class' or `typename'.  */
7261       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7262       /* If it's an identifier, skip it.  */
7263       if (token->type == CPP_NAME)
7264         token = cp_lexer_peek_nth_token (parser->lexer, 3);
7265       /* Now, see if the token looks like the end of a template
7266          parameter.  */
7267       if (token->type == CPP_COMMA 
7268           || token->type == CPP_EQ
7269           || token->type == CPP_GREATER)
7270         return cp_parser_type_parameter (parser);
7271     }
7272
7273   /* Otherwise, it is a non-type parameter.  
7274
7275      [temp.param]
7276
7277      When parsing a default template-argument for a non-type
7278      template-parameter, the first non-nested `>' is taken as the end
7279      of the template parameter-list rather than a greater-than
7280      operator.  */
7281   return 
7282     cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7283 }
7284
7285 /* Parse a type-parameter.
7286
7287    type-parameter:
7288      class identifier [opt]
7289      class identifier [opt] = type-id
7290      typename identifier [opt]
7291      typename identifier [opt] = type-id
7292      template < template-parameter-list > class identifier [opt]
7293      template < template-parameter-list > class identifier [opt] 
7294        = id-expression  
7295
7296    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
7297    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
7298    the declaration of the parameter.  */
7299
7300 static tree
7301 cp_parser_type_parameter (cp_parser* parser)
7302 {
7303   cp_token *token;
7304   tree parameter;
7305
7306   /* Look for a keyword to tell us what kind of parameter this is.  */
7307   token = cp_parser_require (parser, CPP_KEYWORD, 
7308                              "`class', `typename', or `template'");
7309   if (!token)
7310     return error_mark_node;
7311
7312   switch (token->keyword)
7313     {
7314     case RID_CLASS:
7315     case RID_TYPENAME:
7316       {
7317         tree identifier;
7318         tree default_argument;
7319
7320         /* If the next token is an identifier, then it names the
7321            parameter.  */
7322         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7323           identifier = cp_parser_identifier (parser);
7324         else
7325           identifier = NULL_TREE;
7326
7327         /* Create the parameter.  */
7328         parameter = finish_template_type_parm (class_type_node, identifier);
7329
7330         /* If the next token is an `=', we have a default argument.  */
7331         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7332           {
7333             /* Consume the `=' token.  */
7334             cp_lexer_consume_token (parser->lexer);
7335             /* Parse the default-argument.  */
7336             default_argument = cp_parser_type_id (parser);
7337           }
7338         else
7339           default_argument = NULL_TREE;
7340
7341         /* Create the combined representation of the parameter and the
7342            default argument.  */
7343         parameter = build_tree_list (default_argument, parameter);
7344       }
7345       break;
7346
7347     case RID_TEMPLATE:
7348       {
7349         tree parameter_list;
7350         tree identifier;
7351         tree default_argument;
7352
7353         /* Look for the `<'.  */
7354         cp_parser_require (parser, CPP_LESS, "`<'");
7355         /* Parse the template-parameter-list.  */
7356         begin_template_parm_list ();
7357         parameter_list 
7358           = cp_parser_template_parameter_list (parser);
7359         parameter_list = end_template_parm_list (parameter_list);
7360         /* Look for the `>'.  */
7361         cp_parser_require (parser, CPP_GREATER, "`>'");
7362         /* Look for the `class' keyword.  */
7363         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7364         /* If the next token is an `=', then there is a
7365            default-argument.  If the next token is a `>', we are at
7366            the end of the parameter-list.  If the next token is a `,',
7367            then we are at the end of this parameter.  */
7368         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7369             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7370             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7371           identifier = cp_parser_identifier (parser);
7372         else
7373           identifier = NULL_TREE;
7374         /* Create the template parameter.  */
7375         parameter = finish_template_template_parm (class_type_node,
7376                                                    identifier);
7377                                                    
7378         /* If the next token is an `=', then there is a
7379            default-argument.  */
7380         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7381           {
7382             /* Consume the `='.  */
7383             cp_lexer_consume_token (parser->lexer);
7384             /* Parse the id-expression.  */
7385             default_argument 
7386               = cp_parser_id_expression (parser,
7387                                          /*template_keyword_p=*/false,
7388                                          /*check_dependency_p=*/true,
7389                                          /*template_p=*/NULL);
7390             /* Look up the name.  */
7391             default_argument 
7392               = cp_parser_lookup_name_simple (parser, default_argument);
7393             /* See if the default argument is valid.  */
7394             default_argument
7395               = check_template_template_default_arg (default_argument);
7396           }
7397         else
7398           default_argument = NULL_TREE;
7399
7400         /* Create the combined representation of the parameter and the
7401            default argument.  */
7402         parameter =  build_tree_list (default_argument, parameter);
7403       }
7404       break;
7405
7406     default:
7407       /* Anything else is an error.  */
7408       cp_parser_error (parser,
7409                        "expected `class', `typename', or `template'");
7410       parameter = error_mark_node;
7411     }
7412   
7413   return parameter;
7414 }
7415
7416 /* Parse a template-id.
7417
7418    template-id:
7419      template-name < template-argument-list [opt] >
7420
7421    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7422    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
7423    returned.  Otherwise, if the template-name names a function, or set
7424    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
7425    names a class, returns a TYPE_DECL for the specialization.  
7426
7427    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7428    uninstantiated templates.  */
7429
7430 static tree
7431 cp_parser_template_id (cp_parser *parser, 
7432                        bool template_keyword_p, 
7433                        bool check_dependency_p)
7434 {
7435   tree template;
7436   tree arguments;
7437   tree saved_scope;
7438   tree saved_qualifying_scope;
7439   tree saved_object_scope;
7440   tree template_id;
7441   bool saved_greater_than_is_operator_p;
7442   ptrdiff_t start_of_id;
7443   tree access_check = NULL_TREE;
7444   cp_token *next_token;
7445
7446   /* If the next token corresponds to a template-id, there is no need
7447      to reparse it.  */
7448   next_token = cp_lexer_peek_token (parser->lexer);
7449   if (next_token->type == CPP_TEMPLATE_ID)
7450     {
7451       tree value;
7452       tree check;
7453
7454       /* Get the stored value.  */
7455       value = cp_lexer_consume_token (parser->lexer)->value;
7456       /* Perform any access checks that were deferred.  */
7457       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7458         perform_or_defer_access_check (TREE_PURPOSE (check),
7459                                        TREE_VALUE (check));
7460       /* Return the stored value.  */
7461       return TREE_VALUE (value);
7462     }
7463
7464   /* Avoid performing name lookup if there is no possibility of
7465      finding a template-id.  */
7466   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7467       || (next_token->type == CPP_NAME
7468           && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7469     {
7470       cp_parser_error (parser, "expected template-id");
7471       return error_mark_node;
7472     }
7473
7474   /* Remember where the template-id starts.  */
7475   if (cp_parser_parsing_tentatively (parser)
7476       && !cp_parser_committed_to_tentative_parse (parser))
7477     {
7478       next_token = cp_lexer_peek_token (parser->lexer);
7479       start_of_id = cp_lexer_token_difference (parser->lexer,
7480                                                parser->lexer->first_token,
7481                                                next_token);
7482     }
7483   else
7484     start_of_id = -1;
7485
7486   push_deferring_access_checks (dk_deferred);
7487
7488   /* Parse the template-name.  */
7489   template = cp_parser_template_name (parser, template_keyword_p,
7490                                       check_dependency_p);
7491   if (template == error_mark_node)
7492     {
7493       pop_deferring_access_checks ();
7494       return error_mark_node;
7495     }
7496
7497   /* Look for the `<' that starts the template-argument-list.  */
7498   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7499     {
7500       pop_deferring_access_checks ();
7501       return error_mark_node;
7502     }
7503
7504   /* [temp.names]
7505
7506      When parsing a template-id, the first non-nested `>' is taken as
7507      the end of the template-argument-list rather than a greater-than
7508      operator.  */
7509   saved_greater_than_is_operator_p 
7510     = parser->greater_than_is_operator_p;
7511   parser->greater_than_is_operator_p = false;
7512   /* Parsing the argument list may modify SCOPE, so we save it
7513      here.  */
7514   saved_scope = parser->scope;
7515   saved_qualifying_scope = parser->qualifying_scope;
7516   saved_object_scope = parser->object_scope;
7517   /* Parse the template-argument-list itself.  */
7518   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
7519     arguments = NULL_TREE;
7520   else
7521     arguments = cp_parser_template_argument_list (parser);
7522   /* Look for the `>' that ends the template-argument-list.  */
7523   cp_parser_require (parser, CPP_GREATER, "`>'");
7524   /* The `>' token might be a greater-than operator again now.  */
7525   parser->greater_than_is_operator_p 
7526     = saved_greater_than_is_operator_p;
7527   /* Restore the SAVED_SCOPE.  */
7528   parser->scope = saved_scope;
7529   parser->qualifying_scope = saved_qualifying_scope;
7530   parser->object_scope = saved_object_scope;
7531
7532   /* Build a representation of the specialization.  */
7533   if (TREE_CODE (template) == IDENTIFIER_NODE)
7534     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7535   else if (DECL_CLASS_TEMPLATE_P (template)
7536            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7537     template_id 
7538       = finish_template_type (template, arguments, 
7539                               cp_lexer_next_token_is (parser->lexer, 
7540                                                       CPP_SCOPE));
7541   else
7542     {
7543       /* If it's not a class-template or a template-template, it should be
7544          a function-template.  */
7545       my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7546                            || TREE_CODE (template) == OVERLOAD
7547                            || BASELINK_P (template)),
7548                           20010716);
7549       
7550       template_id = lookup_template_function (template, arguments);
7551     }
7552   
7553   /* Retrieve any deferred checks.  Do not pop this access checks yet
7554      so the memory will not be reclaimed during token replacing below.  */
7555   access_check = get_deferred_access_checks ();
7556
7557   /* If parsing tentatively, replace the sequence of tokens that makes
7558      up the template-id with a CPP_TEMPLATE_ID token.  That way,
7559      should we re-parse the token stream, we will not have to repeat
7560      the effort required to do the parse, nor will we issue duplicate
7561      error messages about problems during instantiation of the
7562      template.  */
7563   if (start_of_id >= 0)
7564     {
7565       cp_token *token;
7566
7567       /* Find the token that corresponds to the start of the
7568          template-id.  */
7569       token = cp_lexer_advance_token (parser->lexer, 
7570                                       parser->lexer->first_token,
7571                                       start_of_id);
7572
7573       /* Reset the contents of the START_OF_ID token.  */
7574       token->type = CPP_TEMPLATE_ID;
7575       token->value = build_tree_list (access_check, template_id);
7576       token->keyword = RID_MAX;
7577       /* Purge all subsequent tokens.  */
7578       cp_lexer_purge_tokens_after (parser->lexer, token);
7579     }
7580
7581   pop_deferring_access_checks ();
7582   return template_id;
7583 }
7584
7585 /* Parse a template-name.
7586
7587    template-name:
7588      identifier
7589  
7590    The standard should actually say:
7591
7592    template-name:
7593      identifier
7594      operator-function-id
7595      conversion-function-id
7596
7597    A defect report has been filed about this issue.
7598
7599    If TEMPLATE_KEYWORD_P is true, then we have just seen the
7600    `template' keyword, in a construction like:
7601
7602      T::template f<3>()
7603
7604    In that case `f' is taken to be a template-name, even though there
7605    is no way of knowing for sure.
7606
7607    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7608    name refers to a set of overloaded functions, at least one of which
7609    is a template, or an IDENTIFIER_NODE with the name of the template,
7610    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
7611    names are looked up inside uninstantiated templates.  */
7612
7613 static tree
7614 cp_parser_template_name (cp_parser* parser, 
7615                          bool template_keyword_p, 
7616                          bool check_dependency_p)
7617 {
7618   tree identifier;
7619   tree decl;
7620   tree fns;
7621
7622   /* If the next token is `operator', then we have either an
7623      operator-function-id or a conversion-function-id.  */
7624   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7625     {
7626       /* We don't know whether we're looking at an
7627          operator-function-id or a conversion-function-id.  */
7628       cp_parser_parse_tentatively (parser);
7629       /* Try an operator-function-id.  */
7630       identifier = cp_parser_operator_function_id (parser);
7631       /* If that didn't work, try a conversion-function-id.  */
7632       if (!cp_parser_parse_definitely (parser))
7633         identifier = cp_parser_conversion_function_id (parser);
7634     }
7635   /* Look for the identifier.  */
7636   else
7637     identifier = cp_parser_identifier (parser);
7638   
7639   /* If we didn't find an identifier, we don't have a template-id.  */
7640   if (identifier == error_mark_node)
7641     return error_mark_node;
7642
7643   /* If the name immediately followed the `template' keyword, then it
7644      is a template-name.  However, if the next token is not `<', then
7645      we do not treat it as a template-name, since it is not being used
7646      as part of a template-id.  This enables us to handle constructs
7647      like:
7648
7649        template <typename T> struct S { S(); };
7650        template <typename T> S<T>::S();
7651
7652      correctly.  We would treat `S' as a template -- if it were `S<T>'
7653      -- but we do not if there is no `<'.  */
7654   if (template_keyword_p && processing_template_decl
7655       && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
7656     return identifier;
7657
7658   /* Look up the name.  */
7659   decl = cp_parser_lookup_name (parser, identifier,
7660                                 /*is_type=*/false,
7661                                 /*is_namespace=*/false,
7662                                 check_dependency_p);
7663   decl = maybe_get_template_decl_from_type_decl (decl);
7664
7665   /* If DECL is a template, then the name was a template-name.  */
7666   if (TREE_CODE (decl) == TEMPLATE_DECL)
7667     ;
7668   else 
7669     {
7670       /* The standard does not explicitly indicate whether a name that
7671          names a set of overloaded declarations, some of which are
7672          templates, is a template-name.  However, such a name should
7673          be a template-name; otherwise, there is no way to form a
7674          template-id for the overloaded templates.  */
7675       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
7676       if (TREE_CODE (fns) == OVERLOAD)
7677         {
7678           tree fn;
7679           
7680           for (fn = fns; fn; fn = OVL_NEXT (fn))
7681             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
7682               break;
7683         }
7684       else
7685         {
7686           /* Otherwise, the name does not name a template.  */
7687           cp_parser_error (parser, "expected template-name");
7688           return error_mark_node;
7689         }
7690     }
7691
7692   /* If DECL is dependent, and refers to a function, then just return
7693      its name; we will look it up again during template instantiation.  */
7694   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
7695     {
7696       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
7697       if (TYPE_P (scope) && dependent_type_p (scope))
7698         return identifier;
7699     }
7700
7701   return decl;
7702 }
7703
7704 /* Parse a template-argument-list.
7705
7706    template-argument-list:
7707      template-argument
7708      template-argument-list , template-argument
7709
7710    Returns a TREE_VEC containing the arguments.   */
7711
7712 static tree
7713 cp_parser_template_argument_list (cp_parser* parser)
7714 {
7715   tree fixed_args[10];
7716   unsigned n_args = 0;
7717   unsigned alloced = 10;
7718   tree *arg_ary = fixed_args;
7719   tree vec;
7720
7721   do
7722     {
7723       tree argument;
7724
7725       if (n_args)
7726         /* Consume the comma. */
7727         cp_lexer_consume_token (parser->lexer);
7728       
7729       /* Parse the template-argument.  */
7730       argument = cp_parser_template_argument (parser);
7731       if (n_args == alloced)
7732         {
7733           alloced *= 2;
7734           
7735           if (arg_ary == fixed_args)
7736             {
7737               arg_ary = xmalloc (sizeof (tree) * alloced);
7738               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
7739             }
7740           else
7741             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
7742         }
7743       arg_ary[n_args++] = argument;
7744     }
7745   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
7746
7747   vec = make_tree_vec (n_args);
7748
7749   while (n_args--)
7750     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
7751   
7752   if (arg_ary != fixed_args)
7753     free (arg_ary);
7754   return vec;
7755 }
7756
7757 /* Parse a template-argument.
7758
7759    template-argument:
7760      assignment-expression
7761      type-id
7762      id-expression
7763
7764    The representation is that of an assignment-expression, type-id, or
7765    id-expression -- except that the qualified id-expression is
7766    evaluated, so that the value returned is either a DECL or an
7767    OVERLOAD.  
7768
7769    Although the standard says "assignment-expression", it forbids
7770    throw-expressions or assignments in the template argument.
7771    Therefore, we use "conditional-expression" instead.  */
7772
7773 static tree
7774 cp_parser_template_argument (cp_parser* parser)
7775 {
7776   tree argument;
7777   bool template_p;
7778   bool address_p;
7779   cp_token *token;
7780   cp_id_kind idk;
7781   tree qualifying_class;
7782
7783   /* There's really no way to know what we're looking at, so we just
7784      try each alternative in order.  
7785
7786        [temp.arg]
7787
7788        In a template-argument, an ambiguity between a type-id and an
7789        expression is resolved to a type-id, regardless of the form of
7790        the corresponding template-parameter.  
7791
7792      Therefore, we try a type-id first.  */
7793   cp_parser_parse_tentatively (parser);
7794   argument = cp_parser_type_id (parser);
7795   /* If the next token isn't a `,' or a `>', then this argument wasn't
7796      really finished.  */
7797   if (!cp_parser_next_token_ends_template_argument_p (parser))
7798     cp_parser_error (parser, "expected template-argument");
7799   /* If that worked, we're done.  */
7800   if (cp_parser_parse_definitely (parser))
7801     return argument;
7802   /* We're still not sure what the argument will be.  */
7803   cp_parser_parse_tentatively (parser);
7804   /* Try a template.  */
7805   argument = cp_parser_id_expression (parser, 
7806                                       /*template_keyword_p=*/false,
7807                                       /*check_dependency_p=*/true,
7808                                       &template_p);
7809   /* If the next token isn't a `,' or a `>', then this argument wasn't
7810      really finished.  */
7811   if (!cp_parser_next_token_ends_template_argument_p (parser))
7812     cp_parser_error (parser, "expected template-argument");
7813   if (!cp_parser_error_occurred (parser))
7814     {
7815       /* Figure out what is being referred to.  */
7816       argument = cp_parser_lookup_name_simple (parser, argument);
7817       if (template_p)
7818         argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
7819                                                 TREE_OPERAND (argument, 1),
7820                                                 tf_error);
7821       else if (TREE_CODE (argument) != TEMPLATE_DECL)
7822         cp_parser_error (parser, "expected template-name");
7823     }
7824   if (cp_parser_parse_definitely (parser))
7825     return argument;
7826   /* It must be a non-type argument.  There permitted cases are given
7827      in [temp.arg.nontype]:
7828
7829      -- an integral constant-expression of integral or enumeration
7830         type; or
7831
7832      -- the name of a non-type template-parameter; or
7833
7834      -- the name of an object or function with external linkage...
7835
7836      -- the address of an object or function with external linkage...
7837
7838      -- a pointer to member... */
7839   /* Look for a non-type template parameter.  */
7840   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7841     {
7842       cp_parser_parse_tentatively (parser);
7843       argument = cp_parser_primary_expression (parser,
7844                                                &idk,
7845                                                &qualifying_class);
7846       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
7847           || !cp_parser_next_token_ends_template_argument_p (parser))
7848         cp_parser_simulate_error (parser);
7849       if (cp_parser_parse_definitely (parser))
7850         return argument;
7851     }
7852   /* If the next token is "&", the argument must be the address of an
7853      object or function with external linkage.  */
7854   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
7855   if (address_p)
7856     cp_lexer_consume_token (parser->lexer);
7857   /* See if we might have an id-expression.  */
7858   token = cp_lexer_peek_token (parser->lexer);
7859   if (token->type == CPP_NAME
7860       || token->keyword == RID_OPERATOR
7861       || token->type == CPP_SCOPE
7862       || token->type == CPP_TEMPLATE_ID
7863       || token->type == CPP_NESTED_NAME_SPECIFIER)
7864     {
7865       cp_parser_parse_tentatively (parser);
7866       argument = cp_parser_primary_expression (parser,
7867                                                &idk,
7868                                                &qualifying_class);
7869       if (cp_parser_error_occurred (parser)
7870           || !cp_parser_next_token_ends_template_argument_p (parser))
7871         cp_parser_abort_tentative_parse (parser);
7872       else
7873         {
7874           if (qualifying_class)
7875             argument = finish_qualified_id_expr (qualifying_class,
7876                                                  argument,
7877                                                  /*done=*/true,
7878                                                  address_p);
7879           if (TREE_CODE (argument) == VAR_DECL)
7880             {
7881               /* A variable without external linkage might still be a
7882                  valid constant-expression, so no error is issued here
7883                  if the external-linkage check fails.  */
7884               if (!DECL_EXTERNAL_LINKAGE_P (argument))
7885                 cp_parser_simulate_error (parser);
7886             }
7887           else if (is_overloaded_fn (argument))
7888             /* All overloaded functions are allowed; if the external
7889                linkage test does not pass, an error will be issued
7890                later.  */
7891             ;
7892           else if (address_p
7893                    && (TREE_CODE (argument) == OFFSET_REF 
7894                        || TREE_CODE (argument) == SCOPE_REF))
7895             /* A pointer-to-member.  */
7896             ;
7897           else
7898             cp_parser_simulate_error (parser);
7899
7900           if (cp_parser_parse_definitely (parser))
7901             {
7902               if (address_p)
7903                 argument = build_x_unary_op (ADDR_EXPR, argument);
7904               return argument;
7905             }
7906         }
7907     }
7908   /* If the argument started with "&", there are no other valid
7909      alternatives at this point.  */
7910   if (address_p)
7911     {
7912       cp_parser_error (parser, "invalid non-type template argument");
7913       return error_mark_node;
7914     }
7915   /* The argument must be a constant-expression. */
7916   argument = cp_parser_constant_expression (parser, 
7917                                             /*allow_non_constant_p=*/false,
7918                                             /*non_constant_p=*/NULL);
7919   /* If it's non-dependent, simplify it.  */
7920   return cp_parser_fold_non_dependent_expr (argument);
7921 }
7922
7923 /* Parse an explicit-instantiation.
7924
7925    explicit-instantiation:
7926      template declaration  
7927
7928    Although the standard says `declaration', what it really means is:
7929
7930    explicit-instantiation:
7931      template decl-specifier-seq [opt] declarator [opt] ; 
7932
7933    Things like `template int S<int>::i = 5, int S<double>::j;' are not
7934    supposed to be allowed.  A defect report has been filed about this
7935    issue.  
7936
7937    GNU Extension:
7938   
7939    explicit-instantiation:
7940      storage-class-specifier template 
7941        decl-specifier-seq [opt] declarator [opt] ;
7942      function-specifier template 
7943        decl-specifier-seq [opt] declarator [opt] ;  */
7944
7945 static void
7946 cp_parser_explicit_instantiation (cp_parser* parser)
7947 {
7948   int declares_class_or_enum;
7949   tree decl_specifiers;
7950   tree attributes;
7951   tree extension_specifier = NULL_TREE;
7952
7953   /* Look for an (optional) storage-class-specifier or
7954      function-specifier.  */
7955   if (cp_parser_allow_gnu_extensions_p (parser))
7956     {
7957       extension_specifier 
7958         = cp_parser_storage_class_specifier_opt (parser);
7959       if (!extension_specifier)
7960         extension_specifier = cp_parser_function_specifier_opt (parser);
7961     }
7962
7963   /* Look for the `template' keyword.  */
7964   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
7965   /* Let the front end know that we are processing an explicit
7966      instantiation.  */
7967   begin_explicit_instantiation ();
7968   /* [temp.explicit] says that we are supposed to ignore access
7969      control while processing explicit instantiation directives.  */
7970   push_deferring_access_checks (dk_no_check);
7971   /* Parse a decl-specifier-seq.  */
7972   decl_specifiers 
7973     = cp_parser_decl_specifier_seq (parser,
7974                                     CP_PARSER_FLAGS_OPTIONAL,
7975                                     &attributes,
7976                                     &declares_class_or_enum);
7977   /* If there was exactly one decl-specifier, and it declared a class,
7978      and there's no declarator, then we have an explicit type
7979      instantiation.  */
7980   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
7981     {
7982       tree type;
7983
7984       type = check_tag_decl (decl_specifiers);
7985       /* Turn access control back on for names used during
7986          template instantiation.  */
7987       pop_deferring_access_checks ();
7988       if (type)
7989         do_type_instantiation (type, extension_specifier, /*complain=*/1);
7990     }
7991   else
7992     {
7993       tree declarator;
7994       tree decl;
7995
7996       /* Parse the declarator.  */
7997       declarator 
7998         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7999                                 /*ctor_dtor_or_conv_p=*/NULL);
8000       cp_parser_check_for_definition_in_return_type (declarator, 
8001                                                      declares_class_or_enum);
8002       decl = grokdeclarator (declarator, decl_specifiers, 
8003                              NORMAL, 0, NULL);
8004       /* Turn access control back on for names used during
8005          template instantiation.  */
8006       pop_deferring_access_checks ();
8007       /* Do the explicit instantiation.  */
8008       do_decl_instantiation (decl, extension_specifier);
8009     }
8010   /* We're done with the instantiation.  */
8011   end_explicit_instantiation ();
8012
8013   cp_parser_consume_semicolon_at_end_of_statement (parser);
8014 }
8015
8016 /* Parse an explicit-specialization.
8017
8018    explicit-specialization:
8019      template < > declaration  
8020
8021    Although the standard says `declaration', what it really means is:
8022
8023    explicit-specialization:
8024      template <> decl-specifier [opt] init-declarator [opt] ;
8025      template <> function-definition 
8026      template <> explicit-specialization
8027      template <> template-declaration  */
8028
8029 static void
8030 cp_parser_explicit_specialization (cp_parser* parser)
8031 {
8032   /* Look for the `template' keyword.  */
8033   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8034   /* Look for the `<'.  */
8035   cp_parser_require (parser, CPP_LESS, "`<'");
8036   /* Look for the `>'.  */
8037   cp_parser_require (parser, CPP_GREATER, "`>'");
8038   /* We have processed another parameter list.  */
8039   ++parser->num_template_parameter_lists;
8040   /* Let the front end know that we are beginning a specialization.  */
8041   begin_specialization ();
8042
8043   /* If the next keyword is `template', we need to figure out whether
8044      or not we're looking a template-declaration.  */
8045   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8046     {
8047       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8048           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8049         cp_parser_template_declaration_after_export (parser,
8050                                                      /*member_p=*/false);
8051       else
8052         cp_parser_explicit_specialization (parser);
8053     }
8054   else
8055     /* Parse the dependent declaration.  */
8056     cp_parser_single_declaration (parser, 
8057                                   /*member_p=*/false,
8058                                   /*friend_p=*/NULL);
8059
8060   /* We're done with the specialization.  */
8061   end_specialization ();
8062   /* We're done with this parameter list.  */
8063   --parser->num_template_parameter_lists;
8064 }
8065
8066 /* Parse a type-specifier.
8067
8068    type-specifier:
8069      simple-type-specifier
8070      class-specifier
8071      enum-specifier
8072      elaborated-type-specifier
8073      cv-qualifier
8074
8075    GNU Extension:
8076
8077    type-specifier:
8078      __complex__
8079
8080    Returns a representation of the type-specifier.  If the
8081    type-specifier is a keyword (like `int' or `const', or
8082    `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8083    For a class-specifier, enum-specifier, or elaborated-type-specifier
8084    a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8085
8086    If IS_FRIEND is TRUE then this type-specifier is being declared a
8087    `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
8088    appearing in a decl-specifier-seq.
8089
8090    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8091    class-specifier, enum-specifier, or elaborated-type-specifier, then
8092    *DECLARES_CLASS_OR_ENUM is set to a non-zero value.  The value is 1
8093    if a type is declared; 2 if it is defined.  Otherwise, it is set to
8094    zero.
8095
8096    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8097    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
8098    is set to FALSE.  */
8099
8100 static tree
8101 cp_parser_type_specifier (cp_parser* parser, 
8102                           cp_parser_flags flags, 
8103                           bool is_friend,
8104                           bool is_declaration,
8105                           int* declares_class_or_enum,
8106                           bool* is_cv_qualifier)
8107 {
8108   tree type_spec = NULL_TREE;
8109   cp_token *token;
8110   enum rid keyword;
8111
8112   /* Assume this type-specifier does not declare a new type.  */
8113   if (declares_class_or_enum)
8114     *declares_class_or_enum = false;
8115   /* And that it does not specify a cv-qualifier.  */
8116   if (is_cv_qualifier)
8117     *is_cv_qualifier = false;
8118   /* Peek at the next token.  */
8119   token = cp_lexer_peek_token (parser->lexer);
8120
8121   /* If we're looking at a keyword, we can use that to guide the
8122      production we choose.  */
8123   keyword = token->keyword;
8124   switch (keyword)
8125     {
8126       /* Any of these indicate either a class-specifier, or an
8127          elaborated-type-specifier.  */
8128     case RID_CLASS:
8129     case RID_STRUCT:
8130     case RID_UNION:
8131     case RID_ENUM:
8132       /* Parse tentatively so that we can back up if we don't find a
8133          class-specifier or enum-specifier.  */
8134       cp_parser_parse_tentatively (parser);
8135       /* Look for the class-specifier or enum-specifier.  */
8136       if (keyword == RID_ENUM)
8137         type_spec = cp_parser_enum_specifier (parser);
8138       else
8139         type_spec = cp_parser_class_specifier (parser);
8140
8141       /* If that worked, we're done.  */
8142       if (cp_parser_parse_definitely (parser))
8143         {
8144           if (declares_class_or_enum)
8145             *declares_class_or_enum = 2;
8146           return type_spec;
8147         }
8148
8149       /* Fall through.  */
8150
8151     case RID_TYPENAME:
8152       /* Look for an elaborated-type-specifier.  */
8153       type_spec = cp_parser_elaborated_type_specifier (parser,
8154                                                        is_friend,
8155                                                        is_declaration);
8156       /* We're declaring a class or enum -- unless we're using
8157          `typename'.  */
8158       if (declares_class_or_enum && keyword != RID_TYPENAME)
8159         *declares_class_or_enum = 1;
8160       return type_spec;
8161
8162     case RID_CONST:
8163     case RID_VOLATILE:
8164     case RID_RESTRICT:
8165       type_spec = cp_parser_cv_qualifier_opt (parser);
8166       /* Even though we call a routine that looks for an optional
8167          qualifier, we know that there should be one.  */
8168       my_friendly_assert (type_spec != NULL, 20000328);
8169       /* This type-specifier was a cv-qualified.  */
8170       if (is_cv_qualifier)
8171         *is_cv_qualifier = true;
8172
8173       return type_spec;
8174
8175     case RID_COMPLEX:
8176       /* The `__complex__' keyword is a GNU extension.  */
8177       return cp_lexer_consume_token (parser->lexer)->value;
8178
8179     default:
8180       break;
8181     }
8182
8183   /* If we do not already have a type-specifier, assume we are looking
8184      at a simple-type-specifier.  */
8185   type_spec = cp_parser_simple_type_specifier (parser, flags, 
8186                                                /*identifier_p=*/true);
8187
8188   /* If we didn't find a type-specifier, and a type-specifier was not
8189      optional in this context, issue an error message.  */
8190   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8191     {
8192       cp_parser_error (parser, "expected type specifier");
8193       return error_mark_node;
8194     }
8195
8196   return type_spec;
8197 }
8198
8199 /* Parse a simple-type-specifier.
8200
8201    simple-type-specifier:
8202      :: [opt] nested-name-specifier [opt] type-name
8203      :: [opt] nested-name-specifier template template-id
8204      char
8205      wchar_t
8206      bool
8207      short
8208      int
8209      long
8210      signed
8211      unsigned
8212      float
8213      double
8214      void  
8215
8216    GNU Extension:
8217
8218    simple-type-specifier:
8219      __typeof__ unary-expression
8220      __typeof__ ( type-id )
8221
8222    For the various keywords, the value returned is simply the
8223    TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8224    For the first two productions, and if IDENTIFIER_P is false, the
8225    value returned is the indicated TYPE_DECL.  */
8226
8227 static tree
8228 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8229                                  bool identifier_p)
8230 {
8231   tree type = NULL_TREE;
8232   cp_token *token;
8233
8234   /* Peek at the next token.  */
8235   token = cp_lexer_peek_token (parser->lexer);
8236
8237   /* If we're looking at a keyword, things are easy.  */
8238   switch (token->keyword)
8239     {
8240     case RID_CHAR:
8241       type = char_type_node;
8242       break;
8243     case RID_WCHAR:
8244       type = wchar_type_node;
8245       break;
8246     case RID_BOOL:
8247       type = boolean_type_node;
8248       break;
8249     case RID_SHORT:
8250       type = short_integer_type_node;
8251       break;
8252     case RID_INT:
8253       type = integer_type_node;
8254       break;
8255     case RID_LONG:
8256       type = long_integer_type_node;
8257       break;
8258     case RID_SIGNED:
8259       type = integer_type_node;
8260       break;
8261     case RID_UNSIGNED:
8262       type = unsigned_type_node;
8263       break;
8264     case RID_FLOAT:
8265       type = float_type_node;
8266       break;
8267     case RID_DOUBLE:
8268       type = double_type_node;
8269       break;
8270     case RID_VOID:
8271       type = void_type_node;
8272       break;
8273
8274     case RID_TYPEOF:
8275       {
8276         tree operand;
8277
8278         /* Consume the `typeof' token.  */
8279         cp_lexer_consume_token (parser->lexer);
8280         /* Parse the operand to `typeof'  */
8281         operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8282         /* If it is not already a TYPE, take its type.  */
8283         if (!TYPE_P (operand))
8284           operand = finish_typeof (operand);
8285
8286         return operand;
8287       }
8288
8289     default:
8290       break;
8291     }
8292
8293   /* If the type-specifier was for a built-in type, we're done.  */
8294   if (type)
8295     {
8296       tree id;
8297
8298       /* Consume the token.  */
8299       id = cp_lexer_consume_token (parser->lexer)->value;
8300       return identifier_p ? id : TYPE_NAME (type);
8301     }
8302
8303   /* The type-specifier must be a user-defined type.  */
8304   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) 
8305     {
8306       /* Don't gobble tokens or issue error messages if this is an
8307          optional type-specifier.  */
8308       if (flags & CP_PARSER_FLAGS_OPTIONAL)
8309         cp_parser_parse_tentatively (parser);
8310
8311       /* Look for the optional `::' operator.  */
8312       cp_parser_global_scope_opt (parser,
8313                                   /*current_scope_valid_p=*/false);
8314       /* Look for the nested-name specifier.  */
8315       cp_parser_nested_name_specifier_opt (parser,
8316                                            /*typename_keyword_p=*/false,
8317                                            /*check_dependency_p=*/true,
8318                                            /*type_p=*/false);
8319       /* If we have seen a nested-name-specifier, and the next token
8320          is `template', then we are using the template-id production.  */
8321       if (parser->scope 
8322           && cp_parser_optional_template_keyword (parser))
8323         {
8324           /* Look for the template-id.  */
8325           type = cp_parser_template_id (parser, 
8326                                         /*template_keyword_p=*/true,
8327                                         /*check_dependency_p=*/true);
8328           /* If the template-id did not name a type, we are out of
8329              luck.  */
8330           if (TREE_CODE (type) != TYPE_DECL)
8331             {
8332               cp_parser_error (parser, "expected template-id for type");
8333               type = NULL_TREE;
8334             }
8335         }
8336       /* Otherwise, look for a type-name.  */
8337       else
8338         {
8339           type = cp_parser_type_name (parser);
8340           if (type == error_mark_node)
8341             type = NULL_TREE;
8342         }
8343
8344       /* If it didn't work out, we don't have a TYPE.  */
8345       if ((flags & CP_PARSER_FLAGS_OPTIONAL) 
8346           && !cp_parser_parse_definitely (parser))
8347         type = NULL_TREE;
8348     }
8349
8350   /* If we didn't get a type-name, issue an error message.  */
8351   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8352     {
8353       cp_parser_error (parser, "expected type-name");
8354       return error_mark_node;
8355     }
8356
8357   return type;
8358 }
8359
8360 /* Parse a type-name.
8361
8362    type-name:
8363      class-name
8364      enum-name
8365      typedef-name  
8366
8367    enum-name:
8368      identifier
8369
8370    typedef-name:
8371      identifier 
8372
8373    Returns a TYPE_DECL for the the type.  */
8374
8375 static tree
8376 cp_parser_type_name (cp_parser* parser)
8377 {
8378   tree type_decl;
8379   tree identifier;
8380
8381   /* We can't know yet whether it is a class-name or not.  */
8382   cp_parser_parse_tentatively (parser);
8383   /* Try a class-name.  */
8384   type_decl = cp_parser_class_name (parser, 
8385                                     /*typename_keyword_p=*/false,
8386                                     /*template_keyword_p=*/false,
8387                                     /*type_p=*/false,
8388                                     /*check_dependency_p=*/true,
8389                                     /*class_head_p=*/false);
8390   /* If it's not a class-name, keep looking.  */
8391   if (!cp_parser_parse_definitely (parser))
8392     {
8393       /* It must be a typedef-name or an enum-name.  */
8394       identifier = cp_parser_identifier (parser);
8395       if (identifier == error_mark_node)
8396         return error_mark_node;
8397       
8398       /* Look up the type-name.  */
8399       type_decl = cp_parser_lookup_name_simple (parser, identifier);
8400       /* Issue an error if we did not find a type-name.  */
8401       if (TREE_CODE (type_decl) != TYPE_DECL)
8402         {
8403           cp_parser_error (parser, "expected type-name");
8404           type_decl = error_mark_node;
8405         }
8406       /* Remember that the name was used in the definition of the
8407          current class so that we can check later to see if the
8408          meaning would have been different after the class was
8409          entirely defined.  */
8410       else if (type_decl != error_mark_node
8411                && !parser->scope)
8412         maybe_note_name_used_in_class (identifier, type_decl);
8413     }
8414   
8415   return type_decl;
8416 }
8417
8418
8419 /* Parse an elaborated-type-specifier.  Note that the grammar given
8420    here incorporates the resolution to DR68.
8421
8422    elaborated-type-specifier:
8423      class-key :: [opt] nested-name-specifier [opt] identifier
8424      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8425      enum :: [opt] nested-name-specifier [opt] identifier
8426      typename :: [opt] nested-name-specifier identifier
8427      typename :: [opt] nested-name-specifier template [opt] 
8428        template-id 
8429
8430    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8431    declared `friend'.  If IS_DECLARATION is TRUE, then this
8432    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8433    something is being declared.
8434
8435    Returns the TYPE specified.  */
8436
8437 static tree
8438 cp_parser_elaborated_type_specifier (cp_parser* parser, 
8439                                      bool is_friend, 
8440                                      bool is_declaration)
8441 {
8442   enum tag_types tag_type;
8443   tree identifier;
8444   tree type = NULL_TREE;
8445
8446   /* See if we're looking at the `enum' keyword.  */
8447   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8448     {
8449       /* Consume the `enum' token.  */
8450       cp_lexer_consume_token (parser->lexer);
8451       /* Remember that it's an enumeration type.  */
8452       tag_type = enum_type;
8453     }
8454   /* Or, it might be `typename'.  */
8455   else if (cp_lexer_next_token_is_keyword (parser->lexer,
8456                                            RID_TYPENAME))
8457     {
8458       /* Consume the `typename' token.  */
8459       cp_lexer_consume_token (parser->lexer);
8460       /* Remember that it's a `typename' type.  */
8461       tag_type = typename_type;
8462       /* The `typename' keyword is only allowed in templates.  */
8463       if (!processing_template_decl)
8464         pedwarn ("using `typename' outside of template");
8465     }
8466   /* Otherwise it must be a class-key.  */
8467   else
8468     {
8469       tag_type = cp_parser_class_key (parser);
8470       if (tag_type == none_type)
8471         return error_mark_node;
8472     }
8473
8474   /* Look for the `::' operator.  */
8475   cp_parser_global_scope_opt (parser, 
8476                               /*current_scope_valid_p=*/false);
8477   /* Look for the nested-name-specifier.  */
8478   if (tag_type == typename_type)
8479     {
8480       if (cp_parser_nested_name_specifier (parser,
8481                                            /*typename_keyword_p=*/true,
8482                                            /*check_dependency_p=*/true,
8483                                            /*type_p=*/true) 
8484           == error_mark_node)
8485         return error_mark_node;
8486     }
8487   else
8488     /* Even though `typename' is not present, the proposed resolution
8489        to Core Issue 180 says that in `class A<T>::B', `B' should be
8490        considered a type-name, even if `A<T>' is dependent.  */
8491     cp_parser_nested_name_specifier_opt (parser,
8492                                          /*typename_keyword_p=*/true,
8493                                          /*check_dependency_p=*/true,
8494                                          /*type_p=*/true);
8495   /* For everything but enumeration types, consider a template-id.  */
8496   if (tag_type != enum_type)
8497     {
8498       bool template_p = false;
8499       tree decl;
8500
8501       /* Allow the `template' keyword.  */
8502       template_p = cp_parser_optional_template_keyword (parser);
8503       /* If we didn't see `template', we don't know if there's a
8504          template-id or not.  */
8505       if (!template_p)
8506         cp_parser_parse_tentatively (parser);
8507       /* Parse the template-id.  */
8508       decl = cp_parser_template_id (parser, template_p,
8509                                     /*check_dependency_p=*/true);
8510       /* If we didn't find a template-id, look for an ordinary
8511          identifier.  */
8512       if (!template_p && !cp_parser_parse_definitely (parser))
8513         ;
8514       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8515          in effect, then we must assume that, upon instantiation, the
8516          template will correspond to a class.  */
8517       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8518                && tag_type == typename_type)
8519         type = make_typename_type (parser->scope, decl,
8520                                    /*complain=*/1);
8521       else 
8522         type = TREE_TYPE (decl);
8523     }
8524
8525   /* For an enumeration type, consider only a plain identifier.  */
8526   if (!type)
8527     {
8528       identifier = cp_parser_identifier (parser);
8529
8530       if (identifier == error_mark_node)
8531         {
8532           parser->scope = NULL_TREE;
8533           return error_mark_node;
8534         }
8535
8536       /* For a `typename', we needn't call xref_tag.  */
8537       if (tag_type == typename_type)
8538         return make_typename_type (parser->scope, identifier, 
8539                                    /*complain=*/1);
8540       /* Look up a qualified name in the usual way.  */
8541       if (parser->scope)
8542         {
8543           tree decl;
8544
8545           /* In an elaborated-type-specifier, names are assumed to name
8546              types, so we set IS_TYPE to TRUE when calling
8547              cp_parser_lookup_name.  */
8548           decl = cp_parser_lookup_name (parser, identifier, 
8549                                         /*is_type=*/true,
8550                                         /*is_namespace=*/false,
8551                                         /*check_dependency=*/true);
8552
8553           /* If we are parsing friend declaration, DECL may be a
8554              TEMPLATE_DECL tree node here.  However, we need to check
8555              whether this TEMPLATE_DECL results in valid code.  Consider
8556              the following example:
8557
8558                namespace N {
8559                  template <class T> class C {};
8560                }
8561                class X {
8562                  template <class T> friend class N::C; // #1, valid code
8563                };
8564                template <class T> class Y {
8565                  friend class N::C;                    // #2, invalid code
8566                };
8567
8568              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8569              name lookup of `N::C'.  We see that friend declaration must
8570              be template for the code to be valid.  Note that
8571              processing_template_decl does not work here since it is
8572              always 1 for the above two cases.  */
8573
8574           decl = (cp_parser_maybe_treat_template_as_class 
8575                   (decl, /*tag_name_p=*/is_friend
8576                          && parser->num_template_parameter_lists));
8577
8578           if (TREE_CODE (decl) != TYPE_DECL)
8579             {
8580               error ("expected type-name");
8581               return error_mark_node;
8582             }
8583
8584           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
8585             check_elaborated_type_specifier 
8586               (tag_type, decl,
8587                (parser->num_template_parameter_lists
8588                 || DECL_SELF_REFERENCE_P (decl)));
8589
8590           type = TREE_TYPE (decl);
8591         }
8592       else 
8593         {
8594           /* An elaborated-type-specifier sometimes introduces a new type and
8595              sometimes names an existing type.  Normally, the rule is that it
8596              introduces a new type only if there is not an existing type of
8597              the same name already in scope.  For example, given:
8598
8599                struct S {};
8600                void f() { struct S s; }
8601
8602              the `struct S' in the body of `f' is the same `struct S' as in
8603              the global scope; the existing definition is used.  However, if
8604              there were no global declaration, this would introduce a new 
8605              local class named `S'.
8606
8607              An exception to this rule applies to the following code:
8608
8609                namespace N { struct S; }
8610
8611              Here, the elaborated-type-specifier names a new type
8612              unconditionally; even if there is already an `S' in the
8613              containing scope this declaration names a new type.
8614              This exception only applies if the elaborated-type-specifier
8615              forms the complete declaration:
8616
8617                [class.name] 
8618
8619                A declaration consisting solely of `class-key identifier ;' is
8620                either a redeclaration of the name in the current scope or a
8621                forward declaration of the identifier as a class name.  It
8622                introduces the name into the current scope.
8623
8624              We are in this situation precisely when the next token is a `;'.
8625
8626              An exception to the exception is that a `friend' declaration does
8627              *not* name a new type; i.e., given:
8628
8629                struct S { friend struct T; };
8630
8631              `T' is not a new type in the scope of `S'.  
8632
8633              Also, `new struct S' or `sizeof (struct S)' never results in the
8634              definition of a new type; a new type can only be declared in a
8635              declaration context.  */
8636
8637           type = xref_tag (tag_type, identifier, 
8638                            /*attributes=*/NULL_TREE,
8639                            (is_friend 
8640                             || !is_declaration
8641                             || cp_lexer_next_token_is_not (parser->lexer, 
8642                                                            CPP_SEMICOLON)),
8643                            parser->num_template_parameter_lists);
8644         }
8645     }
8646   if (tag_type != enum_type)
8647     cp_parser_check_class_key (tag_type, type);
8648   return type;
8649 }
8650
8651 /* Parse an enum-specifier.
8652
8653    enum-specifier:
8654      enum identifier [opt] { enumerator-list [opt] }
8655
8656    Returns an ENUM_TYPE representing the enumeration.  */
8657
8658 static tree
8659 cp_parser_enum_specifier (cp_parser* parser)
8660 {
8661   cp_token *token;
8662   tree identifier = NULL_TREE;
8663   tree type;
8664
8665   /* Look for the `enum' keyword.  */
8666   if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
8667     return error_mark_node;
8668   /* Peek at the next token.  */
8669   token = cp_lexer_peek_token (parser->lexer);
8670
8671   /* See if it is an identifier.  */
8672   if (token->type == CPP_NAME)
8673     identifier = cp_parser_identifier (parser);
8674
8675   /* Look for the `{'.  */
8676   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8677     return error_mark_node;
8678
8679   /* At this point, we're going ahead with the enum-specifier, even
8680      if some other problem occurs.  */
8681   cp_parser_commit_to_tentative_parse (parser);
8682
8683   /* Issue an error message if type-definitions are forbidden here.  */
8684   cp_parser_check_type_definition (parser);
8685
8686   /* Create the new type.  */
8687   type = start_enum (identifier ? identifier : make_anon_name ());
8688
8689   /* Peek at the next token.  */
8690   token = cp_lexer_peek_token (parser->lexer);
8691   /* If it's not a `}', then there are some enumerators.  */
8692   if (token->type != CPP_CLOSE_BRACE)
8693     cp_parser_enumerator_list (parser, type);
8694   /* Look for the `}'.  */
8695   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8696
8697   /* Finish up the enumeration.  */
8698   finish_enum (type);
8699
8700   return type;
8701 }
8702
8703 /* Parse an enumerator-list.  The enumerators all have the indicated
8704    TYPE.  
8705
8706    enumerator-list:
8707      enumerator-definition
8708      enumerator-list , enumerator-definition  */
8709
8710 static void
8711 cp_parser_enumerator_list (cp_parser* parser, tree type)
8712 {
8713   while (true)
8714     {
8715       cp_token *token;
8716
8717       /* Parse an enumerator-definition.  */
8718       cp_parser_enumerator_definition (parser, type);
8719       /* Peek at the next token.  */
8720       token = cp_lexer_peek_token (parser->lexer);
8721       /* If it's not a `,', then we've reached the end of the 
8722          list.  */
8723       if (token->type != CPP_COMMA)
8724         break;
8725       /* Otherwise, consume the `,' and keep going.  */
8726       cp_lexer_consume_token (parser->lexer);
8727       /* If the next token is a `}', there is a trailing comma.  */
8728       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8729         {
8730           if (pedantic && !in_system_header)
8731             pedwarn ("comma at end of enumerator list");
8732           break;
8733         }
8734     }
8735 }
8736
8737 /* Parse an enumerator-definition.  The enumerator has the indicated
8738    TYPE.
8739
8740    enumerator-definition:
8741      enumerator
8742      enumerator = constant-expression
8743     
8744    enumerator:
8745      identifier  */
8746
8747 static void
8748 cp_parser_enumerator_definition (cp_parser* parser, tree type)
8749 {
8750   cp_token *token;
8751   tree identifier;
8752   tree value;
8753
8754   /* Look for the identifier.  */
8755   identifier = cp_parser_identifier (parser);
8756   if (identifier == error_mark_node)
8757     return;
8758   
8759   /* Peek at the next token.  */
8760   token = cp_lexer_peek_token (parser->lexer);
8761   /* If it's an `=', then there's an explicit value.  */
8762   if (token->type == CPP_EQ)
8763     {
8764       /* Consume the `=' token.  */
8765       cp_lexer_consume_token (parser->lexer);
8766       /* Parse the value.  */
8767       value = cp_parser_constant_expression (parser, 
8768                                              /*allow_non_constant_p=*/false,
8769                                              NULL);
8770     }
8771   else
8772     value = NULL_TREE;
8773
8774   /* Create the enumerator.  */
8775   build_enumerator (identifier, value, type);
8776 }
8777
8778 /* Parse a namespace-name.
8779
8780    namespace-name:
8781      original-namespace-name
8782      namespace-alias
8783
8784    Returns the NAMESPACE_DECL for the namespace.  */
8785
8786 static tree
8787 cp_parser_namespace_name (cp_parser* parser)
8788 {
8789   tree identifier;
8790   tree namespace_decl;
8791
8792   /* Get the name of the namespace.  */
8793   identifier = cp_parser_identifier (parser);
8794   if (identifier == error_mark_node)
8795     return error_mark_node;
8796
8797   /* Look up the identifier in the currently active scope.  Look only
8798      for namespaces, due to:
8799
8800        [basic.lookup.udir]
8801
8802        When looking up a namespace-name in a using-directive or alias
8803        definition, only namespace names are considered.  
8804
8805      And:
8806
8807        [basic.lookup.qual]
8808
8809        During the lookup of a name preceding the :: scope resolution
8810        operator, object, function, and enumerator names are ignored.  
8811
8812      (Note that cp_parser_class_or_namespace_name only calls this
8813      function if the token after the name is the scope resolution
8814      operator.)  */
8815   namespace_decl = cp_parser_lookup_name (parser, identifier,
8816                                           /*is_type=*/false,
8817                                           /*is_namespace=*/true,
8818                                           /*check_dependency=*/true);
8819   /* If it's not a namespace, issue an error.  */
8820   if (namespace_decl == error_mark_node
8821       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
8822     {
8823       cp_parser_error (parser, "expected namespace-name");
8824       namespace_decl = error_mark_node;
8825     }
8826   
8827   return namespace_decl;
8828 }
8829
8830 /* Parse a namespace-definition.
8831
8832    namespace-definition:
8833      named-namespace-definition
8834      unnamed-namespace-definition  
8835
8836    named-namespace-definition:
8837      original-namespace-definition
8838      extension-namespace-definition
8839
8840    original-namespace-definition:
8841      namespace identifier { namespace-body }
8842    
8843    extension-namespace-definition:
8844      namespace original-namespace-name { namespace-body }
8845  
8846    unnamed-namespace-definition:
8847      namespace { namespace-body } */
8848
8849 static void
8850 cp_parser_namespace_definition (cp_parser* parser)
8851 {
8852   tree identifier;
8853
8854   /* Look for the `namespace' keyword.  */
8855   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8856
8857   /* Get the name of the namespace.  We do not attempt to distinguish
8858      between an original-namespace-definition and an
8859      extension-namespace-definition at this point.  The semantic
8860      analysis routines are responsible for that.  */
8861   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8862     identifier = cp_parser_identifier (parser);
8863   else
8864     identifier = NULL_TREE;
8865
8866   /* Look for the `{' to start the namespace.  */
8867   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
8868   /* Start the namespace.  */
8869   push_namespace (identifier);
8870   /* Parse the body of the namespace.  */
8871   cp_parser_namespace_body (parser);
8872   /* Finish the namespace.  */
8873   pop_namespace ();
8874   /* Look for the final `}'.  */
8875   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8876 }
8877
8878 /* Parse a namespace-body.
8879
8880    namespace-body:
8881      declaration-seq [opt]  */
8882
8883 static void
8884 cp_parser_namespace_body (cp_parser* parser)
8885 {
8886   cp_parser_declaration_seq_opt (parser);
8887 }
8888
8889 /* Parse a namespace-alias-definition.
8890
8891    namespace-alias-definition:
8892      namespace identifier = qualified-namespace-specifier ;  */
8893
8894 static void
8895 cp_parser_namespace_alias_definition (cp_parser* parser)
8896 {
8897   tree identifier;
8898   tree namespace_specifier;
8899
8900   /* Look for the `namespace' keyword.  */
8901   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8902   /* Look for the identifier.  */
8903   identifier = cp_parser_identifier (parser);
8904   if (identifier == error_mark_node)
8905     return;
8906   /* Look for the `=' token.  */
8907   cp_parser_require (parser, CPP_EQ, "`='");
8908   /* Look for the qualified-namespace-specifier.  */
8909   namespace_specifier 
8910     = cp_parser_qualified_namespace_specifier (parser);
8911   /* Look for the `;' token.  */
8912   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8913
8914   /* Register the alias in the symbol table.  */
8915   do_namespace_alias (identifier, namespace_specifier);
8916 }
8917
8918 /* Parse a qualified-namespace-specifier.
8919
8920    qualified-namespace-specifier:
8921      :: [opt] nested-name-specifier [opt] namespace-name
8922
8923    Returns a NAMESPACE_DECL corresponding to the specified
8924    namespace.  */
8925
8926 static tree
8927 cp_parser_qualified_namespace_specifier (cp_parser* parser)
8928 {
8929   /* Look for the optional `::'.  */
8930   cp_parser_global_scope_opt (parser, 
8931                               /*current_scope_valid_p=*/false);
8932
8933   /* Look for the optional nested-name-specifier.  */
8934   cp_parser_nested_name_specifier_opt (parser,
8935                                        /*typename_keyword_p=*/false,
8936                                        /*check_dependency_p=*/true,
8937                                        /*type_p=*/false);
8938
8939   return cp_parser_namespace_name (parser);
8940 }
8941
8942 /* Parse a using-declaration.
8943
8944    using-declaration:
8945      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
8946      using :: unqualified-id ;  */
8947
8948 static void
8949 cp_parser_using_declaration (cp_parser* parser)
8950 {
8951   cp_token *token;
8952   bool typename_p = false;
8953   bool global_scope_p;
8954   tree decl;
8955   tree identifier;
8956   tree scope;
8957
8958   /* Look for the `using' keyword.  */
8959   cp_parser_require_keyword (parser, RID_USING, "`using'");
8960   
8961   /* Peek at the next token.  */
8962   token = cp_lexer_peek_token (parser->lexer);
8963   /* See if it's `typename'.  */
8964   if (token->keyword == RID_TYPENAME)
8965     {
8966       /* Remember that we've seen it.  */
8967       typename_p = true;
8968       /* Consume the `typename' token.  */
8969       cp_lexer_consume_token (parser->lexer);
8970     }
8971
8972   /* Look for the optional global scope qualification.  */
8973   global_scope_p 
8974     = (cp_parser_global_scope_opt (parser,
8975                                    /*current_scope_valid_p=*/false) 
8976        != NULL_TREE);
8977
8978   /* If we saw `typename', or didn't see `::', then there must be a
8979      nested-name-specifier present.  */
8980   if (typename_p || !global_scope_p)
8981     cp_parser_nested_name_specifier (parser, typename_p, 
8982                                      /*check_dependency_p=*/true,
8983                                      /*type_p=*/false);
8984   /* Otherwise, we could be in either of the two productions.  In that
8985      case, treat the nested-name-specifier as optional.  */
8986   else
8987     cp_parser_nested_name_specifier_opt (parser,
8988                                          /*typename_keyword_p=*/false,
8989                                          /*check_dependency_p=*/true,
8990                                          /*type_p=*/false);
8991
8992   /* Parse the unqualified-id.  */
8993   identifier = cp_parser_unqualified_id (parser, 
8994                                          /*template_keyword_p=*/false,
8995                                          /*check_dependency_p=*/true);
8996
8997   /* The function we call to handle a using-declaration is different
8998      depending on what scope we are in.  */
8999   scope = current_scope ();
9000   if (scope && TYPE_P (scope))
9001     {
9002       /* Create the USING_DECL.  */
9003       decl = do_class_using_decl (build_nt (SCOPE_REF,
9004                                             parser->scope,
9005                                             identifier));
9006       /* Add it to the list of members in this class.  */
9007       finish_member_declaration (decl);
9008     }
9009   else
9010     {
9011       decl = cp_parser_lookup_name_simple (parser, identifier);
9012       if (decl == error_mark_node)
9013         {
9014           if (parser->scope && parser->scope != global_namespace)
9015             error ("`%D::%D' has not been declared", 
9016                    parser->scope, identifier);
9017           else
9018             error ("`::%D' has not been declared", identifier);
9019         }
9020       else if (scope)
9021         do_local_using_decl (decl);
9022       else
9023         do_toplevel_using_decl (decl);
9024     }
9025
9026   /* Look for the final `;'.  */
9027   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9028 }
9029
9030 /* Parse a using-directive.  
9031  
9032    using-directive:
9033      using namespace :: [opt] nested-name-specifier [opt]
9034        namespace-name ;  */
9035
9036 static void
9037 cp_parser_using_directive (cp_parser* parser)
9038 {
9039   tree namespace_decl;
9040
9041   /* Look for the `using' keyword.  */
9042   cp_parser_require_keyword (parser, RID_USING, "`using'");
9043   /* And the `namespace' keyword.  */
9044   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9045   /* Look for the optional `::' operator.  */
9046   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9047   /* And the optional nested-name-specifier.  */
9048   cp_parser_nested_name_specifier_opt (parser,
9049                                        /*typename_keyword_p=*/false,
9050                                        /*check_dependency_p=*/true,
9051                                        /*type_p=*/false);
9052   /* Get the namespace being used.  */
9053   namespace_decl = cp_parser_namespace_name (parser);
9054   /* Update the symbol table.  */
9055   do_using_directive (namespace_decl);
9056   /* Look for the final `;'.  */
9057   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9058 }
9059
9060 /* Parse an asm-definition.
9061
9062    asm-definition:
9063      asm ( string-literal ) ;  
9064
9065    GNU Extension:
9066
9067    asm-definition:
9068      asm volatile [opt] ( string-literal ) ;
9069      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9070      asm volatile [opt] ( string-literal : asm-operand-list [opt]
9071                           : asm-operand-list [opt] ) ;
9072      asm volatile [opt] ( string-literal : asm-operand-list [opt] 
9073                           : asm-operand-list [opt] 
9074                           : asm-operand-list [opt] ) ;  */
9075
9076 static void
9077 cp_parser_asm_definition (cp_parser* parser)
9078 {
9079   cp_token *token;
9080   tree string;
9081   tree outputs = NULL_TREE;
9082   tree inputs = NULL_TREE;
9083   tree clobbers = NULL_TREE;
9084   tree asm_stmt;
9085   bool volatile_p = false;
9086   bool extended_p = false;
9087
9088   /* Look for the `asm' keyword.  */
9089   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9090   /* See if the next token is `volatile'.  */
9091   if (cp_parser_allow_gnu_extensions_p (parser)
9092       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9093     {
9094       /* Remember that we saw the `volatile' keyword.  */
9095       volatile_p = true;
9096       /* Consume the token.  */
9097       cp_lexer_consume_token (parser->lexer);
9098     }
9099   /* Look for the opening `('.  */
9100   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9101   /* Look for the string.  */
9102   token = cp_parser_require (parser, CPP_STRING, "asm body");
9103   if (!token)
9104     return;
9105   string = token->value;
9106   /* If we're allowing GNU extensions, check for the extended assembly
9107      syntax.  Unfortunately, the `:' tokens need not be separated by 
9108      a space in C, and so, for compatibility, we tolerate that here
9109      too.  Doing that means that we have to treat the `::' operator as
9110      two `:' tokens.  */
9111   if (cp_parser_allow_gnu_extensions_p (parser)
9112       && at_function_scope_p ()
9113       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9114           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9115     {
9116       bool inputs_p = false;
9117       bool clobbers_p = false;
9118
9119       /* The extended syntax was used.  */
9120       extended_p = true;
9121
9122       /* Look for outputs.  */
9123       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9124         {
9125           /* Consume the `:'.  */
9126           cp_lexer_consume_token (parser->lexer);
9127           /* Parse the output-operands.  */
9128           if (cp_lexer_next_token_is_not (parser->lexer, 
9129                                           CPP_COLON)
9130               && cp_lexer_next_token_is_not (parser->lexer,
9131                                              CPP_SCOPE)
9132               && cp_lexer_next_token_is_not (parser->lexer,
9133                                              CPP_CLOSE_PAREN))
9134             outputs = cp_parser_asm_operand_list (parser);
9135         }
9136       /* If the next token is `::', there are no outputs, and the
9137          next token is the beginning of the inputs.  */
9138       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9139         {
9140           /* Consume the `::' token.  */
9141           cp_lexer_consume_token (parser->lexer);
9142           /* The inputs are coming next.  */
9143           inputs_p = true;
9144         }
9145
9146       /* Look for inputs.  */
9147       if (inputs_p
9148           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9149         {
9150           if (!inputs_p)
9151             /* Consume the `:'.  */
9152             cp_lexer_consume_token (parser->lexer);
9153           /* Parse the output-operands.  */
9154           if (cp_lexer_next_token_is_not (parser->lexer, 
9155                                           CPP_COLON)
9156               && cp_lexer_next_token_is_not (parser->lexer,
9157                                              CPP_SCOPE)
9158               && cp_lexer_next_token_is_not (parser->lexer,
9159                                              CPP_CLOSE_PAREN))
9160             inputs = cp_parser_asm_operand_list (parser);
9161         }
9162       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9163         /* The clobbers are coming next.  */
9164         clobbers_p = true;
9165
9166       /* Look for clobbers.  */
9167       if (clobbers_p 
9168           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9169         {
9170           if (!clobbers_p)
9171             /* Consume the `:'.  */
9172             cp_lexer_consume_token (parser->lexer);
9173           /* Parse the clobbers.  */
9174           if (cp_lexer_next_token_is_not (parser->lexer,
9175                                           CPP_CLOSE_PAREN))
9176             clobbers = cp_parser_asm_clobber_list (parser);
9177         }
9178     }
9179   /* Look for the closing `)'.  */
9180   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9181     cp_parser_skip_to_closing_parenthesis (parser, true, false);
9182   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9183
9184   /* Create the ASM_STMT.  */
9185   if (at_function_scope_p ())
9186     {
9187       asm_stmt = 
9188         finish_asm_stmt (volatile_p 
9189                          ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9190                          string, outputs, inputs, clobbers);
9191       /* If the extended syntax was not used, mark the ASM_STMT.  */
9192       if (!extended_p)
9193         ASM_INPUT_P (asm_stmt) = 1;
9194     }
9195   else
9196     assemble_asm (string);
9197 }
9198
9199 /* Declarators [gram.dcl.decl] */
9200
9201 /* Parse an init-declarator.
9202
9203    init-declarator:
9204      declarator initializer [opt]
9205
9206    GNU Extension:
9207
9208    init-declarator:
9209      declarator asm-specification [opt] attributes [opt] initializer [opt]
9210
9211    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9212    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
9213    then this declarator appears in a class scope.  The new DECL created
9214    by this declarator is returned.
9215
9216    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9217    for a function-definition here as well.  If the declarator is a
9218    declarator for a function-definition, *FUNCTION_DEFINITION_P will
9219    be TRUE upon return.  By that point, the function-definition will
9220    have been completely parsed.
9221
9222    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9223    is FALSE.  */
9224
9225 static tree
9226 cp_parser_init_declarator (cp_parser* parser, 
9227                            tree decl_specifiers, 
9228                            tree prefix_attributes,
9229                            bool function_definition_allowed_p,
9230                            bool member_p,
9231                            int declares_class_or_enum,
9232                            bool* function_definition_p)
9233 {
9234   cp_token *token;
9235   tree declarator;
9236   tree attributes;
9237   tree asm_specification;
9238   tree initializer;
9239   tree decl = NULL_TREE;
9240   tree scope;
9241   bool is_initialized;
9242   bool is_parenthesized_init;
9243   bool is_non_constant_init;
9244   int ctor_dtor_or_conv_p;
9245   bool friend_p;
9246
9247   /* Assume that this is not the declarator for a function
9248      definition.  */
9249   if (function_definition_p)
9250     *function_definition_p = false;
9251
9252   /* Defer access checks while parsing the declarator; we cannot know
9253      what names are accessible until we know what is being 
9254      declared.  */
9255   resume_deferring_access_checks ();
9256
9257   /* Parse the declarator.  */
9258   declarator 
9259     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9260                             &ctor_dtor_or_conv_p);
9261   /* Gather up the deferred checks.  */
9262   stop_deferring_access_checks ();
9263
9264   /* If the DECLARATOR was erroneous, there's no need to go
9265      further.  */
9266   if (declarator == error_mark_node)
9267     return error_mark_node;
9268
9269   cp_parser_check_for_definition_in_return_type (declarator,
9270                                                  declares_class_or_enum);
9271
9272   /* Figure out what scope the entity declared by the DECLARATOR is
9273      located in.  `grokdeclarator' sometimes changes the scope, so
9274      we compute it now.  */
9275   scope = get_scope_of_declarator (declarator);
9276
9277   /* If we're allowing GNU extensions, look for an asm-specification
9278      and attributes.  */
9279   if (cp_parser_allow_gnu_extensions_p (parser))
9280     {
9281       /* Look for an asm-specification.  */
9282       asm_specification = cp_parser_asm_specification_opt (parser);
9283       /* And attributes.  */
9284       attributes = cp_parser_attributes_opt (parser);
9285     }
9286   else
9287     {
9288       asm_specification = NULL_TREE;
9289       attributes = NULL_TREE;
9290     }
9291
9292   /* Peek at the next token.  */
9293   token = cp_lexer_peek_token (parser->lexer);
9294   /* Check to see if the token indicates the start of a
9295      function-definition.  */
9296   if (cp_parser_token_starts_function_definition_p (token))
9297     {
9298       if (!function_definition_allowed_p)
9299         {
9300           /* If a function-definition should not appear here, issue an
9301              error message.  */
9302           cp_parser_error (parser,
9303                            "a function-definition is not allowed here");
9304           return error_mark_node;
9305         }
9306       else
9307         {
9308           /* Neither attributes nor an asm-specification are allowed
9309              on a function-definition.  */
9310           if (asm_specification)
9311             error ("an asm-specification is not allowed on a function-definition");
9312           if (attributes)
9313             error ("attributes are not allowed on a function-definition");
9314           /* This is a function-definition.  */
9315           *function_definition_p = true;
9316
9317           /* Parse the function definition.  */
9318           decl = (cp_parser_function_definition_from_specifiers_and_declarator
9319                   (parser, decl_specifiers, prefix_attributes, declarator));
9320
9321           return decl;
9322         }
9323     }
9324
9325   /* [dcl.dcl]
9326
9327      Only in function declarations for constructors, destructors, and
9328      type conversions can the decl-specifier-seq be omitted.  
9329
9330      We explicitly postpone this check past the point where we handle
9331      function-definitions because we tolerate function-definitions
9332      that are missing their return types in some modes.  */
9333   if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9334     {
9335       cp_parser_error (parser, 
9336                        "expected constructor, destructor, or type conversion");
9337       return error_mark_node;
9338     }
9339
9340   /* An `=' or an `(' indicates an initializer.  */
9341   is_initialized = (token->type == CPP_EQ 
9342                      || token->type == CPP_OPEN_PAREN);
9343   /* If the init-declarator isn't initialized and isn't followed by a
9344      `,' or `;', it's not a valid init-declarator.  */
9345   if (!is_initialized 
9346       && token->type != CPP_COMMA
9347       && token->type != CPP_SEMICOLON)
9348     {
9349       cp_parser_error (parser, "expected init-declarator");
9350       return error_mark_node;
9351     }
9352
9353   /* Because start_decl has side-effects, we should only call it if we
9354      know we're going ahead.  By this point, we know that we cannot
9355      possibly be looking at any other construct.  */
9356   cp_parser_commit_to_tentative_parse (parser);
9357
9358   /* Check to see whether or not this declaration is a friend.  */
9359   friend_p = cp_parser_friend_p (decl_specifiers);
9360
9361   /* Check that the number of template-parameter-lists is OK.  */
9362   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
9363     return error_mark_node;
9364
9365   /* Enter the newly declared entry in the symbol table.  If we're
9366      processing a declaration in a class-specifier, we wait until
9367      after processing the initializer.  */
9368   if (!member_p)
9369     {
9370       if (parser->in_unbraced_linkage_specification_p)
9371         {
9372           decl_specifiers = tree_cons (error_mark_node,
9373                                        get_identifier ("extern"),
9374                                        decl_specifiers);
9375           have_extern_spec = false;
9376         }
9377       decl = start_decl (declarator, decl_specifiers,
9378                          is_initialized, attributes, prefix_attributes);
9379     }
9380
9381   /* Enter the SCOPE.  That way unqualified names appearing in the
9382      initializer will be looked up in SCOPE.  */
9383   if (scope)
9384     push_scope (scope);
9385
9386   /* Perform deferred access control checks, now that we know in which
9387      SCOPE the declared entity resides.  */
9388   if (!member_p && decl) 
9389     {
9390       tree saved_current_function_decl = NULL_TREE;
9391
9392       /* If the entity being declared is a function, pretend that we
9393          are in its scope.  If it is a `friend', it may have access to
9394          things that would not otherwise be accessible.  */
9395       if (TREE_CODE (decl) == FUNCTION_DECL)
9396         {
9397           saved_current_function_decl = current_function_decl;
9398           current_function_decl = decl;
9399         }
9400         
9401       /* Perform the access control checks for the declarator and the
9402          the decl-specifiers.  */
9403       perform_deferred_access_checks ();
9404
9405       /* Restore the saved value.  */
9406       if (TREE_CODE (decl) == FUNCTION_DECL)
9407         current_function_decl = saved_current_function_decl;
9408     }
9409
9410   /* Parse the initializer.  */
9411   if (is_initialized)
9412     initializer = cp_parser_initializer (parser, 
9413                                          &is_parenthesized_init,
9414                                          &is_non_constant_init);
9415   else
9416     {
9417       initializer = NULL_TREE;
9418       is_parenthesized_init = false;
9419       is_non_constant_init = true;
9420     }
9421
9422   /* The old parser allows attributes to appear after a parenthesized
9423      initializer.  Mark Mitchell proposed removing this functionality
9424      on the GCC mailing lists on 2002-08-13.  This parser accepts the
9425      attributes -- but ignores them.  */
9426   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9427     if (cp_parser_attributes_opt (parser))
9428       warning ("attributes after parenthesized initializer ignored");
9429
9430   /* Leave the SCOPE, now that we have processed the initializer.  It
9431      is important to do this before calling cp_finish_decl because it
9432      makes decisions about whether to create DECL_STMTs or not based
9433      on the current scope.  */
9434   if (scope)
9435     pop_scope (scope);
9436
9437   /* For an in-class declaration, use `grokfield' to create the
9438      declaration.  */
9439   if (member_p)
9440     {
9441       decl = grokfield (declarator, decl_specifiers,
9442                         initializer, /*asmspec=*/NULL_TREE,
9443                         /*attributes=*/NULL_TREE);
9444       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9445         cp_parser_save_default_args (parser, decl);
9446     }
9447   
9448   /* Finish processing the declaration.  But, skip friend
9449      declarations.  */
9450   if (!friend_p && decl)
9451     cp_finish_decl (decl, 
9452                     initializer, 
9453                     asm_specification,
9454                     /* If the initializer is in parentheses, then this is
9455                        a direct-initialization, which means that an
9456                        `explicit' constructor is OK.  Otherwise, an
9457                        `explicit' constructor cannot be used.  */
9458                     ((is_parenthesized_init || !is_initialized)
9459                      ? 0 : LOOKUP_ONLYCONVERTING));
9460
9461   /* Remember whether or not variables were initialized by
9462      constant-expressions.  */
9463   if (decl && TREE_CODE (decl) == VAR_DECL 
9464       && is_initialized && !is_non_constant_init)
9465     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9466
9467   return decl;
9468 }
9469
9470 /* Parse a declarator.
9471    
9472    declarator:
9473      direct-declarator
9474      ptr-operator declarator  
9475
9476    abstract-declarator:
9477      ptr-operator abstract-declarator [opt]
9478      direct-abstract-declarator
9479
9480    GNU Extensions:
9481
9482    declarator:
9483      attributes [opt] direct-declarator
9484      attributes [opt] ptr-operator declarator  
9485
9486    abstract-declarator:
9487      attributes [opt] ptr-operator abstract-declarator [opt]
9488      attributes [opt] direct-abstract-declarator
9489      
9490    Returns a representation of the declarator.  If the declarator has
9491    the form `* declarator', then an INDIRECT_REF is returned, whose
9492    only operand is the sub-declarator.  Analogously, `& declarator' is
9493    represented as an ADDR_EXPR.  For `X::* declarator', a SCOPE_REF is
9494    used.  The first operand is the TYPE for `X'.  The second operand
9495    is an INDIRECT_REF whose operand is the sub-declarator.
9496
9497    Otherwise, the representation is as for a direct-declarator.
9498
9499    (It would be better to define a structure type to represent
9500    declarators, rather than abusing `tree' nodes to represent
9501    declarators.  That would be much clearer and save some memory.
9502    There is no reason for declarators to be garbage-collected, for
9503    example; they are created during parser and no longer needed after
9504    `grokdeclarator' has been called.)
9505
9506    For a ptr-operator that has the optional cv-qualifier-seq,
9507    cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9508    node.
9509
9510    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9511    detect constructor, destructor or conversion operators. It is set
9512    to -1 if the declarator is a name, and +1 if it is a
9513    function. Otherwise it is set to zero. Usually you just want to
9514    test for >0, but internally the negative value is used.
9515    
9516    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9517    a decl-specifier-seq unless it declares a constructor, destructor,
9518    or conversion.  It might seem that we could check this condition in
9519    semantic analysis, rather than parsing, but that makes it difficult
9520    to handle something like `f()'.  We want to notice that there are
9521    no decl-specifiers, and therefore realize that this is an
9522    expression, not a declaration.)  */
9523
9524 static tree
9525 cp_parser_declarator (cp_parser* parser, 
9526                       cp_parser_declarator_kind dcl_kind, 
9527                       int* ctor_dtor_or_conv_p)
9528 {
9529   cp_token *token;
9530   tree declarator;
9531   enum tree_code code;
9532   tree cv_qualifier_seq;
9533   tree class_type;
9534   tree attributes = NULL_TREE;
9535
9536   /* Assume this is not a constructor, destructor, or type-conversion
9537      operator.  */
9538   if (ctor_dtor_or_conv_p)
9539     *ctor_dtor_or_conv_p = 0;
9540
9541   if (cp_parser_allow_gnu_extensions_p (parser))
9542     attributes = cp_parser_attributes_opt (parser);
9543   
9544   /* Peek at the next token.  */
9545   token = cp_lexer_peek_token (parser->lexer);
9546   
9547   /* Check for the ptr-operator production.  */
9548   cp_parser_parse_tentatively (parser);
9549   /* Parse the ptr-operator.  */
9550   code = cp_parser_ptr_operator (parser, 
9551                                  &class_type, 
9552                                  &cv_qualifier_seq);
9553   /* If that worked, then we have a ptr-operator.  */
9554   if (cp_parser_parse_definitely (parser))
9555     {
9556       /* The dependent declarator is optional if we are parsing an
9557          abstract-declarator.  */
9558       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9559         cp_parser_parse_tentatively (parser);
9560
9561       /* Parse the dependent declarator.  */
9562       declarator = cp_parser_declarator (parser, dcl_kind,
9563                                          /*ctor_dtor_or_conv_p=*/NULL);
9564
9565       /* If we are parsing an abstract-declarator, we must handle the
9566          case where the dependent declarator is absent.  */
9567       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9568           && !cp_parser_parse_definitely (parser))
9569         declarator = NULL_TREE;
9570         
9571       /* Build the representation of the ptr-operator.  */
9572       if (code == INDIRECT_REF)
9573         declarator = make_pointer_declarator (cv_qualifier_seq, 
9574                                               declarator);
9575       else
9576         declarator = make_reference_declarator (cv_qualifier_seq,
9577                                                 declarator);
9578       /* Handle the pointer-to-member case.  */
9579       if (class_type)
9580         declarator = build_nt (SCOPE_REF, class_type, declarator);
9581     }
9582   /* Everything else is a direct-declarator.  */
9583   else
9584     declarator = cp_parser_direct_declarator (parser, dcl_kind,
9585                                               ctor_dtor_or_conv_p);
9586
9587   if (attributes && declarator != error_mark_node)
9588     declarator = tree_cons (attributes, declarator, NULL_TREE);
9589   
9590   return declarator;
9591 }
9592
9593 /* Parse a direct-declarator or direct-abstract-declarator.
9594
9595    direct-declarator:
9596      declarator-id
9597      direct-declarator ( parameter-declaration-clause )
9598        cv-qualifier-seq [opt] 
9599        exception-specification [opt]
9600      direct-declarator [ constant-expression [opt] ]
9601      ( declarator )  
9602
9603    direct-abstract-declarator:
9604      direct-abstract-declarator [opt]
9605        ( parameter-declaration-clause ) 
9606        cv-qualifier-seq [opt]
9607        exception-specification [opt]
9608      direct-abstract-declarator [opt] [ constant-expression [opt] ]
9609      ( abstract-declarator )
9610
9611    Returns a representation of the declarator.  DCL_KIND is
9612    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9613    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
9614    we are parsing a direct-declarator.  It is
9615    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9616    of ambiguity we prefer an abstract declarator, as per
9617    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
9618    cp_parser_declarator.
9619
9620    For the declarator-id production, the representation is as for an
9621    id-expression, except that a qualified name is represented as a
9622    SCOPE_REF.  A function-declarator is represented as a CALL_EXPR;
9623    see the documentation of the FUNCTION_DECLARATOR_* macros for
9624    information about how to find the various declarator components.
9625    An array-declarator is represented as an ARRAY_REF.  The
9626    direct-declarator is the first operand; the constant-expression
9627    indicating the size of the array is the second operand.  */
9628
9629 static tree
9630 cp_parser_direct_declarator (cp_parser* parser,
9631                              cp_parser_declarator_kind dcl_kind,
9632                              int* ctor_dtor_or_conv_p)
9633 {
9634   cp_token *token;
9635   tree declarator = NULL_TREE;
9636   tree scope = NULL_TREE;
9637   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9638   bool saved_in_declarator_p = parser->in_declarator_p;
9639   bool first = true;
9640   
9641   while (true)
9642     {
9643       /* Peek at the next token.  */
9644       token = cp_lexer_peek_token (parser->lexer);
9645       if (token->type == CPP_OPEN_PAREN)
9646         {
9647           /* This is either a parameter-declaration-clause, or a
9648              parenthesized declarator. When we know we are parsing a
9649              named declarator, it must be a parenthesized declarator
9650              if FIRST is true. For instance, `(int)' is a
9651              parameter-declaration-clause, with an omitted
9652              direct-abstract-declarator. But `((*))', is a
9653              parenthesized abstract declarator. Finally, when T is a
9654              template parameter `(T)' is a
9655              parameter-declaration-clause, and not a parenthesized
9656              named declarator.
9657              
9658              We first try and parse a parameter-declaration-clause,
9659              and then try a nested declarator (if FIRST is true).
9660
9661              It is not an error for it not to be a
9662              parameter-declaration-clause, even when FIRST is
9663              false. Consider,
9664
9665                int i (int);
9666                int i (3);
9667
9668              The first is the declaration of a function while the
9669              second is a the definition of a variable, including its
9670              initializer.
9671
9672              Having seen only the parenthesis, we cannot know which of
9673              these two alternatives should be selected.  Even more
9674              complex are examples like:
9675
9676                int i (int (a));
9677                int i (int (3));
9678
9679              The former is a function-declaration; the latter is a
9680              variable initialization.  
9681
9682              Thus again, we try a parameter-declaration-clause, and if
9683              that fails, we back out and return.  */
9684
9685           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9686             {
9687               tree params;
9688               
9689               cp_parser_parse_tentatively (parser);
9690
9691               /* Consume the `('.  */
9692               cp_lexer_consume_token (parser->lexer);
9693               if (first)
9694                 {
9695                   /* If this is going to be an abstract declarator, we're
9696                      in a declarator and we can't have default args.  */
9697                   parser->default_arg_ok_p = false;
9698                   parser->in_declarator_p = true;
9699                 }
9700           
9701               /* Parse the parameter-declaration-clause.  */
9702               params = cp_parser_parameter_declaration_clause (parser);
9703
9704               /* If all went well, parse the cv-qualifier-seq and the
9705                  exception-specification.  */
9706               if (cp_parser_parse_definitely (parser))
9707                 {
9708                   tree cv_qualifiers;
9709                   tree exception_specification;
9710
9711                   if (ctor_dtor_or_conv_p)
9712                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
9713                   first = false;
9714                   /* Consume the `)'.  */
9715                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9716
9717                   /* Parse the cv-qualifier-seq.  */
9718                   cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
9719                   /* And the exception-specification.  */
9720                   exception_specification 
9721                     = cp_parser_exception_specification_opt (parser);
9722
9723                   /* Create the function-declarator.  */
9724                   declarator = make_call_declarator (declarator,
9725                                                      params,
9726                                                      cv_qualifiers,
9727                                                      exception_specification);
9728                   /* Any subsequent parameter lists are to do with
9729                      return type, so are not those of the declared
9730                      function.  */
9731                   parser->default_arg_ok_p = false;
9732                   
9733                   /* Repeat the main loop.  */
9734                   continue;
9735                 }
9736             }
9737           
9738           /* If this is the first, we can try a parenthesized
9739              declarator.  */
9740           if (first)
9741             {
9742               parser->default_arg_ok_p = saved_default_arg_ok_p;
9743               parser->in_declarator_p = saved_in_declarator_p;
9744               
9745               /* Consume the `('.  */
9746               cp_lexer_consume_token (parser->lexer);
9747               /* Parse the nested declarator.  */
9748               declarator 
9749                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
9750               first = false;
9751               /* Expect a `)'.  */
9752               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9753                 declarator = error_mark_node;
9754               if (declarator == error_mark_node)
9755                 break;
9756               
9757               goto handle_declarator;
9758             }
9759           /* Otherwise, we must be done.  */
9760           else
9761             break;
9762         }
9763       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9764                && token->type == CPP_OPEN_SQUARE)
9765         {
9766           /* Parse an array-declarator.  */
9767           tree bounds;
9768
9769           if (ctor_dtor_or_conv_p)
9770             *ctor_dtor_or_conv_p = 0;
9771           
9772           first = false;
9773           parser->default_arg_ok_p = false;
9774           parser->in_declarator_p = true;
9775           /* Consume the `['.  */
9776           cp_lexer_consume_token (parser->lexer);
9777           /* Peek at the next token.  */
9778           token = cp_lexer_peek_token (parser->lexer);
9779           /* If the next token is `]', then there is no
9780              constant-expression.  */
9781           if (token->type != CPP_CLOSE_SQUARE)
9782             {
9783               bool non_constant_p;
9784
9785               bounds 
9786                 = cp_parser_constant_expression (parser,
9787                                                  /*allow_non_constant=*/true,
9788                                                  &non_constant_p);
9789               if (!non_constant_p)
9790                 bounds = cp_parser_fold_non_dependent_expr (bounds);
9791             }
9792           else
9793             bounds = NULL_TREE;
9794           /* Look for the closing `]'.  */
9795           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
9796             {
9797               declarator = error_mark_node;
9798               break;
9799             }
9800
9801           declarator = build_nt (ARRAY_REF, declarator, bounds);
9802         }
9803       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
9804         {
9805           /* Parse a declarator_id */
9806           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9807             cp_parser_parse_tentatively (parser);
9808           declarator = cp_parser_declarator_id (parser);
9809           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9810             {
9811               if (!cp_parser_parse_definitely (parser))
9812                 declarator = error_mark_node;
9813               else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
9814                 {
9815                   cp_parser_error (parser, "expected unqualified-id");
9816                   declarator = error_mark_node;
9817                 }
9818             }
9819           
9820           if (declarator == error_mark_node)
9821             break;
9822           
9823           if (TREE_CODE (declarator) == SCOPE_REF)
9824             {
9825               tree scope = TREE_OPERAND (declarator, 0);
9826
9827               /* In the declaration of a member of a template class
9828                  outside of the class itself, the SCOPE will sometimes
9829                  be a TYPENAME_TYPE.  For example, given:
9830                   
9831                  template <typename T>
9832                  int S<T>::R::i = 3;
9833                   
9834                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
9835                  this context, we must resolve S<T>::R to an ordinary
9836                  type, rather than a typename type.
9837                   
9838                  The reason we normally avoid resolving TYPENAME_TYPEs
9839                  is that a specialization of `S' might render
9840                  `S<T>::R' not a type.  However, if `S' is
9841                  specialized, then this `i' will not be used, so there
9842                  is no harm in resolving the types here.  */
9843               if (TREE_CODE (scope) == TYPENAME_TYPE)
9844                 {
9845                   tree type;
9846
9847                   /* Resolve the TYPENAME_TYPE.  */
9848                   type = resolve_typename_type (scope,
9849                                                  /*only_current_p=*/false);
9850                   /* If that failed, the declarator is invalid.  */
9851                   if (type != error_mark_node)
9852                     scope = type;
9853                   /* Build a new DECLARATOR.  */
9854                   declarator = build_nt (SCOPE_REF, 
9855                                          scope,
9856                                          TREE_OPERAND (declarator, 1));
9857                 }
9858             }
9859       
9860           /* Check to see whether the declarator-id names a constructor, 
9861              destructor, or conversion.  */
9862           if (declarator && ctor_dtor_or_conv_p 
9863               && ((TREE_CODE (declarator) == SCOPE_REF 
9864                    && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
9865                   || (TREE_CODE (declarator) != SCOPE_REF
9866                       && at_class_scope_p ())))
9867             {
9868               tree unqualified_name;
9869               tree class_type;
9870
9871               /* Get the unqualified part of the name.  */
9872               if (TREE_CODE (declarator) == SCOPE_REF)
9873                 {
9874                   class_type = TREE_OPERAND (declarator, 0);
9875                   unqualified_name = TREE_OPERAND (declarator, 1);
9876                 }
9877               else
9878                 {
9879                   class_type = current_class_type;
9880                   unqualified_name = declarator;
9881                 }
9882
9883               /* See if it names ctor, dtor or conv.  */
9884               if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
9885                   || IDENTIFIER_TYPENAME_P (unqualified_name)
9886                   || constructor_name_p (unqualified_name, class_type))
9887                 *ctor_dtor_or_conv_p = -1;
9888             }
9889
9890         handle_declarator:;
9891           scope = get_scope_of_declarator (declarator);
9892           if (scope)
9893             /* Any names that appear after the declarator-id for a member
9894                are looked up in the containing scope.  */
9895             push_scope (scope);
9896           parser->in_declarator_p = true;
9897           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
9898               || (declarator
9899                   && (TREE_CODE (declarator) == SCOPE_REF
9900                       || TREE_CODE (declarator) == IDENTIFIER_NODE)))
9901             /* Default args are only allowed on function
9902                declarations.  */
9903             parser->default_arg_ok_p = saved_default_arg_ok_p;
9904           else
9905             parser->default_arg_ok_p = false;
9906
9907           first = false;
9908         }
9909       /* We're done.  */
9910       else
9911         break;
9912     }
9913
9914   /* For an abstract declarator, we might wind up with nothing at this
9915      point.  That's an error; the declarator is not optional.  */
9916   if (!declarator)
9917     cp_parser_error (parser, "expected declarator");
9918
9919   /* If we entered a scope, we must exit it now.  */
9920   if (scope)
9921     pop_scope (scope);
9922
9923   parser->default_arg_ok_p = saved_default_arg_ok_p;
9924   parser->in_declarator_p = saved_in_declarator_p;
9925   
9926   return declarator;
9927 }
9928
9929 /* Parse a ptr-operator.  
9930
9931    ptr-operator:
9932      * cv-qualifier-seq [opt]
9933      &
9934      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
9935
9936    GNU Extension:
9937
9938    ptr-operator:
9939      & cv-qualifier-seq [opt]
9940
9941    Returns INDIRECT_REF if a pointer, or pointer-to-member, was
9942    used.  Returns ADDR_EXPR if a reference was used.  In the
9943    case of a pointer-to-member, *TYPE is filled in with the 
9944    TYPE containing the member.  *CV_QUALIFIER_SEQ is filled in
9945    with the cv-qualifier-seq, or NULL_TREE, if there are no
9946    cv-qualifiers.  Returns ERROR_MARK if an error occurred.  */
9947    
9948 static enum tree_code
9949 cp_parser_ptr_operator (cp_parser* parser, 
9950                         tree* type, 
9951                         tree* cv_qualifier_seq)
9952 {
9953   enum tree_code code = ERROR_MARK;
9954   cp_token *token;
9955
9956   /* Assume that it's not a pointer-to-member.  */
9957   *type = NULL_TREE;
9958   /* And that there are no cv-qualifiers.  */
9959   *cv_qualifier_seq = NULL_TREE;
9960
9961   /* Peek at the next token.  */
9962   token = cp_lexer_peek_token (parser->lexer);
9963   /* If it's a `*' or `&' we have a pointer or reference.  */
9964   if (token->type == CPP_MULT || token->type == CPP_AND)
9965     {
9966       /* Remember which ptr-operator we were processing.  */
9967       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
9968
9969       /* Consume the `*' or `&'.  */
9970       cp_lexer_consume_token (parser->lexer);
9971
9972       /* A `*' can be followed by a cv-qualifier-seq, and so can a
9973          `&', if we are allowing GNU extensions.  (The only qualifier
9974          that can legally appear after `&' is `restrict', but that is
9975          enforced during semantic analysis.  */
9976       if (code == INDIRECT_REF 
9977           || cp_parser_allow_gnu_extensions_p (parser))
9978         *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
9979     }
9980   else
9981     {
9982       /* Try the pointer-to-member case.  */
9983       cp_parser_parse_tentatively (parser);
9984       /* Look for the optional `::' operator.  */
9985       cp_parser_global_scope_opt (parser,
9986                                   /*current_scope_valid_p=*/false);
9987       /* Look for the nested-name specifier.  */
9988       cp_parser_nested_name_specifier (parser,
9989                                        /*typename_keyword_p=*/false,
9990                                        /*check_dependency_p=*/true,
9991                                        /*type_p=*/false);
9992       /* If we found it, and the next token is a `*', then we are
9993          indeed looking at a pointer-to-member operator.  */
9994       if (!cp_parser_error_occurred (parser)
9995           && cp_parser_require (parser, CPP_MULT, "`*'"))
9996         {
9997           /* The type of which the member is a member is given by the
9998              current SCOPE.  */
9999           *type = parser->scope;
10000           /* The next name will not be qualified.  */
10001           parser->scope = NULL_TREE;
10002           parser->qualifying_scope = NULL_TREE;
10003           parser->object_scope = NULL_TREE;
10004           /* Indicate that the `*' operator was used.  */
10005           code = INDIRECT_REF;
10006           /* Look for the optional cv-qualifier-seq.  */
10007           *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10008         }
10009       /* If that didn't work we don't have a ptr-operator.  */
10010       if (!cp_parser_parse_definitely (parser))
10011         cp_parser_error (parser, "expected ptr-operator");
10012     }
10013
10014   return code;
10015 }
10016
10017 /* Parse an (optional) cv-qualifier-seq.
10018
10019    cv-qualifier-seq:
10020      cv-qualifier cv-qualifier-seq [opt]  
10021
10022    Returns a TREE_LIST.  The TREE_VALUE of each node is the
10023    representation of a cv-qualifier.  */
10024
10025 static tree
10026 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10027 {
10028   tree cv_qualifiers = NULL_TREE;
10029   
10030   while (true)
10031     {
10032       tree cv_qualifier;
10033
10034       /* Look for the next cv-qualifier.  */
10035       cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10036       /* If we didn't find one, we're done.  */
10037       if (!cv_qualifier)
10038         break;
10039
10040       /* Add this cv-qualifier to the list.  */
10041       cv_qualifiers 
10042         = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10043     }
10044
10045   /* We built up the list in reverse order.  */
10046   return nreverse (cv_qualifiers);
10047 }
10048
10049 /* Parse an (optional) cv-qualifier.
10050
10051    cv-qualifier:
10052      const
10053      volatile  
10054
10055    GNU Extension:
10056
10057    cv-qualifier:
10058      __restrict__ */
10059
10060 static tree
10061 cp_parser_cv_qualifier_opt (cp_parser* parser)
10062 {
10063   cp_token *token;
10064   tree cv_qualifier = NULL_TREE;
10065
10066   /* Peek at the next token.  */
10067   token = cp_lexer_peek_token (parser->lexer);
10068   /* See if it's a cv-qualifier.  */
10069   switch (token->keyword)
10070     {
10071     case RID_CONST:
10072     case RID_VOLATILE:
10073     case RID_RESTRICT:
10074       /* Save the value of the token.  */
10075       cv_qualifier = token->value;
10076       /* Consume the token.  */
10077       cp_lexer_consume_token (parser->lexer);
10078       break;
10079
10080     default:
10081       break;
10082     }
10083
10084   return cv_qualifier;
10085 }
10086
10087 /* Parse a declarator-id.
10088
10089    declarator-id:
10090      id-expression
10091      :: [opt] nested-name-specifier [opt] type-name  
10092
10093    In the `id-expression' case, the value returned is as for
10094    cp_parser_id_expression if the id-expression was an unqualified-id.
10095    If the id-expression was a qualified-id, then a SCOPE_REF is
10096    returned.  The first operand is the scope (either a NAMESPACE_DECL
10097    or TREE_TYPE), but the second is still just a representation of an
10098    unqualified-id.  */
10099
10100 static tree
10101 cp_parser_declarator_id (cp_parser* parser)
10102 {
10103   tree id_expression;
10104
10105   /* The expression must be an id-expression.  Assume that qualified
10106      names are the names of types so that:
10107
10108        template <class T>
10109        int S<T>::R::i = 3;
10110
10111      will work; we must treat `S<T>::R' as the name of a type.
10112      Similarly, assume that qualified names are templates, where
10113      required, so that:
10114
10115        template <class T>
10116        int S<T>::R<T>::i = 3;
10117
10118      will work, too.  */
10119   id_expression = cp_parser_id_expression (parser,
10120                                            /*template_keyword_p=*/false,
10121                                            /*check_dependency_p=*/false,
10122                                            /*template_p=*/NULL);
10123   /* If the name was qualified, create a SCOPE_REF to represent 
10124      that.  */
10125   if (parser->scope)
10126     {
10127       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10128       parser->scope = NULL_TREE;
10129     }
10130
10131   return id_expression;
10132 }
10133
10134 /* Parse a type-id.
10135
10136    type-id:
10137      type-specifier-seq abstract-declarator [opt]
10138
10139    Returns the TYPE specified.  */
10140
10141 static tree
10142 cp_parser_type_id (cp_parser* parser)
10143 {
10144   tree type_specifier_seq;
10145   tree abstract_declarator;
10146
10147   /* Parse the type-specifier-seq.  */
10148   type_specifier_seq 
10149     = cp_parser_type_specifier_seq (parser);
10150   if (type_specifier_seq == error_mark_node)
10151     return error_mark_node;
10152
10153   /* There might or might not be an abstract declarator.  */
10154   cp_parser_parse_tentatively (parser);
10155   /* Look for the declarator.  */
10156   abstract_declarator 
10157     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10158   /* Check to see if there really was a declarator.  */
10159   if (!cp_parser_parse_definitely (parser))
10160     abstract_declarator = NULL_TREE;
10161
10162   return groktypename (build_tree_list (type_specifier_seq,
10163                                         abstract_declarator));
10164 }
10165
10166 /* Parse a type-specifier-seq.
10167
10168    type-specifier-seq:
10169      type-specifier type-specifier-seq [opt]
10170
10171    GNU extension:
10172
10173    type-specifier-seq:
10174      attributes type-specifier-seq [opt]
10175
10176    Returns a TREE_LIST.  Either the TREE_VALUE of each node is a
10177    type-specifier, or the TREE_PURPOSE is a list of attributes.  */
10178
10179 static tree
10180 cp_parser_type_specifier_seq (cp_parser* parser)
10181 {
10182   bool seen_type_specifier = false;
10183   tree type_specifier_seq = NULL_TREE;
10184
10185   /* Parse the type-specifiers and attributes.  */
10186   while (true)
10187     {
10188       tree type_specifier;
10189
10190       /* Check for attributes first.  */
10191       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10192         {
10193           type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10194                                           NULL_TREE,
10195                                           type_specifier_seq);
10196           continue;
10197         }
10198
10199       /* After the first type-specifier, others are optional.  */
10200       if (seen_type_specifier)
10201         cp_parser_parse_tentatively (parser);
10202       /* Look for the type-specifier.  */
10203       type_specifier = cp_parser_type_specifier (parser, 
10204                                                  CP_PARSER_FLAGS_NONE,
10205                                                  /*is_friend=*/false,
10206                                                  /*is_declaration=*/false,
10207                                                  NULL,
10208                                                  NULL);
10209       /* If the first type-specifier could not be found, this is not a
10210          type-specifier-seq at all.  */
10211       if (!seen_type_specifier && type_specifier == error_mark_node)
10212         return error_mark_node;
10213       /* If subsequent type-specifiers could not be found, the
10214          type-specifier-seq is complete.  */
10215       else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10216         break;
10217
10218       /* Add the new type-specifier to the list.  */
10219       type_specifier_seq 
10220         = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10221       seen_type_specifier = true;
10222     }
10223
10224   /* We built up the list in reverse order.  */
10225   return nreverse (type_specifier_seq);
10226 }
10227
10228 /* Parse a parameter-declaration-clause.
10229
10230    parameter-declaration-clause:
10231      parameter-declaration-list [opt] ... [opt]
10232      parameter-declaration-list , ...
10233
10234    Returns a representation for the parameter declarations.  Each node
10235    is a TREE_LIST.  (See cp_parser_parameter_declaration for the exact
10236    representation.)  If the parameter-declaration-clause ends with an
10237    ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10238    list.  A return value of NULL_TREE indicates a
10239    parameter-declaration-clause consisting only of an ellipsis.  */
10240
10241 static tree
10242 cp_parser_parameter_declaration_clause (cp_parser* parser)
10243 {
10244   tree parameters;
10245   cp_token *token;
10246   bool ellipsis_p;
10247
10248   /* Peek at the next token.  */
10249   token = cp_lexer_peek_token (parser->lexer);
10250   /* Check for trivial parameter-declaration-clauses.  */
10251   if (token->type == CPP_ELLIPSIS)
10252     {
10253       /* Consume the `...' token.  */
10254       cp_lexer_consume_token (parser->lexer);
10255       return NULL_TREE;
10256     }
10257   else if (token->type == CPP_CLOSE_PAREN)
10258     /* There are no parameters.  */
10259     {
10260 #ifndef NO_IMPLICIT_EXTERN_C
10261       if (in_system_header && current_class_type == NULL
10262           && current_lang_name == lang_name_c)
10263         return NULL_TREE;
10264       else
10265 #endif
10266         return void_list_node;
10267     }
10268   /* Check for `(void)', too, which is a special case.  */
10269   else if (token->keyword == RID_VOID
10270            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
10271                == CPP_CLOSE_PAREN))
10272     {
10273       /* Consume the `void' token.  */
10274       cp_lexer_consume_token (parser->lexer);
10275       /* There are no parameters.  */
10276       return void_list_node;
10277     }
10278   
10279   /* Parse the parameter-declaration-list.  */
10280   parameters = cp_parser_parameter_declaration_list (parser);
10281   /* If a parse error occurred while parsing the
10282      parameter-declaration-list, then the entire
10283      parameter-declaration-clause is erroneous.  */
10284   if (parameters == error_mark_node)
10285     return error_mark_node;
10286
10287   /* Peek at the next token.  */
10288   token = cp_lexer_peek_token (parser->lexer);
10289   /* If it's a `,', the clause should terminate with an ellipsis.  */
10290   if (token->type == CPP_COMMA)
10291     {
10292       /* Consume the `,'.  */
10293       cp_lexer_consume_token (parser->lexer);
10294       /* Expect an ellipsis.  */
10295       ellipsis_p 
10296         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10297     }
10298   /* It might also be `...' if the optional trailing `,' was 
10299      omitted.  */
10300   else if (token->type == CPP_ELLIPSIS)
10301     {
10302       /* Consume the `...' token.  */
10303       cp_lexer_consume_token (parser->lexer);
10304       /* And remember that we saw it.  */
10305       ellipsis_p = true;
10306     }
10307   else
10308     ellipsis_p = false;
10309
10310   /* Finish the parameter list.  */
10311   return finish_parmlist (parameters, ellipsis_p);
10312 }
10313
10314 /* Parse a parameter-declaration-list.
10315
10316    parameter-declaration-list:
10317      parameter-declaration
10318      parameter-declaration-list , parameter-declaration
10319
10320    Returns a representation of the parameter-declaration-list, as for
10321    cp_parser_parameter_declaration_clause.  However, the
10322    `void_list_node' is never appended to the list.  */
10323
10324 static tree
10325 cp_parser_parameter_declaration_list (cp_parser* parser)
10326 {
10327   tree parameters = NULL_TREE;
10328
10329   /* Look for more parameters.  */
10330   while (true)
10331     {
10332       tree parameter;
10333       /* Parse the parameter.  */
10334       parameter 
10335         = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10336
10337       /* If a parse error occurred parsing the parameter declaration,
10338          then the entire parameter-declaration-list is erroneous.  */
10339       if (parameter == error_mark_node)
10340         {
10341           parameters = error_mark_node;
10342           break;
10343         }
10344       /* Add the new parameter to the list.  */
10345       TREE_CHAIN (parameter) = parameters;
10346       parameters = parameter;
10347
10348       /* Peek at the next token.  */
10349       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10350           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10351         /* The parameter-declaration-list is complete.  */
10352         break;
10353       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10354         {
10355           cp_token *token;
10356
10357           /* Peek at the next token.  */
10358           token = cp_lexer_peek_nth_token (parser->lexer, 2);
10359           /* If it's an ellipsis, then the list is complete.  */
10360           if (token->type == CPP_ELLIPSIS)
10361             break;
10362           /* Otherwise, there must be more parameters.  Consume the
10363              `,'.  */
10364           cp_lexer_consume_token (parser->lexer);
10365         }
10366       else
10367         {
10368           cp_parser_error (parser, "expected `,' or `...'");
10369           break;
10370         }
10371     }
10372
10373   /* We built up the list in reverse order; straighten it out now.  */
10374   return nreverse (parameters);
10375 }
10376
10377 /* Parse a parameter declaration.
10378
10379    parameter-declaration:
10380      decl-specifier-seq declarator
10381      decl-specifier-seq declarator = assignment-expression
10382      decl-specifier-seq abstract-declarator [opt]
10383      decl-specifier-seq abstract-declarator [opt] = assignment-expression
10384
10385    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10386    declares a template parameter.  (In that case, a non-nested `>'
10387    token encountered during the parsing of the assignment-expression
10388    is not interpreted as a greater-than operator.)
10389
10390    Returns a TREE_LIST representing the parameter-declaration.  The
10391    TREE_VALUE is a representation of the decl-specifier-seq and
10392    declarator.  In particular, the TREE_VALUE will be a TREE_LIST
10393    whose TREE_PURPOSE represents the decl-specifier-seq and whose
10394    TREE_VALUE represents the declarator.  */
10395
10396 static tree
10397 cp_parser_parameter_declaration (cp_parser *parser, 
10398                                  bool template_parm_p)
10399 {
10400   int declares_class_or_enum;
10401   bool greater_than_is_operator_p;
10402   tree decl_specifiers;
10403   tree attributes;
10404   tree declarator;
10405   tree default_argument;
10406   tree parameter;
10407   cp_token *token;
10408   const char *saved_message;
10409
10410   /* In a template parameter, `>' is not an operator.
10411
10412      [temp.param]
10413
10414      When parsing a default template-argument for a non-type
10415      template-parameter, the first non-nested `>' is taken as the end
10416      of the template parameter-list rather than a greater-than
10417      operator.  */
10418   greater_than_is_operator_p = !template_parm_p;
10419
10420   /* Type definitions may not appear in parameter types.  */
10421   saved_message = parser->type_definition_forbidden_message;
10422   parser->type_definition_forbidden_message 
10423     = "types may not be defined in parameter types";
10424
10425   /* Parse the declaration-specifiers.  */
10426   decl_specifiers 
10427     = cp_parser_decl_specifier_seq (parser,
10428                                     CP_PARSER_FLAGS_NONE,
10429                                     &attributes,
10430                                     &declares_class_or_enum);
10431   /* If an error occurred, there's no reason to attempt to parse the
10432      rest of the declaration.  */
10433   if (cp_parser_error_occurred (parser))
10434     {
10435       parser->type_definition_forbidden_message = saved_message;
10436       return error_mark_node;
10437     }
10438
10439   /* Peek at the next token.  */
10440   token = cp_lexer_peek_token (parser->lexer);
10441   /* If the next token is a `)', `,', `=', `>', or `...', then there
10442      is no declarator.  */
10443   if (token->type == CPP_CLOSE_PAREN 
10444       || token->type == CPP_COMMA
10445       || token->type == CPP_EQ
10446       || token->type == CPP_ELLIPSIS
10447       || token->type == CPP_GREATER)
10448     declarator = NULL_TREE;
10449   /* Otherwise, there should be a declarator.  */
10450   else
10451     {
10452       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10453       parser->default_arg_ok_p = false;
10454   
10455       declarator = cp_parser_declarator (parser,
10456                                          CP_PARSER_DECLARATOR_EITHER,
10457                                          /*ctor_dtor_or_conv_p=*/NULL);
10458       parser->default_arg_ok_p = saved_default_arg_ok_p;
10459       /* After the declarator, allow more attributes.  */
10460       attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10461     }
10462
10463   /* The restriction on defining new types applies only to the type
10464      of the parameter, not to the default argument.  */
10465   parser->type_definition_forbidden_message = saved_message;
10466
10467   /* If the next token is `=', then process a default argument.  */
10468   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10469     {
10470       bool saved_greater_than_is_operator_p;
10471       /* Consume the `='.  */
10472       cp_lexer_consume_token (parser->lexer);
10473
10474       /* If we are defining a class, then the tokens that make up the
10475          default argument must be saved and processed later.  */
10476       if (!template_parm_p && at_class_scope_p () 
10477           && TYPE_BEING_DEFINED (current_class_type))
10478         {
10479           unsigned depth = 0;
10480
10481           /* Create a DEFAULT_ARG to represented the unparsed default
10482              argument.  */
10483           default_argument = make_node (DEFAULT_ARG);
10484           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10485
10486           /* Add tokens until we have processed the entire default
10487              argument.  */
10488           while (true)
10489             {
10490               bool done = false;
10491               cp_token *token;
10492
10493               /* Peek at the next token.  */
10494               token = cp_lexer_peek_token (parser->lexer);
10495               /* What we do depends on what token we have.  */
10496               switch (token->type)
10497                 {
10498                   /* In valid code, a default argument must be
10499                      immediately followed by a `,' `)', or `...'.  */
10500                 case CPP_COMMA:
10501                 case CPP_CLOSE_PAREN:
10502                 case CPP_ELLIPSIS:
10503                   /* If we run into a non-nested `;', `}', or `]',
10504                      then the code is invalid -- but the default
10505                      argument is certainly over.  */
10506                 case CPP_SEMICOLON:
10507                 case CPP_CLOSE_BRACE:
10508                 case CPP_CLOSE_SQUARE:
10509                   if (depth == 0)
10510                     done = true;
10511                   /* Update DEPTH, if necessary.  */
10512                   else if (token->type == CPP_CLOSE_PAREN
10513                            || token->type == CPP_CLOSE_BRACE
10514                            || token->type == CPP_CLOSE_SQUARE)
10515                     --depth;
10516                   break;
10517
10518                 case CPP_OPEN_PAREN:
10519                 case CPP_OPEN_SQUARE:
10520                 case CPP_OPEN_BRACE:
10521                   ++depth;
10522                   break;
10523
10524                 case CPP_GREATER:
10525                   /* If we see a non-nested `>', and `>' is not an
10526                      operator, then it marks the end of the default
10527                      argument.  */
10528                   if (!depth && !greater_than_is_operator_p)
10529                     done = true;
10530                   break;
10531
10532                   /* If we run out of tokens, issue an error message.  */
10533                 case CPP_EOF:
10534                   error ("file ends in default argument");
10535                   done = true;
10536                   break;
10537
10538                 case CPP_NAME:
10539                 case CPP_SCOPE:
10540                   /* In these cases, we should look for template-ids.
10541                      For example, if the default argument is 
10542                      `X<int, double>()', we need to do name lookup to
10543                      figure out whether or not `X' is a template; if
10544                      so, the `,' does not end the default argument.
10545
10546                      That is not yet done.  */
10547                   break;
10548
10549                 default:
10550                   break;
10551                 }
10552
10553               /* If we've reached the end, stop.  */
10554               if (done)
10555                 break;
10556               
10557               /* Add the token to the token block.  */
10558               token = cp_lexer_consume_token (parser->lexer);
10559               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10560                                          token);
10561             }
10562         }
10563       /* Outside of a class definition, we can just parse the
10564          assignment-expression.  */
10565       else
10566         {
10567           bool saved_local_variables_forbidden_p;
10568
10569           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10570              set correctly.  */
10571           saved_greater_than_is_operator_p 
10572             = parser->greater_than_is_operator_p;
10573           parser->greater_than_is_operator_p = greater_than_is_operator_p;
10574           /* Local variable names (and the `this' keyword) may not
10575              appear in a default argument.  */
10576           saved_local_variables_forbidden_p 
10577             = parser->local_variables_forbidden_p;
10578           parser->local_variables_forbidden_p = true;
10579           /* Parse the assignment-expression.  */
10580           default_argument = cp_parser_assignment_expression (parser);
10581           /* Restore saved state.  */
10582           parser->greater_than_is_operator_p 
10583             = saved_greater_than_is_operator_p;
10584           parser->local_variables_forbidden_p 
10585             = saved_local_variables_forbidden_p; 
10586         }
10587       if (!parser->default_arg_ok_p)
10588         {
10589           if (!flag_pedantic_errors)
10590             warning ("deprecated use of default argument for parameter of non-function");
10591           else
10592             {
10593               error ("default arguments are only permitted for function parameters");
10594               default_argument = NULL_TREE;
10595             }
10596         }
10597     }
10598   else
10599     default_argument = NULL_TREE;
10600   
10601   /* Create the representation of the parameter.  */
10602   if (attributes)
10603     decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10604   parameter = build_tree_list (default_argument, 
10605                                build_tree_list (decl_specifiers,
10606                                                 declarator));
10607
10608   return parameter;
10609 }
10610
10611 /* Parse a function-definition.  
10612
10613    function-definition:
10614      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10615        function-body 
10616      decl-specifier-seq [opt] declarator function-try-block  
10617
10618    GNU Extension:
10619
10620    function-definition:
10621      __extension__ function-definition 
10622
10623    Returns the FUNCTION_DECL for the function.  If FRIEND_P is
10624    non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10625    be a `friend'.  */
10626
10627 static tree
10628 cp_parser_function_definition (cp_parser* parser, bool* friend_p)
10629 {
10630   tree decl_specifiers;
10631   tree attributes;
10632   tree declarator;
10633   tree fn;
10634   cp_token *token;
10635   int declares_class_or_enum;
10636   bool member_p;
10637   /* The saved value of the PEDANTIC flag.  */
10638   int saved_pedantic;
10639
10640   /* Any pending qualification must be cleared by our caller.  It is
10641      more robust to force the callers to clear PARSER->SCOPE than to
10642      do it here since if the qualification is in effect here, it might
10643      also end up in effect elsewhere that it is not intended.  */
10644   my_friendly_assert (!parser->scope, 20010821);
10645
10646   /* Handle `__extension__'.  */
10647   if (cp_parser_extension_opt (parser, &saved_pedantic))
10648     {
10649       /* Parse the function-definition.  */
10650       fn = cp_parser_function_definition (parser, friend_p);
10651       /* Restore the PEDANTIC flag.  */
10652       pedantic = saved_pedantic;
10653
10654       return fn;
10655     }
10656
10657   /* Check to see if this definition appears in a class-specifier.  */
10658   member_p = (at_class_scope_p () 
10659               && TYPE_BEING_DEFINED (current_class_type));
10660   /* Defer access checks in the decl-specifier-seq until we know what
10661      function is being defined.  There is no need to do this for the
10662      definition of member functions; we cannot be defining a member
10663      from another class.  */
10664   push_deferring_access_checks (member_p ? dk_no_check: dk_deferred);
10665
10666   /* Parse the decl-specifier-seq.  */
10667   decl_specifiers 
10668     = cp_parser_decl_specifier_seq (parser,
10669                                     CP_PARSER_FLAGS_OPTIONAL,
10670                                     &attributes,
10671                                     &declares_class_or_enum);
10672   /* Figure out whether this declaration is a `friend'.  */
10673   if (friend_p)
10674     *friend_p = cp_parser_friend_p (decl_specifiers);
10675
10676   /* Parse the declarator.  */
10677   declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10678                                      /*ctor_dtor_or_conv_p=*/NULL);
10679
10680   /* Gather up any access checks that occurred.  */
10681   stop_deferring_access_checks ();
10682
10683   /* If something has already gone wrong, we may as well stop now.  */
10684   if (declarator == error_mark_node)
10685     {
10686       /* Skip to the end of the function, or if this wasn't anything
10687          like a function-definition, to a `;' in the hopes of finding
10688          a sensible place from which to continue parsing.  */
10689       cp_parser_skip_to_end_of_block_or_statement (parser);
10690       pop_deferring_access_checks ();
10691       return error_mark_node;
10692     }
10693
10694   /* The next character should be a `{' (for a simple function
10695      definition), a `:' (for a ctor-initializer), or `try' (for a
10696      function-try block).  */
10697   token = cp_lexer_peek_token (parser->lexer);
10698   if (!cp_parser_token_starts_function_definition_p (token))
10699     {
10700       /* Issue the error-message.  */
10701       cp_parser_error (parser, "expected function-definition");
10702       /* Skip to the next `;'.  */
10703       cp_parser_skip_to_end_of_block_or_statement (parser);
10704
10705       pop_deferring_access_checks ();
10706       return error_mark_node;
10707     }
10708
10709   cp_parser_check_for_definition_in_return_type (declarator,
10710                                                  declares_class_or_enum);
10711
10712   /* If we are in a class scope, then we must handle
10713      function-definitions specially.  In particular, we save away the
10714      tokens that make up the function body, and parse them again
10715      later, in order to handle code like:
10716
10717        struct S {
10718          int f () { return i; }
10719          int i;
10720        }; 
10721  
10722      Here, we cannot parse the body of `f' until after we have seen
10723      the declaration of `i'.  */
10724   if (member_p)
10725     {
10726       cp_token_cache *cache;
10727
10728       /* Create the function-declaration.  */
10729       fn = start_method (decl_specifiers, declarator, attributes);
10730       /* If something went badly wrong, bail out now.  */
10731       if (fn == error_mark_node)
10732         {
10733           /* If there's a function-body, skip it.  */
10734           if (cp_parser_token_starts_function_definition_p 
10735               (cp_lexer_peek_token (parser->lexer)))
10736             cp_parser_skip_to_end_of_block_or_statement (parser);
10737           pop_deferring_access_checks ();
10738           return error_mark_node;
10739         }
10740
10741       /* Remember it, if there default args to post process.  */
10742       cp_parser_save_default_args (parser, fn);
10743       
10744       /* Create a token cache.  */
10745       cache = cp_token_cache_new ();
10746       /* Save away the tokens that make up the body of the 
10747          function.  */
10748       cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10749       /* Handle function try blocks.  */
10750       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
10751         cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10752
10753       /* Save away the inline definition; we will process it when the
10754          class is complete.  */
10755       DECL_PENDING_INLINE_INFO (fn) = cache;
10756       DECL_PENDING_INLINE_P (fn) = 1;
10757
10758       /* We need to know that this was defined in the class, so that
10759          friend templates are handled correctly.  */
10760       DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
10761
10762       /* We're done with the inline definition.  */
10763       finish_method (fn);
10764
10765       /* Add FN to the queue of functions to be parsed later.  */
10766       TREE_VALUE (parser->unparsed_functions_queues)
10767         = tree_cons (NULL_TREE, fn, 
10768                      TREE_VALUE (parser->unparsed_functions_queues));
10769
10770       pop_deferring_access_checks ();
10771       return fn;
10772     }
10773
10774   /* Check that the number of template-parameter-lists is OK.  */
10775   if (!cp_parser_check_declarator_template_parameters (parser, 
10776                                                        declarator))
10777     {
10778       cp_parser_skip_to_end_of_block_or_statement (parser);
10779       pop_deferring_access_checks ();
10780       return error_mark_node;
10781     }
10782
10783   fn = cp_parser_function_definition_from_specifiers_and_declarator
10784           (parser, decl_specifiers, attributes, declarator);
10785   pop_deferring_access_checks ();
10786   return fn;
10787 }
10788
10789 /* Parse a function-body.
10790
10791    function-body:
10792      compound_statement  */
10793
10794 static void
10795 cp_parser_function_body (cp_parser *parser)
10796 {
10797   cp_parser_compound_statement (parser, false);
10798 }
10799
10800 /* Parse a ctor-initializer-opt followed by a function-body.  Return
10801    true if a ctor-initializer was present.  */
10802
10803 static bool
10804 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
10805 {
10806   tree body;
10807   bool ctor_initializer_p;
10808
10809   /* Begin the function body.  */
10810   body = begin_function_body ();
10811   /* Parse the optional ctor-initializer.  */
10812   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
10813   /* Parse the function-body.  */
10814   cp_parser_function_body (parser);
10815   /* Finish the function body.  */
10816   finish_function_body (body);
10817
10818   return ctor_initializer_p;
10819 }
10820
10821 /* Parse an initializer.
10822
10823    initializer:
10824      = initializer-clause
10825      ( expression-list )  
10826
10827    Returns a expression representing the initializer.  If no
10828    initializer is present, NULL_TREE is returned.  
10829
10830    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
10831    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
10832    set to FALSE if there is no initializer present.  If there is an
10833    initializer, and it is not a constant-expression, *NON_CONSTANT_P
10834    is set to true; otherwise it is set to false.  */
10835
10836 static tree
10837 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
10838                        bool* non_constant_p)
10839 {
10840   cp_token *token;
10841   tree init;
10842
10843   /* Peek at the next token.  */
10844   token = cp_lexer_peek_token (parser->lexer);
10845
10846   /* Let our caller know whether or not this initializer was
10847      parenthesized.  */
10848   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
10849   /* Assume that the initializer is constant.  */
10850   *non_constant_p = false;
10851
10852   if (token->type == CPP_EQ)
10853     {
10854       /* Consume the `='.  */
10855       cp_lexer_consume_token (parser->lexer);
10856       /* Parse the initializer-clause.  */
10857       init = cp_parser_initializer_clause (parser, non_constant_p);
10858     }
10859   else if (token->type == CPP_OPEN_PAREN)
10860     init = cp_parser_parenthesized_expression_list (parser, false,
10861                                                     non_constant_p);
10862   else
10863     {
10864       /* Anything else is an error.  */
10865       cp_parser_error (parser, "expected initializer");
10866       init = error_mark_node;
10867     }
10868
10869   return init;
10870 }
10871
10872 /* Parse an initializer-clause.  
10873
10874    initializer-clause:
10875      assignment-expression
10876      { initializer-list , [opt] }
10877      { }
10878
10879    Returns an expression representing the initializer.  
10880
10881    If the `assignment-expression' production is used the value
10882    returned is simply a representation for the expression.  
10883
10884    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
10885    the elements of the initializer-list (or NULL_TREE, if the last
10886    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
10887    NULL_TREE.  There is no way to detect whether or not the optional
10888    trailing `,' was provided.  NON_CONSTANT_P is as for
10889    cp_parser_initializer.  */
10890
10891 static tree
10892 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
10893 {
10894   tree initializer;
10895
10896   /* If it is not a `{', then we are looking at an
10897      assignment-expression.  */
10898   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10899     initializer 
10900       = cp_parser_constant_expression (parser,
10901                                        /*allow_non_constant_p=*/true,
10902                                        non_constant_p);
10903   else
10904     {
10905       /* Consume the `{' token.  */
10906       cp_lexer_consume_token (parser->lexer);
10907       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
10908       initializer = make_node (CONSTRUCTOR);
10909       /* Mark it with TREE_HAS_CONSTRUCTOR.  This should not be
10910          necessary, but check_initializer depends upon it, for 
10911          now.  */
10912       TREE_HAS_CONSTRUCTOR (initializer) = 1;
10913       /* If it's not a `}', then there is a non-trivial initializer.  */
10914       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10915         {
10916           /* Parse the initializer list.  */
10917           CONSTRUCTOR_ELTS (initializer)
10918             = cp_parser_initializer_list (parser, non_constant_p);
10919           /* A trailing `,' token is allowed.  */
10920           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10921             cp_lexer_consume_token (parser->lexer);
10922         }
10923       /* Now, there should be a trailing `}'.  */
10924       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10925     }
10926
10927   return initializer;
10928 }
10929
10930 /* Parse an initializer-list.
10931
10932    initializer-list:
10933      initializer-clause
10934      initializer-list , initializer-clause
10935
10936    GNU Extension:
10937    
10938    initializer-list:
10939      identifier : initializer-clause
10940      initializer-list, identifier : initializer-clause
10941
10942    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
10943    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
10944    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
10945    as for cp_parser_initializer.  */
10946
10947 static tree
10948 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
10949 {
10950   tree initializers = NULL_TREE;
10951
10952   /* Assume all of the expressions are constant.  */
10953   *non_constant_p = false;
10954
10955   /* Parse the rest of the list.  */
10956   while (true)
10957     {
10958       cp_token *token;
10959       tree identifier;
10960       tree initializer;
10961       bool clause_non_constant_p;
10962
10963       /* If the next token is an identifier and the following one is a
10964          colon, we are looking at the GNU designated-initializer
10965          syntax.  */
10966       if (cp_parser_allow_gnu_extensions_p (parser)
10967           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10968           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
10969         {
10970           /* Consume the identifier.  */
10971           identifier = cp_lexer_consume_token (parser->lexer)->value;
10972           /* Consume the `:'.  */
10973           cp_lexer_consume_token (parser->lexer);
10974         }
10975       else
10976         identifier = NULL_TREE;
10977
10978       /* Parse the initializer.  */
10979       initializer = cp_parser_initializer_clause (parser, 
10980                                                   &clause_non_constant_p);
10981       /* If any clause is non-constant, so is the entire initializer.  */
10982       if (clause_non_constant_p)
10983         *non_constant_p = true;
10984       /* Add it to the list.  */
10985       initializers = tree_cons (identifier, initializer, initializers);
10986
10987       /* If the next token is not a comma, we have reached the end of
10988          the list.  */
10989       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10990         break;
10991
10992       /* Peek at the next token.  */
10993       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10994       /* If the next token is a `}', then we're still done.  An
10995          initializer-clause can have a trailing `,' after the
10996          initializer-list and before the closing `}'.  */
10997       if (token->type == CPP_CLOSE_BRACE)
10998         break;
10999
11000       /* Consume the `,' token.  */
11001       cp_lexer_consume_token (parser->lexer);
11002     }
11003
11004   /* The initializers were built up in reverse order, so we need to
11005      reverse them now.  */
11006   return nreverse (initializers);
11007 }
11008
11009 /* Classes [gram.class] */
11010
11011 /* Parse a class-name.
11012
11013    class-name:
11014      identifier
11015      template-id
11016
11017    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11018    to indicate that names looked up in dependent types should be
11019    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
11020    keyword has been used to indicate that the name that appears next
11021    is a template.  TYPE_P is true iff the next name should be treated
11022    as class-name, even if it is declared to be some other kind of name
11023    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11024    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
11025    being defined in a class-head.
11026
11027    Returns the TYPE_DECL representing the class.  */
11028
11029 static tree
11030 cp_parser_class_name (cp_parser *parser, 
11031                       bool typename_keyword_p, 
11032                       bool template_keyword_p, 
11033                       bool type_p,
11034                       bool check_dependency_p,
11035                       bool class_head_p)
11036 {
11037   tree decl;
11038   tree scope;
11039   bool typename_p;
11040   cp_token *token;
11041
11042   /* All class-names start with an identifier.  */
11043   token = cp_lexer_peek_token (parser->lexer);
11044   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11045     {
11046       cp_parser_error (parser, "expected class-name");
11047       return error_mark_node;
11048     }
11049     
11050   /* PARSER->SCOPE can be cleared when parsing the template-arguments
11051      to a template-id, so we save it here.  */
11052   scope = parser->scope;
11053   if (scope == error_mark_node)
11054     return error_mark_node;
11055   
11056   /* Any name names a type if we're following the `typename' keyword
11057      in a qualified name where the enclosing scope is type-dependent.  */
11058   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11059                 && dependent_type_p (scope));
11060   /* Handle the common case (an identifier, but not a template-id)
11061      efficiently.  */
11062   if (token->type == CPP_NAME 
11063       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11064     {
11065       tree identifier;
11066
11067       /* Look for the identifier.  */
11068       identifier = cp_parser_identifier (parser);
11069       /* If the next token isn't an identifier, we are certainly not
11070          looking at a class-name.  */
11071       if (identifier == error_mark_node)
11072         decl = error_mark_node;
11073       /* If we know this is a type-name, there's no need to look it
11074          up.  */
11075       else if (typename_p)
11076         decl = identifier;
11077       else
11078         {
11079           /* If the next token is a `::', then the name must be a type
11080              name.
11081
11082              [basic.lookup.qual]
11083
11084              During the lookup for a name preceding the :: scope
11085              resolution operator, object, function, and enumerator
11086              names are ignored.  */
11087           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11088             type_p = true;
11089           /* Look up the name.  */
11090           decl = cp_parser_lookup_name (parser, identifier, 
11091                                         type_p,
11092                                         /*is_namespace=*/false,
11093                                         check_dependency_p);
11094         }
11095     }
11096   else
11097     {
11098       /* Try a template-id.  */
11099       decl = cp_parser_template_id (parser, template_keyword_p,
11100                                     check_dependency_p);
11101       if (decl == error_mark_node)
11102         return error_mark_node;
11103     }
11104
11105   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11106
11107   /* If this is a typename, create a TYPENAME_TYPE.  */
11108   if (typename_p && decl != error_mark_node)
11109     decl = TYPE_NAME (make_typename_type (scope, decl,
11110                                           /*complain=*/1));
11111
11112   /* Check to see that it is really the name of a class.  */
11113   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 
11114       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11115       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11116     /* Situations like this:
11117
11118          template <typename T> struct A {
11119            typename T::template X<int>::I i; 
11120          };
11121
11122        are problematic.  Is `T::template X<int>' a class-name?  The
11123        standard does not seem to be definitive, but there is no other
11124        valid interpretation of the following `::'.  Therefore, those
11125        names are considered class-names.  */
11126     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11127   else if (decl == error_mark_node
11128            || TREE_CODE (decl) != TYPE_DECL
11129            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11130     {
11131       cp_parser_error (parser, "expected class-name");
11132       return error_mark_node;
11133     }
11134
11135   return decl;
11136 }
11137
11138 /* Parse a class-specifier.
11139
11140    class-specifier:
11141      class-head { member-specification [opt] }
11142
11143    Returns the TREE_TYPE representing the class.  */
11144
11145 static tree
11146 cp_parser_class_specifier (cp_parser* parser)
11147 {
11148   cp_token *token;
11149   tree type;
11150   tree attributes = NULL_TREE;
11151   int has_trailing_semicolon;
11152   bool nested_name_specifier_p;
11153   unsigned saved_num_template_parameter_lists;
11154
11155   push_deferring_access_checks (dk_no_deferred);
11156
11157   /* Parse the class-head.  */
11158   type = cp_parser_class_head (parser,
11159                                &nested_name_specifier_p);
11160   /* If the class-head was a semantic disaster, skip the entire body
11161      of the class.  */
11162   if (!type)
11163     {
11164       cp_parser_skip_to_end_of_block_or_statement (parser);
11165       pop_deferring_access_checks ();
11166       return error_mark_node;
11167     }
11168
11169   /* Look for the `{'.  */
11170   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11171     {
11172       pop_deferring_access_checks ();
11173       return error_mark_node;
11174     }
11175
11176   /* Issue an error message if type-definitions are forbidden here.  */
11177   cp_parser_check_type_definition (parser);
11178   /* Remember that we are defining one more class.  */
11179   ++parser->num_classes_being_defined;
11180   /* Inside the class, surrounding template-parameter-lists do not
11181      apply.  */
11182   saved_num_template_parameter_lists 
11183     = parser->num_template_parameter_lists; 
11184   parser->num_template_parameter_lists = 0;
11185
11186   /* Start the class.  */
11187   type = begin_class_definition (type);
11188   if (type == error_mark_node)
11189     /* If the type is erroneous, skip the entire body of the class.  */
11190     cp_parser_skip_to_closing_brace (parser);
11191   else
11192     /* Parse the member-specification.  */
11193     cp_parser_member_specification_opt (parser);
11194   /* Look for the trailing `}'.  */
11195   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11196   /* We get better error messages by noticing a common problem: a
11197      missing trailing `;'.  */
11198   token = cp_lexer_peek_token (parser->lexer);
11199   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11200   /* Look for attributes to apply to this class.  */
11201   if (cp_parser_allow_gnu_extensions_p (parser))
11202     attributes = cp_parser_attributes_opt (parser);
11203   /* If we got any attributes in class_head, xref_tag will stick them in
11204      TREE_TYPE of the type.  Grab them now.  */
11205   if (type != error_mark_node)
11206     {
11207       attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11208       TYPE_ATTRIBUTES (type) = NULL_TREE;
11209       type = finish_struct (type, attributes);
11210     }
11211   if (nested_name_specifier_p)
11212     pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11213   /* If this class is not itself within the scope of another class,
11214      then we need to parse the bodies of all of the queued function
11215      definitions.  Note that the queued functions defined in a class
11216      are not always processed immediately following the
11217      class-specifier for that class.  Consider:
11218
11219        struct A {
11220          struct B { void f() { sizeof (A); } };
11221        };
11222
11223      If `f' were processed before the processing of `A' were
11224      completed, there would be no way to compute the size of `A'.
11225      Note that the nesting we are interested in here is lexical --
11226      not the semantic nesting given by TYPE_CONTEXT.  In particular,
11227      for:
11228
11229        struct A { struct B; };
11230        struct A::B { void f() { } };
11231
11232      there is no need to delay the parsing of `A::B::f'.  */
11233   if (--parser->num_classes_being_defined == 0) 
11234     {
11235       tree queue_entry;
11236       tree fn;
11237
11238       /* In a first pass, parse default arguments to the functions.
11239          Then, in a second pass, parse the bodies of the functions.
11240          This two-phased approach handles cases like:
11241          
11242             struct S { 
11243               void f() { g(); } 
11244               void g(int i = 3);
11245             };
11246
11247          */
11248       for (TREE_PURPOSE (parser->unparsed_functions_queues)
11249              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11250            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11251            TREE_PURPOSE (parser->unparsed_functions_queues)
11252              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11253         {
11254           fn = TREE_VALUE (queue_entry);
11255           /* Make sure that any template parameters are in scope.  */
11256           maybe_begin_member_template_processing (fn);
11257           /* If there are default arguments that have not yet been processed,
11258              take care of them now.  */
11259           cp_parser_late_parsing_default_args (parser, fn);
11260           /* Remove any template parameters from the symbol table.  */
11261           maybe_end_member_template_processing ();
11262         }
11263       /* Now parse the body of the functions.  */
11264       for (TREE_VALUE (parser->unparsed_functions_queues)
11265              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11266            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11267            TREE_VALUE (parser->unparsed_functions_queues)
11268              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11269         {
11270           /* Figure out which function we need to process.  */
11271           fn = TREE_VALUE (queue_entry);
11272
11273           /* Parse the function.  */
11274           cp_parser_late_parsing_for_member (parser, fn);
11275         }
11276
11277     }
11278
11279   /* Put back any saved access checks.  */
11280   pop_deferring_access_checks ();
11281
11282   /* Restore the count of active template-parameter-lists.  */
11283   parser->num_template_parameter_lists
11284     = saved_num_template_parameter_lists;
11285
11286   return type;
11287 }
11288
11289 /* Parse a class-head.
11290
11291    class-head:
11292      class-key identifier [opt] base-clause [opt]
11293      class-key nested-name-specifier identifier base-clause [opt]
11294      class-key nested-name-specifier [opt] template-id 
11295        base-clause [opt]  
11296
11297    GNU Extensions:
11298      class-key attributes identifier [opt] base-clause [opt]
11299      class-key attributes nested-name-specifier identifier base-clause [opt]
11300      class-key attributes nested-name-specifier [opt] template-id 
11301        base-clause [opt]  
11302
11303    Returns the TYPE of the indicated class.  Sets
11304    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11305    involving a nested-name-specifier was used, and FALSE otherwise.
11306
11307    Returns NULL_TREE if the class-head is syntactically valid, but
11308    semantically invalid in a way that means we should skip the entire
11309    body of the class.  */
11310
11311 static tree
11312 cp_parser_class_head (cp_parser* parser, 
11313                       bool* nested_name_specifier_p)
11314 {
11315   cp_token *token;
11316   tree nested_name_specifier;
11317   enum tag_types class_key;
11318   tree id = NULL_TREE;
11319   tree type = NULL_TREE;
11320   tree attributes;
11321   bool template_id_p = false;
11322   bool qualified_p = false;
11323   bool invalid_nested_name_p = false;
11324   unsigned num_templates;
11325
11326   /* Assume no nested-name-specifier will be present.  */
11327   *nested_name_specifier_p = false;
11328   /* Assume no template parameter lists will be used in defining the
11329      type.  */
11330   num_templates = 0;
11331
11332   /* Look for the class-key.  */
11333   class_key = cp_parser_class_key (parser);
11334   if (class_key == none_type)
11335     return error_mark_node;
11336
11337   /* Parse the attributes.  */
11338   attributes = cp_parser_attributes_opt (parser);
11339
11340   /* If the next token is `::', that is invalid -- but sometimes
11341      people do try to write:
11342
11343        struct ::S {};  
11344
11345      Handle this gracefully by accepting the extra qualifier, and then
11346      issuing an error about it later if this really is a
11347      class-head.  If it turns out just to be an elaborated type
11348      specifier, remain silent.  */
11349   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11350     qualified_p = true;
11351
11352   push_deferring_access_checks (dk_no_check);
11353
11354   /* Determine the name of the class.  Begin by looking for an
11355      optional nested-name-specifier.  */
11356   nested_name_specifier 
11357     = cp_parser_nested_name_specifier_opt (parser,
11358                                            /*typename_keyword_p=*/false,
11359                                            /*check_dependency_p=*/false,
11360                                            /*type_p=*/false);
11361   /* If there was a nested-name-specifier, then there *must* be an
11362      identifier.  */
11363   if (nested_name_specifier)
11364     {
11365       /* Although the grammar says `identifier', it really means
11366          `class-name' or `template-name'.  You are only allowed to
11367          define a class that has already been declared with this
11368          syntax.  
11369
11370          The proposed resolution for Core Issue 180 says that whever
11371          you see `class T::X' you should treat `X' as a type-name.
11372          
11373          It is OK to define an inaccessible class; for example:
11374          
11375            class A { class B; };
11376            class A::B {};
11377          
11378          We do not know if we will see a class-name, or a
11379          template-name.  We look for a class-name first, in case the
11380          class-name is a template-id; if we looked for the
11381          template-name first we would stop after the template-name.  */
11382       cp_parser_parse_tentatively (parser);
11383       type = cp_parser_class_name (parser,
11384                                    /*typename_keyword_p=*/false,
11385                                    /*template_keyword_p=*/false,
11386                                    /*type_p=*/true,
11387                                    /*check_dependency_p=*/false,
11388                                    /*class_head_p=*/true);
11389       /* If that didn't work, ignore the nested-name-specifier.  */
11390       if (!cp_parser_parse_definitely (parser))
11391         {
11392           invalid_nested_name_p = true;
11393           id = cp_parser_identifier (parser);
11394           if (id == error_mark_node)
11395             id = NULL_TREE;
11396         }
11397       /* If we could not find a corresponding TYPE, treat this
11398          declaration like an unqualified declaration.  */
11399       if (type == error_mark_node)
11400         nested_name_specifier = NULL_TREE;
11401       /* Otherwise, count the number of templates used in TYPE and its
11402          containing scopes.  */
11403       else 
11404         {
11405           tree scope;
11406
11407           for (scope = TREE_TYPE (type); 
11408                scope && TREE_CODE (scope) != NAMESPACE_DECL;
11409                scope = (TYPE_P (scope) 
11410                         ? TYPE_CONTEXT (scope)
11411                         : DECL_CONTEXT (scope))) 
11412             if (TYPE_P (scope) 
11413                 && CLASS_TYPE_P (scope)
11414                 && CLASSTYPE_TEMPLATE_INFO (scope)
11415                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11416                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11417               ++num_templates;
11418         }
11419     }
11420   /* Otherwise, the identifier is optional.  */
11421   else
11422     {
11423       /* We don't know whether what comes next is a template-id,
11424          an identifier, or nothing at all.  */
11425       cp_parser_parse_tentatively (parser);
11426       /* Check for a template-id.  */
11427       id = cp_parser_template_id (parser, 
11428                                   /*template_keyword_p=*/false,
11429                                   /*check_dependency_p=*/true);
11430       /* If that didn't work, it could still be an identifier.  */
11431       if (!cp_parser_parse_definitely (parser))
11432         {
11433           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11434             id = cp_parser_identifier (parser);
11435           else
11436             id = NULL_TREE;
11437         }
11438       else
11439         {
11440           template_id_p = true;
11441           ++num_templates;
11442         }
11443     }
11444
11445   pop_deferring_access_checks ();
11446
11447   /* If it's not a `:' or a `{' then we can't really be looking at a
11448      class-head, since a class-head only appears as part of a
11449      class-specifier.  We have to detect this situation before calling
11450      xref_tag, since that has irreversible side-effects.  */
11451   if (!cp_parser_next_token_starts_class_definition_p (parser))
11452     {
11453       cp_parser_error (parser, "expected `{' or `:'");
11454       return error_mark_node;
11455     }
11456
11457   /* At this point, we're going ahead with the class-specifier, even
11458      if some other problem occurs.  */
11459   cp_parser_commit_to_tentative_parse (parser);
11460   /* Issue the error about the overly-qualified name now.  */
11461   if (qualified_p)
11462     cp_parser_error (parser,
11463                      "global qualification of class name is invalid");
11464   else if (invalid_nested_name_p)
11465     cp_parser_error (parser,
11466                      "qualified name does not name a class");
11467   /* Make sure that the right number of template parameters were
11468      present.  */
11469   if (!cp_parser_check_template_parameters (parser, num_templates))
11470     /* If something went wrong, there is no point in even trying to
11471        process the class-definition.  */
11472     return NULL_TREE;
11473
11474   /* Look up the type.  */
11475   if (template_id_p)
11476     {
11477       type = TREE_TYPE (id);
11478       maybe_process_partial_specialization (type);
11479     }
11480   else if (!nested_name_specifier)
11481     {
11482       /* If the class was unnamed, create a dummy name.  */
11483       if (!id)
11484         id = make_anon_name ();
11485       type = xref_tag (class_key, id, attributes, /*globalize=*/false,
11486                        parser->num_template_parameter_lists);
11487     }
11488   else
11489     {
11490       tree class_type;
11491       tree scope;
11492
11493       /* Given:
11494
11495             template <typename T> struct S { struct T };
11496             template <typename T> struct S<T>::T { };
11497
11498          we will get a TYPENAME_TYPE when processing the definition of
11499          `S::T'.  We need to resolve it to the actual type before we
11500          try to define it.  */
11501       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11502         {
11503           class_type = resolve_typename_type (TREE_TYPE (type),
11504                                               /*only_current_p=*/false);
11505           if (class_type != error_mark_node)
11506             type = TYPE_NAME (class_type);
11507           else
11508             {
11509               cp_parser_error (parser, "could not resolve typename type");
11510               type = error_mark_node;
11511             }
11512         }
11513
11514       /* Figure out in what scope the declaration is being placed.  */
11515       scope = current_scope ();
11516       if (!scope)
11517         scope = current_namespace;
11518       /* If that scope does not contain the scope in which the
11519          class was originally declared, the program is invalid.  */
11520       if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11521         {
11522           error ("declaration of `%D' in `%D' which does not "
11523                  "enclose `%D'", type, scope, nested_name_specifier);
11524           return NULL_TREE;
11525         }
11526       /* [dcl.meaning]
11527
11528          A declarator-id shall not be qualified exception of the
11529          definition of a ... nested class outside of its class
11530          ... [or] a the definition or explicit instantiation of a
11531          class member of a namespace outside of its namespace.  */
11532       if (scope == CP_DECL_CONTEXT (type))
11533         {
11534           pedwarn ("extra qualification ignored");
11535           nested_name_specifier = NULL_TREE;
11536         }
11537
11538       maybe_process_partial_specialization (TREE_TYPE (type));
11539       class_type = current_class_type;
11540       /* Enter the scope indicated by the nested-name-specifier.  */
11541       if (nested_name_specifier)
11542         push_scope (nested_name_specifier);
11543       /* Get the canonical version of this type.  */
11544       type = TYPE_MAIN_DECL (TREE_TYPE (type));
11545       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
11546           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
11547         type = push_template_decl (type);
11548       type = TREE_TYPE (type);
11549       if (nested_name_specifier)
11550         *nested_name_specifier_p = true;
11551     }
11552   /* Indicate whether this class was declared as a `class' or as a
11553      `struct'.  */
11554   if (TREE_CODE (type) == RECORD_TYPE)
11555     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11556   cp_parser_check_class_key (class_key, type);
11557
11558   /* Enter the scope containing the class; the names of base classes
11559      should be looked up in that context.  For example, given:
11560
11561        struct A { struct B {}; struct C; };
11562        struct A::C : B {};
11563
11564      is valid.  */
11565   if (nested_name_specifier)
11566     push_scope (nested_name_specifier);
11567   /* Now, look for the base-clause.  */
11568   token = cp_lexer_peek_token (parser->lexer);
11569   if (token->type == CPP_COLON)
11570     {
11571       tree bases;
11572
11573       /* Get the list of base-classes.  */
11574       bases = cp_parser_base_clause (parser);
11575       /* Process them.  */
11576       xref_basetypes (type, bases);
11577     }
11578   /* Leave the scope given by the nested-name-specifier.  We will
11579      enter the class scope itself while processing the members.  */
11580   if (nested_name_specifier)
11581     pop_scope (nested_name_specifier);
11582
11583   return type;
11584 }
11585
11586 /* Parse a class-key.
11587
11588    class-key:
11589      class
11590      struct
11591      union
11592
11593    Returns the kind of class-key specified, or none_type to indicate
11594    error.  */
11595
11596 static enum tag_types
11597 cp_parser_class_key (cp_parser* parser)
11598 {
11599   cp_token *token;
11600   enum tag_types tag_type;
11601
11602   /* Look for the class-key.  */
11603   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11604   if (!token)
11605     return none_type;
11606
11607   /* Check to see if the TOKEN is a class-key.  */
11608   tag_type = cp_parser_token_is_class_key (token);
11609   if (!tag_type)
11610     cp_parser_error (parser, "expected class-key");
11611   return tag_type;
11612 }
11613
11614 /* Parse an (optional) member-specification.
11615
11616    member-specification:
11617      member-declaration member-specification [opt]
11618      access-specifier : member-specification [opt]  */
11619
11620 static void
11621 cp_parser_member_specification_opt (cp_parser* parser)
11622 {
11623   while (true)
11624     {
11625       cp_token *token;
11626       enum rid keyword;
11627
11628       /* Peek at the next token.  */
11629       token = cp_lexer_peek_token (parser->lexer);
11630       /* If it's a `}', or EOF then we've seen all the members.  */
11631       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11632         break;
11633
11634       /* See if this token is a keyword.  */
11635       keyword = token->keyword;
11636       switch (keyword)
11637         {
11638         case RID_PUBLIC:
11639         case RID_PROTECTED:
11640         case RID_PRIVATE:
11641           /* Consume the access-specifier.  */
11642           cp_lexer_consume_token (parser->lexer);
11643           /* Remember which access-specifier is active.  */
11644           current_access_specifier = token->value;
11645           /* Look for the `:'.  */
11646           cp_parser_require (parser, CPP_COLON, "`:'");
11647           break;
11648
11649         default:
11650           /* Otherwise, the next construction must be a
11651              member-declaration.  */
11652           cp_parser_member_declaration (parser);
11653         }
11654     }
11655 }
11656
11657 /* Parse a member-declaration.  
11658
11659    member-declaration:
11660      decl-specifier-seq [opt] member-declarator-list [opt] ;
11661      function-definition ; [opt]
11662      :: [opt] nested-name-specifier template [opt] unqualified-id ;
11663      using-declaration
11664      template-declaration 
11665
11666    member-declarator-list:
11667      member-declarator
11668      member-declarator-list , member-declarator
11669
11670    member-declarator:
11671      declarator pure-specifier [opt] 
11672      declarator constant-initializer [opt]
11673      identifier [opt] : constant-expression 
11674
11675    GNU Extensions:
11676
11677    member-declaration:
11678      __extension__ member-declaration
11679
11680    member-declarator:
11681      declarator attributes [opt] pure-specifier [opt]
11682      declarator attributes [opt] constant-initializer [opt]
11683      identifier [opt] attributes [opt] : constant-expression  */
11684
11685 static void
11686 cp_parser_member_declaration (cp_parser* parser)
11687 {
11688   tree decl_specifiers;
11689   tree prefix_attributes;
11690   tree decl;
11691   int declares_class_or_enum;
11692   bool friend_p;
11693   cp_token *token;
11694   int saved_pedantic;
11695
11696   /* Check for the `__extension__' keyword.  */
11697   if (cp_parser_extension_opt (parser, &saved_pedantic))
11698     {
11699       /* Recurse.  */
11700       cp_parser_member_declaration (parser);
11701       /* Restore the old value of the PEDANTIC flag.  */
11702       pedantic = saved_pedantic;
11703
11704       return;
11705     }
11706
11707   /* Check for a template-declaration.  */
11708   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11709     {
11710       /* Parse the template-declaration.  */
11711       cp_parser_template_declaration (parser, /*member_p=*/true);
11712
11713       return;
11714     }
11715
11716   /* Check for a using-declaration.  */
11717   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11718     {
11719       /* Parse the using-declaration.  */
11720       cp_parser_using_declaration (parser);
11721
11722       return;
11723     }
11724   
11725   /* We can't tell whether we're looking at a declaration or a
11726      function-definition.  */
11727   cp_parser_parse_tentatively (parser);
11728
11729   /* Parse the decl-specifier-seq.  */
11730   decl_specifiers 
11731     = cp_parser_decl_specifier_seq (parser,
11732                                     CP_PARSER_FLAGS_OPTIONAL,
11733                                     &prefix_attributes,
11734                                     &declares_class_or_enum);
11735   /* Check for an invalid type-name.  */
11736   if (cp_parser_diagnose_invalid_type_name (parser))
11737     return;
11738   /* If there is no declarator, then the decl-specifier-seq should
11739      specify a type.  */
11740   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11741     {
11742       /* If there was no decl-specifier-seq, and the next token is a
11743          `;', then we have something like:
11744
11745            struct S { ; };
11746
11747          [class.mem]
11748
11749          Each member-declaration shall declare at least one member
11750          name of the class.  */
11751       if (!decl_specifiers)
11752         {
11753           if (pedantic)
11754             pedwarn ("extra semicolon");
11755         }
11756       else 
11757         {
11758           tree type;
11759           
11760           /* See if this declaration is a friend.  */
11761           friend_p = cp_parser_friend_p (decl_specifiers);
11762           /* If there were decl-specifiers, check to see if there was
11763              a class-declaration.  */
11764           type = check_tag_decl (decl_specifiers);
11765           /* Nested classes have already been added to the class, but
11766              a `friend' needs to be explicitly registered.  */
11767           if (friend_p)
11768             {
11769               /* If the `friend' keyword was present, the friend must
11770                  be introduced with a class-key.  */
11771                if (!declares_class_or_enum)
11772                  error ("a class-key must be used when declaring a friend");
11773                /* In this case:
11774
11775                     template <typename T> struct A { 
11776                       friend struct A<T>::B; 
11777                     };
11778  
11779                   A<T>::B will be represented by a TYPENAME_TYPE, and
11780                   therefore not recognized by check_tag_decl.  */
11781                if (!type)
11782                  {
11783                    tree specifier;
11784
11785                    for (specifier = decl_specifiers; 
11786                         specifier;
11787                         specifier = TREE_CHAIN (specifier))
11788                      {
11789                        tree s = TREE_VALUE (specifier);
11790
11791                        if (TREE_CODE (s) == IDENTIFIER_NODE
11792                            && IDENTIFIER_GLOBAL_VALUE (s))
11793                          type = IDENTIFIER_GLOBAL_VALUE (s);
11794                        if (TREE_CODE (s) == TYPE_DECL)
11795                          s = TREE_TYPE (s);
11796                        if (TYPE_P (s))
11797                          {
11798                            type = s;
11799                            break;
11800                          }
11801                      }
11802                  }
11803                if (!type)
11804                  error ("friend declaration does not name a class or "
11805                         "function");
11806                else
11807                  make_friend_class (current_class_type, type);
11808             }
11809           /* If there is no TYPE, an error message will already have
11810              been issued.  */
11811           else if (!type)
11812             ;
11813           /* An anonymous aggregate has to be handled specially; such
11814              a declaration really declares a data member (with a
11815              particular type), as opposed to a nested class.  */
11816           else if (ANON_AGGR_TYPE_P (type))
11817             {
11818               /* Remove constructors and such from TYPE, now that we
11819                  know it is an anonymous aggregate.  */
11820               fixup_anonymous_aggr (type);
11821               /* And make the corresponding data member.  */
11822               decl = build_decl (FIELD_DECL, NULL_TREE, type);
11823               /* Add it to the class.  */
11824               finish_member_declaration (decl);
11825             }
11826         }
11827     }
11828   else
11829     {
11830       /* See if these declarations will be friends.  */
11831       friend_p = cp_parser_friend_p (decl_specifiers);
11832
11833       /* Keep going until we hit the `;' at the end of the 
11834          declaration.  */
11835       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11836         {
11837           tree attributes = NULL_TREE;
11838           tree first_attribute;
11839
11840           /* Peek at the next token.  */
11841           token = cp_lexer_peek_token (parser->lexer);
11842
11843           /* Check for a bitfield declaration.  */
11844           if (token->type == CPP_COLON
11845               || (token->type == CPP_NAME
11846                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type 
11847                   == CPP_COLON))
11848             {
11849               tree identifier;
11850               tree width;
11851
11852               /* Get the name of the bitfield.  Note that we cannot just
11853                  check TOKEN here because it may have been invalidated by
11854                  the call to cp_lexer_peek_nth_token above.  */
11855               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
11856                 identifier = cp_parser_identifier (parser);
11857               else
11858                 identifier = NULL_TREE;
11859
11860               /* Consume the `:' token.  */
11861               cp_lexer_consume_token (parser->lexer);
11862               /* Get the width of the bitfield.  */
11863               width 
11864                 = cp_parser_constant_expression (parser,
11865                                                  /*allow_non_constant=*/false,
11866                                                  NULL);
11867
11868               /* Look for attributes that apply to the bitfield.  */
11869               attributes = cp_parser_attributes_opt (parser);
11870               /* Remember which attributes are prefix attributes and
11871                  which are not.  */
11872               first_attribute = attributes;
11873               /* Combine the attributes.  */
11874               attributes = chainon (prefix_attributes, attributes);
11875
11876               /* Create the bitfield declaration.  */
11877               decl = grokbitfield (identifier, 
11878                                    decl_specifiers,
11879                                    width);
11880               /* Apply the attributes.  */
11881               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
11882             }
11883           else
11884             {
11885               tree declarator;
11886               tree initializer;
11887               tree asm_specification;
11888               int ctor_dtor_or_conv_p;
11889
11890               /* Parse the declarator.  */
11891               declarator 
11892                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11893                                         &ctor_dtor_or_conv_p);
11894
11895               /* If something went wrong parsing the declarator, make sure
11896                  that we at least consume some tokens.  */
11897               if (declarator == error_mark_node)
11898                 {
11899                   /* Skip to the end of the statement.  */
11900                   cp_parser_skip_to_end_of_statement (parser);
11901                   break;
11902                 }
11903
11904               cp_parser_check_for_definition_in_return_type 
11905                 (declarator, declares_class_or_enum);
11906
11907               /* Look for an asm-specification.  */
11908               asm_specification = cp_parser_asm_specification_opt (parser);
11909               /* Look for attributes that apply to the declaration.  */
11910               attributes = cp_parser_attributes_opt (parser);
11911               /* Remember which attributes are prefix attributes and
11912                  which are not.  */
11913               first_attribute = attributes;
11914               /* Combine the attributes.  */
11915               attributes = chainon (prefix_attributes, attributes);
11916
11917               /* If it's an `=', then we have a constant-initializer or a
11918                  pure-specifier.  It is not correct to parse the
11919                  initializer before registering the member declaration
11920                  since the member declaration should be in scope while
11921                  its initializer is processed.  However, the rest of the
11922                  front end does not yet provide an interface that allows
11923                  us to handle this correctly.  */
11924               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11925                 {
11926                   /* In [class.mem]:
11927
11928                      A pure-specifier shall be used only in the declaration of
11929                      a virtual function.  
11930
11931                      A member-declarator can contain a constant-initializer
11932                      only if it declares a static member of integral or
11933                      enumeration type.  
11934
11935                      Therefore, if the DECLARATOR is for a function, we look
11936                      for a pure-specifier; otherwise, we look for a
11937                      constant-initializer.  When we call `grokfield', it will
11938                      perform more stringent semantics checks.  */
11939                   if (TREE_CODE (declarator) == CALL_EXPR)
11940                     initializer = cp_parser_pure_specifier (parser);
11941                   else
11942                     {
11943                       /* This declaration cannot be a function
11944                          definition.  */
11945                       cp_parser_commit_to_tentative_parse (parser);
11946                       /* Parse the initializer.  */
11947                       initializer = cp_parser_constant_initializer (parser);
11948                     }
11949                 }
11950               /* Otherwise, there is no initializer.  */
11951               else
11952                 initializer = NULL_TREE;
11953
11954               /* See if we are probably looking at a function
11955                  definition.  We are certainly not looking at at a
11956                  member-declarator.  Calling `grokfield' has
11957                  side-effects, so we must not do it unless we are sure
11958                  that we are looking at a member-declarator.  */
11959               if (cp_parser_token_starts_function_definition_p 
11960                   (cp_lexer_peek_token (parser->lexer)))
11961                 decl = error_mark_node;
11962               else
11963                 {
11964                   /* Create the declaration.  */
11965                   decl = grokfield (declarator, decl_specifiers, 
11966                                     initializer, asm_specification,
11967                                     attributes);
11968                   /* Any initialization must have been from a
11969                      constant-expression.  */
11970                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
11971                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
11972                 }
11973             }
11974
11975           /* Reset PREFIX_ATTRIBUTES.  */
11976           while (attributes && TREE_CHAIN (attributes) != first_attribute)
11977             attributes = TREE_CHAIN (attributes);
11978           if (attributes)
11979             TREE_CHAIN (attributes) = NULL_TREE;
11980
11981           /* If there is any qualification still in effect, clear it
11982              now; we will be starting fresh with the next declarator.  */
11983           parser->scope = NULL_TREE;
11984           parser->qualifying_scope = NULL_TREE;
11985           parser->object_scope = NULL_TREE;
11986           /* If it's a `,', then there are more declarators.  */
11987           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11988             cp_lexer_consume_token (parser->lexer);
11989           /* If the next token isn't a `;', then we have a parse error.  */
11990           else if (cp_lexer_next_token_is_not (parser->lexer,
11991                                                CPP_SEMICOLON))
11992             {
11993               cp_parser_error (parser, "expected `;'");
11994               /* Skip tokens until we find a `;'  */
11995               cp_parser_skip_to_end_of_statement (parser);
11996
11997               break;
11998             }
11999
12000           if (decl)
12001             {
12002               /* Add DECL to the list of members.  */
12003               if (!friend_p)
12004                 finish_member_declaration (decl);
12005
12006               if (TREE_CODE (decl) == FUNCTION_DECL)
12007                 cp_parser_save_default_args (parser, decl);
12008             }
12009         }
12010     }
12011
12012   /* If everything went well, look for the `;'.  */
12013   if (cp_parser_parse_definitely (parser))
12014     {
12015       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12016       return;
12017     }
12018
12019   /* Parse the function-definition.  */
12020   decl = cp_parser_function_definition (parser, &friend_p);
12021   /* If the member was not a friend, declare it here.  */
12022   if (!friend_p)
12023     finish_member_declaration (decl);
12024   /* Peek at the next token.  */
12025   token = cp_lexer_peek_token (parser->lexer);
12026   /* If the next token is a semicolon, consume it.  */
12027   if (token->type == CPP_SEMICOLON)
12028     cp_lexer_consume_token (parser->lexer);
12029 }
12030
12031 /* Parse a pure-specifier.
12032
12033    pure-specifier:
12034      = 0
12035
12036    Returns INTEGER_ZERO_NODE if a pure specifier is found.
12037    Otherwiser, ERROR_MARK_NODE is returned.  */
12038
12039 static tree
12040 cp_parser_pure_specifier (cp_parser* parser)
12041 {
12042   cp_token *token;
12043
12044   /* Look for the `=' token.  */
12045   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12046     return error_mark_node;
12047   /* Look for the `0' token.  */
12048   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12049   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
12050      to get information from the lexer about how the number was
12051      spelled in order to fix this problem.  */
12052   if (!token || !integer_zerop (token->value))
12053     return error_mark_node;
12054
12055   return integer_zero_node;
12056 }
12057
12058 /* Parse a constant-initializer.
12059
12060    constant-initializer:
12061      = constant-expression
12062
12063    Returns a representation of the constant-expression.  */
12064
12065 static tree
12066 cp_parser_constant_initializer (cp_parser* parser)
12067 {
12068   /* Look for the `=' token.  */
12069   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12070     return error_mark_node;
12071
12072   /* It is invalid to write:
12073
12074        struct S { static const int i = { 7 }; };
12075
12076      */
12077   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12078     {
12079       cp_parser_error (parser,
12080                        "a brace-enclosed initializer is not allowed here");
12081       /* Consume the opening brace.  */
12082       cp_lexer_consume_token (parser->lexer);
12083       /* Skip the initializer.  */
12084       cp_parser_skip_to_closing_brace (parser);
12085       /* Look for the trailing `}'.  */
12086       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12087       
12088       return error_mark_node;
12089     }
12090
12091   return cp_parser_constant_expression (parser, 
12092                                         /*allow_non_constant=*/false,
12093                                         NULL);
12094 }
12095
12096 /* Derived classes [gram.class.derived] */
12097
12098 /* Parse a base-clause.
12099
12100    base-clause:
12101      : base-specifier-list  
12102
12103    base-specifier-list:
12104      base-specifier
12105      base-specifier-list , base-specifier
12106
12107    Returns a TREE_LIST representing the base-classes, in the order in
12108    which they were declared.  The representation of each node is as
12109    described by cp_parser_base_specifier.  
12110
12111    In the case that no bases are specified, this function will return
12112    NULL_TREE, not ERROR_MARK_NODE.  */
12113
12114 static tree
12115 cp_parser_base_clause (cp_parser* parser)
12116 {
12117   tree bases = NULL_TREE;
12118
12119   /* Look for the `:' that begins the list.  */
12120   cp_parser_require (parser, CPP_COLON, "`:'");
12121
12122   /* Scan the base-specifier-list.  */
12123   while (true)
12124     {
12125       cp_token *token;
12126       tree base;
12127
12128       /* Look for the base-specifier.  */
12129       base = cp_parser_base_specifier (parser);
12130       /* Add BASE to the front of the list.  */
12131       if (base != error_mark_node)
12132         {
12133           TREE_CHAIN (base) = bases;
12134           bases = base;
12135         }
12136       /* Peek at the next token.  */
12137       token = cp_lexer_peek_token (parser->lexer);
12138       /* If it's not a comma, then the list is complete.  */
12139       if (token->type != CPP_COMMA)
12140         break;
12141       /* Consume the `,'.  */
12142       cp_lexer_consume_token (parser->lexer);
12143     }
12144
12145   /* PARSER->SCOPE may still be non-NULL at this point, if the last
12146      base class had a qualified name.  However, the next name that
12147      appears is certainly not qualified.  */
12148   parser->scope = NULL_TREE;
12149   parser->qualifying_scope = NULL_TREE;
12150   parser->object_scope = NULL_TREE;
12151
12152   return nreverse (bases);
12153 }
12154
12155 /* Parse a base-specifier.
12156
12157    base-specifier:
12158      :: [opt] nested-name-specifier [opt] class-name
12159      virtual access-specifier [opt] :: [opt] nested-name-specifier
12160        [opt] class-name
12161      access-specifier virtual [opt] :: [opt] nested-name-specifier
12162        [opt] class-name
12163
12164    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
12165    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12166    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
12167    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
12168        
12169 static tree
12170 cp_parser_base_specifier (cp_parser* parser)
12171 {
12172   cp_token *token;
12173   bool done = false;
12174   bool virtual_p = false;
12175   bool duplicate_virtual_error_issued_p = false;
12176   bool duplicate_access_error_issued_p = false;
12177   bool class_scope_p, template_p;
12178   tree access = access_default_node;
12179   tree type;
12180
12181   /* Process the optional `virtual' and `access-specifier'.  */
12182   while (!done)
12183     {
12184       /* Peek at the next token.  */
12185       token = cp_lexer_peek_token (parser->lexer);
12186       /* Process `virtual'.  */
12187       switch (token->keyword)
12188         {
12189         case RID_VIRTUAL:
12190           /* If `virtual' appears more than once, issue an error.  */
12191           if (virtual_p && !duplicate_virtual_error_issued_p)
12192             {
12193               cp_parser_error (parser,
12194                                "`virtual' specified more than once in base-specified");
12195               duplicate_virtual_error_issued_p = true;
12196             }
12197
12198           virtual_p = true;
12199
12200           /* Consume the `virtual' token.  */
12201           cp_lexer_consume_token (parser->lexer);
12202
12203           break;
12204
12205         case RID_PUBLIC:
12206         case RID_PROTECTED:
12207         case RID_PRIVATE:
12208           /* If more than one access specifier appears, issue an
12209              error.  */
12210           if (access != access_default_node
12211               && !duplicate_access_error_issued_p)
12212             {
12213               cp_parser_error (parser,
12214                                "more than one access specifier in base-specified");
12215               duplicate_access_error_issued_p = true;
12216             }
12217
12218           access = ridpointers[(int) token->keyword];
12219
12220           /* Consume the access-specifier.  */
12221           cp_lexer_consume_token (parser->lexer);
12222
12223           break;
12224
12225         default:
12226           done = true;
12227           break;
12228         }
12229     }
12230
12231   /* Look for the optional `::' operator.  */
12232   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12233   /* Look for the nested-name-specifier.  The simplest way to
12234      implement:
12235
12236        [temp.res]
12237
12238        The keyword `typename' is not permitted in a base-specifier or
12239        mem-initializer; in these contexts a qualified name that
12240        depends on a template-parameter is implicitly assumed to be a
12241        type name.
12242
12243      is to pretend that we have seen the `typename' keyword at this
12244      point.  */ 
12245   cp_parser_nested_name_specifier_opt (parser,
12246                                        /*typename_keyword_p=*/true,
12247                                        /*check_dependency_p=*/true,
12248                                        /*type_p=*/true);
12249   /* If the base class is given by a qualified name, assume that names
12250      we see are type names or templates, as appropriate.  */
12251   class_scope_p = (parser->scope && TYPE_P (parser->scope));
12252   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12253   
12254   /* Finally, look for the class-name.  */
12255   type = cp_parser_class_name (parser, 
12256                                class_scope_p,
12257                                template_p,
12258                                /*type_p=*/true,
12259                                /*check_dependency_p=*/true,
12260                                /*class_head_p=*/false);
12261
12262   if (type == error_mark_node)
12263     return error_mark_node;
12264
12265   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12266 }
12267
12268 /* Exception handling [gram.exception] */
12269
12270 /* Parse an (optional) exception-specification.
12271
12272    exception-specification:
12273      throw ( type-id-list [opt] )
12274
12275    Returns a TREE_LIST representing the exception-specification.  The
12276    TREE_VALUE of each node is a type.  */
12277
12278 static tree
12279 cp_parser_exception_specification_opt (cp_parser* parser)
12280 {
12281   cp_token *token;
12282   tree type_id_list;
12283
12284   /* Peek at the next token.  */
12285   token = cp_lexer_peek_token (parser->lexer);
12286   /* If it's not `throw', then there's no exception-specification.  */
12287   if (!cp_parser_is_keyword (token, RID_THROW))
12288     return NULL_TREE;
12289
12290   /* Consume the `throw'.  */
12291   cp_lexer_consume_token (parser->lexer);
12292
12293   /* Look for the `('.  */
12294   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12295
12296   /* Peek at the next token.  */
12297   token = cp_lexer_peek_token (parser->lexer);
12298   /* If it's not a `)', then there is a type-id-list.  */
12299   if (token->type != CPP_CLOSE_PAREN)
12300     {
12301       const char *saved_message;
12302
12303       /* Types may not be defined in an exception-specification.  */
12304       saved_message = parser->type_definition_forbidden_message;
12305       parser->type_definition_forbidden_message
12306         = "types may not be defined in an exception-specification";
12307       /* Parse the type-id-list.  */
12308       type_id_list = cp_parser_type_id_list (parser);
12309       /* Restore the saved message.  */
12310       parser->type_definition_forbidden_message = saved_message;
12311     }
12312   else
12313     type_id_list = empty_except_spec;
12314
12315   /* Look for the `)'.  */
12316   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12317
12318   return type_id_list;
12319 }
12320
12321 /* Parse an (optional) type-id-list.
12322
12323    type-id-list:
12324      type-id
12325      type-id-list , type-id
12326
12327    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
12328    in the order that the types were presented.  */
12329
12330 static tree
12331 cp_parser_type_id_list (cp_parser* parser)
12332 {
12333   tree types = NULL_TREE;
12334
12335   while (true)
12336     {
12337       cp_token *token;
12338       tree type;
12339
12340       /* Get the next type-id.  */
12341       type = cp_parser_type_id (parser);
12342       /* Add it to the list.  */
12343       types = add_exception_specifier (types, type, /*complain=*/1);
12344       /* Peek at the next token.  */
12345       token = cp_lexer_peek_token (parser->lexer);
12346       /* If it is not a `,', we are done.  */
12347       if (token->type != CPP_COMMA)
12348         break;
12349       /* Consume the `,'.  */
12350       cp_lexer_consume_token (parser->lexer);
12351     }
12352
12353   return nreverse (types);
12354 }
12355
12356 /* Parse a try-block.
12357
12358    try-block:
12359      try compound-statement handler-seq  */
12360
12361 static tree
12362 cp_parser_try_block (cp_parser* parser)
12363 {
12364   tree try_block;
12365
12366   cp_parser_require_keyword (parser, RID_TRY, "`try'");
12367   try_block = begin_try_block ();
12368   cp_parser_compound_statement (parser, false);
12369   finish_try_block (try_block);
12370   cp_parser_handler_seq (parser);
12371   finish_handler_sequence (try_block);
12372
12373   return try_block;
12374 }
12375
12376 /* Parse a function-try-block.
12377
12378    function-try-block:
12379      try ctor-initializer [opt] function-body handler-seq  */
12380
12381 static bool
12382 cp_parser_function_try_block (cp_parser* parser)
12383 {
12384   tree try_block;
12385   bool ctor_initializer_p;
12386
12387   /* Look for the `try' keyword.  */
12388   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12389     return false;
12390   /* Let the rest of the front-end know where we are.  */
12391   try_block = begin_function_try_block ();
12392   /* Parse the function-body.  */
12393   ctor_initializer_p 
12394     = cp_parser_ctor_initializer_opt_and_function_body (parser);
12395   /* We're done with the `try' part.  */
12396   finish_function_try_block (try_block);
12397   /* Parse the handlers.  */
12398   cp_parser_handler_seq (parser);
12399   /* We're done with the handlers.  */
12400   finish_function_handler_sequence (try_block);
12401
12402   return ctor_initializer_p;
12403 }
12404
12405 /* Parse a handler-seq.
12406
12407    handler-seq:
12408      handler handler-seq [opt]  */
12409
12410 static void
12411 cp_parser_handler_seq (cp_parser* parser)
12412 {
12413   while (true)
12414     {
12415       cp_token *token;
12416
12417       /* Parse the handler.  */
12418       cp_parser_handler (parser);
12419       /* Peek at the next token.  */
12420       token = cp_lexer_peek_token (parser->lexer);
12421       /* If it's not `catch' then there are no more handlers.  */
12422       if (!cp_parser_is_keyword (token, RID_CATCH))
12423         break;
12424     }
12425 }
12426
12427 /* Parse a handler.
12428
12429    handler:
12430      catch ( exception-declaration ) compound-statement  */
12431
12432 static void
12433 cp_parser_handler (cp_parser* parser)
12434 {
12435   tree handler;
12436   tree declaration;
12437
12438   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12439   handler = begin_handler ();
12440   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12441   declaration = cp_parser_exception_declaration (parser);
12442   finish_handler_parms (declaration, handler);
12443   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12444   cp_parser_compound_statement (parser, false);
12445   finish_handler (handler);
12446 }
12447
12448 /* Parse an exception-declaration.
12449
12450    exception-declaration:
12451      type-specifier-seq declarator
12452      type-specifier-seq abstract-declarator
12453      type-specifier-seq
12454      ...  
12455
12456    Returns a VAR_DECL for the declaration, or NULL_TREE if the
12457    ellipsis variant is used.  */
12458
12459 static tree
12460 cp_parser_exception_declaration (cp_parser* parser)
12461 {
12462   tree type_specifiers;
12463   tree declarator;
12464   const char *saved_message;
12465
12466   /* If it's an ellipsis, it's easy to handle.  */
12467   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12468     {
12469       /* Consume the `...' token.  */
12470       cp_lexer_consume_token (parser->lexer);
12471       return NULL_TREE;
12472     }
12473
12474   /* Types may not be defined in exception-declarations.  */
12475   saved_message = parser->type_definition_forbidden_message;
12476   parser->type_definition_forbidden_message
12477     = "types may not be defined in exception-declarations";
12478
12479   /* Parse the type-specifier-seq.  */
12480   type_specifiers = cp_parser_type_specifier_seq (parser);
12481   /* If it's a `)', then there is no declarator.  */
12482   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12483     declarator = NULL_TREE;
12484   else
12485     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12486                                        /*ctor_dtor_or_conv_p=*/NULL);
12487
12488   /* Restore the saved message.  */
12489   parser->type_definition_forbidden_message = saved_message;
12490
12491   return start_handler_parms (type_specifiers, declarator);
12492 }
12493
12494 /* Parse a throw-expression. 
12495
12496    throw-expression:
12497      throw assignment-expression [opt]
12498
12499    Returns a THROW_EXPR representing the throw-expression.  */
12500
12501 static tree
12502 cp_parser_throw_expression (cp_parser* parser)
12503 {
12504   tree expression;
12505
12506   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12507   /* We can't be sure if there is an assignment-expression or not.  */
12508   cp_parser_parse_tentatively (parser);
12509   /* Try it.  */
12510   expression = cp_parser_assignment_expression (parser);
12511   /* If it didn't work, this is just a rethrow.  */
12512   if (!cp_parser_parse_definitely (parser))
12513     expression = NULL_TREE;
12514
12515   return build_throw (expression);
12516 }
12517
12518 /* GNU Extensions */
12519
12520 /* Parse an (optional) asm-specification.
12521
12522    asm-specification:
12523      asm ( string-literal )
12524
12525    If the asm-specification is present, returns a STRING_CST
12526    corresponding to the string-literal.  Otherwise, returns
12527    NULL_TREE.  */
12528
12529 static tree
12530 cp_parser_asm_specification_opt (cp_parser* parser)
12531 {
12532   cp_token *token;
12533   tree asm_specification;
12534
12535   /* Peek at the next token.  */
12536   token = cp_lexer_peek_token (parser->lexer);
12537   /* If the next token isn't the `asm' keyword, then there's no 
12538      asm-specification.  */
12539   if (!cp_parser_is_keyword (token, RID_ASM))
12540     return NULL_TREE;
12541
12542   /* Consume the `asm' token.  */
12543   cp_lexer_consume_token (parser->lexer);
12544   /* Look for the `('.  */
12545   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12546
12547   /* Look for the string-literal.  */
12548   token = cp_parser_require (parser, CPP_STRING, "string-literal");
12549   if (token)
12550     asm_specification = token->value;
12551   else
12552     asm_specification = NULL_TREE;
12553
12554   /* Look for the `)'.  */
12555   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12556
12557   return asm_specification;
12558 }
12559
12560 /* Parse an asm-operand-list.  
12561
12562    asm-operand-list:
12563      asm-operand
12564      asm-operand-list , asm-operand
12565      
12566    asm-operand:
12567      string-literal ( expression )  
12568      [ string-literal ] string-literal ( expression )
12569
12570    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
12571    each node is the expression.  The TREE_PURPOSE is itself a
12572    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12573    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12574    is a STRING_CST for the string literal before the parenthesis.  */
12575
12576 static tree
12577 cp_parser_asm_operand_list (cp_parser* parser)
12578 {
12579   tree asm_operands = NULL_TREE;
12580
12581   while (true)
12582     {
12583       tree string_literal;
12584       tree expression;
12585       tree name;
12586       cp_token *token;
12587       
12588       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 
12589         {
12590           /* Consume the `[' token.  */
12591           cp_lexer_consume_token (parser->lexer);
12592           /* Read the operand name.  */
12593           name = cp_parser_identifier (parser);
12594           if (name != error_mark_node) 
12595             name = build_string (IDENTIFIER_LENGTH (name),
12596                                  IDENTIFIER_POINTER (name));
12597           /* Look for the closing `]'.  */
12598           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12599         }
12600       else
12601         name = NULL_TREE;
12602       /* Look for the string-literal.  */
12603       token = cp_parser_require (parser, CPP_STRING, "string-literal");
12604       string_literal = token ? token->value : error_mark_node;
12605       /* Look for the `('.  */
12606       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12607       /* Parse the expression.  */
12608       expression = cp_parser_expression (parser);
12609       /* Look for the `)'.  */
12610       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12611       /* Add this operand to the list.  */
12612       asm_operands = tree_cons (build_tree_list (name, string_literal),
12613                                 expression, 
12614                                 asm_operands);
12615       /* If the next token is not a `,', there are no more 
12616          operands.  */
12617       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12618         break;
12619       /* Consume the `,'.  */
12620       cp_lexer_consume_token (parser->lexer);
12621     }
12622
12623   return nreverse (asm_operands);
12624 }
12625
12626 /* Parse an asm-clobber-list.  
12627
12628    asm-clobber-list:
12629      string-literal
12630      asm-clobber-list , string-literal  
12631
12632    Returns a TREE_LIST, indicating the clobbers in the order that they
12633    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
12634
12635 static tree
12636 cp_parser_asm_clobber_list (cp_parser* parser)
12637 {
12638   tree clobbers = NULL_TREE;
12639
12640   while (true)
12641     {
12642       cp_token *token;
12643       tree string_literal;
12644
12645       /* Look for the string literal.  */
12646       token = cp_parser_require (parser, CPP_STRING, "string-literal");
12647       string_literal = token ? token->value : error_mark_node;
12648       /* Add it to the list.  */
12649       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12650       /* If the next token is not a `,', then the list is 
12651          complete.  */
12652       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12653         break;
12654       /* Consume the `,' token.  */
12655       cp_lexer_consume_token (parser->lexer);
12656     }
12657
12658   return clobbers;
12659 }
12660
12661 /* Parse an (optional) series of attributes.
12662
12663    attributes:
12664      attributes attribute
12665
12666    attribute:
12667      __attribute__ (( attribute-list [opt] ))  
12668
12669    The return value is as for cp_parser_attribute_list.  */
12670      
12671 static tree
12672 cp_parser_attributes_opt (cp_parser* parser)
12673 {
12674   tree attributes = NULL_TREE;
12675
12676   while (true)
12677     {
12678       cp_token *token;
12679       tree attribute_list;
12680
12681       /* Peek at the next token.  */
12682       token = cp_lexer_peek_token (parser->lexer);
12683       /* If it's not `__attribute__', then we're done.  */
12684       if (token->keyword != RID_ATTRIBUTE)
12685         break;
12686
12687       /* Consume the `__attribute__' keyword.  */
12688       cp_lexer_consume_token (parser->lexer);
12689       /* Look for the two `(' tokens.  */
12690       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12691       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12692
12693       /* Peek at the next token.  */
12694       token = cp_lexer_peek_token (parser->lexer);
12695       if (token->type != CPP_CLOSE_PAREN)
12696         /* Parse the attribute-list.  */
12697         attribute_list = cp_parser_attribute_list (parser);
12698       else
12699         /* If the next token is a `)', then there is no attribute
12700            list.  */
12701         attribute_list = NULL;
12702
12703       /* Look for the two `)' tokens.  */
12704       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12705       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12706
12707       /* Add these new attributes to the list.  */
12708       attributes = chainon (attributes, attribute_list);
12709     }
12710
12711   return attributes;
12712 }
12713
12714 /* Parse an attribute-list.  
12715
12716    attribute-list:  
12717      attribute 
12718      attribute-list , attribute
12719
12720    attribute:
12721      identifier     
12722      identifier ( identifier )
12723      identifier ( identifier , expression-list )
12724      identifier ( expression-list ) 
12725
12726    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
12727    TREE_PURPOSE of each node is the identifier indicating which
12728    attribute is in use.  The TREE_VALUE represents the arguments, if
12729    any.  */
12730
12731 static tree
12732 cp_parser_attribute_list (cp_parser* parser)
12733 {
12734   tree attribute_list = NULL_TREE;
12735
12736   while (true)
12737     {
12738       cp_token *token;
12739       tree identifier;
12740       tree attribute;
12741
12742       /* Look for the identifier.  We also allow keywords here; for
12743          example `__attribute__ ((const))' is legal.  */
12744       token = cp_lexer_peek_token (parser->lexer);
12745       if (token->type != CPP_NAME 
12746           && token->type != CPP_KEYWORD)
12747         return error_mark_node;
12748       /* Consume the token.  */
12749       token = cp_lexer_consume_token (parser->lexer);
12750       
12751       /* Save away the identifier that indicates which attribute this is.  */
12752       identifier = token->value;
12753       attribute = build_tree_list (identifier, NULL_TREE);
12754
12755       /* Peek at the next token.  */
12756       token = cp_lexer_peek_token (parser->lexer);
12757       /* If it's an `(', then parse the attribute arguments.  */
12758       if (token->type == CPP_OPEN_PAREN)
12759         {
12760           tree arguments;
12761
12762           arguments = (cp_parser_parenthesized_expression_list 
12763                        (parser, true, /*non_constant_p=*/NULL));
12764           /* Save the identifier and arguments away.  */
12765           TREE_VALUE (attribute) = arguments;
12766         }
12767
12768       /* Add this attribute to the list.  */
12769       TREE_CHAIN (attribute) = attribute_list;
12770       attribute_list = attribute;
12771
12772       /* Now, look for more attributes.  */
12773       token = cp_lexer_peek_token (parser->lexer);
12774       /* If the next token isn't a `,', we're done.  */
12775       if (token->type != CPP_COMMA)
12776         break;
12777
12778       /* Consume the commma and keep going.  */
12779       cp_lexer_consume_token (parser->lexer);
12780     }
12781
12782   /* We built up the list in reverse order.  */
12783   return nreverse (attribute_list);
12784 }
12785
12786 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
12787    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
12788    current value of the PEDANTIC flag, regardless of whether or not
12789    the `__extension__' keyword is present.  The caller is responsible
12790    for restoring the value of the PEDANTIC flag.  */
12791
12792 static bool
12793 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
12794 {
12795   /* Save the old value of the PEDANTIC flag.  */
12796   *saved_pedantic = pedantic;
12797
12798   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
12799     {
12800       /* Consume the `__extension__' token.  */
12801       cp_lexer_consume_token (parser->lexer);
12802       /* We're not being pedantic while the `__extension__' keyword is
12803          in effect.  */
12804       pedantic = 0;
12805
12806       return true;
12807     }
12808
12809   return false;
12810 }
12811
12812 /* Parse a label declaration.
12813
12814    label-declaration:
12815      __label__ label-declarator-seq ;
12816
12817    label-declarator-seq:
12818      identifier , label-declarator-seq
12819      identifier  */
12820
12821 static void
12822 cp_parser_label_declaration (cp_parser* parser)
12823 {
12824   /* Look for the `__label__' keyword.  */
12825   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
12826
12827   while (true)
12828     {
12829       tree identifier;
12830
12831       /* Look for an identifier.  */
12832       identifier = cp_parser_identifier (parser);
12833       /* Declare it as a lobel.  */
12834       finish_label_decl (identifier);
12835       /* If the next token is a `;', stop.  */
12836       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12837         break;
12838       /* Look for the `,' separating the label declarations.  */
12839       cp_parser_require (parser, CPP_COMMA, "`,'");
12840     }
12841
12842   /* Look for the final `;'.  */
12843   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12844 }
12845
12846 /* Support Functions */
12847
12848 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
12849    NAME should have one of the representations used for an
12850    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
12851    is returned.  If PARSER->SCOPE is a dependent type, then a
12852    SCOPE_REF is returned.
12853
12854    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
12855    returned; the name was already resolved when the TEMPLATE_ID_EXPR
12856    was formed.  Abstractly, such entities should not be passed to this
12857    function, because they do not need to be looked up, but it is
12858    simpler to check for this special case here, rather than at the
12859    call-sites.
12860
12861    In cases not explicitly covered above, this function returns a
12862    DECL, OVERLOAD, or baselink representing the result of the lookup.
12863    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
12864    is returned.
12865
12866    If IS_TYPE is TRUE, bindings that do not refer to types are
12867    ignored.
12868
12869    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
12870    are ignored.
12871
12872    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
12873    types.  */
12874
12875 static tree
12876 cp_parser_lookup_name (cp_parser *parser, tree name, 
12877                        bool is_type, bool is_namespace, bool check_dependency)
12878 {
12879   tree decl;
12880   tree object_type = parser->context->object_type;
12881
12882   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
12883      no longer valid.  Note that if we are parsing tentatively, and
12884      the parse fails, OBJECT_TYPE will be automatically restored.  */
12885   parser->context->object_type = NULL_TREE;
12886
12887   if (name == error_mark_node)
12888     return error_mark_node;
12889
12890   /* A template-id has already been resolved; there is no lookup to
12891      do.  */
12892   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12893     return name;
12894   if (BASELINK_P (name))
12895     {
12896       my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
12897                            == TEMPLATE_ID_EXPR),
12898                           20020909);
12899       return name;
12900     }
12901
12902   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
12903      it should already have been checked to make sure that the name
12904      used matches the type being destroyed.  */
12905   if (TREE_CODE (name) == BIT_NOT_EXPR)
12906     {
12907       tree type;
12908
12909       /* Figure out to which type this destructor applies.  */
12910       if (parser->scope)
12911         type = parser->scope;
12912       else if (object_type)
12913         type = object_type;
12914       else
12915         type = current_class_type;
12916       /* If that's not a class type, there is no destructor.  */
12917       if (!type || !CLASS_TYPE_P (type))
12918         return error_mark_node;
12919       /* If it was a class type, return the destructor.  */
12920       return CLASSTYPE_DESTRUCTORS (type);
12921     }
12922
12923   /* By this point, the NAME should be an ordinary identifier.  If
12924      the id-expression was a qualified name, the qualifying scope is
12925      stored in PARSER->SCOPE at this point.  */
12926   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
12927                       20000619);
12928   
12929   /* Perform the lookup.  */
12930   if (parser->scope)
12931     { 
12932       bool dependent_p;
12933
12934       if (parser->scope == error_mark_node)
12935         return error_mark_node;
12936
12937       /* If the SCOPE is dependent, the lookup must be deferred until
12938          the template is instantiated -- unless we are explicitly
12939          looking up names in uninstantiated templates.  Even then, we
12940          cannot look up the name if the scope is not a class type; it
12941          might, for example, be a template type parameter.  */
12942       dependent_p = (TYPE_P (parser->scope)
12943                      && !(parser->in_declarator_p
12944                           && currently_open_class (parser->scope))
12945                      && dependent_type_p (parser->scope));
12946       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
12947            && dependent_p)
12948         {
12949           if (!is_type)
12950             decl = build_nt (SCOPE_REF, parser->scope, name);
12951           else
12952             /* The resolution to Core Issue 180 says that `struct A::B'
12953                should be considered a type-name, even if `A' is
12954                dependent.  */
12955             decl = TYPE_NAME (make_typename_type (parser->scope,
12956                                                   name,
12957                                                   /*complain=*/1));
12958         }
12959       else
12960         {
12961           /* If PARSER->SCOPE is a dependent type, then it must be a
12962              class type, and we must not be checking dependencies;
12963              otherwise, we would have processed this lookup above.  So
12964              that PARSER->SCOPE is not considered a dependent base by
12965              lookup_member, we must enter the scope here.  */
12966           if (dependent_p)
12967             push_scope (parser->scope);
12968           /* If the PARSER->SCOPE is a a template specialization, it
12969              may be instantiated during name lookup.  In that case,
12970              errors may be issued.  Even if we rollback the current
12971              tentative parse, those errors are valid.  */
12972           decl = lookup_qualified_name (parser->scope, name, is_type,
12973                                         /*complain=*/true);
12974           if (dependent_p)
12975             pop_scope (parser->scope);
12976         }
12977       parser->qualifying_scope = parser->scope;
12978       parser->object_scope = NULL_TREE;
12979     }
12980   else if (object_type)
12981     {
12982       tree object_decl = NULL_TREE;
12983       /* Look up the name in the scope of the OBJECT_TYPE, unless the
12984          OBJECT_TYPE is not a class.  */
12985       if (CLASS_TYPE_P (object_type))
12986         /* If the OBJECT_TYPE is a template specialization, it may
12987            be instantiated during name lookup.  In that case, errors
12988            may be issued.  Even if we rollback the current tentative
12989            parse, those errors are valid.  */
12990         object_decl = lookup_member (object_type,
12991                                      name,
12992                                      /*protect=*/0, is_type);
12993       /* Look it up in the enclosing context, too.  */
12994       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
12995                                is_namespace,
12996                                /*flags=*/0);
12997       parser->object_scope = object_type;
12998       parser->qualifying_scope = NULL_TREE;
12999       if (object_decl)
13000         decl = object_decl;
13001     }
13002   else
13003     {
13004       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13005                                is_namespace,
13006                                /*flags=*/0);
13007       parser->qualifying_scope = NULL_TREE;
13008       parser->object_scope = NULL_TREE;
13009     }
13010
13011   /* If the lookup failed, let our caller know.  */
13012   if (!decl 
13013       || decl == error_mark_node
13014       || (TREE_CODE (decl) == FUNCTION_DECL 
13015           && DECL_ANTICIPATED (decl)))
13016     return error_mark_node;
13017
13018   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
13019   if (TREE_CODE (decl) == TREE_LIST)
13020     {
13021       /* The error message we have to print is too complicated for
13022          cp_parser_error, so we incorporate its actions directly.  */
13023       if (!cp_parser_simulate_error (parser))
13024         {
13025           error ("reference to `%D' is ambiguous", name);
13026           print_candidates (decl);
13027         }
13028       return error_mark_node;
13029     }
13030
13031   my_friendly_assert (DECL_P (decl) 
13032                       || TREE_CODE (decl) == OVERLOAD
13033                       || TREE_CODE (decl) == SCOPE_REF
13034                       || BASELINK_P (decl),
13035                       20000619);
13036
13037   /* If we have resolved the name of a member declaration, check to
13038      see if the declaration is accessible.  When the name resolves to
13039      set of overloaded functions, accessibility is checked when
13040      overload resolution is done.  
13041
13042      During an explicit instantiation, access is not checked at all,
13043      as per [temp.explicit].  */
13044   if (DECL_P (decl))
13045     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13046
13047   return decl;
13048 }
13049
13050 /* Like cp_parser_lookup_name, but for use in the typical case where
13051    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13052    TRUE.  */
13053
13054 static tree
13055 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13056 {
13057   return cp_parser_lookup_name (parser, name, 
13058                                 /*is_type=*/false,
13059                                 /*is_namespace=*/false,
13060                                 /*check_dependency=*/true);
13061 }
13062
13063 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13064    the current context, return the TYPE_DECL.  If TAG_NAME_P is
13065    true, the DECL indicates the class being defined in a class-head,
13066    or declared in an elaborated-type-specifier.
13067
13068    Otherwise, return DECL.  */
13069
13070 static tree
13071 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13072 {
13073   /* If the TEMPLATE_DECL is being declared as part of a class-head,
13074      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13075
13076        struct A { 
13077          template <typename T> struct B;
13078        };
13079
13080        template <typename T> struct A::B {}; 
13081    
13082      Similarly, in a elaborated-type-specifier:
13083
13084        namespace N { struct X{}; }
13085
13086        struct A {
13087          template <typename T> friend struct N::X;
13088        };
13089
13090      However, if the DECL refers to a class type, and we are in
13091      the scope of the class, then the name lookup automatically
13092      finds the TYPE_DECL created by build_self_reference rather
13093      than a TEMPLATE_DECL.  For example, in:
13094
13095        template <class T> struct S {
13096          S s;
13097        };
13098
13099      there is no need to handle such case.  */
13100
13101   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13102     return DECL_TEMPLATE_RESULT (decl);
13103
13104   return decl;
13105 }
13106
13107 /* If too many, or too few, template-parameter lists apply to the
13108    declarator, issue an error message.  Returns TRUE if all went well,
13109    and FALSE otherwise.  */
13110
13111 static bool
13112 cp_parser_check_declarator_template_parameters (cp_parser* parser, 
13113                                                 tree declarator)
13114 {
13115   unsigned num_templates;
13116
13117   /* We haven't seen any classes that involve template parameters yet.  */
13118   num_templates = 0;
13119
13120   switch (TREE_CODE (declarator))
13121     {
13122     case CALL_EXPR:
13123     case ARRAY_REF:
13124     case INDIRECT_REF:
13125     case ADDR_EXPR:
13126       {
13127         tree main_declarator = TREE_OPERAND (declarator, 0);
13128         return
13129           cp_parser_check_declarator_template_parameters (parser, 
13130                                                           main_declarator);
13131       }
13132
13133     case SCOPE_REF:
13134       {
13135         tree scope;
13136         tree member;
13137
13138         scope = TREE_OPERAND (declarator, 0);
13139         member = TREE_OPERAND (declarator, 1);
13140
13141         /* If this is a pointer-to-member, then we are not interested
13142            in the SCOPE, because it does not qualify the thing that is
13143            being declared.  */
13144         if (TREE_CODE (member) == INDIRECT_REF)
13145           return (cp_parser_check_declarator_template_parameters
13146                   (parser, member));
13147
13148         while (scope && CLASS_TYPE_P (scope))
13149           {
13150             /* You're supposed to have one `template <...>'
13151                for every template class, but you don't need one
13152                for a full specialization.  For example:
13153                
13154                template <class T> struct S{};
13155                template <> struct S<int> { void f(); };
13156                void S<int>::f () {}
13157                
13158                is correct; there shouldn't be a `template <>' for
13159                the definition of `S<int>::f'.  */
13160             if (CLASSTYPE_TEMPLATE_INFO (scope)
13161                 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13162                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13163                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13164               ++num_templates;
13165
13166             scope = TYPE_CONTEXT (scope);
13167           }
13168       }
13169
13170       /* Fall through.  */
13171
13172     default:
13173       /* If the DECLARATOR has the form `X<y>' then it uses one
13174          additional level of template parameters.  */
13175       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13176         ++num_templates;
13177
13178       return cp_parser_check_template_parameters (parser, 
13179                                                   num_templates);
13180     }
13181 }
13182
13183 /* NUM_TEMPLATES were used in the current declaration.  If that is
13184    invalid, return FALSE and issue an error messages.  Otherwise,
13185    return TRUE.  */
13186
13187 static bool
13188 cp_parser_check_template_parameters (cp_parser* parser,
13189                                      unsigned num_templates)
13190 {
13191   /* If there are more template classes than parameter lists, we have
13192      something like:
13193      
13194        template <class T> void S<T>::R<T>::f ();  */
13195   if (parser->num_template_parameter_lists < num_templates)
13196     {
13197       error ("too few template-parameter-lists");
13198       return false;
13199     }
13200   /* If there are the same number of template classes and parameter
13201      lists, that's OK.  */
13202   if (parser->num_template_parameter_lists == num_templates)
13203     return true;
13204   /* If there are more, but only one more, then we are referring to a
13205      member template.  That's OK too.  */
13206   if (parser->num_template_parameter_lists == num_templates + 1)
13207       return true;
13208   /* Otherwise, there are too many template parameter lists.  We have
13209      something like:
13210
13211      template <class T> template <class U> void S::f();  */
13212   error ("too many template-parameter-lists");
13213   return false;
13214 }
13215
13216 /* Parse a binary-expression of the general form:
13217
13218    binary-expression:
13219      <expr>
13220      binary-expression <token> <expr>
13221
13222    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
13223    to parser the <expr>s.  If the first production is used, then the
13224    value returned by FN is returned directly.  Otherwise, a node with
13225    the indicated EXPR_TYPE is returned, with operands corresponding to
13226    the two sub-expressions.  */
13227
13228 static tree
13229 cp_parser_binary_expression (cp_parser* parser, 
13230                              const cp_parser_token_tree_map token_tree_map, 
13231                              cp_parser_expression_fn fn)
13232 {
13233   tree lhs;
13234
13235   /* Parse the first expression.  */
13236   lhs = (*fn) (parser);
13237   /* Now, look for more expressions.  */
13238   while (true)
13239     {
13240       cp_token *token;
13241       const cp_parser_token_tree_map_node *map_node;
13242       tree rhs;
13243
13244       /* Peek at the next token.  */
13245       token = cp_lexer_peek_token (parser->lexer);
13246       /* If the token is `>', and that's not an operator at the
13247          moment, then we're done.  */
13248       if (token->type == CPP_GREATER
13249           && !parser->greater_than_is_operator_p)
13250         break;
13251       /* If we find one of the tokens we want, build the corresponding
13252          tree representation.  */
13253       for (map_node = token_tree_map; 
13254            map_node->token_type != CPP_EOF;
13255            ++map_node)
13256         if (map_node->token_type == token->type)
13257           {
13258             /* Consume the operator token.  */
13259             cp_lexer_consume_token (parser->lexer);
13260             /* Parse the right-hand side of the expression.  */
13261             rhs = (*fn) (parser);
13262             /* Build the binary tree node.  */
13263             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13264             break;
13265           }
13266
13267       /* If the token wasn't one of the ones we want, we're done.  */
13268       if (map_node->token_type == CPP_EOF)
13269         break;
13270     }
13271
13272   return lhs;
13273 }
13274
13275 /* Parse an optional `::' token indicating that the following name is
13276    from the global namespace.  If so, PARSER->SCOPE is set to the
13277    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13278    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13279    Returns the new value of PARSER->SCOPE, if the `::' token is
13280    present, and NULL_TREE otherwise.  */
13281
13282 static tree
13283 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13284 {
13285   cp_token *token;
13286
13287   /* Peek at the next token.  */
13288   token = cp_lexer_peek_token (parser->lexer);
13289   /* If we're looking at a `::' token then we're starting from the
13290      global namespace, not our current location.  */
13291   if (token->type == CPP_SCOPE)
13292     {
13293       /* Consume the `::' token.  */
13294       cp_lexer_consume_token (parser->lexer);
13295       /* Set the SCOPE so that we know where to start the lookup.  */
13296       parser->scope = global_namespace;
13297       parser->qualifying_scope = global_namespace;
13298       parser->object_scope = NULL_TREE;
13299
13300       return parser->scope;
13301     }
13302   else if (!current_scope_valid_p)
13303     {
13304       parser->scope = NULL_TREE;
13305       parser->qualifying_scope = NULL_TREE;
13306       parser->object_scope = NULL_TREE;
13307     }
13308
13309   return NULL_TREE;
13310 }
13311
13312 /* Returns TRUE if the upcoming token sequence is the start of a
13313    constructor declarator.  If FRIEND_P is true, the declarator is
13314    preceded by the `friend' specifier.  */
13315
13316 static bool
13317 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13318 {
13319   bool constructor_p;
13320   tree type_decl = NULL_TREE;
13321   bool nested_name_p;
13322   cp_token *next_token;
13323
13324   /* The common case is that this is not a constructor declarator, so
13325      try to avoid doing lots of work if at all possible.  It's not
13326      valid declare a constructor at function scope.  */
13327   if (at_function_scope_p ())
13328     return false;
13329   /* And only certain tokens can begin a constructor declarator.  */
13330   next_token = cp_lexer_peek_token (parser->lexer);
13331   if (next_token->type != CPP_NAME
13332       && next_token->type != CPP_SCOPE
13333       && next_token->type != CPP_NESTED_NAME_SPECIFIER
13334       && next_token->type != CPP_TEMPLATE_ID)
13335     return false;
13336
13337   /* Parse tentatively; we are going to roll back all of the tokens
13338      consumed here.  */
13339   cp_parser_parse_tentatively (parser);
13340   /* Assume that we are looking at a constructor declarator.  */
13341   constructor_p = true;
13342
13343   /* Look for the optional `::' operator.  */
13344   cp_parser_global_scope_opt (parser,
13345                               /*current_scope_valid_p=*/false);
13346   /* Look for the nested-name-specifier.  */
13347   nested_name_p 
13348     = (cp_parser_nested_name_specifier_opt (parser,
13349                                             /*typename_keyword_p=*/false,
13350                                             /*check_dependency_p=*/false,
13351                                             /*type_p=*/false)
13352        != NULL_TREE);
13353   /* Outside of a class-specifier, there must be a
13354      nested-name-specifier.  */
13355   if (!nested_name_p && 
13356       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13357        || friend_p))
13358     constructor_p = false;
13359   /* If we still think that this might be a constructor-declarator,
13360      look for a class-name.  */
13361   if (constructor_p)
13362     {
13363       /* If we have:
13364
13365            template <typename T> struct S { S(); };
13366            template <typename T> S<T>::S ();
13367
13368          we must recognize that the nested `S' names a class.
13369          Similarly, for:
13370
13371            template <typename T> S<T>::S<T> ();
13372
13373          we must recognize that the nested `S' names a template.  */
13374       type_decl = cp_parser_class_name (parser,
13375                                         /*typename_keyword_p=*/false,
13376                                         /*template_keyword_p=*/false,
13377                                         /*type_p=*/false,
13378                                         /*check_dependency_p=*/false,
13379                                         /*class_head_p=*/false);
13380       /* If there was no class-name, then this is not a constructor.  */
13381       constructor_p = !cp_parser_error_occurred (parser);
13382     }
13383
13384   /* If we're still considering a constructor, we have to see a `(',
13385      to begin the parameter-declaration-clause, followed by either a
13386      `)', an `...', or a decl-specifier.  We need to check for a
13387      type-specifier to avoid being fooled into thinking that:
13388
13389        S::S (f) (int);
13390
13391      is a constructor.  (It is actually a function named `f' that
13392      takes one parameter (of type `int') and returns a value of type
13393      `S::S'.  */
13394   if (constructor_p 
13395       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13396     {
13397       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13398           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13399           && !cp_parser_storage_class_specifier_opt (parser))
13400         {
13401           tree type;
13402
13403           /* Names appearing in the type-specifier should be looked up
13404              in the scope of the class.  */
13405           if (current_class_type)
13406             type = NULL_TREE;
13407           else
13408             {
13409               type = TREE_TYPE (type_decl);
13410               if (TREE_CODE (type) == TYPENAME_TYPE)
13411                 {
13412                   type = resolve_typename_type (type, 
13413                                                 /*only_current_p=*/false);
13414                   if (type == error_mark_node)
13415                     {
13416                       cp_parser_abort_tentative_parse (parser);
13417                       return false;
13418                     }
13419                 }
13420               push_scope (type);
13421             }
13422           /* Look for the type-specifier.  */
13423           cp_parser_type_specifier (parser,
13424                                     CP_PARSER_FLAGS_NONE,
13425                                     /*is_friend=*/false,
13426                                     /*is_declarator=*/true,
13427                                     /*declares_class_or_enum=*/NULL,
13428                                     /*is_cv_qualifier=*/NULL);
13429           /* Leave the scope of the class.  */
13430           if (type)
13431             pop_scope (type);
13432
13433           constructor_p = !cp_parser_error_occurred (parser);
13434         }
13435     }
13436   else
13437     constructor_p = false;
13438   /* We did not really want to consume any tokens.  */
13439   cp_parser_abort_tentative_parse (parser);
13440
13441   return constructor_p;
13442 }
13443
13444 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13445    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
13446    they must be performed once we are in the scope of the function.
13447
13448    Returns the function defined.  */
13449
13450 static tree
13451 cp_parser_function_definition_from_specifiers_and_declarator
13452   (cp_parser* parser,
13453    tree decl_specifiers,
13454    tree attributes,
13455    tree declarator)
13456 {
13457   tree fn;
13458   bool success_p;
13459
13460   /* Begin the function-definition.  */
13461   success_p = begin_function_definition (decl_specifiers, 
13462                                          attributes, 
13463                                          declarator);
13464
13465   /* If there were names looked up in the decl-specifier-seq that we
13466      did not check, check them now.  We must wait until we are in the
13467      scope of the function to perform the checks, since the function
13468      might be a friend.  */
13469   perform_deferred_access_checks ();
13470
13471   if (!success_p)
13472     {
13473       /* If begin_function_definition didn't like the definition, skip
13474          the entire function.  */
13475       error ("invalid function declaration");
13476       cp_parser_skip_to_end_of_block_or_statement (parser);
13477       fn = error_mark_node;
13478     }
13479   else
13480     fn = cp_parser_function_definition_after_declarator (parser,
13481                                                          /*inline_p=*/false);
13482
13483   return fn;
13484 }
13485
13486 /* Parse the part of a function-definition that follows the
13487    declarator.  INLINE_P is TRUE iff this function is an inline
13488    function defined with a class-specifier.
13489
13490    Returns the function defined.  */
13491
13492 static tree 
13493 cp_parser_function_definition_after_declarator (cp_parser* parser, 
13494                                                 bool inline_p)
13495 {
13496   tree fn;
13497   bool ctor_initializer_p = false;
13498   bool saved_in_unbraced_linkage_specification_p;
13499   unsigned saved_num_template_parameter_lists;
13500
13501   /* If the next token is `return', then the code may be trying to
13502      make use of the "named return value" extension that G++ used to
13503      support.  */
13504   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13505     {
13506       /* Consume the `return' keyword.  */
13507       cp_lexer_consume_token (parser->lexer);
13508       /* Look for the identifier that indicates what value is to be
13509          returned.  */
13510       cp_parser_identifier (parser);
13511       /* Issue an error message.  */
13512       error ("named return values are no longer supported");
13513       /* Skip tokens until we reach the start of the function body.  */
13514       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13515         cp_lexer_consume_token (parser->lexer);
13516     }
13517   /* The `extern' in `extern "C" void f () { ... }' does not apply to
13518      anything declared inside `f'.  */
13519   saved_in_unbraced_linkage_specification_p 
13520     = parser->in_unbraced_linkage_specification_p;
13521   parser->in_unbraced_linkage_specification_p = false;
13522   /* Inside the function, surrounding template-parameter-lists do not
13523      apply.  */
13524   saved_num_template_parameter_lists 
13525     = parser->num_template_parameter_lists; 
13526   parser->num_template_parameter_lists = 0;
13527   /* If the next token is `try', then we are looking at a
13528      function-try-block.  */
13529   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13530     ctor_initializer_p = cp_parser_function_try_block (parser);
13531   /* A function-try-block includes the function-body, so we only do
13532      this next part if we're not processing a function-try-block.  */
13533   else
13534     ctor_initializer_p 
13535       = cp_parser_ctor_initializer_opt_and_function_body (parser);
13536
13537   /* Finish the function.  */
13538   fn = finish_function ((ctor_initializer_p ? 1 : 0) | 
13539                         (inline_p ? 2 : 0));
13540   /* Generate code for it, if necessary.  */
13541   expand_or_defer_fn (fn);
13542   /* Restore the saved values.  */
13543   parser->in_unbraced_linkage_specification_p 
13544     = saved_in_unbraced_linkage_specification_p;
13545   parser->num_template_parameter_lists 
13546     = saved_num_template_parameter_lists;
13547
13548   return fn;
13549 }
13550
13551 /* Parse a template-declaration, assuming that the `export' (and
13552    `extern') keywords, if present, has already been scanned.  MEMBER_P
13553    is as for cp_parser_template_declaration.  */
13554
13555 static void
13556 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
13557 {
13558   tree decl = NULL_TREE;
13559   tree parameter_list;
13560   bool friend_p = false;
13561
13562   /* Look for the `template' keyword.  */
13563   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13564     return;
13565       
13566   /* And the `<'.  */
13567   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13568     return;
13569       
13570   /* Parse the template parameters.  */
13571   begin_template_parm_list ();
13572   /* If the next token is `>', then we have an invalid
13573      specialization.  Rather than complain about an invalid template
13574      parameter, issue an error message here.  */
13575   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13576     {
13577       cp_parser_error (parser, "invalid explicit specialization");
13578       parameter_list = NULL_TREE;
13579     }
13580   else
13581     parameter_list = cp_parser_template_parameter_list (parser);
13582   parameter_list = end_template_parm_list (parameter_list);
13583   /* Look for the `>'.  */
13584   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13585   /* We just processed one more parameter list.  */
13586   ++parser->num_template_parameter_lists;
13587   /* If the next token is `template', there are more template
13588      parameters.  */
13589   if (cp_lexer_next_token_is_keyword (parser->lexer, 
13590                                       RID_TEMPLATE))
13591     cp_parser_template_declaration_after_export (parser, member_p);
13592   else
13593     {
13594       decl = cp_parser_single_declaration (parser,
13595                                            member_p,
13596                                            &friend_p);
13597
13598       /* If this is a member template declaration, let the front
13599          end know.  */
13600       if (member_p && !friend_p && decl)
13601         decl = finish_member_template_decl (decl);
13602       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
13603         make_friend_class (current_class_type, TREE_TYPE (decl));
13604     }
13605   /* We are done with the current parameter list.  */
13606   --parser->num_template_parameter_lists;
13607
13608   /* Finish up.  */
13609   finish_template_decl (parameter_list);
13610
13611   /* Register member declarations.  */
13612   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13613     finish_member_declaration (decl);
13614
13615   /* If DECL is a function template, we must return to parse it later.
13616      (Even though there is no definition, there might be default
13617      arguments that need handling.)  */
13618   if (member_p && decl 
13619       && (TREE_CODE (decl) == FUNCTION_DECL
13620           || DECL_FUNCTION_TEMPLATE_P (decl)))
13621     TREE_VALUE (parser->unparsed_functions_queues)
13622       = tree_cons (NULL_TREE, decl, 
13623                    TREE_VALUE (parser->unparsed_functions_queues));
13624 }
13625
13626 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13627    `function-definition' sequence.  MEMBER_P is true, this declaration
13628    appears in a class scope.
13629
13630    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
13631    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
13632
13633 static tree
13634 cp_parser_single_declaration (cp_parser* parser, 
13635                               bool member_p,
13636                               bool* friend_p)
13637 {
13638   int declares_class_or_enum;
13639   tree decl = NULL_TREE;
13640   tree decl_specifiers;
13641   tree attributes;
13642
13643   /* Parse the dependent declaration.  We don't know yet
13644      whether it will be a function-definition.  */
13645   cp_parser_parse_tentatively (parser);
13646   /* Defer access checks until we know what is being declared.  */
13647   push_deferring_access_checks (dk_deferred);
13648
13649   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13650      alternative.  */
13651   decl_specifiers 
13652     = cp_parser_decl_specifier_seq (parser,
13653                                     CP_PARSER_FLAGS_OPTIONAL,
13654                                     &attributes,
13655                                     &declares_class_or_enum);
13656   /* Gather up the access checks that occurred the
13657      decl-specifier-seq.  */
13658   stop_deferring_access_checks ();
13659
13660   /* Check for the declaration of a template class.  */
13661   if (declares_class_or_enum)
13662     {
13663       if (cp_parser_declares_only_class_p (parser))
13664         {
13665           decl = shadow_tag (decl_specifiers);
13666           if (decl)
13667             decl = TYPE_NAME (decl);
13668           else
13669             decl = error_mark_node;
13670         }
13671     }
13672   else
13673     decl = NULL_TREE;
13674   /* If it's not a template class, try for a template function.  If
13675      the next token is a `;', then this declaration does not declare
13676      anything.  But, if there were errors in the decl-specifiers, then
13677      the error might well have come from an attempted class-specifier.
13678      In that case, there's no need to warn about a missing declarator.  */
13679   if (!decl
13680       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13681           || !value_member (error_mark_node, decl_specifiers)))
13682     decl = cp_parser_init_declarator (parser, 
13683                                       decl_specifiers,
13684                                       attributes,
13685                                       /*function_definition_allowed_p=*/false,
13686                                       member_p,
13687                                       declares_class_or_enum,
13688                                       /*function_definition_p=*/NULL);
13689
13690   pop_deferring_access_checks ();
13691
13692   /* Clear any current qualification; whatever comes next is the start
13693      of something new.  */
13694   parser->scope = NULL_TREE;
13695   parser->qualifying_scope = NULL_TREE;
13696   parser->object_scope = NULL_TREE;
13697   /* Look for a trailing `;' after the declaration.  */
13698   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")
13699       && cp_parser_committed_to_tentative_parse (parser))
13700     cp_parser_skip_to_end_of_block_or_statement (parser);
13701   /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS.  */
13702   if (cp_parser_parse_definitely (parser))
13703     {
13704       if (friend_p)
13705         *friend_p = cp_parser_friend_p (decl_specifiers);
13706     }
13707   /* Otherwise, try a function-definition.  */
13708   else
13709     decl = cp_parser_function_definition (parser, friend_p);
13710
13711   return decl;
13712 }
13713
13714 /* Parse a cast-expression that is not the operand of a unary "&".  */
13715
13716 static tree
13717 cp_parser_simple_cast_expression (cp_parser *parser)
13718 {
13719   return cp_parser_cast_expression (parser, /*address_p=*/false);
13720 }
13721
13722 /* Parse a functional cast to TYPE.  Returns an expression
13723    representing the cast.  */
13724
13725 static tree
13726 cp_parser_functional_cast (cp_parser* parser, tree type)
13727 {
13728   tree expression_list;
13729
13730   expression_list 
13731     = cp_parser_parenthesized_expression_list (parser, false,
13732                                                /*non_constant_p=*/NULL);
13733
13734   return build_functional_cast (type, expression_list);
13735 }
13736
13737 /* MEMBER_FUNCTION is a member function, or a friend.  If default
13738    arguments, or the body of the function have not yet been parsed,
13739    parse them now.  */
13740
13741 static void
13742 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
13743 {
13744   cp_lexer *saved_lexer;
13745
13746   /* If this member is a template, get the underlying
13747      FUNCTION_DECL.  */
13748   if (DECL_FUNCTION_TEMPLATE_P (member_function))
13749     member_function = DECL_TEMPLATE_RESULT (member_function);
13750
13751   /* There should not be any class definitions in progress at this
13752      point; the bodies of members are only parsed outside of all class
13753      definitions.  */
13754   my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
13755   /* While we're parsing the member functions we might encounter more
13756      classes.  We want to handle them right away, but we don't want
13757      them getting mixed up with functions that are currently in the
13758      queue.  */
13759   parser->unparsed_functions_queues
13760     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
13761
13762   /* Make sure that any template parameters are in scope.  */
13763   maybe_begin_member_template_processing (member_function);
13764
13765   /* If the body of the function has not yet been parsed, parse it
13766      now.  */
13767   if (DECL_PENDING_INLINE_P (member_function))
13768     {
13769       tree function_scope;
13770       cp_token_cache *tokens;
13771
13772       /* The function is no longer pending; we are processing it.  */
13773       tokens = DECL_PENDING_INLINE_INFO (member_function);
13774       DECL_PENDING_INLINE_INFO (member_function) = NULL;
13775       DECL_PENDING_INLINE_P (member_function) = 0;
13776       /* If this was an inline function in a local class, enter the scope
13777          of the containing function.  */
13778       function_scope = decl_function_context (member_function);
13779       if (function_scope)
13780         push_function_context_to (function_scope);
13781       
13782       /* Save away the current lexer.  */
13783       saved_lexer = parser->lexer;
13784       /* Make a new lexer to feed us the tokens saved for this function.  */
13785       parser->lexer = cp_lexer_new_from_tokens (tokens);
13786       parser->lexer->next = saved_lexer;
13787       
13788       /* Set the current source position to be the location of the first
13789          token in the saved inline body.  */
13790       cp_lexer_peek_token (parser->lexer);
13791       
13792       /* Let the front end know that we going to be defining this
13793          function.  */
13794       start_function (NULL_TREE, member_function, NULL_TREE,
13795                       SF_PRE_PARSED | SF_INCLASS_INLINE);
13796       
13797       /* Now, parse the body of the function.  */
13798       cp_parser_function_definition_after_declarator (parser,
13799                                                       /*inline_p=*/true);
13800       
13801       /* Leave the scope of the containing function.  */
13802       if (function_scope)
13803         pop_function_context_from (function_scope);
13804       /* Restore the lexer.  */
13805       parser->lexer = saved_lexer;
13806     }
13807
13808   /* Remove any template parameters from the symbol table.  */
13809   maybe_end_member_template_processing ();
13810
13811   /* Restore the queue.  */
13812   parser->unparsed_functions_queues 
13813     = TREE_CHAIN (parser->unparsed_functions_queues);
13814 }
13815
13816 /* If DECL contains any default args, remeber it on the unparsed
13817    functions queue.  */
13818
13819 static void
13820 cp_parser_save_default_args (cp_parser* parser, tree decl)
13821 {
13822   tree probe;
13823
13824   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
13825        probe;
13826        probe = TREE_CHAIN (probe))
13827     if (TREE_PURPOSE (probe))
13828       {
13829         TREE_PURPOSE (parser->unparsed_functions_queues)
13830           = tree_cons (NULL_TREE, decl, 
13831                        TREE_PURPOSE (parser->unparsed_functions_queues));
13832         break;
13833       }
13834   return;
13835 }
13836
13837 /* FN is a FUNCTION_DECL which may contains a parameter with an
13838    unparsed DEFAULT_ARG.  Parse the default args now.  */
13839
13840 static void
13841 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
13842 {
13843   cp_lexer *saved_lexer;
13844   cp_token_cache *tokens;
13845   bool saved_local_variables_forbidden_p;
13846   tree parameters;
13847
13848   for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
13849        parameters;
13850        parameters = TREE_CHAIN (parameters))
13851     {
13852       if (!TREE_PURPOSE (parameters)
13853           || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
13854         continue;
13855   
13856        /* Save away the current lexer.  */
13857       saved_lexer = parser->lexer;
13858        /* Create a new one, using the tokens we have saved.  */
13859       tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
13860       parser->lexer = cp_lexer_new_from_tokens (tokens);
13861
13862        /* Set the current source position to be the location of the
13863           first token in the default argument.  */
13864       cp_lexer_peek_token (parser->lexer);
13865
13866        /* Local variable names (and the `this' keyword) may not appear
13867           in a default argument.  */
13868       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13869       parser->local_variables_forbidden_p = true;
13870        /* Parse the assignment-expression.  */
13871       if (DECL_CONTEXT (fn))
13872         push_nested_class (DECL_CONTEXT (fn));
13873       TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
13874       if (DECL_CONTEXT (fn))
13875         pop_nested_class ();
13876
13877        /* Restore saved state.  */
13878       parser->lexer = saved_lexer;
13879       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13880     }
13881 }
13882
13883 /* Parse the operand of `sizeof' (or a similar operator).  Returns
13884    either a TYPE or an expression, depending on the form of the
13885    input.  The KEYWORD indicates which kind of expression we have
13886    encountered.  */
13887
13888 static tree
13889 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
13890 {
13891   static const char *format;
13892   tree expr = NULL_TREE;
13893   const char *saved_message;
13894   bool saved_constant_expression_p;
13895
13896   /* Initialize FORMAT the first time we get here.  */
13897   if (!format)
13898     format = "types may not be defined in `%s' expressions";
13899
13900   /* Types cannot be defined in a `sizeof' expression.  Save away the
13901      old message.  */
13902   saved_message = parser->type_definition_forbidden_message;
13903   /* And create the new one.  */
13904   parser->type_definition_forbidden_message 
13905     = xmalloc (strlen (format) 
13906                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
13907                + 1 /* `\0' */);
13908   sprintf ((char *) parser->type_definition_forbidden_message,
13909            format, IDENTIFIER_POINTER (ridpointers[keyword]));
13910
13911   /* The restrictions on constant-expressions do not apply inside
13912      sizeof expressions.  */
13913   saved_constant_expression_p = parser->constant_expression_p;
13914   parser->constant_expression_p = false;
13915
13916   /* Do not actually evaluate the expression.  */
13917   ++skip_evaluation;
13918   /* If it's a `(', then we might be looking at the type-id
13919      construction.  */
13920   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13921     {
13922       tree type;
13923
13924       /* We can't be sure yet whether we're looking at a type-id or an
13925          expression.  */
13926       cp_parser_parse_tentatively (parser);
13927       /* Consume the `('.  */
13928       cp_lexer_consume_token (parser->lexer);
13929       /* Parse the type-id.  */
13930       type = cp_parser_type_id (parser);
13931       /* Now, look for the trailing `)'.  */
13932       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13933       /* If all went well, then we're done.  */
13934       if (cp_parser_parse_definitely (parser))
13935         {
13936           /* Build a list of decl-specifiers; right now, we have only
13937              a single type-specifier.  */
13938           type = build_tree_list (NULL_TREE,
13939                                   type);
13940
13941           /* Call grokdeclarator to figure out what type this is.  */
13942           expr = grokdeclarator (NULL_TREE,
13943                                  type,
13944                                  TYPENAME,
13945                                  /*initialized=*/0,
13946                                  /*attrlist=*/NULL);
13947         }
13948     }
13949
13950   /* If the type-id production did not work out, then we must be
13951      looking at the unary-expression production.  */
13952   if (!expr)
13953     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
13954   /* Go back to evaluating expressions.  */
13955   --skip_evaluation;
13956
13957   /* Free the message we created.  */
13958   free ((char *) parser->type_definition_forbidden_message);
13959   /* And restore the old one.  */
13960   parser->type_definition_forbidden_message = saved_message;
13961   parser->constant_expression_p = saved_constant_expression_p;
13962
13963   return expr;
13964 }
13965
13966 /* If the current declaration has no declarator, return true.  */
13967
13968 static bool
13969 cp_parser_declares_only_class_p (cp_parser *parser)
13970 {
13971   /* If the next token is a `;' or a `,' then there is no 
13972      declarator.  */
13973   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13974           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13975 }
13976
13977 /* Simplify EXPR if it is a non-dependent expression.  Returns the
13978    (possibly simplified) expression.  */
13979
13980 static tree
13981 cp_parser_fold_non_dependent_expr (tree expr)
13982 {
13983   /* If we're in a template, but EXPR isn't value dependent, simplify
13984      it.  We're supposed to treat:
13985      
13986        template <typename T> void f(T[1 + 1]);
13987        template <typename T> void f(T[2]);
13988                    
13989      as two declarations of the same function, for example.  */
13990   if (processing_template_decl
13991       && !type_dependent_expression_p (expr)
13992       && !value_dependent_expression_p (expr))
13993     {
13994       HOST_WIDE_INT saved_processing_template_decl;
13995
13996       saved_processing_template_decl = processing_template_decl;
13997       processing_template_decl = 0;
13998       expr = tsubst_copy_and_build (expr,
13999                                     /*args=*/NULL_TREE,
14000                                     tf_error,
14001                                     /*in_decl=*/NULL_TREE,
14002                                     /*function_p=*/false);
14003       processing_template_decl = saved_processing_template_decl;
14004     }
14005   return expr;
14006 }
14007
14008 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14009    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
14010
14011 static bool
14012 cp_parser_friend_p (tree decl_specifiers)
14013 {
14014   while (decl_specifiers)
14015     {
14016       /* See if this decl-specifier is `friend'.  */
14017       if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14018           && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14019         return true;
14020
14021       /* Go on to the next decl-specifier.  */
14022       decl_specifiers = TREE_CHAIN (decl_specifiers);
14023     }
14024
14025   return false;
14026 }
14027
14028 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
14029    issue an error message indicating that TOKEN_DESC was expected.
14030    
14031    Returns the token consumed, if the token had the appropriate type.
14032    Otherwise, returns NULL.  */
14033
14034 static cp_token *
14035 cp_parser_require (cp_parser* parser,
14036                    enum cpp_ttype type,
14037                    const char* token_desc)
14038 {
14039   if (cp_lexer_next_token_is (parser->lexer, type))
14040     return cp_lexer_consume_token (parser->lexer);
14041   else
14042     {
14043       /* Output the MESSAGE -- unless we're parsing tentatively.  */
14044       if (!cp_parser_simulate_error (parser))
14045         error ("expected %s", token_desc);
14046       return NULL;
14047     }
14048 }
14049
14050 /* Like cp_parser_require, except that tokens will be skipped until
14051    the desired token is found.  An error message is still produced if
14052    the next token is not as expected.  */
14053
14054 static void
14055 cp_parser_skip_until_found (cp_parser* parser, 
14056                             enum cpp_ttype type, 
14057                             const char* token_desc)
14058 {
14059   cp_token *token;
14060   unsigned nesting_depth = 0;
14061
14062   if (cp_parser_require (parser, type, token_desc))
14063     return;
14064
14065   /* Skip tokens until the desired token is found.  */
14066   while (true)
14067     {
14068       /* Peek at the next token.  */
14069       token = cp_lexer_peek_token (parser->lexer);
14070       /* If we've reached the token we want, consume it and 
14071          stop.  */
14072       if (token->type == type && !nesting_depth)
14073         {
14074           cp_lexer_consume_token (parser->lexer);
14075           return;
14076         }
14077       /* If we've run out of tokens, stop.  */
14078       if (token->type == CPP_EOF)
14079         return;
14080       if (token->type == CPP_OPEN_BRACE 
14081           || token->type == CPP_OPEN_PAREN
14082           || token->type == CPP_OPEN_SQUARE)
14083         ++nesting_depth;
14084       else if (token->type == CPP_CLOSE_BRACE 
14085                || token->type == CPP_CLOSE_PAREN
14086                || token->type == CPP_CLOSE_SQUARE)
14087         {
14088           if (nesting_depth-- == 0)
14089             return;
14090         }
14091       /* Consume this token.  */
14092       cp_lexer_consume_token (parser->lexer);
14093     }
14094 }
14095
14096 /* If the next token is the indicated keyword, consume it.  Otherwise,
14097    issue an error message indicating that TOKEN_DESC was expected.
14098    
14099    Returns the token consumed, if the token had the appropriate type.
14100    Otherwise, returns NULL.  */
14101
14102 static cp_token *
14103 cp_parser_require_keyword (cp_parser* parser,
14104                            enum rid keyword,
14105                            const char* token_desc)
14106 {
14107   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14108
14109   if (token && token->keyword != keyword)
14110     {
14111       dyn_string_t error_msg;
14112
14113       /* Format the error message.  */
14114       error_msg = dyn_string_new (0);
14115       dyn_string_append_cstr (error_msg, "expected ");
14116       dyn_string_append_cstr (error_msg, token_desc);
14117       cp_parser_error (parser, error_msg->s);
14118       dyn_string_delete (error_msg);
14119       return NULL;
14120     }
14121
14122   return token;
14123 }
14124
14125 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14126    function-definition.  */
14127
14128 static bool 
14129 cp_parser_token_starts_function_definition_p (cp_token* token)
14130 {
14131   return (/* An ordinary function-body begins with an `{'.  */
14132           token->type == CPP_OPEN_BRACE
14133           /* A ctor-initializer begins with a `:'.  */
14134           || token->type == CPP_COLON
14135           /* A function-try-block begins with `try'.  */
14136           || token->keyword == RID_TRY
14137           /* The named return value extension begins with `return'.  */
14138           || token->keyword == RID_RETURN);
14139 }
14140
14141 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14142    definition.  */
14143
14144 static bool
14145 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14146 {
14147   cp_token *token;
14148
14149   token = cp_lexer_peek_token (parser->lexer);
14150   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14151 }
14152
14153 /* Returns TRUE iff the next token is the "," or ">" ending a
14154    template-argument.  */
14155
14156 static bool
14157 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14158 {
14159   cp_token *token;
14160
14161   token = cp_lexer_peek_token (parser->lexer);
14162   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
14163 }
14164  
14165 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14166    or none_type otherwise.  */
14167
14168 static enum tag_types
14169 cp_parser_token_is_class_key (cp_token* token)
14170 {
14171   switch (token->keyword)
14172     {
14173     case RID_CLASS:
14174       return class_type;
14175     case RID_STRUCT:
14176       return record_type;
14177     case RID_UNION:
14178       return union_type;
14179       
14180     default:
14181       return none_type;
14182     }
14183 }
14184
14185 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
14186
14187 static void
14188 cp_parser_check_class_key (enum tag_types class_key, tree type)
14189 {
14190   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14191     pedwarn ("`%s' tag used in naming `%#T'",
14192             class_key == union_type ? "union"
14193              : class_key == record_type ? "struct" : "class", 
14194              type);
14195 }
14196                            
14197 /* Look for the `template' keyword, as a syntactic disambiguator.
14198    Return TRUE iff it is present, in which case it will be 
14199    consumed.  */
14200
14201 static bool
14202 cp_parser_optional_template_keyword (cp_parser *parser)
14203 {
14204   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14205     {
14206       /* The `template' keyword can only be used within templates;
14207          outside templates the parser can always figure out what is a
14208          template and what is not.  */
14209       if (!processing_template_decl)
14210         {
14211           error ("`template' (as a disambiguator) is only allowed "
14212                  "within templates");
14213           /* If this part of the token stream is rescanned, the same
14214              error message would be generated.  So, we purge the token
14215              from the stream.  */
14216           cp_lexer_purge_token (parser->lexer);
14217           return false;
14218         }
14219       else
14220         {
14221           /* Consume the `template' keyword.  */
14222           cp_lexer_consume_token (parser->lexer);
14223           return true;
14224         }
14225     }
14226
14227   return false;
14228 }
14229
14230 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
14231    set PARSER->SCOPE, and perform other related actions.  */
14232
14233 static void
14234 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14235 {
14236   tree value;
14237   tree check;
14238
14239   /* Get the stored value.  */
14240   value = cp_lexer_consume_token (parser->lexer)->value;
14241   /* Perform any access checks that were deferred.  */
14242   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14243     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
14244   /* Set the scope from the stored value.  */
14245   parser->scope = TREE_VALUE (value);
14246   parser->qualifying_scope = TREE_TYPE (value);
14247   parser->object_scope = NULL_TREE;
14248 }
14249
14250 /* Add tokens to CACHE until an non-nested END token appears.  */
14251
14252 static void
14253 cp_parser_cache_group (cp_parser *parser, 
14254                        cp_token_cache *cache,
14255                        enum cpp_ttype end,
14256                        unsigned depth)
14257 {
14258   while (true)
14259     {
14260       cp_token *token;
14261
14262       /* Abort a parenthesized expression if we encounter a brace.  */
14263       if ((end == CPP_CLOSE_PAREN || depth == 0)
14264           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14265         return;
14266       /* Consume the next token.  */
14267       token = cp_lexer_consume_token (parser->lexer);
14268       /* If we've reached the end of the file, stop.  */
14269       if (token->type == CPP_EOF)
14270         return;
14271       /* Add this token to the tokens we are saving.  */
14272       cp_token_cache_push_token (cache, token);
14273       /* See if it starts a new group.  */
14274       if (token->type == CPP_OPEN_BRACE)
14275         {
14276           cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14277           if (depth == 0)
14278             return;
14279         }
14280       else if (token->type == CPP_OPEN_PAREN)
14281         cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14282       else if (token->type == end)
14283         return;
14284     }
14285 }
14286
14287 /* Begin parsing tentatively.  We always save tokens while parsing
14288    tentatively so that if the tentative parsing fails we can restore the
14289    tokens.  */
14290
14291 static void
14292 cp_parser_parse_tentatively (cp_parser* parser)
14293 {
14294   /* Enter a new parsing context.  */
14295   parser->context = cp_parser_context_new (parser->context);
14296   /* Begin saving tokens.  */
14297   cp_lexer_save_tokens (parser->lexer);
14298   /* In order to avoid repetitive access control error messages,
14299      access checks are queued up until we are no longer parsing
14300      tentatively.  */
14301   push_deferring_access_checks (dk_deferred);
14302 }
14303
14304 /* Commit to the currently active tentative parse.  */
14305
14306 static void
14307 cp_parser_commit_to_tentative_parse (cp_parser* parser)
14308 {
14309   cp_parser_context *context;
14310   cp_lexer *lexer;
14311
14312   /* Mark all of the levels as committed.  */
14313   lexer = parser->lexer;
14314   for (context = parser->context; context->next; context = context->next)
14315     {
14316       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14317         break;
14318       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14319       while (!cp_lexer_saving_tokens (lexer))
14320         lexer = lexer->next;
14321       cp_lexer_commit_tokens (lexer);
14322     }
14323 }
14324
14325 /* Abort the currently active tentative parse.  All consumed tokens
14326    will be rolled back, and no diagnostics will be issued.  */
14327
14328 static void
14329 cp_parser_abort_tentative_parse (cp_parser* parser)
14330 {
14331   cp_parser_simulate_error (parser);
14332   /* Now, pretend that we want to see if the construct was
14333      successfully parsed.  */
14334   cp_parser_parse_definitely (parser);
14335 }
14336
14337 /* Stop parsing tentatively.  If a parse error has occurred, restore the
14338    token stream.  Otherwise, commit to the tokens we have consumed.
14339    Returns true if no error occurred; false otherwise.  */
14340
14341 static bool
14342 cp_parser_parse_definitely (cp_parser* parser)
14343 {
14344   bool error_occurred;
14345   cp_parser_context *context;
14346
14347   /* Remember whether or not an error occurred, since we are about to
14348      destroy that information.  */
14349   error_occurred = cp_parser_error_occurred (parser);
14350   /* Remove the topmost context from the stack.  */
14351   context = parser->context;
14352   parser->context = context->next;
14353   /* If no parse errors occurred, commit to the tentative parse.  */
14354   if (!error_occurred)
14355     {
14356       /* Commit to the tokens read tentatively, unless that was
14357          already done.  */
14358       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14359         cp_lexer_commit_tokens (parser->lexer);
14360
14361       pop_to_parent_deferring_access_checks ();
14362     }
14363   /* Otherwise, if errors occurred, roll back our state so that things
14364      are just as they were before we began the tentative parse.  */
14365   else
14366     {
14367       cp_lexer_rollback_tokens (parser->lexer);
14368       pop_deferring_access_checks ();
14369     }
14370   /* Add the context to the front of the free list.  */
14371   context->next = cp_parser_context_free_list;
14372   cp_parser_context_free_list = context;
14373
14374   return !error_occurred;
14375 }
14376
14377 /* Returns true if we are parsing tentatively -- but have decided that
14378    we will stick with this tentative parse, even if errors occur.  */
14379
14380 static bool
14381 cp_parser_committed_to_tentative_parse (cp_parser* parser)
14382 {
14383   return (cp_parser_parsing_tentatively (parser)
14384           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14385 }
14386
14387 /* Returns nonzero iff an error has occurred during the most recent
14388    tentative parse.  */
14389    
14390 static bool
14391 cp_parser_error_occurred (cp_parser* parser)
14392 {
14393   return (cp_parser_parsing_tentatively (parser)
14394           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14395 }
14396
14397 /* Returns nonzero if GNU extensions are allowed.  */
14398
14399 static bool
14400 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
14401 {
14402   return parser->allow_gnu_extensions_p;
14403 }
14404
14405 \f
14406
14407 /* The parser.  */
14408
14409 static GTY (()) cp_parser *the_parser;
14410
14411 /* External interface.  */
14412
14413 /* Parse one entire translation unit.  */
14414
14415 void
14416 c_parse_file (void)
14417 {
14418   bool error_occurred;
14419
14420   the_parser = cp_parser_new ();
14421   push_deferring_access_checks (flag_access_control
14422                                 ? dk_no_deferred : dk_no_check);
14423   error_occurred = cp_parser_translation_unit (the_parser);
14424   the_parser = NULL;
14425 }
14426
14427 /* Clean up after parsing the entire translation unit.  */
14428
14429 void
14430 free_parser_stacks (void)
14431 {
14432   /* Nothing to do.  */
14433 }
14434
14435 /* This variable must be provided by every front end.  */
14436
14437 int yydebug;
14438
14439 #include "gt-cp-parser.h"