OSDN Git Service

* parser.c (cp_parser_declaration): Accept the __extension__
[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 "ggc.h"
36 #include "toplev.h"
37 #include "output.h"
38
39 \f
40 /* The lexer.  */
41
42 /* Overview
43    --------
44
45    A cp_lexer represents a stream of cp_tokens.  It allows arbitrary
46    look-ahead.
47
48    Methodology
49    -----------
50
51    We use a circular buffer to store incoming tokens.
52
53    Some artifacts of the C++ language (such as the
54    expression/declaration ambiguity) require arbitrary look-ahead.
55    The strategy we adopt for dealing with these problems is to attempt
56    to parse one construct (e.g., the declaration) and fall back to the
57    other (e.g., the expression) if that attempt does not succeed.
58    Therefore, we must sometimes store an arbitrary number of tokens.
59
60    The parser routinely peeks at the next token, and then consumes it
61    later.  That also requires a buffer in which to store the tokens.
62      
63    In order to easily permit adding tokens to the end of the buffer,
64    while removing them from the beginning of the buffer, we use a
65    circular buffer.  */
66
67 /* A C++ token.  */
68
69 typedef struct cp_token GTY (())
70 {
71   /* The kind of token.  */
72   enum cpp_ttype type;
73   /* The value associated with this token, if any.  */
74   tree value;
75   /* If this token is a keyword, this value indicates which keyword.
76      Otherwise, this value is RID_MAX.  */
77   enum rid keyword;
78   /* The file in which this token was found.  */
79   const char *file_name;
80   /* The line at which this token was found.  */
81   int line_number;
82 } cp_token;
83
84 /* The number of tokens in a single token block.  */
85
86 #define CP_TOKEN_BLOCK_NUM_TOKENS 32
87
88 /* A group of tokens.  These groups are chained together to store
89    large numbers of tokens.  (For example, a token block is created
90    when the body of an inline member function is first encountered;
91    the tokens are processed later after the class definition is
92    complete.)  
93
94    This somewhat ungainly data structure (as opposed to, say, a
95    variable-length array), is used due to contraints imposed by the
96    current garbage-collection methodology.  If it is made more
97    flexible, we could perhaps simplify the data structures involved.  */
98
99 typedef struct cp_token_block GTY (())
100 {
101   /* The tokens.  */
102   cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
103   /* The number of tokens in this block.  */
104   size_t num_tokens;
105   /* The next token block in the chain.  */
106   struct cp_token_block *next;
107   /* The previous block in the chain.  */
108   struct cp_token_block *prev;
109 } cp_token_block;
110
111 typedef struct cp_token_cache GTY (())
112 {
113   /* The first block in the cache.  NULL if there are no tokens in the
114      cache.  */
115   cp_token_block *first;
116   /* The last block in the cache.  NULL If there are no tokens in the
117      cache.  */
118   cp_token_block *last;
119 } cp_token_cache;
120
121 /* Prototypes. */
122
123 static cp_token_cache *cp_token_cache_new 
124   (void);
125 static void cp_token_cache_push_token
126   (cp_token_cache *, cp_token *);
127
128 /* Create a new cp_token_cache.  */
129
130 static cp_token_cache *
131 cp_token_cache_new ()
132 {
133   return (cp_token_cache *) ggc_alloc_cleared (sizeof (cp_token_cache));
134 }
135
136 /* Add *TOKEN to *CACHE.  */
137
138 static void
139 cp_token_cache_push_token (cp_token_cache *cache,
140                            cp_token *token)
141 {
142   cp_token_block *b = cache->last;
143
144   /* See if we need to allocate a new token block.  */
145   if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
146     {
147       b = ((cp_token_block *) ggc_alloc_cleared (sizeof (cp_token_block)));
148       b->prev = cache->last;
149       if (cache->last)
150         {
151           cache->last->next = b;
152           cache->last = b;
153         }
154       else
155         cache->first = cache->last = b;
156     }
157   /* Add this token to the current token block.  */
158   b->tokens[b->num_tokens++] = *token;
159 }
160
161 /* The cp_lexer structure represents the C++ lexer.  It is responsible
162    for managing the token stream from the preprocessor and supplying
163    it to the parser.  */
164
165 typedef struct cp_lexer GTY (())
166 {
167   /* The memory allocated for the buffer.  Never NULL.  */
168   cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
169   /* A pointer just past the end of the memory allocated for the buffer.  */
170   cp_token * GTY ((skip (""))) buffer_end;
171   /* The first valid token in the buffer, or NULL if none.  */
172   cp_token * GTY ((skip (""))) first_token;
173   /* The next available token.  If NEXT_TOKEN is NULL, then there are
174      no more available tokens.  */
175   cp_token * GTY ((skip (""))) next_token;
176   /* A pointer just past the last available token.  If FIRST_TOKEN is
177      NULL, however, there are no available tokens, and then this
178      location is simply the place in which the next token read will be
179      placed.  If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180      When the LAST_TOKEN == BUFFER, then the last token is at the
181      highest memory address in the BUFFER.  */
182   cp_token * GTY ((skip (""))) last_token;
183
184   /* A stack indicating positions at which cp_lexer_save_tokens was
185      called.  The top entry is the most recent position at which we
186      began saving tokens.  The entries are differences in token
187      position between FIRST_TOKEN and the first saved token.
188
189      If the stack is non-empty, we are saving tokens.  When a token is
190      consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191      pointer will not.  The token stream will be preserved so that it
192      can be reexamined later.
193
194      If the stack is empty, then we are not saving tokens.  Whenever a
195      token is consumed, the FIRST_TOKEN pointer will be moved, and the
196      consumed token will be gone forever.  */
197   varray_type saved_tokens;
198
199   /* The STRING_CST tokens encountered while processing the current
200      string literal.  */
201   varray_type string_tokens;
202
203   /* True if we should obtain more tokens from the preprocessor; false
204      if we are processing a saved token cache.  */
205   bool main_lexer_p;
206
207   /* True if we should output debugging information.  */
208   bool debugging_p;
209
210   /* The next lexer in a linked list of lexers.  */
211   struct cp_lexer *next;
212 } cp_lexer;
213
214 /* Prototypes.  */
215
216 static cp_lexer *cp_lexer_new
217   PARAMS ((bool));
218 static cp_lexer *cp_lexer_new_from_tokens
219   PARAMS ((struct cp_token_cache *));
220 static int cp_lexer_saving_tokens
221   PARAMS ((const cp_lexer *));
222 static cp_token *cp_lexer_next_token
223   PARAMS ((cp_lexer *, cp_token *));
224 static ptrdiff_t cp_lexer_token_difference
225   PARAMS ((cp_lexer *, cp_token *, cp_token *));
226 static cp_token *cp_lexer_read_token
227   PARAMS ((cp_lexer *));
228 static void cp_lexer_maybe_grow_buffer
229   PARAMS ((cp_lexer *));
230 static void cp_lexer_get_preprocessor_token
231   PARAMS ((cp_lexer *, cp_token *));
232 static cp_token *cp_lexer_peek_token
233   PARAMS ((cp_lexer *));
234 static cp_token *cp_lexer_peek_nth_token
235   PARAMS ((cp_lexer *, size_t));
236 static inline bool cp_lexer_next_token_is
237   PARAMS ((cp_lexer *, enum cpp_ttype));
238 static bool cp_lexer_next_token_is_not
239   PARAMS ((cp_lexer *, enum cpp_ttype));
240 static bool cp_lexer_next_token_is_keyword
241   PARAMS ((cp_lexer *, enum rid));
242 static cp_token *cp_lexer_consume_token
243   PARAMS ((cp_lexer *));
244 static void cp_lexer_purge_token
245   (cp_lexer *);
246 static void cp_lexer_purge_tokens_after
247   (cp_lexer *, cp_token *);
248 static void cp_lexer_save_tokens
249   PARAMS ((cp_lexer *));
250 static void cp_lexer_commit_tokens
251   PARAMS ((cp_lexer *));
252 static void cp_lexer_rollback_tokens
253   PARAMS ((cp_lexer *));
254 static inline void cp_lexer_set_source_position_from_token 
255   PARAMS ((cp_lexer *, const cp_token *));
256 static void cp_lexer_print_token
257   PARAMS ((FILE *, cp_token *));
258 static inline bool cp_lexer_debugging_p 
259   PARAMS ((cp_lexer *));
260 static void cp_lexer_start_debugging
261   PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
262 static void cp_lexer_stop_debugging
263   PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
264
265 /* Manifest constants.  */
266
267 #define CP_TOKEN_BUFFER_SIZE 5
268 #define CP_SAVED_TOKENS_SIZE 5
269
270 /* A token type for keywords, as opposed to ordinary identifiers.  */
271 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
272
273 /* A token type for template-ids.  If a template-id is processed while
274    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
275    the value of the CPP_TEMPLATE_ID is whatever was returned by
276    cp_parser_template_id.  */
277 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
278
279 /* A token type for nested-name-specifiers.  If a
280    nested-name-specifier is processed while parsing tentatively, it is
281    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
282    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
283    cp_parser_nested_name_specifier_opt.  */
284 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
285
286 /* A token type for tokens that are not tokens at all; these are used
287    to mark the end of a token block.  */
288 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
289
290 /* Variables.  */
291
292 /* The stream to which debugging output should be written.  */
293 static FILE *cp_lexer_debug_stream;
294
295 /* Create a new C++ lexer.  If MAIN_LEXER_P is true the new lexer is
296    the main lexer -- i.e, the lexer that gets tokens from the
297    preprocessor.  Otherwise, it is a lexer that uses a cache of stored
298    tokens.  */
299
300 static cp_lexer *
301 cp_lexer_new (bool main_lexer_p)
302 {
303   cp_lexer *lexer;
304
305   /* Allocate the memory.  */
306   lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
307
308   /* Create the circular buffer.  */
309   lexer->buffer = ((cp_token *) 
310                    ggc_alloc (CP_TOKEN_BUFFER_SIZE * sizeof (cp_token)));
311   lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
312
313   /* There are no tokens in the buffer.  */
314   lexer->last_token = lexer->buffer;
315
316   /* This lexer obtains more tokens by calling c_lex.  */
317   lexer->main_lexer_p = main_lexer_p;
318
319   /* Create the SAVED_TOKENS stack.  */
320   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
321   
322   /* Create the STRINGS array.  */
323   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
324
325   /* Assume we are not debugging.  */
326   lexer->debugging_p = false;
327
328   return lexer;
329 }
330
331 /* Create a new lexer whose token stream is primed with the TOKENS.
332    When these tokens are exhausted, no new tokens will be read.  */
333
334 static cp_lexer *
335 cp_lexer_new_from_tokens (cp_token_cache *tokens)
336 {
337   cp_lexer *lexer;
338   cp_token *token;
339   cp_token_block *block;
340   ptrdiff_t num_tokens;
341
342   /* Create the lexer.  */
343   lexer = cp_lexer_new (/*main_lexer_p=*/false);
344
345   /* Create a new buffer, appropriately sized.  */
346   num_tokens = 0;
347   for (block = tokens->first; block != NULL; block = block->next)
348     num_tokens += block->num_tokens;
349   lexer->buffer = ((cp_token *) 
350                    ggc_alloc (num_tokens * sizeof (cp_token)));
351   lexer->buffer_end = lexer->buffer + num_tokens;
352   
353   /* Install the tokens.  */
354   token = lexer->buffer;
355   for (block = tokens->first; block != NULL; block = block->next)
356     {
357       memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
358       token += block->num_tokens;
359     }
360
361   /* The FIRST_TOKEN is the beginning of the buffer.  */
362   lexer->first_token = lexer->buffer;
363   /* The next available token is also at the beginning of the buffer.  */
364   lexer->next_token = lexer->buffer;
365   /* The buffer is full.  */
366   lexer->last_token = lexer->first_token;
367
368   return lexer;
369 }
370
371 /* Returns non-zero if debugging information should be output.  */
372
373 static inline bool
374 cp_lexer_debugging_p (cp_lexer *lexer)
375 {
376   return lexer->debugging_p;
377 }
378
379 /* Set the current source position from the information stored in
380    TOKEN.  */
381
382 static inline void
383 cp_lexer_set_source_position_from_token (lexer, token)
384      cp_lexer *lexer ATTRIBUTE_UNUSED;
385      const cp_token *token;
386 {
387   /* Ideally, the source position information would not be a global
388      variable, but it is.  */
389
390   /* Update the line number.  */
391   if (token->type != CPP_EOF)
392     {
393       lineno = token->line_number;
394       input_filename = token->file_name;
395     }
396 }
397
398 /* TOKEN points into the circular token buffer.  Return a pointer to
399    the next token in the buffer.  */
400
401 static inline cp_token *
402 cp_lexer_next_token (lexer, token)
403      cp_lexer *lexer;
404      cp_token *token;
405 {
406   token++;
407   if (token == lexer->buffer_end)
408     token = lexer->buffer;
409   return token;
410 }
411
412 /* Non-zero if we are presently saving tokens.  */
413
414 static int
415 cp_lexer_saving_tokens (lexer)
416      const cp_lexer *lexer;
417 {
418   return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
419 }
420
421 /* Return a pointer to the token that is N tokens beyond TOKEN in the
422    buffer.  */
423
424 static cp_token *
425 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
426 {
427   token += n;
428   if (token >= lexer->buffer_end)
429     token = lexer->buffer + (token - lexer->buffer_end);
430   return token;
431 }
432
433 /* Returns the number of times that START would have to be incremented
434    to reach FINISH.  If START and FINISH are the same, returns zero.  */
435
436 static ptrdiff_t
437 cp_lexer_token_difference (lexer, start, finish)
438      cp_lexer *lexer;
439      cp_token *start;
440      cp_token *finish;
441 {
442   if (finish >= start)
443     return finish - start;
444   else
445     return ((lexer->buffer_end - lexer->buffer)
446             - (start - finish));
447 }
448
449 /* Obtain another token from the C preprocessor and add it to the
450    token buffer.  Returns the newly read token.  */
451
452 static cp_token *
453 cp_lexer_read_token (lexer)
454      cp_lexer *lexer;
455 {
456   cp_token *token;
457
458   /* Make sure there is room in the buffer.  */
459   cp_lexer_maybe_grow_buffer (lexer);
460
461   /* If there weren't any tokens, then this one will be the first.  */
462   if (!lexer->first_token)
463     lexer->first_token = lexer->last_token;
464   /* Similarly, if there were no available tokens, there is one now.  */
465   if (!lexer->next_token)
466     lexer->next_token = lexer->last_token;
467
468   /* Figure out where we're going to store the new token.  */
469   token = lexer->last_token;
470
471   /* Get a new token from the preprocessor.  */
472   cp_lexer_get_preprocessor_token (lexer, token);
473
474   /* Increment LAST_TOKEN.  */
475   lexer->last_token = cp_lexer_next_token (lexer, token);
476
477   /* The preprocessor does not yet do translation phase six, i.e., the
478      combination of adjacent string literals.  Therefore, we do it
479      here.  */
480   if (token->type == CPP_STRING || token->type == CPP_WSTRING)
481     {
482       ptrdiff_t delta;
483       int i;
484
485       /* When we grow the buffer, we may invalidate TOKEN.  So, save
486          the distance from the beginning of the BUFFER so that we can
487          recaulate it.  */
488       delta = cp_lexer_token_difference (lexer, lexer->buffer, token);
489       /* Make sure there is room in the buffer for another token.  */
490       cp_lexer_maybe_grow_buffer (lexer);
491       /* Restore TOKEN.  */
492       token = lexer->buffer;
493       for (i = 0; i < delta; ++i)
494         token = cp_lexer_next_token (lexer, token);
495
496       VARRAY_PUSH_TREE (lexer->string_tokens, token->value);
497       while (true)
498         {
499           /* Read the token after TOKEN.  */
500           cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
501           /* See whether it's another string constant.  */
502           if (lexer->last_token->type != token->type)
503             {
504               /* If not, then it will be the next real token.  */
505               lexer->last_token = cp_lexer_next_token (lexer, 
506                                                        lexer->last_token);
507               break;
508             }
509
510           /* Chain the strings together.  */
511           VARRAY_PUSH_TREE (lexer->string_tokens, 
512                             lexer->last_token->value);
513         }
514
515       /* Create a single STRING_CST.  Curiously we have to call
516          combine_strings even if there is only a single string in
517          order to get the type set correctly.  */
518       token->value = combine_strings (lexer->string_tokens);
519       VARRAY_CLEAR (lexer->string_tokens);
520       token->value = fix_string_type (token->value);
521       /* Strings should have type `const char []'.  Right now, we will
522          have an ARRAY_TYPE that is constant rather than an array of
523          constant elements.  */
524       if (flag_const_strings)
525         {
526           tree type;
527
528           /* Get the current type.  It will be an ARRAY_TYPE.  */
529           type = TREE_TYPE (token->value);
530           /* Use build_cplus_array_type to rebuild the array, thereby
531              getting the right type.  */
532           type = build_cplus_array_type (TREE_TYPE (type),
533                                          TYPE_DOMAIN (type));
534           /* Reset the type of the token.  */
535           TREE_TYPE (token->value) = type;
536         }
537     }
538
539   return token;
540 }
541
542 /* If the circular buffer is full, make it bigger.  */
543
544 static void
545 cp_lexer_maybe_grow_buffer (lexer)
546      cp_lexer *lexer;
547 {
548   /* If the buffer is full, enlarge it.  */
549   if (lexer->last_token == lexer->first_token)
550     {
551       cp_token *new_buffer;
552       cp_token *old_buffer;
553       cp_token *new_first_token;
554       ptrdiff_t buffer_length;
555       size_t num_tokens_to_copy;
556
557       /* Remember the current buffer pointer.  It will become invalid,
558          but we will need to do pointer arithmetic involving this
559          value.  */
560       old_buffer = lexer->buffer;
561       /* Compute the current buffer size.  */
562       buffer_length = lexer->buffer_end - lexer->buffer;
563       /* Allocate a buffer twice as big.  */
564       new_buffer = ((cp_token *)
565                     ggc_realloc (lexer->buffer, 
566                                  2 * buffer_length * sizeof (cp_token)));
567       
568       /* Because the buffer is circular, logically consecutive tokens
569          are not necessarily placed consecutively in memory.
570          Therefore, we must keep move the tokens that were before
571          FIRST_TOKEN to the second half of the newly allocated
572          buffer.  */
573       num_tokens_to_copy = (lexer->first_token - old_buffer);
574       memcpy (new_buffer + buffer_length,
575               new_buffer,
576               num_tokens_to_copy * sizeof (cp_token));
577       /* Clear the rest of the buffer.  We never look at this storage,
578          but the garbage collector may.  */
579       memset (new_buffer + buffer_length + num_tokens_to_copy, 0, 
580               (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
581
582       /* Now recompute all of the buffer pointers.  */
583       new_first_token 
584         = new_buffer + (lexer->first_token - old_buffer);
585       if (lexer->next_token != NULL)
586         {
587           ptrdiff_t next_token_delta;
588
589           if (lexer->next_token > lexer->first_token)
590             next_token_delta = lexer->next_token - lexer->first_token;
591           else
592             next_token_delta = 
593               buffer_length - (lexer->first_token - lexer->next_token);
594           lexer->next_token = new_first_token + next_token_delta;
595         }
596       lexer->last_token = new_first_token + buffer_length;
597       lexer->buffer = new_buffer;
598       lexer->buffer_end = new_buffer + buffer_length * 2;
599       lexer->first_token = new_first_token;
600     }
601 }
602
603 /* Store the next token from the preprocessor in *TOKEN.  */
604
605 static void 
606 cp_lexer_get_preprocessor_token (lexer, token)
607      cp_lexer *lexer ATTRIBUTE_UNUSED;
608      cp_token *token;
609 {
610   bool done;
611
612   /* If this not the main lexer, return a terminating CPP_EOF token.  */
613   if (!lexer->main_lexer_p)
614     {
615       token->type = CPP_EOF;
616       token->line_number = 0;
617       token->file_name = NULL;
618       token->value = NULL_TREE;
619       token->keyword = RID_MAX;
620
621       return;
622     }
623
624   done = false;
625   /* Keep going until we get a token we like.  */
626   while (!done)
627     {
628       /* Get a new token from the preprocessor.  */
629       token->type = c_lex (&token->value);
630       /* Issue messages about tokens we cannot process.  */
631       switch (token->type)
632         {
633         case CPP_ATSIGN:
634         case CPP_HASH:
635         case CPP_PASTE:
636           error ("invalid token");
637           break;
638
639         case CPP_OTHER:
640           /* These tokens are already warned about by c_lex.  */
641           break;
642
643         default:
644           /* This is a good token, so we exit the loop.  */
645           done = true;
646           break;
647         }
648     }
649   /* Now we've got our token.  */
650   token->line_number = lineno;
651   token->file_name = input_filename;
652
653   /* Check to see if this token is a keyword.  */
654   if (token->type == CPP_NAME 
655       && C_IS_RESERVED_WORD (token->value))
656     {
657       /* Mark this token as a keyword.  */
658       token->type = CPP_KEYWORD;
659       /* Record which keyword.  */
660       token->keyword = C_RID_CODE (token->value);
661       /* Update the value.  Some keywords are mapped to particular
662          entities, rather than simply having the value of the
663          corresponding IDENTIFIER_NODE.  For example, `__const' is
664          mapped to `const'.  */
665       token->value = ridpointers[token->keyword];
666     }
667   else
668     token->keyword = RID_MAX;
669 }
670
671 /* Return a pointer to the next token in the token stream, but do not
672    consume it.  */
673
674 static cp_token *
675 cp_lexer_peek_token (lexer)
676      cp_lexer *lexer;
677 {
678   cp_token *token;
679
680   /* If there are no tokens, read one now.  */
681   if (!lexer->next_token)
682     cp_lexer_read_token (lexer);
683
684   /* Provide debugging output.  */
685   if (cp_lexer_debugging_p (lexer))
686     {
687       fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
688       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
689       fprintf (cp_lexer_debug_stream, "\n");
690     }
691
692   token = lexer->next_token;
693   cp_lexer_set_source_position_from_token (lexer, token);
694   return token;
695 }
696
697 /* Return true if the next token has the indicated TYPE.  */
698
699 static bool
700 cp_lexer_next_token_is (lexer, type)
701      cp_lexer *lexer;
702      enum cpp_ttype type;
703 {
704   cp_token *token;
705
706   /* Peek at the next token.  */
707   token = cp_lexer_peek_token (lexer);
708   /* Check to see if it has the indicated TYPE.  */
709   return token->type == type;
710 }
711
712 /* Return true if the next token does not have the indicated TYPE.  */
713
714 static bool
715 cp_lexer_next_token_is_not (lexer, type)
716      cp_lexer *lexer;
717      enum cpp_ttype type;
718 {
719   return !cp_lexer_next_token_is (lexer, type);
720 }
721
722 /* Return true if the next token is the indicated KEYWORD.  */
723
724 static bool
725 cp_lexer_next_token_is_keyword (lexer, keyword)
726      cp_lexer *lexer;
727      enum rid keyword;
728 {
729   cp_token *token;
730
731   /* Peek at the next token.  */
732   token = cp_lexer_peek_token (lexer);
733   /* Check to see if it is the indicated keyword.  */
734   return token->keyword == keyword;
735 }
736
737 /* Return a pointer to the Nth token in the token stream.  If N is 1,
738    then this is precisely equivalent to cp_lexer_peek_token.  */
739
740 static cp_token *
741 cp_lexer_peek_nth_token (lexer, n)
742      cp_lexer *lexer;
743      size_t n;
744 {
745   cp_token *token;
746
747   /* N is 1-based, not zero-based.  */
748   my_friendly_assert (n > 0, 20000224);
749
750   /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary.  */
751   token = lexer->next_token;
752   /* If there are no tokens in the buffer, get one now.  */
753   if (!token)
754     {
755       cp_lexer_read_token (lexer);
756       token = lexer->next_token;
757     }
758
759   /* Now, read tokens until we have enough.  */
760   while (--n > 0)
761     {
762       /* Advance to the next token.  */
763       token = cp_lexer_next_token (lexer, token);
764       /* If that's all the tokens we have, read a new one.  */
765       if (token == lexer->last_token)
766         token = cp_lexer_read_token (lexer);
767     }
768
769   return token;
770 }
771
772 /* Consume the next token.  The pointer returned is valid only until
773    another token is read.  Callers should preserve copy the token
774    explicitly if they will need its value for a longer period of
775    time.  */
776
777 static cp_token *
778 cp_lexer_consume_token (lexer)
779      cp_lexer *lexer;
780 {
781   cp_token *token;
782
783   /* If there are no tokens, read one now.  */
784   if (!lexer->next_token)
785     cp_lexer_read_token (lexer);
786
787   /* Remember the token we'll be returning.  */
788   token = lexer->next_token;
789
790   /* Increment NEXT_TOKEN.  */
791   lexer->next_token = cp_lexer_next_token (lexer, 
792                                            lexer->next_token);
793   /* Check to see if we're all out of tokens.  */
794   if (lexer->next_token == lexer->last_token)
795     lexer->next_token = NULL;
796
797   /* If we're not saving tokens, then move FIRST_TOKEN too.  */
798   if (!cp_lexer_saving_tokens (lexer))
799     {
800       /* If there are no tokens available, set FIRST_TOKEN to NULL.  */
801       if (!lexer->next_token)
802         lexer->first_token = NULL;
803       else
804         lexer->first_token = lexer->next_token;
805     }
806
807   /* Provide debugging output.  */
808   if (cp_lexer_debugging_p (lexer))
809     {
810       fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
811       cp_lexer_print_token (cp_lexer_debug_stream, token);
812       fprintf (cp_lexer_debug_stream, "\n");
813     }
814
815   return token;
816 }
817
818 /* Permanently remove the next token from the token stream.  There
819    must be a valid next token already; this token never reads
820    additional tokens from the preprocessor.  */
821
822 static void
823 cp_lexer_purge_token (cp_lexer *lexer)
824 {
825   cp_token *token;
826   cp_token *next_token;
827
828   token = lexer->next_token;
829   while (true) 
830     {
831       next_token = cp_lexer_next_token (lexer, token);
832       if (next_token == lexer->last_token)
833         break;
834       *token = *next_token;
835       token = next_token;
836     }
837
838   lexer->last_token = token;
839   /* The token purged may have been the only token remaining; if so,
840      clear NEXT_TOKEN.  */
841   if (lexer->next_token == token)
842     lexer->next_token = NULL;
843 }
844
845 /* Permanently remove all tokens after TOKEN, up to, but not
846    including, the token that will be returned next by
847    cp_lexer_peek_token.  */
848
849 static void
850 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
851 {
852   cp_token *peek;
853   cp_token *t1;
854   cp_token *t2;
855
856   if (lexer->next_token)
857     {
858       /* Copy the tokens that have not yet been read to the location
859          immediately following TOKEN.  */
860       t1 = cp_lexer_next_token (lexer, token);
861       t2 = peek = cp_lexer_peek_token (lexer);
862       /* Move tokens into the vacant area between TOKEN and PEEK.  */
863       while (t2 != lexer->last_token)
864         {
865           *t1 = *t2;
866           t1 = cp_lexer_next_token (lexer, t1);
867           t2 = cp_lexer_next_token (lexer, t2);
868         }
869       /* Now, the next available token is right after TOKEN.  */
870       lexer->next_token = cp_lexer_next_token (lexer, token);
871       /* And the last token is wherever we ended up.  */
872       lexer->last_token = t1;
873     }
874   else
875     {
876       /* There are no tokens in the buffer, so there is nothing to
877          copy.  The last token in the buffer is TOKEN itself.  */
878       lexer->last_token = cp_lexer_next_token (lexer, token);
879     }
880 }
881
882 /* Begin saving tokens.  All tokens consumed after this point will be
883    preserved.  */
884
885 static void
886 cp_lexer_save_tokens (lexer)
887      cp_lexer *lexer;
888 {
889   /* Provide debugging output.  */
890   if (cp_lexer_debugging_p (lexer))
891     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
892
893   /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
894      restore the tokens if required.  */
895   if (!lexer->next_token)
896     cp_lexer_read_token (lexer);
897
898   VARRAY_PUSH_INT (lexer->saved_tokens,
899                    cp_lexer_token_difference (lexer,
900                                               lexer->first_token,
901                                               lexer->next_token));
902 }
903
904 /* Commit to the portion of the token stream most recently saved.  */
905
906 static void
907 cp_lexer_commit_tokens (lexer)
908      cp_lexer *lexer;
909 {
910   /* Provide debugging output.  */
911   if (cp_lexer_debugging_p (lexer))
912     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
913
914   VARRAY_POP (lexer->saved_tokens);
915 }
916
917 /* Return all tokens saved since the last call to cp_lexer_save_tokens
918    to the token stream.  Stop saving tokens.  */
919
920 static void
921 cp_lexer_rollback_tokens (lexer)
922      cp_lexer *lexer;
923 {
924   size_t delta;
925
926   /* Provide debugging output.  */
927   if (cp_lexer_debugging_p (lexer))
928     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
929
930   /* Find the token that was the NEXT_TOKEN when we started saving
931      tokens.  */
932   delta = VARRAY_TOP_INT(lexer->saved_tokens);
933   /* Make it the next token again now.  */
934   lexer->next_token = cp_lexer_advance_token (lexer,
935                                               lexer->first_token, 
936                                               delta);
937   /* It might be the case that there wer no tokens when we started
938      saving tokens, but that there are some tokens now.  */
939   if (!lexer->next_token && lexer->first_token)
940     lexer->next_token = lexer->first_token;
941
942   /* Stop saving tokens.  */
943   VARRAY_POP (lexer->saved_tokens);
944 }
945
946 /* Print a representation of the TOKEN on the STREAM.  */
947
948 static void
949 cp_lexer_print_token (stream, token)
950      FILE *stream;
951      cp_token *token;
952 {
953   const char *token_type = NULL;
954
955   /* Figure out what kind of token this is.  */
956   switch (token->type)
957     {
958     case CPP_EQ:
959       token_type = "EQ";
960       break;
961
962     case CPP_COMMA:
963       token_type = "COMMA";
964       break;
965
966     case CPP_OPEN_PAREN:
967       token_type = "OPEN_PAREN";
968       break;
969
970     case CPP_CLOSE_PAREN:
971       token_type = "CLOSE_PAREN";
972       break;
973
974     case CPP_OPEN_BRACE:
975       token_type = "OPEN_BRACE";
976       break;
977
978     case CPP_CLOSE_BRACE:
979       token_type = "CLOSE_BRACE";
980       break;
981
982     case CPP_SEMICOLON:
983       token_type = "SEMICOLON";
984       break;
985
986     case CPP_NAME:
987       token_type = "NAME";
988       break;
989
990     case CPP_EOF:
991       token_type = "EOF";
992       break;
993
994     case CPP_KEYWORD:
995       token_type = "keyword";
996       break;
997
998       /* This is not a token that we know how to handle yet.  */
999     default:
1000       break;
1001     }
1002
1003   /* If we have a name for the token, print it out.  Otherwise, we
1004      simply give the numeric code.  */
1005   if (token_type)
1006     fprintf (stream, "%s", token_type);
1007   else
1008     fprintf (stream, "%d", token->type);
1009   /* And, for an identifier, print the identifier name.  */
1010   if (token->type == CPP_NAME 
1011       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1012          For example, `struct' is mapped to an INTEGER_CST.  */
1013       || (token->type == CPP_KEYWORD 
1014           && TREE_CODE (token->value) == IDENTIFIER_NODE))
1015     fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
1016 }
1017
1018 /* Start emitting debugging information.  */
1019
1020 static void
1021 cp_lexer_start_debugging (lexer)
1022      cp_lexer *lexer;
1023 {
1024   ++lexer->debugging_p;
1025 }
1026   
1027 /* Stop emitting debugging information.  */
1028
1029 static void
1030 cp_lexer_stop_debugging (lexer)
1031      cp_lexer *lexer;
1032 {
1033   --lexer->debugging_p;
1034 }
1035
1036 \f
1037 /* The parser.  */
1038
1039 /* Overview
1040    --------
1041
1042    A cp_parser parses the token stream as specified by the C++
1043    grammar.  Its job is purely parsing, not semantic analysis.  For
1044    example, the parser breaks the token stream into declarators,
1045    expressions, statements, and other similar syntactic constructs.
1046    It does not check that the types of the expressions on either side
1047    of an assignment-statement are compatible, or that a function is
1048    not declared with a parameter of type `void'.
1049
1050    The parser invokes routines elsewhere in the compiler to perform
1051    semantic analysis and to build up the abstract syntax tree for the
1052    code processed.  
1053
1054    The parser (and the template instantiation code, which is, in a
1055    way, a close relative of parsing) are the only parts of the
1056    compiler that should be calling push_scope and pop_scope, or
1057    related functions.  The parser (and template instantiation code)
1058    keeps track of what scope is presently active; everything else
1059    should simply honor that.  (The code that generates static
1060    initializers may also need to set the scope, in order to check
1061    access control correctly when emitting the initializers.)
1062
1063    Methodology
1064    -----------
1065    
1066    The parser is of the standard recursive-descent variety.  Upcoming
1067    tokens in the token stream are examined in order to determine which
1068    production to use when parsing a non-terminal.  Some C++ constructs
1069    require arbitrary look ahead to disambiguate.  For example, it is
1070    impossible, in the general case, to tell whether a statement is an
1071    expression or declaration without scanning the entire statement.
1072    Therefore, the parser is capable of "parsing tentatively."  When the
1073    parser is not sure what construct comes next, it enters this mode.
1074    Then, while we attempt to parse the construct, the parser queues up
1075    error messages, rather than issuing them immediately, and saves the
1076    tokens it consumes.  If the construct is parsed successfully, the
1077    parser "commits", i.e., it issues any queued error messages and
1078    the tokens that were being preserved are permanently discarded.
1079    If, however, the construct is not parsed successfully, the parser
1080    rolls back its state completely so that it can resume parsing using
1081    a different alternative.
1082
1083    Future Improvements
1084    -------------------
1085    
1086    The performance of the parser could probably be improved
1087    substantially.  Some possible improvements include:
1088
1089      - The expression parser recurses through the various levels of
1090        precedence as specified in the grammar, rather than using an
1091        operator-precedence technique.  Therefore, parsing a simple
1092        identifier requires multiple recursive calls.
1093
1094      - We could often eliminate the need to parse tentatively by
1095        looking ahead a little bit.  In some places, this approach
1096        might not entirely eliminate the need to parse tentatively, but
1097        it might still speed up the average case.  */
1098
1099 /* Flags that are passed to some parsing functions.  These values can
1100    be bitwise-ored together.  */
1101
1102 typedef enum cp_parser_flags
1103 {
1104   /* No flags.  */
1105   CP_PARSER_FLAGS_NONE = 0x0,
1106   /* The construct is optional.  If it is not present, then no error
1107      should be issued.  */
1108   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1109   /* When parsing a type-specifier, do not allow user-defined types.  */
1110   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1111 } cp_parser_flags;
1112
1113 /* The different kinds of ids that we ecounter.  */
1114
1115 typedef enum cp_parser_id_kind
1116 {
1117   /* Not an id at all.  */
1118   CP_PARSER_ID_KIND_NONE,
1119   /* An unqualified-id that is not a template-id.  */
1120   CP_PARSER_ID_KIND_UNQUALIFIED,
1121   /* An unqualified template-id.  */
1122   CP_PARSER_ID_KIND_TEMPLATE_ID,
1123   /* A qualified-id.  */
1124   CP_PARSER_ID_KIND_QUALIFIED
1125 } cp_parser_id_kind;
1126
1127 /* A mapping from a token type to a corresponding tree node type.  */
1128
1129 typedef struct cp_parser_token_tree_map_node
1130 {
1131   /* The token type.  */
1132   enum cpp_ttype token_type;
1133   /* The corresponding tree code.  */
1134   enum tree_code tree_type;
1135 } cp_parser_token_tree_map_node;
1136
1137 /* A complete map consists of several ordinary entries, followed by a
1138    terminator.  The terminating entry has a token_type of CPP_EOF.  */
1139
1140 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1141
1142 /* The status of a tentative parse.  */
1143
1144 typedef enum cp_parser_status_kind
1145 {
1146   /* No errors have occurred.  */
1147   CP_PARSER_STATUS_KIND_NO_ERROR,
1148   /* An error has occurred.  */
1149   CP_PARSER_STATUS_KIND_ERROR,
1150   /* We are committed to this tentative parse, whether or not an error
1151      has occurred.  */
1152   CP_PARSER_STATUS_KIND_COMMITTED
1153 } cp_parser_status_kind;
1154
1155 /* Context that is saved and restored when parsing tentatively.  */
1156
1157 typedef struct cp_parser_context GTY (())
1158 {
1159   /* If this is a tentative parsing context, the status of the
1160      tentative parse.  */
1161   enum cp_parser_status_kind status;
1162   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1163      that are looked up in this context must be looked up both in the
1164      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1165      the context of the containing expression.  */
1166   tree object_type;
1167   /* A TREE_LIST representing name-lookups for which we have deferred
1168      checking access controls.  We cannot check the accessibility of
1169      names used in a decl-specifier-seq until we know what is being
1170      declared because code like:
1171
1172        class A { 
1173          class B {};
1174          B* f();
1175        }
1176
1177        A::B* A::f() { return 0; }
1178
1179      is valid, even though `A::B' is not generally accessible.  
1180
1181      The TREE_PURPOSE of each node is the scope used to qualify the
1182      name being looked up; the TREE_VALUE is the DECL to which the
1183      name was resolved.  */
1184   tree deferred_access_checks;
1185   /* TRUE iff we are deferring access checks.  */
1186   bool deferring_access_checks_p;
1187   /* The next parsing context in the stack.  */
1188   struct cp_parser_context *next;
1189 } cp_parser_context;
1190
1191 /* Prototypes.  */
1192
1193 /* Constructors and destructors.  */
1194
1195 static cp_parser_context *cp_parser_context_new
1196   PARAMS ((cp_parser_context *));
1197
1198 /* Class variables.  */
1199
1200 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1201
1202 /* Constructors and destructors.  */
1203
1204 /* Construct a new context.  The context below this one on the stack
1205    is given by NEXT.  */
1206
1207 static cp_parser_context *
1208 cp_parser_context_new (next)
1209      cp_parser_context *next;
1210 {
1211   cp_parser_context *context;
1212
1213   /* Allocate the storage.  */
1214   if (cp_parser_context_free_list != NULL)
1215     {
1216       /* Pull the first entry from the free list.  */
1217       context = cp_parser_context_free_list;
1218       cp_parser_context_free_list = context->next;
1219       memset ((char *)context, 0, sizeof (*context));
1220     }
1221   else
1222     context = ((cp_parser_context *) 
1223                ggc_alloc_cleared (sizeof (cp_parser_context)));
1224   /* No errors have occurred yet in this context.  */
1225   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1226   /* If this is not the bottomost context, copy information that we
1227      need from the previous context.  */
1228   if (next)
1229     {
1230       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1231          expression, then we are parsing one in this context, too.  */
1232       context->object_type = next->object_type;
1233       /* We are deferring access checks here if we were in the NEXT
1234          context.  */
1235       context->deferring_access_checks_p 
1236         = next->deferring_access_checks_p;
1237       /* Thread the stack.  */
1238       context->next = next;
1239     }
1240
1241   return context;
1242 }
1243
1244 /* The cp_parser structure represents the C++ parser.  */
1245
1246 typedef struct cp_parser GTY(())
1247 {
1248   /* The lexer from which we are obtaining tokens.  */
1249   cp_lexer *lexer;
1250
1251   /* The scope in which names should be looked up.  If NULL_TREE, then
1252      we look up names in the scope that is currently open in the
1253      source program.  If non-NULL, this is either a TYPE or
1254      NAMESPACE_DECL for the scope in which we should look.  
1255
1256      This value is not cleared automatically after a name is looked
1257      up, so we must be careful to clear it before starting a new look
1258      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1259      will look up `Z' in the scope of `X', rather than the current
1260      scope.)  Unfortunately, it is difficult to tell when name lookup
1261      is complete, because we sometimes peek at a token, look it up,
1262      and then decide not to consume it.  */
1263   tree scope;
1264
1265   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1266      last lookup took place.  OBJECT_SCOPE is used if an expression
1267      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1268      respectively.  QUALIFYING_SCOPE is used for an expression of the 
1269      form "X::Y"; it refers to X.  */
1270   tree object_scope;
1271   tree qualifying_scope;
1272
1273   /* A stack of parsing contexts.  All but the bottom entry on the
1274      stack will be tentative contexts.
1275
1276      We parse tentatively in order to determine which construct is in
1277      use in some situations.  For example, in order to determine
1278      whether a statement is an expression-statement or a
1279      declaration-statement we parse it tentatively as a
1280      declaration-statement.  If that fails, we then reparse the same
1281      token stream as an expression-statement.  */
1282   cp_parser_context *context;
1283
1284   /* True if we are parsing GNU C++.  If this flag is not set, then
1285      GNU extensions are not recognized.  */
1286   bool allow_gnu_extensions_p;
1287
1288   /* TRUE if the `>' token should be interpreted as the greater-than
1289      operator.  FALSE if it is the end of a template-id or
1290      template-parameter-list.  */
1291   bool greater_than_is_operator_p;
1292
1293   /* TRUE if default arguments are allowed within a parameter list
1294      that starts at this point. FALSE if only a gnu extension makes
1295      them permissable.  */
1296   bool default_arg_ok_p;
1297   
1298   /* TRUE if we are parsing an integral constant-expression.  See
1299      [expr.const] for a precise definition.  */
1300   /* FIXME: Need to implement code that checks this flag.  */
1301   bool constant_expression_p;
1302
1303   /* TRUE if local variable names and `this' are forbidden in the
1304      current context.  */
1305   bool local_variables_forbidden_p;
1306
1307   /* TRUE if the declaration we are parsing is part of a
1308      linkage-specification of the form `extern string-literal
1309      declaration'.  */
1310   bool in_unbraced_linkage_specification_p;
1311
1312   /* TRUE if we are presently parsing a declarator, after the
1313      direct-declarator.  */
1314   bool in_declarator_p;
1315
1316   /* If non-NULL, then we are parsing a construct where new type
1317      definitions are not permitted.  The string stored here will be
1318      issued as an error message if a type is defined.  */
1319   const char *type_definition_forbidden_message;
1320
1321   /* List of FUNCTION_TYPEs which contain unprocessed DEFAULT_ARGs
1322      during class parsing, and are not FUNCTION_DECLs.  G++ has an
1323      awkward extension allowing default args on pointers to functions
1324      etc.  */
1325   tree default_arg_types;
1326
1327   /* A TREE_LIST of queues of functions whose bodies have been lexed,
1328      but may not have been parsed.  These functions are friends of
1329      members defined within a class-specification; they are not
1330      procssed until the class is complete.  The active queue is at the
1331      front of the list.
1332
1333      Within each queue, functions appear in the reverse order that
1334      they appeared in the source.  The TREE_PURPOSE of each node is
1335      the class in which the function was defined or declared; the
1336      TREE_VALUE is the FUNCTION_DECL itself.  */
1337   tree unparsed_functions_queues;
1338
1339   /* The number of classes whose definitions are currently in
1340      progress.  */
1341   unsigned num_classes_being_defined;
1342
1343   /* The number of template parameter lists that apply directly to the
1344      current declaration.  */
1345   unsigned num_template_parameter_lists;
1346 } cp_parser;
1347
1348 /* The type of a function that parses some kind of expression  */
1349 typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1350
1351 /* Prototypes.  */
1352
1353 /* Constructors and destructors.  */
1354
1355 static cp_parser *cp_parser_new
1356   PARAMS ((void));
1357
1358 /* Routines to parse various constructs.  
1359
1360    Those that return `tree' will return the error_mark_node (rather
1361    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1362    Sometimes, they will return an ordinary node if error-recovery was
1363    attempted, even though a parse error occurrred.  So, to check
1364    whether or not a parse error occurred, you should always use
1365    cp_parser_error_occurred.  If the construct is optional (indicated
1366    either by an `_opt' in the name of the function that does the
1367    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1368    the construct is not present.  */
1369
1370 /* Lexical conventions [gram.lex]  */
1371
1372 static tree cp_parser_identifier
1373   PARAMS ((cp_parser *));
1374
1375 /* Basic concepts [gram.basic]  */
1376
1377 static bool cp_parser_translation_unit
1378   PARAMS ((cp_parser *));
1379
1380 /* Expressions [gram.expr]  */
1381
1382 static tree cp_parser_primary_expression
1383   (cp_parser *, cp_parser_id_kind *, tree *);
1384 static tree cp_parser_id_expression
1385   PARAMS ((cp_parser *, bool, bool, bool *));
1386 static tree cp_parser_unqualified_id
1387   PARAMS ((cp_parser *, bool, bool));
1388 static tree cp_parser_nested_name_specifier_opt
1389   (cp_parser *, bool, bool, bool);
1390 static tree cp_parser_nested_name_specifier
1391   (cp_parser *, bool, bool, bool);
1392 static tree cp_parser_class_or_namespace_name
1393   (cp_parser *, bool, bool, bool, bool);
1394 static tree cp_parser_postfix_expression
1395   (cp_parser *, bool);
1396 static tree cp_parser_expression_list
1397   PARAMS ((cp_parser *));
1398 static void cp_parser_pseudo_destructor_name
1399   PARAMS ((cp_parser *, tree *, tree *));
1400 static tree cp_parser_unary_expression
1401   (cp_parser *, bool);
1402 static enum tree_code cp_parser_unary_operator
1403   PARAMS ((cp_token *));
1404 static tree cp_parser_new_expression
1405   PARAMS ((cp_parser *));
1406 static tree cp_parser_new_placement
1407   PARAMS ((cp_parser *));
1408 static tree cp_parser_new_type_id
1409   PARAMS ((cp_parser *));
1410 static tree cp_parser_new_declarator_opt
1411   PARAMS ((cp_parser *));
1412 static tree cp_parser_direct_new_declarator
1413   PARAMS ((cp_parser *));
1414 static tree cp_parser_new_initializer
1415   PARAMS ((cp_parser *));
1416 static tree cp_parser_delete_expression
1417   PARAMS ((cp_parser *));
1418 static tree cp_parser_cast_expression 
1419   (cp_parser *, bool);
1420 static tree cp_parser_pm_expression
1421   PARAMS ((cp_parser *));
1422 static tree cp_parser_multiplicative_expression
1423   PARAMS ((cp_parser *));
1424 static tree cp_parser_additive_expression
1425   PARAMS ((cp_parser *));
1426 static tree cp_parser_shift_expression
1427   PARAMS ((cp_parser *));
1428 static tree cp_parser_relational_expression
1429   PARAMS ((cp_parser *));
1430 static tree cp_parser_equality_expression
1431   PARAMS ((cp_parser *));
1432 static tree cp_parser_and_expression
1433   PARAMS ((cp_parser *));
1434 static tree cp_parser_exclusive_or_expression
1435   PARAMS ((cp_parser *));
1436 static tree cp_parser_inclusive_or_expression
1437   PARAMS ((cp_parser *));
1438 static tree cp_parser_logical_and_expression
1439   PARAMS ((cp_parser *));
1440 static tree cp_parser_logical_or_expression 
1441   PARAMS ((cp_parser *));
1442 static tree cp_parser_conditional_expression
1443   PARAMS ((cp_parser *));
1444 static tree cp_parser_question_colon_clause
1445   PARAMS ((cp_parser *, tree));
1446 static tree cp_parser_assignment_expression
1447   PARAMS ((cp_parser *));
1448 static enum tree_code cp_parser_assignment_operator_opt
1449   PARAMS ((cp_parser *));
1450 static tree cp_parser_expression
1451   PARAMS ((cp_parser *));
1452 static tree cp_parser_constant_expression
1453   PARAMS ((cp_parser *));
1454
1455 /* Statements [gram.stmt.stmt]  */
1456
1457 static void cp_parser_statement
1458   PARAMS ((cp_parser *));
1459 static tree cp_parser_labeled_statement
1460   PARAMS ((cp_parser *));
1461 static tree cp_parser_expression_statement
1462   PARAMS ((cp_parser *));
1463 static tree cp_parser_compound_statement
1464   (cp_parser *);
1465 static void cp_parser_statement_seq_opt
1466   PARAMS ((cp_parser *));
1467 static tree cp_parser_selection_statement
1468   PARAMS ((cp_parser *));
1469 static tree cp_parser_condition
1470   PARAMS ((cp_parser *));
1471 static tree cp_parser_iteration_statement
1472   PARAMS ((cp_parser *));
1473 static void cp_parser_for_init_statement
1474   PARAMS ((cp_parser *));
1475 static tree cp_parser_jump_statement
1476   PARAMS ((cp_parser *));
1477 static void cp_parser_declaration_statement
1478   PARAMS ((cp_parser *));
1479
1480 static tree cp_parser_implicitly_scoped_statement
1481   PARAMS ((cp_parser *));
1482 static void cp_parser_already_scoped_statement
1483   PARAMS ((cp_parser *));
1484
1485 /* Declarations [gram.dcl.dcl] */
1486
1487 static void cp_parser_declaration_seq_opt
1488   PARAMS ((cp_parser *));
1489 static void cp_parser_declaration
1490   PARAMS ((cp_parser *));
1491 static void cp_parser_block_declaration
1492   PARAMS ((cp_parser *, bool));
1493 static void cp_parser_simple_declaration
1494   PARAMS ((cp_parser *, bool));
1495 static tree cp_parser_decl_specifier_seq 
1496   PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1497 static tree cp_parser_storage_class_specifier_opt
1498   PARAMS ((cp_parser *));
1499 static tree cp_parser_function_specifier_opt
1500   PARAMS ((cp_parser *));
1501 static tree cp_parser_type_specifier
1502  (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1503 static tree cp_parser_simple_type_specifier
1504   PARAMS ((cp_parser *, cp_parser_flags));
1505 static tree cp_parser_type_name
1506   PARAMS ((cp_parser *));
1507 static tree cp_parser_elaborated_type_specifier
1508   PARAMS ((cp_parser *, bool, bool));
1509 static tree cp_parser_enum_specifier
1510   PARAMS ((cp_parser *));
1511 static void cp_parser_enumerator_list
1512   PARAMS ((cp_parser *, tree));
1513 static void cp_parser_enumerator_definition 
1514   PARAMS ((cp_parser *, tree));
1515 static tree cp_parser_namespace_name
1516   PARAMS ((cp_parser *));
1517 static void cp_parser_namespace_definition
1518   PARAMS ((cp_parser *));
1519 static void cp_parser_namespace_body
1520   PARAMS ((cp_parser *));
1521 static tree cp_parser_qualified_namespace_specifier
1522   PARAMS ((cp_parser *));
1523 static void cp_parser_namespace_alias_definition
1524   PARAMS ((cp_parser *));
1525 static void cp_parser_using_declaration
1526   PARAMS ((cp_parser *));
1527 static void cp_parser_using_directive
1528   PARAMS ((cp_parser *));
1529 static void cp_parser_asm_definition
1530   PARAMS ((cp_parser *));
1531 static void cp_parser_linkage_specification
1532   PARAMS ((cp_parser *));
1533
1534 /* Declarators [gram.dcl.decl] */
1535
1536 static tree cp_parser_init_declarator
1537   PARAMS ((cp_parser *, tree, tree, tree, bool, bool, bool *));
1538 static tree cp_parser_declarator
1539   PARAMS ((cp_parser *, bool, bool *));
1540 static tree cp_parser_direct_declarator
1541   PARAMS ((cp_parser *, bool, bool *));
1542 static enum tree_code cp_parser_ptr_operator
1543   PARAMS ((cp_parser *, tree *, tree *));
1544 static tree cp_parser_cv_qualifier_seq_opt
1545   PARAMS ((cp_parser *));
1546 static tree cp_parser_cv_qualifier_opt
1547   PARAMS ((cp_parser *));
1548 static tree cp_parser_declarator_id
1549   PARAMS ((cp_parser *));
1550 static tree cp_parser_type_id
1551   PARAMS ((cp_parser *));
1552 static tree cp_parser_type_specifier_seq
1553   PARAMS ((cp_parser *));
1554 static tree cp_parser_parameter_declaration_clause
1555   PARAMS ((cp_parser *));
1556 static tree cp_parser_parameter_declaration_list
1557   PARAMS ((cp_parser *));
1558 static tree cp_parser_parameter_declaration
1559   PARAMS ((cp_parser *, bool));
1560 static tree cp_parser_function_definition
1561   PARAMS ((cp_parser *, bool *));
1562 static void cp_parser_function_body
1563   (cp_parser *);
1564 static tree cp_parser_initializer
1565   PARAMS ((cp_parser *, bool *));
1566 static tree cp_parser_initializer_clause
1567   PARAMS ((cp_parser *));
1568 static tree cp_parser_initializer_list
1569   PARAMS ((cp_parser *));
1570
1571 static bool cp_parser_ctor_initializer_opt_and_function_body
1572   (cp_parser *);
1573
1574 /* Classes [gram.class] */
1575
1576 static tree cp_parser_class_name
1577   (cp_parser *, bool, bool, bool, bool, bool, bool);
1578 static tree cp_parser_class_specifier
1579   PARAMS ((cp_parser *));
1580 static tree cp_parser_class_head
1581   PARAMS ((cp_parser *, bool *, bool *, tree *));
1582 static enum tag_types cp_parser_class_key
1583   PARAMS ((cp_parser *));
1584 static void cp_parser_member_specification_opt
1585   PARAMS ((cp_parser *));
1586 static void cp_parser_member_declaration
1587   PARAMS ((cp_parser *));
1588 static tree cp_parser_pure_specifier
1589   PARAMS ((cp_parser *));
1590 static tree cp_parser_constant_initializer
1591   PARAMS ((cp_parser *));
1592
1593 /* Derived classes [gram.class.derived] */
1594
1595 static tree cp_parser_base_clause
1596   PARAMS ((cp_parser *));
1597 static tree cp_parser_base_specifier
1598   PARAMS ((cp_parser *));
1599
1600 /* Special member functions [gram.special] */
1601
1602 static tree cp_parser_conversion_function_id
1603   PARAMS ((cp_parser *));
1604 static tree cp_parser_conversion_type_id
1605   PARAMS ((cp_parser *));
1606 static tree cp_parser_conversion_declarator_opt
1607   PARAMS ((cp_parser *));
1608 static bool cp_parser_ctor_initializer_opt
1609   PARAMS ((cp_parser *));
1610 static void cp_parser_mem_initializer_list
1611   PARAMS ((cp_parser *));
1612 static tree cp_parser_mem_initializer
1613   PARAMS ((cp_parser *));
1614 static tree cp_parser_mem_initializer_id
1615   PARAMS ((cp_parser *));
1616
1617 /* Overloading [gram.over] */
1618
1619 static tree cp_parser_operator_function_id
1620   PARAMS ((cp_parser *));
1621 static tree cp_parser_operator
1622   PARAMS ((cp_parser *));
1623
1624 /* Templates [gram.temp] */
1625
1626 static void cp_parser_template_declaration
1627   PARAMS ((cp_parser *, bool));
1628 static tree cp_parser_template_parameter_list
1629   PARAMS ((cp_parser *));
1630 static tree cp_parser_template_parameter
1631   PARAMS ((cp_parser *));
1632 static tree cp_parser_type_parameter
1633   PARAMS ((cp_parser *));
1634 static tree cp_parser_template_id
1635   PARAMS ((cp_parser *, bool, bool));
1636 static tree cp_parser_template_name
1637   PARAMS ((cp_parser *, bool, bool));
1638 static tree cp_parser_template_argument_list
1639   PARAMS ((cp_parser *));
1640 static tree cp_parser_template_argument
1641   PARAMS ((cp_parser *));
1642 static void cp_parser_explicit_instantiation
1643   PARAMS ((cp_parser *));
1644 static void cp_parser_explicit_specialization
1645   PARAMS ((cp_parser *));
1646
1647 /* Exception handling [gram.exception] */
1648
1649 static tree cp_parser_try_block 
1650   PARAMS ((cp_parser *));
1651 static bool cp_parser_function_try_block
1652   PARAMS ((cp_parser *));
1653 static void cp_parser_handler_seq
1654   PARAMS ((cp_parser *));
1655 static void cp_parser_handler
1656   PARAMS ((cp_parser *));
1657 static tree cp_parser_exception_declaration
1658   PARAMS ((cp_parser *));
1659 static tree cp_parser_throw_expression
1660   PARAMS ((cp_parser *));
1661 static tree cp_parser_exception_specification_opt
1662   PARAMS ((cp_parser *));
1663 static tree cp_parser_type_id_list
1664   PARAMS ((cp_parser *));
1665
1666 /* GNU Extensions */
1667
1668 static tree cp_parser_asm_specification_opt
1669   PARAMS ((cp_parser *));
1670 static tree cp_parser_asm_operand_list
1671   PARAMS ((cp_parser *));
1672 static tree cp_parser_asm_clobber_list
1673   PARAMS ((cp_parser *));
1674 static tree cp_parser_attributes_opt
1675   PARAMS ((cp_parser *));
1676 static tree cp_parser_attribute_list
1677   PARAMS ((cp_parser *));
1678 static bool cp_parser_extension_opt
1679   PARAMS ((cp_parser *, int *));
1680 static void cp_parser_label_declaration
1681   PARAMS ((cp_parser *));
1682
1683 /* Utility Routines */
1684
1685 static tree cp_parser_lookup_name
1686   PARAMS ((cp_parser *, tree, bool, bool, bool, bool));
1687 static tree cp_parser_lookup_name_simple
1688   PARAMS ((cp_parser *, tree));
1689 static tree cp_parser_resolve_typename_type
1690   PARAMS ((cp_parser *, tree));
1691 static tree cp_parser_maybe_treat_template_as_class
1692   (tree, bool);
1693 static bool cp_parser_check_declarator_template_parameters
1694   PARAMS ((cp_parser *, tree));
1695 static bool cp_parser_check_template_parameters
1696   PARAMS ((cp_parser *, unsigned));
1697 static tree cp_parser_binary_expression
1698   PARAMS ((cp_parser *, 
1699            cp_parser_token_tree_map,
1700            cp_parser_expression_fn));
1701 static tree cp_parser_global_scope_opt
1702   PARAMS ((cp_parser *, bool));
1703 static bool cp_parser_constructor_declarator_p
1704   (cp_parser *, bool);
1705 static tree cp_parser_function_definition_from_specifiers_and_declarator
1706   PARAMS ((cp_parser *, tree, tree, tree, tree));
1707 static tree cp_parser_function_definition_after_declarator
1708   PARAMS ((cp_parser *, bool));
1709 static void cp_parser_template_declaration_after_export
1710   PARAMS ((cp_parser *, bool));
1711 static tree cp_parser_single_declaration
1712   PARAMS ((cp_parser *, bool, bool *));
1713 static tree cp_parser_functional_cast
1714   PARAMS ((cp_parser *, tree));
1715 static void cp_parser_late_parsing_for_member
1716   PARAMS ((cp_parser *, tree));
1717 static void cp_parser_late_parsing_default_args
1718   (cp_parser *, tree, tree);
1719 static tree cp_parser_sizeof_operand
1720   PARAMS ((cp_parser *, enum rid));
1721 static bool cp_parser_declares_only_class_p
1722   PARAMS ((cp_parser *));
1723 static bool cp_parser_friend_p
1724   PARAMS ((tree));
1725 static cp_token *cp_parser_require
1726   PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1727 static cp_token *cp_parser_require_keyword
1728   PARAMS ((cp_parser *, enum rid, const char *));
1729 static bool cp_parser_token_starts_function_definition_p 
1730   PARAMS ((cp_token *));
1731 static bool cp_parser_next_token_starts_class_definition_p
1732   (cp_parser *);
1733 static enum tag_types cp_parser_token_is_class_key
1734   PARAMS ((cp_token *));
1735 static void cp_parser_check_class_key
1736   (enum tag_types, tree type);
1737 static bool cp_parser_optional_template_keyword
1738   (cp_parser *);
1739 static void cp_parser_cache_group
1740   (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1741 static void cp_parser_parse_tentatively 
1742   PARAMS ((cp_parser *));
1743 static void cp_parser_commit_to_tentative_parse
1744   PARAMS ((cp_parser *));
1745 static void cp_parser_abort_tentative_parse
1746   PARAMS ((cp_parser *));
1747 static bool cp_parser_parse_definitely
1748   PARAMS ((cp_parser *));
1749 static inline bool cp_parser_parsing_tentatively
1750   PARAMS ((cp_parser *));
1751 static bool cp_parser_committed_to_tentative_parse
1752   PARAMS ((cp_parser *));
1753 static void cp_parser_error
1754   PARAMS ((cp_parser *, const char *));
1755 static bool cp_parser_simulate_error
1756   PARAMS ((cp_parser *));
1757 static void cp_parser_check_type_definition
1758   PARAMS ((cp_parser *));
1759 static bool cp_parser_skip_to_closing_parenthesis
1760   PARAMS ((cp_parser *));
1761 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1762   (cp_parser *);
1763 static void cp_parser_skip_to_end_of_statement
1764   PARAMS ((cp_parser *));
1765 static void cp_parser_skip_to_end_of_block_or_statement
1766   PARAMS ((cp_parser *));
1767 static void cp_parser_skip_to_closing_brace
1768   (cp_parser *);
1769 static void cp_parser_skip_until_found
1770   PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1771 static bool cp_parser_error_occurred
1772   PARAMS ((cp_parser *));
1773 static bool cp_parser_allow_gnu_extensions_p
1774   PARAMS ((cp_parser *));
1775 static bool cp_parser_is_string_literal
1776   PARAMS ((cp_token *));
1777 static bool cp_parser_is_keyword 
1778   PARAMS ((cp_token *, enum rid));
1779 static bool cp_parser_dependent_type_p
1780   (tree);
1781 static bool cp_parser_value_dependent_expression_p
1782   (tree);
1783 static bool cp_parser_type_dependent_expression_p
1784   (tree);
1785 static bool cp_parser_dependent_template_arg_p
1786   (tree);
1787 static bool cp_parser_dependent_template_id_p
1788   (tree, tree);
1789 static bool cp_parser_dependent_template_p
1790   (tree);
1791 static void cp_parser_defer_access_check
1792   (cp_parser *, tree, tree);
1793 static void cp_parser_start_deferring_access_checks
1794   (cp_parser *);
1795 static tree cp_parser_stop_deferring_access_checks
1796   PARAMS ((cp_parser *));
1797 static void cp_parser_perform_deferred_access_checks
1798   PARAMS ((tree));
1799 static tree cp_parser_scope_through_which_access_occurs
1800   (tree, tree, tree);
1801
1802 /* Returns non-zero if we are parsing tentatively.  */
1803
1804 static inline bool
1805 cp_parser_parsing_tentatively (parser)
1806      cp_parser *parser;
1807 {
1808   return parser->context->next != NULL;
1809 }
1810
1811 /* Returns non-zero if TOKEN is a string literal.  */
1812
1813 static bool
1814 cp_parser_is_string_literal (token)
1815      cp_token *token;
1816 {
1817   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1818 }
1819
1820 /* Returns non-zero if TOKEN is the indicated KEYWORD.  */
1821
1822 static bool
1823 cp_parser_is_keyword (token, keyword)
1824      cp_token *token;
1825      enum rid keyword;
1826 {
1827   return token->keyword == keyword;
1828 }
1829
1830 /* Returns TRUE if TYPE is dependent, in the sense of
1831    [temp.dep.type].  */
1832
1833 static bool
1834 cp_parser_dependent_type_p (type)
1835      tree type;
1836 {
1837   tree scope;
1838
1839   if (!processing_template_decl)
1840     return false;
1841
1842   /* If the type is NULL, we have not computed a type for the entity
1843      in question; in that case, the type is dependent.  */
1844   if (!type)
1845     return true;
1846
1847   /* Erroneous types can be considered non-dependent.  */
1848   if (type == error_mark_node)
1849     return false;
1850
1851   /* [temp.dep.type]
1852
1853      A type is dependent if it is:
1854
1855      -- a template parameter.  */
1856   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1857     return true;
1858   /* -- a qualified-id with a nested-name-specifier which contains a
1859         class-name that names a dependent type or whose unqualified-id
1860         names a dependent type.  */
1861   if (TREE_CODE (type) == TYPENAME_TYPE)
1862     return true;
1863   /* -- a cv-qualified type where the cv-unqualified type is
1864         dependent.  */
1865   type = TYPE_MAIN_VARIANT (type);
1866   /* -- a compound type constructed from any dependent type.  */
1867   if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1868     return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1869             || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE 
1870                                            (type)));
1871   else if (TREE_CODE (type) == POINTER_TYPE
1872            || TREE_CODE (type) == REFERENCE_TYPE)
1873     return cp_parser_dependent_type_p (TREE_TYPE (type));
1874   else if (TREE_CODE (type) == FUNCTION_TYPE
1875            || TREE_CODE (type) == METHOD_TYPE)
1876     {
1877       tree arg_type;
1878
1879       if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1880         return true;
1881       for (arg_type = TYPE_ARG_TYPES (type); 
1882            arg_type; 
1883            arg_type = TREE_CHAIN (arg_type))
1884         if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1885           return true;
1886       return false;
1887     }
1888   /* -- an array type constructed from any dependent type or whose
1889         size is specified by a constant expression that is
1890         value-dependent.  */
1891   if (TREE_CODE (type) == ARRAY_TYPE)
1892     {
1893       if (TYPE_DOMAIN (type)
1894           && ((cp_parser_value_dependent_expression_p 
1895                (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1896               || (cp_parser_type_dependent_expression_p
1897                   (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1898         return true;
1899       return cp_parser_dependent_type_p (TREE_TYPE (type));
1900     }
1901   /* -- a template-id in which either the template name is a template
1902         parameter or any of the template arguments is a dependent type or
1903         an expression that is type-dependent or value-dependent.  
1904
1905      This language seems somewhat confused; for example, it does not
1906      discuss template template arguments.  Therefore, we use the
1907      definition for dependent template arguments in [temp.dep.temp].  */
1908   if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1909       && (cp_parser_dependent_template_id_p
1910           (CLASSTYPE_TI_TEMPLATE (type),
1911            CLASSTYPE_TI_ARGS (type))))
1912     return true;
1913   else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1914     return true;
1915   /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1916      expression is not type-dependent, then it should already been
1917      have resolved.  */
1918   if (TREE_CODE (type) == TYPEOF_TYPE)
1919     return true;
1920   /* The standard does not specifically mention types that are local
1921      to template functions or local classes, but they should be
1922      considered dependent too.  For example:
1923
1924        template <int I> void f() { 
1925          enum E { a = I }; 
1926          S<sizeof (E)> s;
1927        }
1928
1929      The size of `E' cannot be known until the value of `I' has been
1930      determined.  Therefore, `E' must be considered dependent.  */
1931   scope = TYPE_CONTEXT (type);
1932   if (scope && TYPE_P (scope))
1933     return cp_parser_dependent_type_p (scope);
1934   else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1935     return cp_parser_type_dependent_expression_p (scope);
1936
1937   /* Other types are non-dependent.  */
1938   return false;
1939 }
1940
1941 /* Returns TRUE if the EXPRESSION is value-dependent.  */
1942
1943 static bool
1944 cp_parser_value_dependent_expression_p (tree expression)
1945 {
1946   if (!processing_template_decl)
1947     return false;
1948
1949   /* A name declared with a dependent type.  */
1950   if (DECL_P (expression)
1951       && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1952     return true;
1953   /* A non-type template parameter.  */
1954   if ((TREE_CODE (expression) == CONST_DECL
1955        && DECL_TEMPLATE_PARM_P (expression))
1956       || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1957     return true;
1958   /* A constant with integral or enumeration type and is initialized 
1959      with an expression that is value-dependent.  */
1960   if (TREE_CODE (expression) == VAR_DECL
1961       && DECL_INITIAL (expression)
1962       && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1963           || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1964       && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1965     return true;
1966   /* These expressions are value-dependent if the type to which the
1967      cast occurs is dependent.  */
1968   if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1969        || TREE_CODE (expression) == STATIC_CAST_EXPR
1970        || TREE_CODE (expression) == CONST_CAST_EXPR
1971        || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
1972        || TREE_CODE (expression) == CAST_EXPR)
1973       && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1974     return true;
1975   /* A `sizeof' expression where the sizeof operand is a type is
1976      value-dependent if the type is dependent.  If the type was not
1977      dependent, we would no longer have a SIZEOF_EXPR, so any
1978      SIZEOF_EXPR is dependent.  */
1979   if (TREE_CODE (expression) == SIZEOF_EXPR)
1980     return true;
1981   /* A constant expression is value-dependent if any subexpression is
1982      value-dependent.  */
1983   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
1984     {
1985       switch (TREE_CODE_CLASS (TREE_CODE (expression)))
1986         {
1987         case '1':
1988           return (cp_parser_value_dependent_expression_p 
1989                   (TREE_OPERAND (expression, 0)));
1990         case '<':
1991         case '2':
1992           return ((cp_parser_value_dependent_expression_p 
1993                    (TREE_OPERAND (expression, 0)))
1994                   || (cp_parser_value_dependent_expression_p 
1995                       (TREE_OPERAND (expression, 1))));
1996         case 'e':
1997           {
1998             int i;
1999             for (i = 0; 
2000                  i < TREE_CODE_LENGTH (TREE_CODE (expression));
2001                  ++i)
2002               if (cp_parser_value_dependent_expression_p
2003                   (TREE_OPERAND (expression, i)))
2004                 return true;
2005             return false;
2006           }
2007         }
2008     }
2009
2010   /* The expression is not value-dependent.  */
2011   return false;
2012 }
2013
2014 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
2015    [temp.dep.expr].  */
2016
2017 static bool
2018 cp_parser_type_dependent_expression_p (expression)
2019      tree expression;
2020 {
2021   if (!processing_template_decl)
2022     return false;
2023
2024   /* Some expression forms are never type-dependent.  */
2025   if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2026       || TREE_CODE (expression) == SIZEOF_EXPR
2027       || TREE_CODE (expression) == ALIGNOF_EXPR
2028       || TREE_CODE (expression) == TYPEID_EXPR
2029       || TREE_CODE (expression) == DELETE_EXPR
2030       || TREE_CODE (expression) == VEC_DELETE_EXPR
2031       || TREE_CODE (expression) == THROW_EXPR)
2032     return false;
2033
2034   /* The types of these expressions depends only on the type to which
2035      the cast occurs.  */
2036   if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2037       || TREE_CODE (expression) == STATIC_CAST_EXPR
2038       || TREE_CODE (expression) == CONST_CAST_EXPR
2039       || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2040       || TREE_CODE (expression) == CAST_EXPR)
2041     return cp_parser_dependent_type_p (TREE_TYPE (expression));
2042   /* The types of these expressions depends only on the type created
2043      by the expression.  */
2044   else if (TREE_CODE (expression) == NEW_EXPR
2045            || TREE_CODE (expression) == VEC_NEW_EXPR)
2046     return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2047
2048   if (TREE_CODE (expression) == FUNCTION_DECL
2049       && DECL_LANG_SPECIFIC (expression)
2050       && DECL_TEMPLATE_INFO (expression)
2051       && (cp_parser_dependent_template_id_p
2052           (DECL_TI_TEMPLATE (expression),
2053            INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2054     return true;
2055
2056   return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2057 }
2058
2059 /* Returns TRUE if the ARG (a template argument) is dependent.  */
2060
2061 static bool
2062 cp_parser_dependent_template_arg_p (tree arg)
2063 {
2064   if (!processing_template_decl)
2065     return false;
2066
2067   if (TREE_CODE (arg) == TEMPLATE_DECL
2068       || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2069     return cp_parser_dependent_template_p (arg);
2070   else if (TYPE_P (arg))
2071     return cp_parser_dependent_type_p (arg);
2072   else
2073     return (cp_parser_type_dependent_expression_p (arg)
2074             || cp_parser_value_dependent_expression_p (arg));
2075 }
2076
2077 /* Returns TRUE if the specialization TMPL<ARGS> is dependent.  */
2078
2079 static bool
2080 cp_parser_dependent_template_id_p (tree tmpl, tree args)
2081 {
2082   int i;
2083
2084   if (cp_parser_dependent_template_p (tmpl))
2085     return true;
2086   for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2087     if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2088       return true;
2089   return false;
2090 }
2091
2092 /* Returns TRUE if the template TMPL is dependent.  */
2093
2094 static bool
2095 cp_parser_dependent_template_p (tree tmpl)
2096 {
2097   /* Template template parameters are dependent.  */
2098   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2099       || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2100     return true;
2101   /* So are member templates of dependent classes.  */
2102   if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2103     return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2104   return false;
2105 }
2106
2107 /* Defer checking the accessibility of DECL, when looked up in
2108    CLASS_TYPE.  */
2109
2110 static void
2111 cp_parser_defer_access_check (cp_parser *parser, 
2112                               tree class_type,
2113                               tree decl)
2114 {
2115   tree check;
2116
2117   /* If we are not supposed to defer access checks, just check now.  */
2118   if (!parser->context->deferring_access_checks_p)
2119     {
2120       enforce_access (class_type, decl);
2121       return;
2122     }
2123
2124   /* See if we are already going to perform this check.  */
2125   for (check = parser->context->deferred_access_checks;
2126        check;
2127        check = TREE_CHAIN (check))
2128     if (TREE_VALUE (check) == decl
2129         && same_type_p (TREE_PURPOSE (check), class_type))
2130       return;
2131   /* If not, record the check.  */
2132   parser->context->deferred_access_checks
2133     = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2134 }
2135
2136 /* Start deferring access control checks.  */
2137
2138 static void
2139 cp_parser_start_deferring_access_checks (cp_parser *parser)
2140 {
2141   parser->context->deferring_access_checks_p = true;
2142 }
2143
2144 /* Stop deferring access control checks.  Returns a TREE_LIST
2145    representing the deferred checks.  The TREE_PURPOSE of each node is
2146    the type through which the access occurred; the TREE_VALUE is the
2147    declaration named.  */
2148
2149 static tree
2150 cp_parser_stop_deferring_access_checks (parser)
2151      cp_parser *parser;
2152 {
2153   tree access_checks;
2154
2155   parser->context->deferring_access_checks_p = false;
2156   access_checks = parser->context->deferred_access_checks;
2157   parser->context->deferred_access_checks = NULL_TREE;
2158
2159   return access_checks;
2160 }
2161
2162 /* Perform the deferred ACCESS_CHECKS, whose representation is as
2163    documented with cp_parser_stop_deferrring_access_checks.  */
2164
2165 static void
2166 cp_parser_perform_deferred_access_checks (access_checks)
2167      tree access_checks;
2168 {
2169   tree deferred_check;
2170
2171   /* Look through all the deferred checks.  */
2172   for (deferred_check = access_checks;
2173        deferred_check;
2174        deferred_check = TREE_CHAIN (deferred_check))
2175     /* Check access.  */
2176     enforce_access (TREE_PURPOSE (deferred_check), 
2177                     TREE_VALUE (deferred_check));
2178 }
2179
2180 /* Returns the scope through which DECL is being accessed, or
2181    NULL_TREE if DECL is not a member.  If OBJECT_TYPE is non-NULL, we
2182    have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2183    or `x', respectively.  If the DECL was named as `A::B' then
2184    NESTED_NAME_SPECIFIER is `A'.  */
2185
2186 tree
2187 cp_parser_scope_through_which_access_occurs (decl, 
2188                                              object_type,
2189                                              nested_name_specifier)
2190      tree decl;
2191      tree object_type;
2192      tree nested_name_specifier;
2193 {
2194   tree scope;
2195   tree qualifying_type = NULL_TREE;
2196   
2197   /* Determine the SCOPE of DECL.  */
2198   scope = context_for_name_lookup (decl);
2199   /* If the SCOPE is not a type, then DECL is not a member.  */
2200   if (!TYPE_P (scope))
2201     return NULL_TREE;
2202   /* Figure out the type through which DECL is being accessed.  */
2203   if (object_type && DERIVED_FROM_P (scope, object_type))
2204     /* If we are processing a `->' or `.' expression, use the type of the
2205        left-hand side.  */
2206     qualifying_type = object_type;
2207   else if (nested_name_specifier)
2208     {
2209       /* If the reference is to a non-static member of the
2210          current class, treat it as if it were referenced through
2211          `this'.  */
2212       if (DECL_NONSTATIC_MEMBER_P (decl)
2213           && current_class_ptr
2214           && DERIVED_FROM_P (scope, current_class_type))
2215         qualifying_type = current_class_type;
2216       /* Otherwise, use the type indicated by the
2217          nested-name-specifier.  */
2218       else
2219         qualifying_type = nested_name_specifier;
2220     }
2221   else
2222     /* Otherwise, the name must be from the current class or one of
2223        its bases.  */
2224     qualifying_type = currently_open_derived_class (scope);
2225
2226   return qualifying_type;
2227 }
2228
2229 /* Issue the indicated error MESSAGE.  */
2230
2231 static void
2232 cp_parser_error (parser, message)
2233      cp_parser *parser;
2234      const char *message;
2235 {
2236   /* Output the MESSAGE -- unless we're parsing tentatively.  */
2237   if (!cp_parser_simulate_error (parser))
2238     error (message);
2239 }
2240
2241 /* If we are parsing tentatively, remember that an error has occurred
2242    during this tentative parse.  Returns true if the error was
2243    simulated; false if a messgae should be issued by the caller.  */
2244
2245 static bool
2246 cp_parser_simulate_error (parser)
2247      cp_parser *parser;
2248 {
2249   if (cp_parser_parsing_tentatively (parser)
2250       && !cp_parser_committed_to_tentative_parse (parser))
2251     {
2252       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2253       return true;
2254     }
2255   return false;
2256 }
2257
2258 /* This function is called when a type is defined.  If type
2259    definitions are forbidden at this point, an error message is
2260    issued.  */
2261
2262 static void
2263 cp_parser_check_type_definition (parser)
2264      cp_parser *parser;
2265 {
2266   /* If types are forbidden here, issue a message.  */
2267   if (parser->type_definition_forbidden_message)
2268     /* Use `%s' to print the string in case there are any escape
2269        characters in the message.  */
2270     error ("%s", parser->type_definition_forbidden_message);
2271 }
2272
2273 /* Consume tokens up to, and including, the next non-nested closing `)'. 
2274    Returns TRUE iff we found a closing `)'.  */
2275
2276 static bool
2277 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2278 {
2279   unsigned nesting_depth = 0;
2280
2281   while (true)
2282     {
2283       cp_token *token;
2284
2285       /* If we've run out of tokens, then there is no closing `)'.  */
2286       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2287         return false;
2288       /* Consume the token.  */
2289       token = cp_lexer_consume_token (parser->lexer);
2290       /* If it is an `(', we have entered another level of nesting.  */
2291       if (token->type == CPP_OPEN_PAREN)
2292         ++nesting_depth;
2293       /* If it is a `)', then we might be done.  */
2294       else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2295         return true;
2296     }
2297 }
2298
2299 /* Consume tokens until the next token is a `)', or a `,'.  Returns
2300    TRUE if the next token is a `,'.  */
2301
2302 static bool
2303 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2304 {
2305   unsigned nesting_depth = 0;
2306
2307   while (true)
2308     {
2309       cp_token *token = cp_lexer_peek_token (parser->lexer);
2310
2311       /* If we've run out of tokens, then there is no closing `)'.  */
2312       if (token->type == CPP_EOF)
2313         return false;
2314       /* If it is a `,' stop.  */
2315       else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2316         return true;
2317       /* If it is a `)', stop.  */
2318       else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2319         return false;
2320       /* If it is an `(', we have entered another level of nesting.  */
2321       else if (token->type == CPP_OPEN_PAREN)
2322         ++nesting_depth;
2323       /* Consume the token.  */
2324       token = cp_lexer_consume_token (parser->lexer);
2325     }
2326 }
2327
2328 /* Consume tokens until we reach the end of the current statement.
2329    Normally, that will be just before consuming a `;'.  However, if a
2330    non-nested `}' comes first, then we stop before consuming that.  */
2331
2332 static void
2333 cp_parser_skip_to_end_of_statement (parser)
2334      cp_parser *parser;
2335 {
2336   unsigned nesting_depth = 0;
2337
2338   while (true)
2339     {
2340       cp_token *token;
2341
2342       /* Peek at the next token.  */
2343       token = cp_lexer_peek_token (parser->lexer);
2344       /* If we've run out of tokens, stop.  */
2345       if (token->type == CPP_EOF)
2346         break;
2347       /* If the next token is a `;', we have reached the end of the
2348          statement.  */
2349       if (token->type == CPP_SEMICOLON && !nesting_depth)
2350         break;
2351       /* If the next token is a non-nested `}', then we have reached
2352          the end of the current block.  */
2353       if (token->type == CPP_CLOSE_BRACE)
2354         {
2355           /* If this is a non-nested `}', stop before consuming it.
2356              That way, when confronted with something like:
2357
2358                { 3 + } 
2359
2360              we stop before consuming the closing `}', even though we
2361              have not yet reached a `;'.  */
2362           if (nesting_depth == 0)
2363             break;
2364           /* If it is the closing `}' for a block that we have
2365              scanned, stop -- but only after consuming the token.
2366              That way given:
2367
2368                 void f g () { ... }
2369                 typedef int I;
2370
2371              we will stop after the body of the erroneously declared
2372              function, but before consuming the following `typedef'
2373              declaration.  */
2374           if (--nesting_depth == 0)
2375             {
2376               cp_lexer_consume_token (parser->lexer);
2377               break;
2378             }
2379         }
2380       /* If it the next token is a `{', then we are entering a new
2381          block.  Consume the entire block.  */
2382       else if (token->type == CPP_OPEN_BRACE)
2383         ++nesting_depth;
2384       /* Consume the token.  */
2385       cp_lexer_consume_token (parser->lexer);
2386     }
2387 }
2388
2389 /* Skip tokens until we have consumed an entire block, or until we
2390    have consumed a non-nested `;'.  */
2391
2392 static void
2393 cp_parser_skip_to_end_of_block_or_statement (parser)
2394      cp_parser *parser;
2395 {
2396   unsigned nesting_depth = 0;
2397
2398   while (true)
2399     {
2400       cp_token *token;
2401
2402       /* Peek at the next token.  */
2403       token = cp_lexer_peek_token (parser->lexer);
2404       /* If we've run out of tokens, stop.  */
2405       if (token->type == CPP_EOF)
2406         break;
2407       /* If the next token is a `;', we have reached the end of the
2408          statement.  */
2409       if (token->type == CPP_SEMICOLON && !nesting_depth)
2410         {
2411           /* Consume the `;'.  */
2412           cp_lexer_consume_token (parser->lexer);
2413           break;
2414         }
2415       /* Consume the token.  */
2416       token = cp_lexer_consume_token (parser->lexer);
2417       /* If the next token is a non-nested `}', then we have reached
2418          the end of the current block.  */
2419       if (token->type == CPP_CLOSE_BRACE 
2420           && (nesting_depth == 0 || --nesting_depth == 0))
2421         break;
2422       /* If it the next token is a `{', then we are entering a new
2423          block.  Consume the entire block.  */
2424       if (token->type == CPP_OPEN_BRACE)
2425         ++nesting_depth;
2426     }
2427 }
2428
2429 /* Skip tokens until a non-nested closing curly brace is the next
2430    token.  */
2431
2432 static void
2433 cp_parser_skip_to_closing_brace (cp_parser *parser)
2434 {
2435   unsigned nesting_depth = 0;
2436
2437   while (true)
2438     {
2439       cp_token *token;
2440
2441       /* Peek at the next token.  */
2442       token = cp_lexer_peek_token (parser->lexer);
2443       /* If we've run out of tokens, stop.  */
2444       if (token->type == CPP_EOF)
2445         break;
2446       /* If the next token is a non-nested `}', then we have reached
2447          the end of the current block.  */
2448       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2449         break;
2450       /* If it the next token is a `{', then we are entering a new
2451          block.  Consume the entire block.  */
2452       else if (token->type == CPP_OPEN_BRACE)
2453         ++nesting_depth;
2454       /* Consume the token.  */
2455       cp_lexer_consume_token (parser->lexer);
2456     }
2457 }
2458
2459 /* Create a new C++ parser.  */
2460
2461 static cp_parser *
2462 cp_parser_new ()
2463 {
2464   cp_parser *parser;
2465
2466   parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2467   parser->lexer = cp_lexer_new (/*main_lexer_p=*/true);
2468   parser->context = cp_parser_context_new (NULL);
2469
2470   /* For now, we always accept GNU extensions.  */
2471   parser->allow_gnu_extensions_p = 1;
2472
2473   /* The `>' token is a greater-than operator, not the end of a
2474      template-id.  */
2475   parser->greater_than_is_operator_p = true;
2476
2477   parser->default_arg_ok_p = true;
2478   
2479   /* We are not parsing a constant-expression.  */
2480   parser->constant_expression_p = false;
2481
2482   /* Local variable names are not forbidden.  */
2483   parser->local_variables_forbidden_p = false;
2484
2485   /* We are not procesing an `extern "C"' declaration.  */
2486   parser->in_unbraced_linkage_specification_p = false;
2487
2488   /* We are not processing a declarator.  */
2489   parser->in_declarator_p = false;
2490
2491   /* There are no default args to process.  */
2492   parser->default_arg_types = NULL;
2493
2494   /* The unparsed function queue is empty.  */
2495   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2496
2497   /* There are no classes being defined.  */
2498   parser->num_classes_being_defined = 0;
2499
2500   /* No template parameters apply.  */
2501   parser->num_template_parameter_lists = 0;
2502
2503   return parser;
2504 }
2505
2506 /* Lexical conventions [gram.lex]  */
2507
2508 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2509    identifier.  */
2510
2511 static tree 
2512 cp_parser_identifier (parser)
2513      cp_parser *parser;
2514 {
2515   cp_token *token;
2516
2517   /* Look for the identifier.  */
2518   token = cp_parser_require (parser, CPP_NAME, "identifier");
2519   /* Return the value.  */
2520   return token ? token->value : error_mark_node;
2521 }
2522
2523 /* Basic concepts [gram.basic]  */
2524
2525 /* Parse a translation-unit.
2526
2527    translation-unit:
2528      declaration-seq [opt]  
2529
2530    Returns TRUE if all went well.  */
2531
2532 static bool
2533 cp_parser_translation_unit (parser)
2534      cp_parser *parser;
2535 {
2536   while (true)
2537     {
2538       cp_parser_declaration_seq_opt (parser);
2539
2540       /* If there are no tokens left then all went well.  */
2541       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2542         break;
2543       
2544       /* Otherwise, issue an error message.  */
2545       cp_parser_error (parser, "expected declaration");
2546       return false;
2547     }
2548
2549   /* Consume the EOF token.  */
2550   cp_parser_require (parser, CPP_EOF, "end-of-file");
2551   
2552   /* Finish up.  */
2553   finish_translation_unit ();
2554
2555   /* All went well.  */
2556   return true;
2557 }
2558
2559 /* Expressions [gram.expr] */
2560
2561 /* Parse a primary-expression.
2562
2563    primary-expression:
2564      literal
2565      this
2566      ( expression )
2567      id-expression
2568
2569    GNU Extensions:
2570
2571    primary-expression:
2572      ( compound-statement )
2573      __builtin_va_arg ( assignment-expression , type-id )
2574
2575    literal:
2576      __null
2577
2578    Returns a representation of the expression.  
2579
2580    *IDK indicates what kind of id-expression (if any) was present.  
2581
2582    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2583    used as the operand of a pointer-to-member.  In that case,
2584    *QUALIFYING_CLASS gives the class that is used as the qualifying
2585    class in the pointer-to-member.  */
2586
2587 static tree
2588 cp_parser_primary_expression (cp_parser *parser, 
2589                               cp_parser_id_kind *idk,
2590                               tree *qualifying_class)
2591 {
2592   cp_token *token;
2593
2594   /* Assume the primary expression is not an id-expression.  */
2595   *idk = CP_PARSER_ID_KIND_NONE;
2596   /* And that it cannot be used as pointer-to-member.  */
2597   *qualifying_class = NULL_TREE;
2598
2599   /* Peek at the next token.  */
2600   token = cp_lexer_peek_token (parser->lexer);
2601   switch (token->type)
2602     {
2603       /* literal:
2604            integer-literal
2605            character-literal
2606            floating-literal
2607            string-literal
2608            boolean-literal  */
2609     case CPP_CHAR:
2610     case CPP_WCHAR:
2611     case CPP_STRING:
2612     case CPP_WSTRING:
2613     case CPP_NUMBER:
2614       token = cp_lexer_consume_token (parser->lexer);
2615       return token->value;
2616
2617     case CPP_OPEN_PAREN:
2618       {
2619         tree expr;
2620         bool saved_greater_than_is_operator_p;
2621
2622         /* Consume the `('.  */
2623         cp_lexer_consume_token (parser->lexer);
2624         /* Within a parenthesized expression, a `>' token is always
2625            the greater-than operator.  */
2626         saved_greater_than_is_operator_p 
2627           = parser->greater_than_is_operator_p;
2628         parser->greater_than_is_operator_p = true;
2629         /* If we see `( { ' then we are looking at the beginning of
2630            a GNU statement-expression.  */
2631         if (cp_parser_allow_gnu_extensions_p (parser)
2632             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2633           {
2634             /* Statement-expressions are not allowed by the standard.  */
2635             if (pedantic)
2636               pedwarn ("ISO C++ forbids braced-groups within expressions");  
2637             
2638             /* And they're not allowed outside of a function-body; you
2639                cannot, for example, write:
2640                
2641                  int i = ({ int j = 3; j + 1; });
2642                
2643                at class or namespace scope.  */
2644             if (!at_function_scope_p ())
2645               error ("statement-expressions are allowed only inside functions");
2646             /* Start the statement-expression.  */
2647             expr = begin_stmt_expr ();
2648             /* Parse the compound-statement.  */
2649             cp_parser_compound_statement (parser);
2650             /* Finish up.  */
2651             expr = finish_stmt_expr (expr);
2652           }
2653         else
2654           {
2655             /* Parse the parenthesized expression.  */
2656             expr = cp_parser_expression (parser);
2657             /* Let the front end know that this expression was
2658                enclosed in parentheses. This matters in case, for
2659                example, the expression is of the form `A::B', since
2660                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2661                not.  */
2662             finish_parenthesized_expr (expr);
2663           }
2664         /* The `>' token might be the end of a template-id or
2665            template-parameter-list now.  */
2666         parser->greater_than_is_operator_p 
2667           = saved_greater_than_is_operator_p;
2668         /* Consume the `)'.  */
2669         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2670           cp_parser_skip_to_end_of_statement (parser);
2671
2672         return expr;
2673       }
2674
2675     case CPP_KEYWORD:
2676       switch (token->keyword)
2677         {
2678           /* These two are the boolean literals.  */
2679         case RID_TRUE:
2680           cp_lexer_consume_token (parser->lexer);
2681           return boolean_true_node;
2682         case RID_FALSE:
2683           cp_lexer_consume_token (parser->lexer);
2684           return boolean_false_node;
2685           
2686           /* The `__null' literal.  */
2687         case RID_NULL:
2688           cp_lexer_consume_token (parser->lexer);
2689           return null_node;
2690
2691           /* Recognize the `this' keyword.  */
2692         case RID_THIS:
2693           cp_lexer_consume_token (parser->lexer);
2694           if (parser->local_variables_forbidden_p)
2695             {
2696               error ("`this' may not be used in this context");
2697               return error_mark_node;
2698             }
2699           return finish_this_expr ();
2700
2701           /* The `operator' keyword can be the beginning of an
2702              id-expression.  */
2703         case RID_OPERATOR:
2704           goto id_expression;
2705
2706         case RID_FUNCTION_NAME:
2707         case RID_PRETTY_FUNCTION_NAME:
2708         case RID_C99_FUNCTION_NAME:
2709           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2710              __func__ are the names of variables -- but they are
2711              treated specially.  Therefore, they are handled here,
2712              rather than relying on the generic id-expression logic
2713              below.  Gramatically, these names are id-expressions.  
2714
2715              Consume the token.  */
2716           token = cp_lexer_consume_token (parser->lexer);
2717           /* Look up the name.  */
2718           return finish_fname (token->value);
2719
2720         case RID_VA_ARG:
2721           {
2722             tree expression;
2723             tree type;
2724
2725             /* The `__builtin_va_arg' construct is used to handle
2726                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2727             cp_lexer_consume_token (parser->lexer);
2728             /* Look for the opening `('.  */
2729             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2730             /* Now, parse the assignment-expression.  */
2731             expression = cp_parser_assignment_expression (parser);
2732             /* Look for the `,'.  */
2733             cp_parser_require (parser, CPP_COMMA, "`,'");
2734             /* Parse the type-id.  */
2735             type = cp_parser_type_id (parser);
2736             /* Look for the closing `)'.  */
2737             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2738
2739             return build_x_va_arg (expression, type);
2740           }
2741
2742         default:
2743           cp_parser_error (parser, "expected primary-expression");
2744           return error_mark_node;
2745         }
2746       /* Fall through. */
2747
2748       /* An id-expression can start with either an identifier, a
2749          `::' as the beginning of a qualified-id, or the "operator"
2750          keyword.  */
2751     case CPP_NAME:
2752     case CPP_SCOPE:
2753     case CPP_TEMPLATE_ID:
2754     case CPP_NESTED_NAME_SPECIFIER:
2755       {
2756         tree id_expression;
2757         tree decl;
2758
2759       id_expression:
2760         /* Parse the id-expression.  */
2761         id_expression 
2762           = cp_parser_id_expression (parser, 
2763                                      /*template_keyword_p=*/false,
2764                                      /*check_dependency_p=*/true,
2765                                      /*template_p=*/NULL);
2766         if (id_expression == error_mark_node)
2767           return error_mark_node;
2768         /* If we have a template-id, then no further lookup is
2769            required.  If the template-id was for a template-class, we
2770            will sometimes have a TYPE_DECL at this point.  */
2771         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2772             || TREE_CODE (id_expression) == TYPE_DECL)
2773           decl = id_expression;
2774         /* Look up the name.  */
2775         else 
2776           {
2777             decl = cp_parser_lookup_name_simple (parser, id_expression);
2778             /* If name lookup gives us a SCOPE_REF, then the
2779                qualifying scope was dependent.  Just propagate the
2780                name.  */
2781             if (TREE_CODE (decl) == SCOPE_REF)
2782               {
2783                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2784                   *qualifying_class = TREE_OPERAND (decl, 0);
2785                 return decl;
2786               }
2787             /* Check to see if DECL is a local variable in a context
2788                where that is forbidden.  */
2789             if (parser->local_variables_forbidden_p
2790                 && local_variable_p (decl))
2791               {
2792                 /* It might be that we only found DECL because we are
2793                    trying to be generous with pre-ISO scoping rules.
2794                    For example, consider:
2795
2796                      int i;
2797                      void g() {
2798                        for (int i = 0; i < 10; ++i) {}
2799                        extern void f(int j = i);
2800                      }
2801
2802                    Here, name look up will originally find the out 
2803                    of scope `i'.  We need to issue a warning message,
2804                    but then use the global `i'.  */
2805                 decl = check_for_out_of_scope_variable (decl);
2806                 if (local_variable_p (decl))
2807                   {
2808                     error ("local variable `%D' may not appear in this context",
2809                            decl);
2810                     return error_mark_node;
2811                   }
2812               }
2813
2814             /* If unqualified name lookup fails while processing a
2815                template, that just means that we need to do name
2816                lookup again when the template is instantiated.  */
2817             if (!parser->scope 
2818                 && decl == error_mark_node
2819                 && processing_template_decl)
2820               {
2821                 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2822                 return build_min_nt (LOOKUP_EXPR, id_expression);
2823               }
2824             else if (decl == error_mark_node
2825                      && !processing_template_decl)
2826               {
2827                 if (!parser->scope)
2828                   {
2829                     /* It may be resolvable as a koenig lookup function
2830                        call.  */
2831                     *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2832                     return id_expression;
2833                   }
2834                 else if (TYPE_P (parser->scope)
2835                          && !COMPLETE_TYPE_P (parser->scope))
2836                   error ("incomplete type `%T' used in nested name specifier",
2837                          parser->scope);
2838                 else if (parser->scope != global_namespace)
2839                   error ("`%D' is not a member of `%D'",
2840                          id_expression, parser->scope);
2841                 else
2842                   error ("`::%D' has not been declared", id_expression);
2843               }
2844             /* If DECL is a variable would be out of scope under
2845                ANSI/ISO rules, but in scope in the ARM, name lookup
2846                will succeed.  Issue a diagnostic here.  */
2847             else
2848               decl = check_for_out_of_scope_variable (decl);
2849
2850             /* Remember that the name was used in the definition of
2851                the current class so that we can check later to see if
2852                the meaning would have been different after the class
2853                was entirely defined.  */
2854             if (!parser->scope && decl != error_mark_node)
2855               maybe_note_name_used_in_class (id_expression, decl);
2856           }
2857
2858         /* If we didn't find anything, or what we found was a type,
2859            then this wasn't really an id-expression.  */
2860         if (TREE_CODE (decl) == TYPE_DECL
2861             || TREE_CODE (decl) == NAMESPACE_DECL
2862             || (TREE_CODE (decl) == TEMPLATE_DECL
2863                 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2864           {
2865             cp_parser_error (parser, 
2866                              "expected primary-expression");
2867             return error_mark_node;
2868           }
2869
2870         /* If the name resolved to a template parameter, there is no
2871            need to look it up again later.  Similarly, we resolve
2872            enumeration constants to their underlying values.  */
2873         if (TREE_CODE (decl) == CONST_DECL)
2874           {
2875             *idk = CP_PARSER_ID_KIND_NONE;
2876             if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2877               return DECL_INITIAL (decl);
2878             return decl;
2879           }
2880         else
2881           {
2882             bool dependent_p;
2883             
2884             /* If the declaration was explicitly qualified indicate
2885                that.  The semantics of `A::f(3)' are different than
2886                `f(3)' if `f' is virtual.  */
2887             *idk = (parser->scope 
2888                     ? CP_PARSER_ID_KIND_QUALIFIED
2889                     : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2890                        ? CP_PARSER_ID_KIND_TEMPLATE_ID
2891                        : CP_PARSER_ID_KIND_UNQUALIFIED));
2892
2893
2894             /* [temp.dep.expr]
2895                
2896                An id-expression is type-dependent if it contains an
2897                identifier that was declared with a dependent type.
2898                
2899                As an optimization, we could choose not to create a
2900                LOOKUP_EXPR for a name that resolved to a local
2901                variable in the template function that we are currently
2902                declaring; such a name cannot ever resolve to anything
2903                else.  If we did that we would not have to look up
2904                these names at instantiation time.
2905                
2906                The standard is not very specific about an
2907                id-expression that names a set of overloaded functions.
2908                What if some of them have dependent types and some of
2909                them do not?  Presumably, such a name should be treated
2910                as a dependent name.  */
2911             /* Assume the name is not dependent.  */
2912             dependent_p = false;
2913             if (!processing_template_decl)
2914               /* No names are dependent outside a template.  */
2915               ;
2916             /* A template-id where the name of the template was not
2917                resolved is definitely dependent.  */
2918             else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2919                      && (TREE_CODE (TREE_OPERAND (decl, 0)) 
2920                          == IDENTIFIER_NODE))
2921               dependent_p = true;
2922             /* For anything except an overloaded function, just check
2923                its type.  */
2924             else if (!is_overloaded_fn (decl))
2925               dependent_p 
2926                 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2927             /* For a set of overloaded functions, check each of the
2928                functions.  */
2929             else
2930               {
2931                 tree fns = decl;
2932
2933                 if (BASELINK_P (fns))
2934                   fns = BASELINK_FUNCTIONS (fns);
2935                   
2936                 /* For a template-id, check to see if the template
2937                    arguments are dependent.  */
2938                 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2939                   {
2940                     tree args = TREE_OPERAND (fns, 1);
2941
2942                     if (args && TREE_CODE (args) == TREE_LIST)
2943                       {
2944                         while (args)
2945                           {
2946                             if (cp_parser_dependent_template_arg_p
2947                                 (TREE_VALUE (args)))
2948                               {
2949                                 dependent_p = true;
2950                                 break;
2951                               }
2952                             args = TREE_CHAIN (args);
2953                           }
2954                       }
2955                     else if (args && TREE_CODE (args) == TREE_VEC)
2956                       {
2957                         int i; 
2958                         for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2959                           if (cp_parser_dependent_template_arg_p
2960                               (TREE_VEC_ELT (args, i)))
2961                             {
2962                               dependent_p = true;
2963                               break;
2964                             }
2965                       }
2966
2967                     /* The functions are those referred to by the
2968                        template-id.  */
2969                     fns = TREE_OPERAND (fns, 0);
2970                   }
2971
2972                 /* If there are no dependent template arguments, go
2973                    through the overlaoded functions.  */
2974                 while (fns && !dependent_p)
2975                   {
2976                     tree fn = OVL_CURRENT (fns);
2977                     
2978                     /* Member functions of dependent classes are
2979                        dependent.  */
2980                     if (TREE_CODE (fn) == FUNCTION_DECL
2981                         && cp_parser_type_dependent_expression_p (fn))
2982                       dependent_p = true;
2983                     else if (TREE_CODE (fn) == TEMPLATE_DECL
2984                              && cp_parser_dependent_template_p (fn))
2985                       dependent_p = true;
2986                     
2987                     fns = OVL_NEXT (fns);
2988                   }
2989               }
2990
2991             /* If the name was dependent on a template parameter,
2992                we will resolve the name at instantiation time.  */
2993             if (dependent_p)
2994               {
2995                 /* Create a SCOPE_REF for qualified names.  */
2996                 if (parser->scope)
2997                   {
2998                     if (TYPE_P (parser->scope))
2999                       *qualifying_class = parser->scope;
3000                     return build_nt (SCOPE_REF, 
3001                                      parser->scope, 
3002                                      id_expression);
3003                   }
3004                 /* A TEMPLATE_ID already contains all the information
3005                    we need.  */
3006                 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3007                   return id_expression;
3008                 /* Create a LOOKUP_EXPR for other unqualified names.  */
3009                 return build_min_nt (LOOKUP_EXPR, id_expression);
3010               }
3011
3012             if (parser->scope)
3013               {
3014                 decl = (adjust_result_of_qualified_name_lookup 
3015                         (decl, parser->scope, current_class_type));
3016                 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3017                   *qualifying_class = parser->scope;
3018               }
3019             /* Resolve references to variables of anonymous unions
3020                into COMPONENT_REFs.  */
3021             else if (TREE_CODE (decl) == ALIAS_DECL)
3022               decl = DECL_INITIAL (decl);
3023             else
3024               /* Transform references to non-static data members into
3025                  COMPONENT_REFs.  */
3026               decl = hack_identifier (decl, id_expression);
3027           }
3028
3029         if (TREE_DEPRECATED (decl))
3030           warn_deprecated_use (decl);
3031
3032         return decl;
3033       }
3034
3035       /* Anything else is an error.  */
3036     default:
3037       cp_parser_error (parser, "expected primary-expression");
3038       return error_mark_node;
3039     }
3040 }
3041
3042 /* Parse an id-expression.
3043
3044    id-expression:
3045      unqualified-id
3046      qualified-id
3047
3048    qualified-id:
3049      :: [opt] nested-name-specifier template [opt] unqualified-id
3050      :: identifier
3051      :: operator-function-id
3052      :: template-id
3053
3054    Return a representation of the unqualified portion of the
3055    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3056    a `::' or nested-name-specifier.
3057
3058    Often, if the id-expression was a qualified-id, the caller will
3059    want to make a SCOPE_REF to represent the qualified-id.  This
3060    function does not do this in order to avoid wastefully creating
3061    SCOPE_REFs when they are not required.
3062
3063    If ASSUME_TYPENAME_P is true then we assume that qualified names
3064    are typenames.  This flag is set when parsing a declarator-id;
3065    for something like:
3066
3067      template <class T>
3068      int S<T>::R::i = 3;
3069
3070    we are supposed to assume that `S<T>::R' is a class.
3071
3072    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3073    `template' keyword.
3074
3075    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3076    uninstantiated templates.  
3077
3078    If *TEMPLATE_KEYWORD_P is non-NULL, it is set to true iff the
3079    `template' keyword is used to explicitly indicate that the entity
3080    named is a template.  */
3081
3082 static tree
3083 cp_parser_id_expression (cp_parser *parser,
3084                          bool template_keyword_p,
3085                          bool check_dependency_p,
3086                          bool *template_p)
3087 {
3088   bool global_scope_p;
3089   bool nested_name_specifier_p;
3090
3091   /* Assume the `template' keyword was not used.  */
3092   if (template_p)
3093     *template_p = false;
3094
3095   /* Look for the optional `::' operator.  */
3096   global_scope_p 
3097     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) 
3098        != NULL_TREE);
3099   /* Look for the optional nested-name-specifier.  */
3100   nested_name_specifier_p 
3101     = (cp_parser_nested_name_specifier_opt (parser,
3102                                             /*typename_keyword_p=*/false,
3103                                             check_dependency_p,
3104                                             /*type_p=*/false)
3105        != NULL_TREE);
3106   /* If there is a nested-name-specifier, then we are looking at
3107      the first qualified-id production.  */
3108   if (nested_name_specifier_p)
3109     {
3110       tree saved_scope;
3111       tree saved_object_scope;
3112       tree saved_qualifying_scope;
3113       tree unqualified_id;
3114       bool is_template;
3115
3116       /* See if the next token is the `template' keyword.  */
3117       if (!template_p)
3118         template_p = &is_template;
3119       *template_p = cp_parser_optional_template_keyword (parser);
3120       /* Name lookup we do during the processing of the
3121          unqualified-id might obliterate SCOPE.  */
3122       saved_scope = parser->scope;
3123       saved_object_scope = parser->object_scope;
3124       saved_qualifying_scope = parser->qualifying_scope;
3125       /* Process the final unqualified-id.  */
3126       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3127                                                  check_dependency_p);
3128       /* Restore the SAVED_SCOPE for our caller.  */
3129       parser->scope = saved_scope;
3130       parser->object_scope = saved_object_scope;
3131       parser->qualifying_scope = saved_qualifying_scope;
3132
3133       return unqualified_id;
3134     }
3135   /* Otherwise, if we are in global scope, then we are looking at one
3136      of the other qualified-id productions.  */
3137   else if (global_scope_p)
3138     {
3139       cp_token *token;
3140       tree id;
3141
3142       /* Peek at the next token.  */
3143       token = cp_lexer_peek_token (parser->lexer);
3144
3145       /* If it's an identifier, and the next token is not a "<", then
3146          we can avoid the template-id case.  This is an optimization
3147          for this common case.  */
3148       if (token->type == CPP_NAME 
3149           && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
3150         return cp_parser_identifier (parser);
3151
3152       cp_parser_parse_tentatively (parser);
3153       /* Try a template-id.  */
3154       id = cp_parser_template_id (parser, 
3155                                   /*template_keyword_p=*/false,
3156                                   /*check_dependency_p=*/true);
3157       /* If that worked, we're done.  */
3158       if (cp_parser_parse_definitely (parser))
3159         return id;
3160
3161       /* Peek at the next token.  (Changes in the token buffer may
3162          have invalidated the pointer obtained above.)  */
3163       token = cp_lexer_peek_token (parser->lexer);
3164
3165       switch (token->type)
3166         {
3167         case CPP_NAME:
3168           return cp_parser_identifier (parser);
3169
3170         case CPP_KEYWORD:
3171           if (token->keyword == RID_OPERATOR)
3172             return cp_parser_operator_function_id (parser);
3173           /* Fall through.  */
3174           
3175         default:
3176           cp_parser_error (parser, "expected id-expression");
3177           return error_mark_node;
3178         }
3179     }
3180   else
3181     return cp_parser_unqualified_id (parser, template_keyword_p,
3182                                      /*check_dependency_p=*/true);
3183 }
3184
3185 /* Parse an unqualified-id.
3186
3187    unqualified-id:
3188      identifier
3189      operator-function-id
3190      conversion-function-id
3191      ~ class-name
3192      template-id
3193
3194    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3195    keyword, in a construct like `A::template ...'.
3196
3197    Returns a representation of unqualified-id.  For the `identifier'
3198    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3199    production a BIT_NOT_EXPR is returned; the operand of the
3200    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3201    other productions, see the documentation accompanying the
3202    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3203    names are looked up in uninstantiated templates.  */
3204
3205 static tree
3206 cp_parser_unqualified_id (parser, template_keyword_p,
3207                           check_dependency_p)
3208      cp_parser *parser;
3209      bool template_keyword_p;
3210      bool check_dependency_p;
3211 {
3212   cp_token *token;
3213
3214   /* Peek at the next token.  */
3215   token = cp_lexer_peek_token (parser->lexer);
3216   
3217   switch (token->type)
3218     {
3219     case CPP_NAME:
3220       {
3221         tree id;
3222
3223         /* We don't know yet whether or not this will be a
3224            template-id.  */
3225         cp_parser_parse_tentatively (parser);
3226         /* Try a template-id.  */
3227         id = cp_parser_template_id (parser, template_keyword_p,
3228                                     check_dependency_p);
3229         /* If it worked, we're done.  */
3230         if (cp_parser_parse_definitely (parser))
3231           return id;
3232         /* Otherwise, it's an ordinary identifier.  */
3233         return cp_parser_identifier (parser);
3234       }
3235
3236     case CPP_TEMPLATE_ID:
3237       return cp_parser_template_id (parser, template_keyword_p,
3238                                     check_dependency_p);
3239
3240     case CPP_COMPL:
3241       {
3242         tree type_decl;
3243         tree qualifying_scope;
3244         tree object_scope;
3245         tree scope;
3246
3247         /* Consume the `~' token.  */
3248         cp_lexer_consume_token (parser->lexer);
3249         /* Parse the class-name.  The standard, as written, seems to
3250            say that:
3251
3252              template <typename T> struct S { ~S (); };
3253              template <typename T> S<T>::~S() {}
3254
3255            is invalid, since `~' must be followed by a class-name, but
3256            `S<T>' is dependent, and so not known to be a class.
3257            That's not right; we need to look in uninstantiated
3258            templates.  A further complication arises from:
3259
3260              template <typename T> void f(T t) {
3261                t.T::~T();
3262              } 
3263
3264            Here, it is not possible to look up `T' in the scope of `T'
3265            itself.  We must look in both the current scope, and the
3266            scope of the containing complete expression.  
3267
3268            Yet another issue is:
3269
3270              struct S {
3271                int S;
3272                ~S();
3273              };
3274
3275              S::~S() {}
3276
3277            The standard does not seem to say that the `S' in `~S'
3278            should refer to the type `S' and not the data member
3279            `S::S'.  */
3280
3281         /* DR 244 says that we look up the name after the "~" in the
3282            same scope as we looked up the qualifying name.  That idea
3283            isn't fully worked out; it's more complicated than that.  */
3284         scope = parser->scope;
3285         object_scope = parser->object_scope;
3286         qualifying_scope = parser->qualifying_scope;
3287
3288         /* If the name is of the form "X::~X" it's OK.  */
3289         if (scope && TYPE_P (scope)
3290             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3291             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
3292                 == CPP_OPEN_PAREN)
3293             && (cp_lexer_peek_token (parser->lexer)->value 
3294                 == TYPE_IDENTIFIER (scope)))
3295           {
3296             cp_lexer_consume_token (parser->lexer);
3297             return build_nt (BIT_NOT_EXPR, scope);
3298           }
3299
3300         /* If there was an explicit qualification (S::~T), first look
3301            in the scope given by the qualification (i.e., S).  */
3302         if (scope)
3303           {
3304             cp_parser_parse_tentatively (parser);
3305             type_decl = cp_parser_class_name (parser, 
3306                                               /*typename_keyword_p=*/false,
3307                                               /*template_keyword_p=*/false,
3308                                               /*type_p=*/false,
3309                                               /*check_access_p=*/true,
3310                                               /*check_dependency=*/false,
3311                                               /*class_head_p=*/false);
3312             if (cp_parser_parse_definitely (parser))
3313               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3314           }
3315         /* In "N::S::~S", look in "N" as well.  */
3316         if (scope && qualifying_scope)
3317           {
3318             cp_parser_parse_tentatively (parser);
3319             parser->scope = qualifying_scope;
3320             parser->object_scope = NULL_TREE;
3321             parser->qualifying_scope = NULL_TREE;
3322             type_decl 
3323               = cp_parser_class_name (parser, 
3324                                       /*typename_keyword_p=*/false,
3325                                       /*template_keyword_p=*/false,
3326                                       /*type_p=*/false,
3327                                       /*check_access_p=*/true,
3328                                       /*check_dependency=*/false,
3329                                       /*class_head_p=*/false);
3330             if (cp_parser_parse_definitely (parser))
3331               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3332           }
3333         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3334         else if (object_scope)
3335           {
3336             cp_parser_parse_tentatively (parser);
3337             parser->scope = object_scope;
3338             parser->object_scope = NULL_TREE;
3339             parser->qualifying_scope = NULL_TREE;
3340             type_decl 
3341               = cp_parser_class_name (parser, 
3342                                       /*typename_keyword_p=*/false,
3343                                       /*template_keyword_p=*/false,
3344                                       /*type_p=*/false,
3345                                       /*check_access_p=*/true,
3346                                       /*check_dependency=*/false,
3347                                       /*class_head_p=*/false);
3348             if (cp_parser_parse_definitely (parser))
3349               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3350           }
3351         /* Look in the surrounding context.  */
3352         parser->scope = NULL_TREE;
3353         parser->object_scope = NULL_TREE;
3354         parser->qualifying_scope = NULL_TREE;
3355         type_decl 
3356           = cp_parser_class_name (parser, 
3357                                   /*typename_keyword_p=*/false,
3358                                   /*template_keyword_p=*/false,
3359                                   /*type_p=*/false,
3360                                   /*check_access_p=*/true,
3361                                   /*check_dependency=*/false,
3362                                   /*class_head_p=*/false);
3363         /* If an error occurred, assume that the name of the
3364            destructor is the same as the name of the qualifying
3365            class.  That allows us to keep parsing after running
3366            into ill-formed destructor names.  */
3367         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3368           return build_nt (BIT_NOT_EXPR, scope);
3369         else if (type_decl == error_mark_node)
3370           return error_mark_node;
3371
3372         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3373       }
3374
3375     case CPP_KEYWORD:
3376       if (token->keyword == RID_OPERATOR)
3377         {
3378           tree id;
3379
3380           /* This could be a template-id, so we try that first.  */
3381           cp_parser_parse_tentatively (parser);
3382           /* Try a template-id.  */
3383           id = cp_parser_template_id (parser, template_keyword_p,
3384                                       /*check_dependency_p=*/true);
3385           /* If that worked, we're done.  */
3386           if (cp_parser_parse_definitely (parser))
3387             return id;
3388           /* We still don't know whether we're looking at an
3389              operator-function-id or a conversion-function-id.  */
3390           cp_parser_parse_tentatively (parser);
3391           /* Try an operator-function-id.  */
3392           id = cp_parser_operator_function_id (parser);
3393           /* If that didn't work, try a conversion-function-id.  */
3394           if (!cp_parser_parse_definitely (parser))
3395             id = cp_parser_conversion_function_id (parser);
3396
3397           return id;
3398         }
3399       /* Fall through.  */
3400
3401     default:
3402       cp_parser_error (parser, "expected unqualified-id");
3403       return error_mark_node;
3404     }
3405 }
3406
3407 /* Parse an (optional) nested-name-specifier.
3408
3409    nested-name-specifier:
3410      class-or-namespace-name :: nested-name-specifier [opt]
3411      class-or-namespace-name :: template nested-name-specifier [opt]
3412
3413    PARSER->SCOPE should be set appropriately before this function is
3414    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3415    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3416    in name lookups.
3417
3418    Sets PARSER->SCOPE to the class (TYPE) or namespace
3419    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3420    it unchanged if there is no nested-name-specifier.  Returns the new
3421    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.  */
3422
3423 static tree
3424 cp_parser_nested_name_specifier_opt (cp_parser *parser, 
3425                                      bool typename_keyword_p, 
3426                                      bool check_dependency_p,
3427                                      bool type_p)
3428 {
3429   bool success = false;
3430   tree access_check = NULL_TREE;
3431   ptrdiff_t start;
3432
3433   /* If the next token corresponds to a nested name specifier, there
3434      is no need to reparse it.  */
3435   if (cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3436     {
3437       tree value;
3438       tree check;
3439
3440       /* Get the stored value.  */
3441       value = cp_lexer_consume_token (parser->lexer)->value;
3442       /* Perform any access checks that were deferred.  */
3443       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
3444         cp_parser_defer_access_check (parser, 
3445                                       TREE_PURPOSE (check),
3446                                       TREE_VALUE (check));
3447       /* Set the scope from the stored value.  */
3448       parser->scope = TREE_VALUE (value);
3449       parser->qualifying_scope = TREE_TYPE (value);
3450       parser->object_scope = NULL_TREE;
3451       return parser->scope;
3452     }
3453
3454   /* Remember where the nested-name-specifier starts.  */
3455   if (cp_parser_parsing_tentatively (parser)
3456       && !cp_parser_committed_to_tentative_parse (parser))
3457     {
3458       cp_token *next_token = cp_lexer_peek_token (parser->lexer);
3459       start = cp_lexer_token_difference (parser->lexer,
3460                                          parser->lexer->first_token,
3461                                          next_token);
3462       access_check = parser->context->deferred_access_checks;
3463     }
3464   else
3465     start = -1;
3466
3467   while (true)
3468     {
3469       tree new_scope;
3470       tree old_scope;
3471       tree saved_qualifying_scope;
3472       cp_token *token;
3473       bool template_keyword_p;
3474
3475       /* Spot cases that cannot be the beginning of a
3476          nested-name-specifier.  On the second and subsequent times
3477          through the loop, we look for the `template' keyword.  */
3478       token = cp_lexer_peek_token (parser->lexer);
3479       if (success && token->keyword == RID_TEMPLATE)
3480         ;
3481       /* A template-id can start a nested-name-specifier.  */
3482       else if (token->type == CPP_TEMPLATE_ID)
3483         ;
3484       else
3485         {
3486           /* If the next token is not an identifier, then it is
3487              definitely not a class-or-namespace-name.  */
3488           if (token->type != CPP_NAME)
3489             break;
3490           /* If the following token is neither a `<' (to begin a
3491              template-id), nor a `::', then we are not looking at a
3492              nested-name-specifier.  */
3493           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3494           if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3495             break;
3496         }
3497
3498       /* The nested-name-specifier is optional, so we parse
3499          tentatively.  */
3500       cp_parser_parse_tentatively (parser);
3501
3502       /* Look for the optional `template' keyword, if this isn't the
3503          first time through the loop.  */
3504       if (success)
3505         template_keyword_p = cp_parser_optional_template_keyword (parser);
3506       else
3507         template_keyword_p = false;
3508
3509       /* Save the old scope since the name lookup we are about to do
3510          might destroy it.  */
3511       old_scope = parser->scope;
3512       saved_qualifying_scope = parser->qualifying_scope;
3513       /* Parse the qualifying entity.  */
3514       new_scope 
3515         = cp_parser_class_or_namespace_name (parser,
3516                                              typename_keyword_p,
3517                                              template_keyword_p,
3518                                              check_dependency_p,
3519                                              type_p);
3520       /* Look for the `::' token.  */
3521       cp_parser_require (parser, CPP_SCOPE, "`::'");
3522
3523       /* If we found what we wanted, we keep going; otherwise, we're
3524          done.  */
3525       if (!cp_parser_parse_definitely (parser))
3526         {
3527           bool error_p = false;
3528
3529           /* Restore the OLD_SCOPE since it was valid before the
3530              failed attempt at finding the last
3531              class-or-namespace-name.  */
3532           parser->scope = old_scope;
3533           parser->qualifying_scope = saved_qualifying_scope;
3534           /* If the next token is an identifier, and the one after
3535              that is a `::', then any valid interpretation would have
3536              found a class-or-namespace-name.  */
3537           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3538                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
3539                      == CPP_SCOPE)
3540                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
3541                      != CPP_COMPL))
3542             {
3543               token = cp_lexer_consume_token (parser->lexer);
3544               if (!error_p) 
3545                 {
3546                   tree decl;
3547
3548                   decl = cp_parser_lookup_name_simple (parser, token->value);
3549                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3550                     error ("`%D' used without template parameters",
3551                            decl);
3552                   else if (parser->scope)
3553                     {
3554                       if (TYPE_P (parser->scope))
3555                         error ("`%T::%D' is not a class-name or "
3556                                "namespace-name",
3557                                parser->scope, token->value);
3558                       else
3559                         error ("`%D::%D' is not a class-name or "
3560                                "namespace-name",
3561                                parser->scope, token->value);
3562                     }
3563                   else
3564                     error ("`%D' is not a class-name or namespace-name",
3565                            token->value);
3566                   parser->scope = NULL_TREE;
3567                   error_p = true;
3568                   /* Treat this as a successful nested-name-specifier
3569                      due to:
3570
3571                      [basic.lookup.qual]
3572
3573                      If the name found is not a class-name (clause
3574                      _class_) or namespace-name (_namespace.def_), the
3575                      program is ill-formed.  */
3576                   success = true;
3577                 }
3578               cp_lexer_consume_token (parser->lexer);
3579             }
3580           break;
3581         }
3582
3583       /* We've found one valid nested-name-specifier.  */
3584       success = true;
3585       /* Make sure we look in the right scope the next time through
3586          the loop.  */
3587       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL 
3588                        ? TREE_TYPE (new_scope)
3589                        : new_scope);
3590       /* If it is a class scope, try to complete it; we are about to
3591          be looking up names inside the class.  */
3592       if (TYPE_P (parser->scope))
3593         complete_type (parser->scope);
3594     }
3595
3596   /* If parsing tentatively, replace the sequence of tokens that makes
3597      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3598      token.  That way, should we re-parse the token stream, we will
3599      not have to repeat the effort required to do the parse, nor will
3600      we issue duplicate error messages.  */
3601   if (success && start >= 0)
3602     {
3603       cp_token *token;
3604       tree c;
3605
3606       /* Find the token that corresponds to the start of the
3607          template-id.  */
3608       token = cp_lexer_advance_token (parser->lexer, 
3609                                       parser->lexer->first_token,
3610                                       start);
3611
3612       /* Remember the access checks associated with this
3613          nested-name-specifier.  */
3614       c = parser->context->deferred_access_checks;
3615       if (c == access_check)
3616         access_check = NULL_TREE;
3617       else
3618         {
3619           while (TREE_CHAIN (c) != access_check)
3620             c = TREE_CHAIN (c);
3621           access_check = parser->context->deferred_access_checks;
3622           parser->context->deferred_access_checks = TREE_CHAIN (c);
3623           TREE_CHAIN (c) = NULL_TREE;
3624         }
3625
3626       /* Reset the contents of the START token.  */
3627       token->type = CPP_NESTED_NAME_SPECIFIER;
3628       token->value = build_tree_list (access_check, parser->scope);
3629       TREE_TYPE (token->value) = parser->qualifying_scope;
3630       token->keyword = RID_MAX;
3631       /* Purge all subsequent tokens.  */
3632       cp_lexer_purge_tokens_after (parser->lexer, token);
3633     }
3634
3635   return success ? parser->scope : NULL_TREE;
3636 }
3637
3638 /* Parse a nested-name-specifier.  See
3639    cp_parser_nested_name_specifier_opt for details.  This function
3640    behaves identically, except that it will an issue an error if no
3641    nested-name-specifier is present, and it will return
3642    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3643    is present.  */
3644
3645 static tree
3646 cp_parser_nested_name_specifier (cp_parser *parser, 
3647                                  bool typename_keyword_p, 
3648                                  bool check_dependency_p,
3649                                  bool type_p)
3650 {
3651   tree scope;
3652
3653   /* Look for the nested-name-specifier.  */
3654   scope = cp_parser_nested_name_specifier_opt (parser,
3655                                                typename_keyword_p,
3656                                                check_dependency_p,
3657                                                type_p);
3658   /* If it was not present, issue an error message.  */
3659   if (!scope)
3660     {
3661       cp_parser_error (parser, "expected nested-name-specifier");
3662       return error_mark_node;
3663     }
3664
3665   return scope;
3666 }
3667
3668 /* Parse a class-or-namespace-name.
3669
3670    class-or-namespace-name:
3671      class-name
3672      namespace-name
3673
3674    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3675    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3676    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3677    TYPE_P is TRUE iff the next name should be taken as a class-name,
3678    even the same name is declared to be another entity in the same
3679    scope.
3680
3681    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3682    specified by the class-or-namespace-name.  If neither is found the
3683    ERROR_MARK_NODE is returned.  */
3684
3685 static tree
3686 cp_parser_class_or_namespace_name (cp_parser *parser, 
3687                                    bool typename_keyword_p,
3688                                    bool template_keyword_p,
3689                                    bool check_dependency_p,
3690                                    bool type_p)
3691 {
3692   tree saved_scope;
3693   tree saved_qualifying_scope;
3694   tree saved_object_scope;
3695   tree scope;
3696   bool only_class_p;
3697
3698   /* If the next token is the `template' keyword, we know that we are
3699      looking at a class-name.  */
3700   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3701     return cp_parser_class_name (parser, 
3702                                  typename_keyword_p,
3703                                  template_keyword_p,
3704                                  type_p,
3705                                  /*check_access_p=*/true,
3706                                  check_dependency_p,
3707                                  /*class_head_p=*/false);
3708   /* Before we try to parse the class-name, we must save away the
3709      current PARSER->SCOPE since cp_parser_class_name will destroy
3710      it.  */
3711   saved_scope = parser->scope;
3712   saved_qualifying_scope = parser->qualifying_scope;
3713   saved_object_scope = parser->object_scope;
3714   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3715      there is no need to look for a namespace-name.  */
3716   only_class_p = saved_scope && TYPE_P (saved_scope);
3717   if (!only_class_p)
3718     cp_parser_parse_tentatively (parser);
3719   scope = cp_parser_class_name (parser, 
3720                                 typename_keyword_p,
3721                                 template_keyword_p,
3722                                 type_p,
3723                                 /*check_access_p=*/true,
3724                                 check_dependency_p,
3725                                 /*class_head_p=*/false);
3726   /* If that didn't work, try for a namespace-name.  */
3727   if (!only_class_p && !cp_parser_parse_definitely (parser))
3728     {
3729       /* Restore the saved scope.  */
3730       parser->scope = saved_scope;
3731       parser->qualifying_scope = saved_qualifying_scope;
3732       parser->object_scope = saved_object_scope;
3733       /* If we are not looking at an identifier followed by the scope
3734          resolution operator, then this is not part of a
3735          nested-name-specifier.  (Note that this function is only used
3736          to parse the components of a nested-name-specifier.)  */
3737       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3738           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3739         return error_mark_node;
3740       scope = cp_parser_namespace_name (parser);
3741     }
3742
3743   return scope;
3744 }
3745
3746 /* Parse a postfix-expression.
3747
3748    postfix-expression:
3749      primary-expression
3750      postfix-expression [ expression ]
3751      postfix-expression ( expression-list [opt] )
3752      simple-type-specifier ( expression-list [opt] )
3753      typename :: [opt] nested-name-specifier identifier 
3754        ( expression-list [opt] )
3755      typename :: [opt] nested-name-specifier template [opt] template-id
3756        ( expression-list [opt] )
3757      postfix-expression . template [opt] id-expression
3758      postfix-expression -> template [opt] id-expression
3759      postfix-expression . pseudo-destructor-name
3760      postfix-expression -> pseudo-destructor-name
3761      postfix-expression ++
3762      postfix-expression --
3763      dynamic_cast < type-id > ( expression )
3764      static_cast < type-id > ( expression )
3765      reinterpret_cast < type-id > ( expression )
3766      const_cast < type-id > ( expression )
3767      typeid ( expression )
3768      typeid ( type-id )
3769
3770    GNU Extension:
3771      
3772    postfix-expression:
3773      ( type-id ) { initializer-list , [opt] }
3774
3775    This extension is a GNU version of the C99 compound-literal
3776    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3777    but they are essentially the same concept.)
3778
3779    If ADDRESS_P is true, the postfix expression is the operand of the
3780    `&' operator.
3781
3782    Returns a representation of the expression.  */
3783
3784 static tree
3785 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3786 {
3787   cp_token *token;
3788   enum rid keyword;
3789   cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3790   tree postfix_expression = NULL_TREE;
3791   /* Non-NULL only if the current postfix-expression can be used to
3792      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3793      class used to qualify the member.  */
3794   tree qualifying_class = NULL_TREE;
3795   bool done;
3796
3797   /* Peek at the next token.  */
3798   token = cp_lexer_peek_token (parser->lexer);
3799   /* Some of the productions are determined by keywords.  */
3800   keyword = token->keyword;
3801   switch (keyword)
3802     {
3803     case RID_DYNCAST:
3804     case RID_STATCAST:
3805     case RID_REINTCAST:
3806     case RID_CONSTCAST:
3807       {
3808         tree type;
3809         tree expression;
3810         const char *saved_message;
3811
3812         /* All of these can be handled in the same way from the point
3813            of view of parsing.  Begin by consuming the token
3814            identifying the cast.  */
3815         cp_lexer_consume_token (parser->lexer);
3816         
3817         /* New types cannot be defined in the cast.  */
3818         saved_message = parser->type_definition_forbidden_message;
3819         parser->type_definition_forbidden_message
3820           = "types may not be defined in casts";
3821
3822         /* Look for the opening `<'.  */
3823         cp_parser_require (parser, CPP_LESS, "`<'");
3824         /* Parse the type to which we are casting.  */
3825         type = cp_parser_type_id (parser);
3826         /* Look for the closing `>'.  */
3827         cp_parser_require (parser, CPP_GREATER, "`>'");
3828         /* Restore the old message.  */
3829         parser->type_definition_forbidden_message = saved_message;
3830
3831         /* And the expression which is being cast.  */
3832         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3833         expression = cp_parser_expression (parser);
3834         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3835
3836         switch (keyword)
3837           {
3838           case RID_DYNCAST:
3839             postfix_expression
3840               = build_dynamic_cast (type, expression);
3841             break;
3842           case RID_STATCAST:
3843             postfix_expression
3844               = build_static_cast (type, expression);
3845             break;
3846           case RID_REINTCAST:
3847             postfix_expression
3848               = build_reinterpret_cast (type, expression);
3849             break;
3850           case RID_CONSTCAST:
3851             postfix_expression
3852               = build_const_cast (type, expression);
3853             break;
3854           default:
3855             abort ();
3856           }
3857       }
3858       break;
3859
3860     case RID_TYPEID:
3861       {
3862         tree type;
3863         const char *saved_message;
3864
3865         /* Consume the `typeid' token.  */
3866         cp_lexer_consume_token (parser->lexer);
3867         /* Look for the `(' token.  */
3868         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3869         /* Types cannot be defined in a `typeid' expression.  */
3870         saved_message = parser->type_definition_forbidden_message;
3871         parser->type_definition_forbidden_message
3872           = "types may not be defined in a `typeid\' expression";
3873         /* We can't be sure yet whether we're looking at a type-id or an
3874            expression.  */
3875         cp_parser_parse_tentatively (parser);
3876         /* Try a type-id first.  */
3877         type = cp_parser_type_id (parser);
3878         /* Look for the `)' token.  Otherwise, we can't be sure that
3879            we're not looking at an expression: consider `typeid (int
3880            (3))', for example.  */
3881         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3882         /* If all went well, simply lookup the type-id.  */
3883         if (cp_parser_parse_definitely (parser))
3884           postfix_expression = get_typeid (type);
3885         /* Otherwise, fall back to the expression variant.  */
3886         else
3887           {
3888             tree expression;
3889
3890             /* Look for an expression.  */
3891             expression = cp_parser_expression (parser);
3892             /* Compute its typeid.  */
3893             postfix_expression = build_typeid (expression);
3894             /* Look for the `)' token.  */
3895             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3896           }
3897
3898         /* Restore the saved message.  */
3899         parser->type_definition_forbidden_message = saved_message;
3900       }
3901       break;
3902       
3903     case RID_TYPENAME:
3904       {
3905         bool template_p = false;
3906         tree id;
3907         tree type;
3908
3909         /* Consume the `typename' token.  */
3910         cp_lexer_consume_token (parser->lexer);
3911         /* Look for the optional `::' operator.  */
3912         cp_parser_global_scope_opt (parser, 
3913                                     /*current_scope_valid_p=*/false);
3914         /* Look for the nested-name-specifier.  */
3915         cp_parser_nested_name_specifier (parser,
3916                                          /*typename_keyword_p=*/true,
3917                                          /*check_dependency_p=*/true,
3918                                          /*type_p=*/true);
3919         /* Look for the optional `template' keyword.  */
3920         template_p = cp_parser_optional_template_keyword (parser);
3921         /* We don't know whether we're looking at a template-id or an
3922            identifier.  */
3923         cp_parser_parse_tentatively (parser);
3924         /* Try a template-id.  */
3925         id = cp_parser_template_id (parser, template_p,
3926                                     /*check_dependency_p=*/true);
3927         /* If that didn't work, try an identifier.  */
3928         if (!cp_parser_parse_definitely (parser))
3929           id = cp_parser_identifier (parser);
3930         /* Create a TYPENAME_TYPE to represent the type to which the
3931            functional cast is being performed.  */
3932         type = make_typename_type (parser->scope, id, 
3933                                    /*complain=*/1);
3934
3935         postfix_expression = cp_parser_functional_cast (parser, type);
3936       }
3937       break;
3938
3939     default:
3940       {
3941         tree type;
3942
3943         /* If the next thing is a simple-type-specifier, we may be
3944            looking at a functional cast.  We could also be looking at
3945            an id-expression.  So, we try the functional cast, and if
3946            that doesn't work we fall back to the primary-expression.  */
3947         cp_parser_parse_tentatively (parser);
3948         /* Look for the simple-type-specifier.  */
3949         type = cp_parser_simple_type_specifier (parser, 
3950                                                 CP_PARSER_FLAGS_NONE);
3951         /* Parse the cast itself.  */
3952         if (!cp_parser_error_occurred (parser))
3953           postfix_expression 
3954             = cp_parser_functional_cast (parser, type);
3955         /* If that worked, we're done.  */
3956         if (cp_parser_parse_definitely (parser))
3957           break;
3958
3959         /* If the functional-cast didn't work out, try a
3960            compound-literal.  */
3961         if (cp_parser_allow_gnu_extensions_p (parser))
3962           {
3963             tree initializer_list = NULL_TREE;
3964
3965             cp_parser_parse_tentatively (parser);
3966             /* Look for the `('.  */
3967             if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3968               {
3969                 type = cp_parser_type_id (parser);
3970                 /* Look for the `)'.  */
3971                 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3972                 /* Look for the `{'.  */
3973                 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3974                 /* If things aren't going well, there's no need to
3975                    keep going.  */
3976                 if (!cp_parser_error_occurred (parser))
3977                   {
3978                     /* Parse the initializer-list.  */
3979                     initializer_list 
3980                       = cp_parser_initializer_list (parser);
3981                     /* Allow a trailing `,'.  */
3982                     if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3983                       cp_lexer_consume_token (parser->lexer);
3984                     /* Look for the final `}'.  */
3985                     cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3986                   }
3987               }
3988             /* If that worked, we're definitely looking at a
3989                compound-literal expression.  */
3990             if (cp_parser_parse_definitely (parser))
3991               {
3992                 /* Warn the user that a compound literal is not
3993                    allowed in standard C++.  */
3994                 if (pedantic)
3995                   pedwarn ("ISO C++ forbids compound-literals");
3996                 /* Form the representation of the compound-literal.  */
3997                 postfix_expression 
3998                   = finish_compound_literal (type, initializer_list);
3999                 break;
4000               }
4001           }
4002
4003         /* It must be a primary-expression.  */
4004         postfix_expression = cp_parser_primary_expression (parser, 
4005                                                            &idk,
4006                                                            &qualifying_class);
4007       }
4008       break;
4009     }
4010
4011   /* Peek at the next token.  */
4012   token = cp_lexer_peek_token (parser->lexer);
4013   done = (token->type != CPP_OPEN_SQUARE
4014           && token->type != CPP_OPEN_PAREN
4015           && token->type != CPP_DOT
4016           && token->type != CPP_DEREF
4017           && token->type != CPP_PLUS_PLUS
4018           && token->type != CPP_MINUS_MINUS);
4019
4020   /* If the postfix expression is complete, finish up.  */
4021   if (address_p && qualifying_class && done)
4022     {
4023       if (TREE_CODE (postfix_expression) == SCOPE_REF)
4024         postfix_expression = TREE_OPERAND (postfix_expression, 1);
4025       postfix_expression 
4026         = build_offset_ref (qualifying_class, postfix_expression);
4027       return postfix_expression;
4028     }
4029
4030   /* Otherwise, if we were avoiding committing until we knew
4031      whether or not we had a pointer-to-member, we now know that
4032      the expression is an ordinary reference to a qualified name.  */
4033   if (qualifying_class && !processing_template_decl)
4034     {
4035       if (TREE_CODE (postfix_expression) == FIELD_DECL)
4036         postfix_expression 
4037           = finish_non_static_data_member (postfix_expression,
4038                                            qualifying_class);
4039       else if (BASELINK_P (postfix_expression))
4040         {
4041           tree fn;
4042           tree fns;
4043
4044           /* See if any of the functions are non-static members.  */
4045           fns = BASELINK_FUNCTIONS (postfix_expression);
4046           if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4047             fns = TREE_OPERAND (fns, 0);
4048           for (fn = fns; fn; fn = OVL_NEXT (fn))
4049             if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4050               break;
4051           /* If so, the expression may be relative to the current
4052              class.  */
4053           if (fn && current_class_type 
4054               && DERIVED_FROM_P (qualifying_class, current_class_type))
4055             postfix_expression 
4056               = (build_class_member_access_expr 
4057                  (maybe_dummy_object (qualifying_class, NULL),
4058                   postfix_expression,
4059                   BASELINK_ACCESS_BINFO (postfix_expression),
4060                   /*preserve_reference=*/false));
4061           else if (done)
4062             return build_offset_ref (qualifying_class,
4063                                      postfix_expression);
4064         }
4065     }
4066
4067   /* Remember that there was a reference to this entity.  */
4068   if (DECL_P (postfix_expression))
4069     mark_used (postfix_expression);
4070
4071   /* Keep looping until the postfix-expression is complete.  */
4072   while (true)
4073     {
4074       if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4075           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4076         {
4077           /* It is not a Koenig lookup function call.  */
4078           unqualified_name_lookup_error (postfix_expression);
4079           postfix_expression = error_mark_node;
4080         }
4081       
4082       /* Peek at the next token.  */
4083       token = cp_lexer_peek_token (parser->lexer);
4084
4085       switch (token->type)
4086         {
4087         case CPP_OPEN_SQUARE:
4088           /* postfix-expression [ expression ] */
4089           {
4090             tree index;
4091
4092             /* Consume the `[' token.  */
4093             cp_lexer_consume_token (parser->lexer);
4094             /* Parse the index expression.  */
4095             index = cp_parser_expression (parser);
4096             /* Look for the closing `]'.  */
4097             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4098
4099             /* Build the ARRAY_REF.  */
4100             postfix_expression 
4101               = grok_array_decl (postfix_expression, index);
4102             idk = CP_PARSER_ID_KIND_NONE;
4103           }
4104           break;
4105
4106         case CPP_OPEN_PAREN:
4107           /* postfix-expression ( expression-list [opt] ) */
4108           {
4109             tree args;
4110
4111             /* Consume the `(' token.  */
4112             cp_lexer_consume_token (parser->lexer);
4113             /* If the next token is not a `)', then there are some
4114                arguments.  */
4115             if (cp_lexer_next_token_is_not (parser->lexer, 
4116                                             CPP_CLOSE_PAREN))
4117               args = cp_parser_expression_list (parser);
4118             else
4119               args = NULL_TREE;
4120             /* Look for the closing `)'.  */
4121             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4122
4123             if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4124                 && (is_overloaded_fn (postfix_expression)
4125                     || DECL_P (postfix_expression)
4126                     || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4127                 && args)
4128               {
4129                 tree arg;
4130                 tree identifier = NULL_TREE;
4131                 tree functions = NULL_TREE;
4132
4133                 /* Find the name of the overloaded function.  */
4134                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4135                   identifier = postfix_expression;
4136                 else if (is_overloaded_fn (postfix_expression))
4137                   {
4138                     functions = postfix_expression;
4139                     identifier = DECL_NAME (get_first_fn (functions));
4140                   }
4141                 else if (DECL_P (postfix_expression))
4142                   {
4143                     functions = postfix_expression;
4144                     identifier = DECL_NAME (postfix_expression);
4145                   }
4146
4147                 /* A call to a namespace-scope function using an
4148                    unqualified name.
4149
4150                    Do Koenig lookup -- unless any of the arguments are
4151                    type-dependent.  */
4152                 for (arg = args; arg; arg = TREE_CHAIN (arg))
4153                   if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4154                       break;
4155                 if (!arg)
4156                   {
4157                     postfix_expression 
4158                       = lookup_arg_dependent(identifier, functions, args);
4159                     if (!postfix_expression)
4160                       {
4161                         /* The unqualified name could not be resolved.  */
4162                         unqualified_name_lookup_error (identifier);
4163                         postfix_expression = error_mark_node;
4164                       }
4165                     postfix_expression
4166                       = build_call_from_tree (postfix_expression, args, 
4167                                               /*diallow_virtual=*/false);
4168                     break;
4169                   }
4170                 postfix_expression = build_min_nt (LOOKUP_EXPR,
4171                                                    identifier);
4172               }
4173             else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED 
4174                      && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4175               {
4176                 /* The unqualified name could not be resolved.  */
4177                 unqualified_name_lookup_error (postfix_expression);
4178                 postfix_expression = error_mark_node;
4179                 break;
4180               }
4181
4182             /* In the body of a template, no further processing is
4183                required.  */
4184             if (processing_template_decl)
4185               {
4186                 postfix_expression = build_nt (CALL_EXPR,
4187                                                postfix_expression, 
4188                                                args);
4189                 break;
4190               }
4191
4192             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4193               postfix_expression
4194                 = (build_new_method_call 
4195                    (TREE_OPERAND (postfix_expression, 0),
4196                     TREE_OPERAND (postfix_expression, 1),
4197                     args, NULL_TREE, 
4198                     (idk == CP_PARSER_ID_KIND_QUALIFIED 
4199                      ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4200             else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4201               postfix_expression = (build_offset_ref_call_from_tree
4202                                     (postfix_expression, args));
4203             else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4204               {
4205                 /* A call to a static class member, or a
4206                    namespace-scope function.  */
4207                 postfix_expression
4208                   = finish_call_expr (postfix_expression, args,
4209                                       /*disallow_virtual=*/true);
4210               }
4211             else
4212               {
4213                 /* All other function calls.  */
4214                 postfix_expression 
4215                   = finish_call_expr (postfix_expression, args, 
4216                                       /*disallow_virtual=*/false);
4217               }
4218
4219             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4220             idk = CP_PARSER_ID_KIND_NONE;
4221           }
4222           break;
4223           
4224         case CPP_DOT:
4225         case CPP_DEREF:
4226           /* postfix-expression . template [opt] id-expression  
4227              postfix-expression . pseudo-destructor-name 
4228              postfix-expression -> template [opt] id-expression
4229              postfix-expression -> pseudo-destructor-name */
4230           {
4231             tree name;
4232             bool dependent_p;
4233             bool template_p;
4234             tree scope = NULL_TREE;
4235
4236             /* If this is a `->' operator, dereference the pointer.  */
4237             if (token->type == CPP_DEREF)
4238               postfix_expression = build_x_arrow (postfix_expression);
4239             /* Check to see whether or not the expression is
4240                type-dependent.  */
4241             dependent_p = (cp_parser_type_dependent_expression_p 
4242                            (postfix_expression));
4243             /* The identifier following the `->' or `.' is not
4244                qualified.  */
4245             parser->scope = NULL_TREE;
4246             parser->qualifying_scope = NULL_TREE;
4247             parser->object_scope = NULL_TREE;
4248             /* Enter the scope corresponding to the type of the object
4249                given by the POSTFIX_EXPRESSION.  */
4250             if (!dependent_p 
4251                 && TREE_TYPE (postfix_expression) != NULL_TREE)
4252               {
4253                 scope = TREE_TYPE (postfix_expression);
4254                 /* According to the standard, no expression should
4255                    ever have reference type.  Unfortunately, we do not
4256                    currently match the standard in this respect in
4257                    that our internal representation of an expression
4258                    may have reference type even when the standard says
4259                    it does not.  Therefore, we have to manually obtain
4260                    the underlying type here.  */
4261                 if (TREE_CODE (scope) == REFERENCE_TYPE)
4262                   scope = TREE_TYPE (scope);
4263                 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4264                    type of the field.  We get an OFFSET_TYPE for
4265                    something like:
4266
4267                      S::T.a ...
4268
4269                    Probably, we should not get an OFFSET_TYPE here;
4270                    that transformation should be made only if `&S::T'
4271                    is written.  */
4272                 if (TREE_CODE (scope) == OFFSET_TYPE)
4273                   scope = TREE_TYPE (scope);
4274                 /* The type of the POSTFIX_EXPRESSION must be
4275                    complete.  */
4276                 scope = complete_type_or_else (scope, NULL_TREE);
4277                 /* Let the name lookup machinery know that we are
4278                    processing a class member access expression.  */
4279                 parser->context->object_type = scope;
4280                 /* If something went wrong, we want to be able to
4281                    discern that case, as opposed to the case where
4282                    there was no SCOPE due to the type of expression
4283                    being dependent.  */
4284                 if (!scope)
4285                   scope = error_mark_node;
4286               }
4287
4288             /* Consume the `.' or `->' operator.  */
4289             cp_lexer_consume_token (parser->lexer);
4290             /* If the SCOPE is not a scalar type, we are looking at an
4291                ordinary class member access expression, rather than a
4292                pseudo-destructor-name.  */
4293             if (!scope || !SCALAR_TYPE_P (scope))
4294               {
4295                 template_p = cp_parser_optional_template_keyword (parser);
4296                 /* Parse the id-expression.  */
4297                 name = cp_parser_id_expression (parser,
4298                                                 template_p,
4299                                                 /*check_dependency_p=*/true,
4300                                                 /*template_p=*/NULL);
4301                 /* In general, build a SCOPE_REF if the member name is
4302                    qualified.  However, if the name was not dependent
4303                    and has already been resolved; there is no need to
4304                    build the SCOPE_REF.  For example;
4305
4306                      struct X { void f(); };
4307                      template <typename T> void f(T* t) { t->X::f(); }
4308  
4309                    Even though "t" is dependent, "X::f" is not and has 
4310                    except that for a BASELINK there is no need to
4311                    include scope information.  */
4312                 if (name != error_mark_node 
4313                     && !BASELINK_P (name)
4314                     && parser->scope)
4315                   {
4316                     name = build_nt (SCOPE_REF, parser->scope, name);
4317                     parser->scope = NULL_TREE;
4318                     parser->qualifying_scope = NULL_TREE;
4319                     parser->object_scope = NULL_TREE;
4320                   }
4321                 postfix_expression 
4322                   = finish_class_member_access_expr (postfix_expression, name);
4323               }
4324             /* Otherwise, try the pseudo-destructor-name production.  */
4325             else
4326               {
4327                 tree s;
4328                 tree type;
4329
4330                 /* Parse the pseudo-destructor-name.  */
4331                 cp_parser_pseudo_destructor_name (parser, &s, &type);
4332                 /* Form the call.  */
4333                 postfix_expression 
4334                   = finish_pseudo_destructor_expr (postfix_expression,
4335                                                    s, TREE_TYPE (type));
4336               }
4337
4338             /* We no longer need to look up names in the scope of the
4339                object on the left-hand side of the `.' or `->'
4340                operator.  */
4341             parser->context->object_type = NULL_TREE;
4342             idk = CP_PARSER_ID_KIND_NONE;
4343           }
4344           break;
4345
4346         case CPP_PLUS_PLUS:
4347           /* postfix-expression ++  */
4348           /* Consume the `++' token.  */
4349           cp_lexer_consume_token (parser->lexer);
4350           /* Generate a reprsentation for the complete expression.  */
4351           postfix_expression 
4352             = finish_increment_expr (postfix_expression, 
4353                                      POSTINCREMENT_EXPR);
4354           idk = CP_PARSER_ID_KIND_NONE;
4355           break;
4356
4357         case CPP_MINUS_MINUS:
4358           /* postfix-expression -- */
4359           /* Consume the `--' token.  */
4360           cp_lexer_consume_token (parser->lexer);
4361           /* Generate a reprsentation for the complete expression.  */
4362           postfix_expression 
4363             = finish_increment_expr (postfix_expression, 
4364                                      POSTDECREMENT_EXPR);
4365           idk = CP_PARSER_ID_KIND_NONE;
4366           break;
4367
4368         default:
4369           return postfix_expression;
4370         }
4371     }
4372
4373   /* We should never get here.  */
4374   abort ();
4375   return error_mark_node;
4376 }
4377
4378 /* Parse an expression-list.
4379
4380    expression-list:
4381      assignment-expression
4382      expression-list, assignment-expression
4383
4384    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4385    representation of an assignment-expression.  Note that a TREE_LIST
4386    is returned even if there is only a single expression in the list.  */
4387
4388 static tree
4389 cp_parser_expression_list (parser)
4390      cp_parser *parser;
4391 {
4392   tree expression_list = NULL_TREE;
4393
4394   /* Consume expressions until there are no more.  */
4395   while (true)
4396     {
4397       tree expr;
4398
4399       /* Parse the next assignment-expression.  */
4400       expr = cp_parser_assignment_expression (parser);
4401       /* Add it to the list.  */
4402       expression_list = tree_cons (NULL_TREE, expr, expression_list);
4403
4404       /* If the next token isn't a `,', then we are done.  */
4405       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4406         {
4407           /* All uses of expression-list in the grammar are followed
4408              by a `)'.  Therefore, if the next token is not a `)' an
4409              error will be issued, unless we are parsing tentatively.
4410              Skip ahead to see if there is another `,' before the `)';
4411              if so, we can go there and recover.  */
4412           if (cp_parser_parsing_tentatively (parser)
4413               || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4414               || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4415             break;
4416         }
4417
4418       /* Otherwise, consume the `,' and keep going.  */
4419       cp_lexer_consume_token (parser->lexer);
4420     }
4421
4422   /* We built up the list in reverse order so we must reverse it now.  */
4423   return nreverse (expression_list);
4424 }
4425
4426 /* Parse a pseudo-destructor-name.
4427
4428    pseudo-destructor-name:
4429      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4430      :: [opt] nested-name-specifier template template-id :: ~ type-name
4431      :: [opt] nested-name-specifier [opt] ~ type-name
4432
4433    If either of the first two productions is used, sets *SCOPE to the
4434    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4435    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4436    or ERROR_MARK_NODE if no type-name is present.  */
4437
4438 static void
4439 cp_parser_pseudo_destructor_name (parser, scope, type)
4440      cp_parser *parser;
4441      tree *scope;
4442      tree *type;
4443 {
4444   bool nested_name_specifier_p;
4445
4446   /* Look for the optional `::' operator.  */
4447   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4448   /* Look for the optional nested-name-specifier.  */
4449   nested_name_specifier_p 
4450     = (cp_parser_nested_name_specifier_opt (parser,
4451                                             /*typename_keyword_p=*/false,
4452                                             /*check_dependency_p=*/true,
4453                                             /*type_p=*/false) 
4454        != NULL_TREE);
4455   /* Now, if we saw a nested-name-specifier, we might be doing the
4456      second production.  */
4457   if (nested_name_specifier_p 
4458       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4459     {
4460       /* Consume the `template' keyword.  */
4461       cp_lexer_consume_token (parser->lexer);
4462       /* Parse the template-id.  */
4463       cp_parser_template_id (parser, 
4464                              /*template_keyword_p=*/true,
4465                              /*check_dependency_p=*/false);
4466       /* Look for the `::' token.  */
4467       cp_parser_require (parser, CPP_SCOPE, "`::'");
4468     }
4469   /* If the next token is not a `~', then there might be some
4470      additional qualification. */
4471   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4472     {
4473       /* Look for the type-name.  */
4474       *scope = TREE_TYPE (cp_parser_type_name (parser));
4475       /* Look for the `::' token.  */
4476       cp_parser_require (parser, CPP_SCOPE, "`::'");
4477     }
4478   else
4479     *scope = NULL_TREE;
4480
4481   /* Look for the `~'.  */
4482   cp_parser_require (parser, CPP_COMPL, "`~'");
4483   /* Look for the type-name again.  We are not responsible for
4484      checking that it matches the first type-name.  */
4485   *type = cp_parser_type_name (parser);
4486 }
4487
4488 /* Parse a unary-expression.
4489
4490    unary-expression:
4491      postfix-expression
4492      ++ cast-expression
4493      -- cast-expression
4494      unary-operator cast-expression
4495      sizeof unary-expression
4496      sizeof ( type-id )
4497      new-expression
4498      delete-expression
4499
4500    GNU Extensions:
4501
4502    unary-expression:
4503      __extension__ cast-expression
4504      __alignof__ unary-expression
4505      __alignof__ ( type-id )
4506      __real__ cast-expression
4507      __imag__ cast-expression
4508      && identifier
4509
4510    ADDRESS_P is true iff the unary-expression is appearing as the
4511    operand of the `&' operator.
4512
4513    Returns a representation of the expresion.  */
4514
4515 static tree
4516 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4517 {
4518   cp_token *token;
4519   enum tree_code unary_operator;
4520
4521   /* Peek at the next token.  */
4522   token = cp_lexer_peek_token (parser->lexer);
4523   /* Some keywords give away the kind of expression.  */
4524   if (token->type == CPP_KEYWORD)
4525     {
4526       enum rid keyword = token->keyword;
4527
4528       switch (keyword)
4529         {
4530         case RID_ALIGNOF:
4531           {
4532             /* Consume the `alignof' token.  */
4533             cp_lexer_consume_token (parser->lexer);
4534             /* Parse the operand.  */
4535             return finish_alignof (cp_parser_sizeof_operand 
4536                                    (parser, keyword));
4537           }
4538
4539         case RID_SIZEOF:
4540           {
4541             tree operand;
4542             
4543             /* Consume the `sizeof' token.   */
4544             cp_lexer_consume_token (parser->lexer);
4545             /* Parse the operand.  */
4546             operand = cp_parser_sizeof_operand (parser, keyword);
4547
4548             /* If the type of the operand cannot be determined build a
4549                SIZEOF_EXPR.  */
4550             if (TYPE_P (operand)
4551                 ? cp_parser_dependent_type_p (operand)
4552                 : cp_parser_type_dependent_expression_p (operand))
4553               return build_min (SIZEOF_EXPR, size_type_node, operand);
4554             /* Otherwise, compute the constant value.  */
4555             else
4556               return finish_sizeof (operand);
4557           }
4558
4559         case RID_NEW:
4560           return cp_parser_new_expression (parser);
4561
4562         case RID_DELETE:
4563           return cp_parser_delete_expression (parser);
4564           
4565         case RID_EXTENSION:
4566           {
4567             /* The saved value of the PEDANTIC flag.  */
4568             int saved_pedantic;
4569             tree expr;
4570
4571             /* Save away the PEDANTIC flag.  */
4572             cp_parser_extension_opt (parser, &saved_pedantic);
4573             /* Parse the cast-expression.  */
4574             expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4575             /* Restore the PEDANTIC flag.  */
4576             pedantic = saved_pedantic;
4577
4578             return expr;
4579           }
4580
4581         case RID_REALPART:
4582         case RID_IMAGPART:
4583           {
4584             tree expression;
4585
4586             /* Consume the `__real__' or `__imag__' token.  */
4587             cp_lexer_consume_token (parser->lexer);
4588             /* Parse the cast-expression.  */
4589             expression = cp_parser_cast_expression (parser,
4590                                                     /*address_p=*/false);
4591             /* Create the complete representation.  */
4592             return build_x_unary_op ((keyword == RID_REALPART
4593                                       ? REALPART_EXPR : IMAGPART_EXPR),
4594                                      expression);
4595           }
4596           break;
4597
4598         default:
4599           break;
4600         }
4601     }
4602
4603   /* Look for the `:: new' and `:: delete', which also signal the
4604      beginning of a new-expression, or delete-expression,
4605      respectively.  If the next token is `::', then it might be one of
4606      these.  */
4607   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4608     {
4609       enum rid keyword;
4610
4611       /* See if the token after the `::' is one of the keywords in
4612          which we're interested.  */
4613       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4614       /* If it's `new', we have a new-expression.  */
4615       if (keyword == RID_NEW)
4616         return cp_parser_new_expression (parser);
4617       /* Similarly, for `delete'.  */
4618       else if (keyword == RID_DELETE)
4619         return cp_parser_delete_expression (parser);
4620     }
4621
4622   /* Look for a unary operator.  */
4623   unary_operator = cp_parser_unary_operator (token);
4624   /* The `++' and `--' operators can be handled similarly, even though
4625      they are not technically unary-operators in the grammar.  */
4626   if (unary_operator == ERROR_MARK)
4627     {
4628       if (token->type == CPP_PLUS_PLUS)
4629         unary_operator = PREINCREMENT_EXPR;
4630       else if (token->type == CPP_MINUS_MINUS)
4631         unary_operator = PREDECREMENT_EXPR;
4632       /* Handle the GNU address-of-label extension.  */
4633       else if (cp_parser_allow_gnu_extensions_p (parser)
4634                && token->type == CPP_AND_AND)
4635         {
4636           tree identifier;
4637
4638           /* Consume the '&&' token.  */
4639           cp_lexer_consume_token (parser->lexer);
4640           /* Look for the identifier.  */
4641           identifier = cp_parser_identifier (parser);
4642           /* Create an expression representing the address.  */
4643           return finish_label_address_expr (identifier);
4644         }
4645     }
4646   if (unary_operator != ERROR_MARK)
4647     {
4648       tree cast_expression;
4649
4650       /* Consume the operator token.  */
4651       token = cp_lexer_consume_token (parser->lexer);
4652       /* Parse the cast-expression.  */
4653       cast_expression 
4654         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4655       /* Now, build an appropriate representation.  */
4656       switch (unary_operator)
4657         {
4658         case INDIRECT_REF:
4659           return build_x_indirect_ref (cast_expression, "unary *");
4660           
4661         case ADDR_EXPR:
4662           return build_x_unary_op (ADDR_EXPR, cast_expression);
4663           
4664         case CONVERT_EXPR:
4665         case NEGATE_EXPR:
4666         case TRUTH_NOT_EXPR:
4667         case PREINCREMENT_EXPR:
4668         case PREDECREMENT_EXPR:
4669           return finish_unary_op_expr (unary_operator, cast_expression);
4670
4671         case BIT_NOT_EXPR:
4672           return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4673
4674         default:
4675           abort ();
4676           return error_mark_node;
4677         }
4678     }
4679
4680   return cp_parser_postfix_expression (parser, address_p);
4681 }
4682
4683 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4684    unary-operator, the corresponding tree code is returned.  */
4685
4686 static enum tree_code
4687 cp_parser_unary_operator (token)
4688      cp_token *token;
4689 {
4690   switch (token->type)
4691     {
4692     case CPP_MULT:
4693       return INDIRECT_REF;
4694
4695     case CPP_AND:
4696       return ADDR_EXPR;
4697
4698     case CPP_PLUS:
4699       return CONVERT_EXPR;
4700
4701     case CPP_MINUS:
4702       return NEGATE_EXPR;
4703
4704     case CPP_NOT:
4705       return TRUTH_NOT_EXPR;
4706       
4707     case CPP_COMPL:
4708       return BIT_NOT_EXPR;
4709
4710     default:
4711       return ERROR_MARK;
4712     }
4713 }
4714
4715 /* Parse a new-expression.
4716
4717      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4718      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4719
4720    Returns a representation of the expression.  */
4721
4722 static tree
4723 cp_parser_new_expression (parser)
4724      cp_parser *parser;
4725 {
4726   bool global_scope_p;
4727   tree placement;
4728   tree type;
4729   tree initializer;
4730
4731   /* Look for the optional `::' operator.  */
4732   global_scope_p 
4733     = (cp_parser_global_scope_opt (parser,
4734                                    /*current_scope_valid_p=*/false)
4735        != NULL_TREE);
4736   /* Look for the `new' operator.  */
4737   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4738   /* There's no easy way to tell a new-placement from the
4739      `( type-id )' construct.  */
4740   cp_parser_parse_tentatively (parser);
4741   /* Look for a new-placement.  */
4742   placement = cp_parser_new_placement (parser);
4743   /* If that didn't work out, there's no new-placement.  */
4744   if (!cp_parser_parse_definitely (parser))
4745     placement = NULL_TREE;
4746
4747   /* If the next token is a `(', then we have a parenthesized
4748      type-id.  */
4749   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4750     {
4751       /* Consume the `('.  */
4752       cp_lexer_consume_token (parser->lexer);
4753       /* Parse the type-id.  */
4754       type = cp_parser_type_id (parser);
4755       /* Look for the closing `)'.  */
4756       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4757     }
4758   /* Otherwise, there must be a new-type-id.  */
4759   else
4760     type = cp_parser_new_type_id (parser);
4761
4762   /* If the next token is a `(', then we have a new-initializer.  */
4763   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4764     initializer = cp_parser_new_initializer (parser);
4765   else
4766     initializer = NULL_TREE;
4767
4768   /* Create a representation of the new-expression.  */
4769   return build_new (placement, type, initializer, global_scope_p);
4770 }
4771
4772 /* Parse a new-placement.
4773
4774    new-placement:
4775      ( expression-list )
4776
4777    Returns the same representation as for an expression-list.  */
4778
4779 static tree
4780 cp_parser_new_placement (parser)
4781      cp_parser *parser;
4782 {
4783   tree expression_list;
4784
4785   /* Look for the opening `('.  */
4786   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4787     return error_mark_node;
4788   /* Parse the expression-list.  */
4789   expression_list = cp_parser_expression_list (parser);
4790   /* Look for the closing `)'.  */
4791   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4792
4793   return expression_list;
4794 }
4795
4796 /* Parse a new-type-id.
4797
4798    new-type-id:
4799      type-specifier-seq new-declarator [opt]
4800
4801    Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4802    and whose TREE_VALUE is the new-declarator.  */
4803
4804 static tree
4805 cp_parser_new_type_id (parser)
4806      cp_parser *parser;
4807 {
4808   tree type_specifier_seq;
4809   tree declarator;
4810   const char *saved_message;
4811
4812   /* The type-specifier sequence must not contain type definitions.
4813      (It cannot contain declarations of new types either, but if they
4814      are not definitions we will catch that because they are not
4815      complete.)  */
4816   saved_message = parser->type_definition_forbidden_message;
4817   parser->type_definition_forbidden_message
4818     = "types may not be defined in a new-type-id";
4819   /* Parse the type-specifier-seq.  */
4820   type_specifier_seq = cp_parser_type_specifier_seq (parser);
4821   /* Restore the old message.  */
4822   parser->type_definition_forbidden_message = saved_message;
4823   /* Parse the new-declarator.  */
4824   declarator = cp_parser_new_declarator_opt (parser);
4825
4826   return build_tree_list (type_specifier_seq, declarator);
4827 }
4828
4829 /* Parse an (optional) new-declarator.
4830
4831    new-declarator:
4832      ptr-operator new-declarator [opt]
4833      direct-new-declarator
4834
4835    Returns a representation of the declarator.  See
4836    cp_parser_declarator for the representations used.  */
4837
4838 static tree
4839 cp_parser_new_declarator_opt (parser)
4840      cp_parser *parser;
4841 {
4842   enum tree_code code;
4843   tree type;
4844   tree cv_qualifier_seq;
4845
4846   /* We don't know if there's a ptr-operator next, or not.  */
4847   cp_parser_parse_tentatively (parser);
4848   /* Look for a ptr-operator.  */
4849   code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4850   /* If that worked, look for more new-declarators.  */
4851   if (cp_parser_parse_definitely (parser))
4852     {
4853       tree declarator;
4854
4855       /* Parse another optional declarator.  */
4856       declarator = cp_parser_new_declarator_opt (parser);
4857
4858       /* Create the representation of the declarator.  */
4859       if (code == INDIRECT_REF)
4860         declarator = make_pointer_declarator (cv_qualifier_seq,
4861                                               declarator);
4862       else
4863         declarator = make_reference_declarator (cv_qualifier_seq,
4864                                                 declarator);
4865
4866      /* Handle the pointer-to-member case.  */
4867      if (type)
4868        declarator = build_nt (SCOPE_REF, type, declarator);
4869
4870       return declarator;
4871     }
4872
4873   /* If the next token is a `[', there is a direct-new-declarator.  */
4874   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4875     return cp_parser_direct_new_declarator (parser);
4876
4877   return NULL_TREE;
4878 }
4879
4880 /* Parse a direct-new-declarator.
4881
4882    direct-new-declarator:
4883      [ expression ]
4884      direct-new-declarator [constant-expression]  
4885
4886    Returns an ARRAY_REF, following the same conventions as are
4887    documented for cp_parser_direct_declarator.  */
4888
4889 static tree
4890 cp_parser_direct_new_declarator (parser)
4891      cp_parser *parser;
4892 {
4893   tree declarator = NULL_TREE;
4894
4895   while (true)
4896     {
4897       tree expression;
4898
4899       /* Look for the opening `['.  */
4900       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4901       /* The first expression is not required to be constant.  */
4902       if (!declarator)
4903         {
4904           expression = cp_parser_expression (parser);
4905           /* The standard requires that the expression have integral
4906              type.  DR 74 adds enumeration types.  We believe that the
4907              real intent is that these expressions be handled like the
4908              expression in a `switch' condition, which also allows
4909              classes with a single conversion to integral or
4910              enumeration type.  */
4911           if (!processing_template_decl)
4912             {
4913               expression 
4914                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4915                                               expression,
4916                                               /*complain=*/true);
4917               if (!expression)
4918                 {
4919                   error ("expression in new-declarator must have integral or enumeration type");
4920                   expression = error_mark_node;
4921                 }
4922             }
4923         }
4924       /* But all the other expressions must be.  */
4925       else
4926         expression = cp_parser_constant_expression (parser);
4927       /* Look for the closing `]'.  */
4928       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4929
4930       /* Add this bound to the declarator.  */
4931       declarator = build_nt (ARRAY_REF, declarator, expression);
4932
4933       /* If the next token is not a `[', then there are no more
4934          bounds.  */
4935       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4936         break;
4937     }
4938
4939   return declarator;
4940 }
4941
4942 /* Parse a new-initializer.
4943
4944    new-initializer:
4945      ( expression-list [opt] )
4946
4947    Returns a reprsentation of the expression-list.  If there is no
4948    expression-list, VOID_ZERO_NODE is returned.  */
4949
4950 static tree
4951 cp_parser_new_initializer (parser)
4952      cp_parser *parser;
4953 {
4954   tree expression_list;
4955
4956   /* Look for the opening parenthesis.  */
4957   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4958   /* If the next token is not a `)', then there is an
4959      expression-list.  */
4960   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4961     expression_list = cp_parser_expression_list (parser);
4962   else
4963     expression_list = void_zero_node;
4964   /* Look for the closing parenthesis.  */
4965   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4966
4967   return expression_list;
4968 }
4969
4970 /* Parse a delete-expression.
4971
4972    delete-expression:
4973      :: [opt] delete cast-expression
4974      :: [opt] delete [ ] cast-expression
4975
4976    Returns a representation of the expression.  */
4977
4978 static tree
4979 cp_parser_delete_expression (parser)
4980      cp_parser *parser;
4981 {
4982   bool global_scope_p;
4983   bool array_p;
4984   tree expression;
4985
4986   /* Look for the optional `::' operator.  */
4987   global_scope_p
4988     = (cp_parser_global_scope_opt (parser,
4989                                    /*current_scope_valid_p=*/false)
4990        != NULL_TREE);
4991   /* Look for the `delete' keyword.  */
4992   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4993   /* See if the array syntax is in use.  */
4994   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4995     {
4996       /* Consume the `[' token.  */
4997       cp_lexer_consume_token (parser->lexer);
4998       /* Look for the `]' token.  */
4999       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5000       /* Remember that this is the `[]' construct.  */
5001       array_p = true;
5002     }
5003   else
5004     array_p = false;
5005
5006   /* Parse the cast-expression.  */
5007   expression = cp_parser_cast_expression (parser, /*address_p=*/false);
5008
5009   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5010 }
5011
5012 /* Parse a cast-expression.
5013
5014    cast-expression:
5015      unary-expression
5016      ( type-id ) cast-expression
5017
5018    Returns a representation of the expression.  */
5019
5020 static tree
5021 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5022 {
5023   /* If it's a `(', then we might be looking at a cast.  */
5024   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5025     {
5026       tree type = NULL_TREE;
5027       tree expr = NULL_TREE;
5028       bool compound_literal_p;
5029       const char *saved_message;
5030
5031       /* There's no way to know yet whether or not this is a cast.
5032          For example, `(int (3))' is a unary-expression, while `(int)
5033          3' is a cast.  So, we resort to parsing tentatively.  */
5034       cp_parser_parse_tentatively (parser);
5035       /* Types may not be defined in a cast.  */
5036       saved_message = parser->type_definition_forbidden_message;
5037       parser->type_definition_forbidden_message
5038         = "types may not be defined in casts";
5039       /* Consume the `('.  */
5040       cp_lexer_consume_token (parser->lexer);
5041       /* A very tricky bit is that `(struct S) { 3 }' is a
5042          compound-literal (which we permit in C++ as an extension).
5043          But, that construct is not a cast-expression -- it is a
5044          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5045          is legal; if the compound-literal were a cast-expression,
5046          you'd need an extra set of parentheses.)  But, if we parse
5047          the type-id, and it happens to be a class-specifier, then we
5048          will commit to the parse at that point, because we cannot
5049          undo the action that is done when creating a new class.  So,
5050          then we cannot back up and do a postfix-expression.  
5051
5052          Therefore, we scan ahead to the closing `)', and check to see
5053          if the token after the `)' is a `{'.  If so, we are not
5054          looking at a cast-expression.  
5055
5056          Save tokens so that we can put them back.  */
5057       cp_lexer_save_tokens (parser->lexer);
5058       /* Skip tokens until the next token is a closing parenthesis.
5059          If we find the closing `)', and the next token is a `{', then
5060          we are looking at a compound-literal.  */
5061       compound_literal_p 
5062         = (cp_parser_skip_to_closing_parenthesis (parser)
5063            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5064       /* Roll back the tokens we skipped.  */
5065       cp_lexer_rollback_tokens (parser->lexer);
5066       /* If we were looking at a compound-literal, simulate an error
5067          so that the call to cp_parser_parse_definitely below will
5068          fail.  */
5069       if (compound_literal_p)
5070         cp_parser_simulate_error (parser);
5071       else
5072         {
5073           /* Look for the type-id.  */
5074           type = cp_parser_type_id (parser);
5075           /* Look for the closing `)'.  */
5076           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5077         }
5078
5079       /* Restore the saved message.  */
5080       parser->type_definition_forbidden_message = saved_message;
5081
5082       /* If all went well, this is a cast.  */
5083       if (cp_parser_parse_definitely (parser))
5084         {
5085           /* Parse the dependent expression.  */
5086           expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5087           /* Warn about old-style casts, if so requested.  */
5088           if (warn_old_style_cast 
5089               && !in_system_header 
5090               && !VOID_TYPE_P (type) 
5091               && current_lang_name != lang_name_c)
5092             warning ("use of old-style cast");
5093           /* Perform the cast.  */
5094           expr = build_c_cast (type, expr);
5095         }
5096
5097       if (expr)
5098         return expr;
5099     }
5100
5101   /* If we get here, then it's not a cast, so it must be a
5102      unary-expression.  */
5103   return cp_parser_unary_expression (parser, address_p);
5104 }
5105
5106 /* Parse a pm-expression.
5107
5108    pm-expression:
5109      cast-expression
5110      pm-expression .* cast-expression
5111      pm-expression ->* cast-expression
5112
5113      Returns a representation of the expression.  */
5114
5115 static tree
5116 cp_parser_pm_expression (parser)
5117      cp_parser *parser;
5118 {
5119   tree cast_expr;
5120   tree pm_expr;
5121
5122   /* Parse the cast-expresion.  */
5123   cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5124   pm_expr = cast_expr;
5125   /* Now look for pointer-to-member operators.  */
5126   while (true)
5127     {
5128       cp_token *token;
5129       enum cpp_ttype token_type;
5130
5131       /* Peek at the next token.  */
5132       token = cp_lexer_peek_token (parser->lexer);
5133       token_type = token->type;
5134       /* If it's not `.*' or `->*' there's no pointer-to-member
5135          operation.  */
5136       if (token_type != CPP_DOT_STAR 
5137           && token_type != CPP_DEREF_STAR)
5138         break;
5139
5140       /* Consume the token.  */
5141       cp_lexer_consume_token (parser->lexer);
5142
5143       /* Parse another cast-expression.  */
5144       cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5145
5146       /* Build the representation of the pointer-to-member 
5147          operation.  */
5148       if (token_type == CPP_DEREF_STAR)
5149         pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5150       else
5151         pm_expr = build_m_component_ref (pm_expr, cast_expr);
5152     }
5153
5154   return pm_expr;
5155 }
5156
5157 /* Parse a multiplicative-expression.
5158
5159    mulitplicative-expression:
5160      pm-expression
5161      multiplicative-expression * pm-expression
5162      multiplicative-expression / pm-expression
5163      multiplicative-expression % pm-expression
5164
5165    Returns a representation of the expression.  */
5166
5167 static tree
5168 cp_parser_multiplicative_expression (parser)
5169      cp_parser *parser;
5170 {
5171   static cp_parser_token_tree_map map = {
5172     { CPP_MULT, MULT_EXPR },
5173     { CPP_DIV, TRUNC_DIV_EXPR },
5174     { CPP_MOD, TRUNC_MOD_EXPR },
5175     { CPP_EOF, ERROR_MARK }
5176   };
5177
5178   return cp_parser_binary_expression (parser,
5179                                       map,
5180                                       cp_parser_pm_expression);
5181 }
5182
5183 /* Parse an additive-expression.
5184
5185    additive-expression:
5186      multiplicative-expression
5187      additive-expression + multiplicative-expression
5188      additive-expression - multiplicative-expression
5189
5190    Returns a representation of the expression.  */
5191
5192 static tree
5193 cp_parser_additive_expression (parser)
5194      cp_parser *parser;
5195 {
5196   static cp_parser_token_tree_map map = {
5197     { CPP_PLUS, PLUS_EXPR },
5198     { CPP_MINUS, MINUS_EXPR },
5199     { CPP_EOF, ERROR_MARK }
5200   };
5201
5202   return cp_parser_binary_expression (parser,
5203                                       map,
5204                                       cp_parser_multiplicative_expression);
5205 }
5206
5207 /* Parse a shift-expression.
5208
5209    shift-expression:
5210      additive-expression
5211      shift-expression << additive-expression
5212      shift-expression >> additive-expression
5213
5214    Returns a representation of the expression.  */
5215
5216 static tree
5217 cp_parser_shift_expression (parser)
5218      cp_parser *parser;
5219 {
5220   static cp_parser_token_tree_map map = {
5221     { CPP_LSHIFT, LSHIFT_EXPR },
5222     { CPP_RSHIFT, RSHIFT_EXPR },
5223     { CPP_EOF, ERROR_MARK }
5224   };
5225
5226   return cp_parser_binary_expression (parser,
5227                                       map,
5228                                       cp_parser_additive_expression);
5229 }
5230
5231 /* Parse a relational-expression.
5232
5233    relational-expression:
5234      shift-expression
5235      relational-expression < shift-expression
5236      relational-expression > shift-expression
5237      relational-expression <= shift-expression
5238      relational-expression >= shift-expression
5239
5240    GNU Extension:
5241
5242    relational-expression:
5243      relational-expression <? shift-expression
5244      relational-expression >? shift-expression
5245
5246    Returns a representation of the expression.  */
5247
5248 static tree
5249 cp_parser_relational_expression (parser)
5250      cp_parser *parser;
5251 {
5252   static cp_parser_token_tree_map map = {
5253     { CPP_LESS, LT_EXPR },
5254     { CPP_GREATER, GT_EXPR },
5255     { CPP_LESS_EQ, LE_EXPR },
5256     { CPP_GREATER_EQ, GE_EXPR },
5257     { CPP_MIN, MIN_EXPR },
5258     { CPP_MAX, MAX_EXPR },
5259     { CPP_EOF, ERROR_MARK }
5260   };
5261
5262   return cp_parser_binary_expression (parser,
5263                                       map,
5264                                       cp_parser_shift_expression);
5265 }
5266
5267 /* Parse an equality-expression.
5268
5269    equality-expression:
5270      relational-expression
5271      equality-expression == relational-expression
5272      equality-expression != relational-expression
5273
5274    Returns a representation of the expression.  */
5275
5276 static tree
5277 cp_parser_equality_expression (parser)
5278      cp_parser *parser;
5279 {
5280   static cp_parser_token_tree_map map = {
5281     { CPP_EQ_EQ, EQ_EXPR },
5282     { CPP_NOT_EQ, NE_EXPR },
5283     { CPP_EOF, ERROR_MARK }
5284   };
5285
5286   return cp_parser_binary_expression (parser,
5287                                       map,
5288                                       cp_parser_relational_expression);
5289 }
5290
5291 /* Parse an and-expression.
5292
5293    and-expression:
5294      equality-expression
5295      and-expression & equality-expression
5296
5297    Returns a representation of the expression.  */
5298
5299 static tree
5300 cp_parser_and_expression (parser)
5301      cp_parser *parser;
5302 {
5303   static cp_parser_token_tree_map map = {
5304     { CPP_AND, BIT_AND_EXPR },
5305     { CPP_EOF, ERROR_MARK }
5306   };
5307
5308   return cp_parser_binary_expression (parser,
5309                                       map,
5310                                       cp_parser_equality_expression);
5311 }
5312
5313 /* Parse an exclusive-or-expression.
5314
5315    exclusive-or-expression:
5316      and-expression
5317      exclusive-or-expression ^ and-expression
5318
5319    Returns a representation of the expression.  */
5320
5321 static tree
5322 cp_parser_exclusive_or_expression (parser)
5323      cp_parser *parser;
5324 {
5325   static cp_parser_token_tree_map map = {
5326     { CPP_XOR, BIT_XOR_EXPR },
5327     { CPP_EOF, ERROR_MARK }
5328   };
5329
5330   return cp_parser_binary_expression (parser,
5331                                       map,
5332                                       cp_parser_and_expression);
5333 }
5334
5335
5336 /* Parse an inclusive-or-expression.
5337
5338    inclusive-or-expression:
5339      exclusive-or-expression
5340      inclusive-or-expression | exclusive-or-expression
5341
5342    Returns a representation of the expression.  */
5343
5344 static tree
5345 cp_parser_inclusive_or_expression (parser)
5346      cp_parser *parser;
5347 {
5348   static cp_parser_token_tree_map map = {
5349     { CPP_OR, BIT_IOR_EXPR },
5350     { CPP_EOF, ERROR_MARK }
5351   };
5352
5353   return cp_parser_binary_expression (parser,
5354                                       map,
5355                                       cp_parser_exclusive_or_expression);
5356 }
5357
5358 /* Parse a logical-and-expression.
5359
5360    logical-and-expression:
5361      inclusive-or-expression
5362      logical-and-expression && inclusive-or-expression
5363
5364    Returns a representation of the expression.  */
5365
5366 static tree
5367 cp_parser_logical_and_expression (parser)
5368      cp_parser *parser;
5369 {
5370   static cp_parser_token_tree_map map = {
5371     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5372     { CPP_EOF, ERROR_MARK }
5373   };
5374
5375   return cp_parser_binary_expression (parser,
5376                                       map,
5377                                       cp_parser_inclusive_or_expression);
5378 }
5379
5380 /* Parse a logical-or-expression.
5381
5382    logical-or-expression:
5383      logical-and-expresion
5384      logical-or-expression || logical-and-expression
5385
5386    Returns a representation of the expression.  */
5387
5388 static tree
5389 cp_parser_logical_or_expression (parser)
5390      cp_parser *parser;
5391 {
5392   static cp_parser_token_tree_map map = {
5393     { CPP_OR_OR, TRUTH_ORIF_EXPR },
5394     { CPP_EOF, ERROR_MARK }
5395   };
5396
5397   return cp_parser_binary_expression (parser,
5398                                       map,
5399                                       cp_parser_logical_and_expression);
5400 }
5401
5402 /* Parse a conditional-expression.
5403
5404    conditional-expression:
5405      logical-or-expression
5406      logical-or-expression ? expression : assignment-expression
5407      
5408    GNU Extensions:
5409    
5410    conditional-expression:
5411      logical-or-expression ?  : assignment-expression
5412
5413    Returns a representation of the expression.  */
5414
5415 static tree
5416 cp_parser_conditional_expression (parser)
5417      cp_parser *parser;
5418 {
5419   tree logical_or_expr;
5420
5421   /* Parse the logical-or-expression.  */
5422   logical_or_expr = cp_parser_logical_or_expression (parser);
5423   /* If the next token is a `?', then we have a real conditional
5424      expression.  */
5425   if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5426     return cp_parser_question_colon_clause (parser, logical_or_expr);
5427   /* Otherwise, the value is simply the logical-or-expression.  */
5428   else
5429     return logical_or_expr;
5430 }
5431
5432 /* Parse the `? expression : assignment-expression' part of a
5433    conditional-expression.  The LOGICAL_OR_EXPR is the
5434    logical-or-expression that started the conditional-expression.
5435    Returns a representation of the entire conditional-expression.
5436
5437    This routine exists only so that it can be shared between
5438    cp_parser_conditional_expression and
5439    cp_parser_assignment_expression.
5440
5441      ? expression : assignment-expression
5442    
5443    GNU Extensions:
5444    
5445      ? : assignment-expression */
5446
5447 static tree
5448 cp_parser_question_colon_clause (parser, logical_or_expr)
5449      cp_parser *parser;
5450      tree logical_or_expr;
5451 {
5452   tree expr;
5453   tree assignment_expr;
5454
5455   /* Consume the `?' token.  */
5456   cp_lexer_consume_token (parser->lexer);
5457   if (cp_parser_allow_gnu_extensions_p (parser)
5458       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5459     /* Implicit true clause.  */
5460     expr = NULL_TREE;
5461   else
5462     /* Parse the expression.  */
5463     expr = cp_parser_expression (parser);
5464   
5465   /* The next token should be a `:'.  */
5466   cp_parser_require (parser, CPP_COLON, "`:'");
5467   /* Parse the assignment-expression.  */
5468   assignment_expr = cp_parser_assignment_expression (parser);
5469
5470   /* Build the conditional-expression.  */
5471   return build_x_conditional_expr (logical_or_expr,
5472                                    expr,
5473                                    assignment_expr);
5474 }
5475
5476 /* Parse an assignment-expression.
5477
5478    assignment-expression:
5479      conditional-expression
5480      logical-or-expression assignment-operator assignment_expression
5481      throw-expression
5482
5483    Returns a representation for the expression.  */
5484
5485 static tree
5486 cp_parser_assignment_expression (parser)
5487      cp_parser *parser;
5488 {
5489   tree expr;
5490
5491   /* If the next token is the `throw' keyword, then we're looking at
5492      a throw-expression.  */
5493   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5494     expr = cp_parser_throw_expression (parser);
5495   /* Otherwise, it must be that we are looking at a
5496      logical-or-expression.  */
5497   else
5498     {
5499       /* Parse the logical-or-expression.  */
5500       expr = cp_parser_logical_or_expression (parser);
5501       /* If the next token is a `?' then we're actually looking at a
5502          conditional-expression.  */
5503       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5504         return cp_parser_question_colon_clause (parser, expr);
5505       else 
5506         {
5507           enum tree_code assignment_operator;
5508
5509           /* If it's an assignment-operator, we're using the second
5510              production.  */
5511           assignment_operator 
5512             = cp_parser_assignment_operator_opt (parser);
5513           if (assignment_operator != ERROR_MARK)
5514             {
5515               tree rhs;
5516
5517               /* Parse the right-hand side of the assignment.  */
5518               rhs = cp_parser_assignment_expression (parser);
5519               /* Build the asignment expression.  */
5520               expr = build_x_modify_expr (expr, 
5521                                           assignment_operator, 
5522                                           rhs);
5523             }
5524         }
5525     }
5526
5527   return expr;
5528 }
5529
5530 /* Parse an (optional) assignment-operator.
5531
5532    assignment-operator: one of 
5533      = *= /= %= += -= >>= <<= &= ^= |=  
5534
5535    GNU Extension:
5536    
5537    assignment-operator: one of
5538      <?= >?=
5539
5540    If the next token is an assignment operator, the corresponding tree
5541    code is returned, and the token is consumed.  For example, for
5542    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5543    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5544    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5545    operator, ERROR_MARK is returned.  */
5546
5547 static enum tree_code
5548 cp_parser_assignment_operator_opt (parser)
5549      cp_parser *parser;
5550 {
5551   enum tree_code op;
5552   cp_token *token;
5553
5554   /* Peek at the next toen.  */
5555   token = cp_lexer_peek_token (parser->lexer);
5556
5557   switch (token->type)
5558     {
5559     case CPP_EQ:
5560       op = NOP_EXPR;
5561       break;
5562
5563     case CPP_MULT_EQ:
5564       op = MULT_EXPR;
5565       break;
5566
5567     case CPP_DIV_EQ:
5568       op = TRUNC_DIV_EXPR;
5569       break;
5570
5571     case CPP_MOD_EQ:
5572       op = TRUNC_MOD_EXPR;
5573       break;
5574
5575     case CPP_PLUS_EQ:
5576       op = PLUS_EXPR;
5577       break;
5578
5579     case CPP_MINUS_EQ:
5580       op = MINUS_EXPR;
5581       break;
5582
5583     case CPP_RSHIFT_EQ:
5584       op = RSHIFT_EXPR;
5585       break;
5586
5587     case CPP_LSHIFT_EQ:
5588       op = LSHIFT_EXPR;
5589       break;
5590
5591     case CPP_AND_EQ:
5592       op = BIT_AND_EXPR;
5593       break;
5594
5595     case CPP_XOR_EQ:
5596       op = BIT_XOR_EXPR;
5597       break;
5598
5599     case CPP_OR_EQ:
5600       op = BIT_IOR_EXPR;
5601       break;
5602
5603     case CPP_MIN_EQ:
5604       op = MIN_EXPR;
5605       break;
5606
5607     case CPP_MAX_EQ:
5608       op = MAX_EXPR;
5609       break;
5610
5611     default: 
5612       /* Nothing else is an assignment operator.  */
5613       op = ERROR_MARK;
5614     }
5615
5616   /* If it was an assignment operator, consume it.  */
5617   if (op != ERROR_MARK)
5618     cp_lexer_consume_token (parser->lexer);
5619
5620   return op;
5621 }
5622
5623 /* Parse an expression.
5624
5625    expression:
5626      assignment-expression
5627      expression , assignment-expression
5628
5629    Returns a representation of the expression.  */
5630
5631 static tree
5632 cp_parser_expression (parser)
5633      cp_parser *parser;
5634 {
5635   tree expression = NULL_TREE;
5636   bool saw_comma_p = false;
5637
5638   while (true)
5639     {
5640       tree assignment_expression;
5641
5642       /* Parse the next assignment-expression.  */
5643       assignment_expression 
5644         = cp_parser_assignment_expression (parser);
5645       /* If this is the first assignment-expression, we can just
5646          save it away.  */
5647       if (!expression)
5648         expression = assignment_expression;
5649       /* Otherwise, chain the expressions together.  It is unclear why
5650          we do not simply build COMPOUND_EXPRs as we go.  */
5651       else
5652         expression = tree_cons (NULL_TREE, 
5653                                 assignment_expression,
5654                                 expression);
5655       /* If the next token is not a comma, then we are done with the
5656          expression.  */
5657       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5658         break;
5659       /* Consume the `,'.  */
5660       cp_lexer_consume_token (parser->lexer);
5661       /* The first time we see a `,', we must take special action
5662          because the representation used for a single expression is
5663          different from that used for a list containing the single
5664          expression.  */
5665       if (!saw_comma_p)
5666         {
5667           /* Remember that this expression has a `,' in it.  */
5668           saw_comma_p = true;
5669           /* Turn the EXPRESSION into a TREE_LIST so that we can link
5670              additional expressions to it.  */
5671           expression = build_tree_list (NULL_TREE, expression);
5672         }
5673     }
5674
5675   /* Build a COMPOUND_EXPR to represent the entire expression, if
5676      necessary.  We built up the list in reverse order, so we must
5677      straighten it out here.  */
5678   if (saw_comma_p)
5679     expression = build_x_compound_expr (nreverse (expression));
5680
5681   return expression;
5682 }
5683
5684 /* Parse a constant-expression. 
5685
5686    constant-expression:
5687      conditional-expression  */
5688
5689 static tree
5690 cp_parser_constant_expression (parser)
5691      cp_parser *parser;
5692 {
5693   bool saved_constant_expression_p;
5694   tree expression;
5695
5696   /* It might seem that we could simply parse the
5697      conditional-expression, and then check to see if it were
5698      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5699      one that the compiler can figure out is constant, possibly after
5700      doing some simplifications or optimizations.  The standard has a
5701      precise definition of constant-expression, and we must honor
5702      that, even though it is somewhat more restrictive.
5703
5704      For example:
5705
5706        int i[(2, 3)];
5707
5708      is not a legal declaration, because `(2, 3)' is not a
5709      constant-expression.  The `,' operator is forbidden in a
5710      constant-expression.  However, GCC's constant-folding machinery
5711      will fold this operation to an INTEGER_CST for `3'.  */
5712
5713   /* Save the old setting of CONSTANT_EXPRESSION_P.  */
5714   saved_constant_expression_p = parser->constant_expression_p;
5715   /* We are now parsing a constant-expression.  */
5716   parser->constant_expression_p = true;
5717   /* Parse the conditional-expression.  */
5718   expression = cp_parser_conditional_expression (parser);
5719   /* Restore the old setting of CONSTANT_EXPRESSION_P.  */
5720   parser->constant_expression_p = saved_constant_expression_p;
5721
5722   return expression;
5723 }
5724
5725 /* Statements [gram.stmt.stmt]  */
5726
5727 /* Parse a statement.  
5728
5729    statement:
5730      labeled-statement
5731      expression-statement
5732      compound-statement
5733      selection-statement
5734      iteration-statement
5735      jump-statement
5736      declaration-statement
5737      try-block  */
5738
5739 static void
5740 cp_parser_statement (parser)
5741      cp_parser *parser;
5742 {
5743   tree statement;
5744   cp_token *token;
5745   int statement_line_number;
5746
5747   /* There is no statement yet.  */
5748   statement = NULL_TREE;
5749   /* Peek at the next token.  */
5750   token = cp_lexer_peek_token (parser->lexer);
5751   /* Remember the line number of the first token in the statement.  */
5752   statement_line_number = token->line_number;
5753   /* If this is a keyword, then that will often determine what kind of
5754      statement we have.  */
5755   if (token->type == CPP_KEYWORD)
5756     {
5757       enum rid keyword = token->keyword;
5758
5759       switch (keyword)
5760         {
5761         case RID_CASE:
5762         case RID_DEFAULT:
5763           statement = cp_parser_labeled_statement (parser);
5764           break;
5765
5766         case RID_IF:
5767         case RID_SWITCH:
5768           statement = cp_parser_selection_statement (parser);
5769           break;
5770
5771         case RID_WHILE:
5772         case RID_DO:
5773         case RID_FOR:
5774           statement = cp_parser_iteration_statement (parser);
5775           break;
5776
5777         case RID_BREAK:
5778         case RID_CONTINUE:
5779         case RID_RETURN:
5780         case RID_GOTO:
5781           statement = cp_parser_jump_statement (parser);
5782           break;
5783
5784         case RID_TRY:
5785           statement = cp_parser_try_block (parser);
5786           break;
5787
5788         default:
5789           /* It might be a keyword like `int' that can start a
5790              declaration-statement.  */
5791           break;
5792         }
5793     }
5794   else if (token->type == CPP_NAME)
5795     {
5796       /* If the next token is a `:', then we are looking at a
5797          labeled-statement.  */
5798       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5799       if (token->type == CPP_COLON)
5800         statement = cp_parser_labeled_statement (parser);
5801     }
5802   /* Anything that starts with a `{' must be a compound-statement.  */
5803   else if (token->type == CPP_OPEN_BRACE)
5804     statement = cp_parser_compound_statement (parser);
5805
5806   /* Everything else must be a declaration-statement or an
5807      expression-statement.  Try for the declaration-statement 
5808      first, unless we are looking at a `;', in which case we know that
5809      we have an expression-statement.  */
5810   if (!statement)
5811     {
5812       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5813         {
5814           cp_parser_parse_tentatively (parser);
5815           /* Try to parse the declaration-statement.  */
5816           cp_parser_declaration_statement (parser);
5817           /* If that worked, we're done.  */
5818           if (cp_parser_parse_definitely (parser))
5819             return;
5820         }
5821       /* Look for an expression-statement instead.  */
5822       statement = cp_parser_expression_statement (parser);
5823     }
5824
5825   /* Set the line number for the statement.  */
5826   if (statement && statement_code_p (TREE_CODE (statement)))
5827     STMT_LINENO (statement) = statement_line_number;
5828 }
5829
5830 /* Parse a labeled-statement.
5831
5832    labeled-statement:
5833      identifier : statement
5834      case constant-expression : statement
5835      default : statement  
5836
5837    Returns the new CASE_LABEL, for a `case' or `default' label.  For
5838    an ordinary label, returns a LABEL_STMT.  */
5839
5840 static tree
5841 cp_parser_labeled_statement (parser)
5842      cp_parser *parser;
5843 {
5844   cp_token *token;
5845   tree statement = NULL_TREE;
5846
5847   /* The next token should be an identifier.  */
5848   token = cp_lexer_peek_token (parser->lexer);
5849   if (token->type != CPP_NAME
5850       && token->type != CPP_KEYWORD)
5851     {
5852       cp_parser_error (parser, "expected labeled-statement");
5853       return error_mark_node;
5854     }
5855
5856   switch (token->keyword)
5857     {
5858     case RID_CASE:
5859       {
5860         tree expr;
5861
5862         /* Consume the `case' token.  */
5863         cp_lexer_consume_token (parser->lexer);
5864         /* Parse the constant-expression.  */
5865         expr = cp_parser_constant_expression (parser);
5866         /* Create the label.  */
5867         statement = finish_case_label (expr, NULL_TREE);
5868       }
5869       break;
5870
5871     case RID_DEFAULT:
5872       /* Consume the `default' token.  */
5873       cp_lexer_consume_token (parser->lexer);
5874       /* Create the label.  */
5875       statement = finish_case_label (NULL_TREE, NULL_TREE);
5876       break;
5877
5878     default:
5879       /* Anything else must be an ordinary label.  */
5880       statement = finish_label_stmt (cp_parser_identifier (parser));
5881       break;
5882     }
5883
5884   /* Require the `:' token.  */
5885   cp_parser_require (parser, CPP_COLON, "`:'");
5886   /* Parse the labeled statement.  */
5887   cp_parser_statement (parser);
5888
5889   /* Return the label, in the case of a `case' or `default' label.  */
5890   return statement;
5891 }
5892
5893 /* Parse an expression-statement.
5894
5895    expression-statement:
5896      expression [opt] ;
5897
5898    Returns the new EXPR_STMT -- or NULL_TREE if the expression
5899    statement consists of nothing more than an `;'.  */
5900
5901 static tree
5902 cp_parser_expression_statement (parser)
5903      cp_parser *parser;
5904 {
5905   tree statement;
5906
5907   /* If the next token is not a `;', then there is an expression to parse.  */
5908   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5909     statement = finish_expr_stmt (cp_parser_expression (parser));
5910   /* Otherwise, we do not even bother to build an EXPR_STMT.  */
5911   else
5912     {
5913       finish_stmt ();
5914       statement = NULL_TREE;
5915     }
5916   /* Consume the final `;'.  */
5917   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5918     {
5919       /* If there is additional (erroneous) input, skip to the end of
5920          the statement.  */
5921       cp_parser_skip_to_end_of_statement (parser);
5922       /* If the next token is now a `;', consume it.  */
5923       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5924         cp_lexer_consume_token (parser->lexer);
5925     }
5926
5927   return statement;
5928 }
5929
5930 /* Parse a compound-statement.
5931
5932    compound-statement:
5933      { statement-seq [opt] }
5934      
5935    Returns a COMPOUND_STMT representing the statement.  */
5936
5937 static tree
5938 cp_parser_compound_statement (cp_parser *parser)
5939 {
5940   tree compound_stmt;
5941
5942   /* Consume the `{'.  */
5943   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5944     return error_mark_node;
5945   /* Begin the compound-statement.  */
5946   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5947   /* Parse an (optional) statement-seq.  */
5948   cp_parser_statement_seq_opt (parser);
5949   /* Finish the compound-statement.  */
5950   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5951   /* Consume the `}'.  */
5952   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5953
5954   return compound_stmt;
5955 }
5956
5957 /* Parse an (optional) statement-seq.
5958
5959    statement-seq:
5960      statement
5961      statement-seq [opt] statement  */
5962
5963 static void
5964 cp_parser_statement_seq_opt (parser)
5965      cp_parser *parser;
5966 {
5967   /* Scan statements until there aren't any more.  */
5968   while (true)
5969     {
5970       /* If we're looking at a `}', then we've run out of statements.  */
5971       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5972           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5973         break;
5974
5975       /* Parse the statement.  */
5976       cp_parser_statement (parser);
5977     }
5978 }
5979
5980 /* Parse a selection-statement.
5981
5982    selection-statement:
5983      if ( condition ) statement
5984      if ( condition ) statement else statement
5985      switch ( condition ) statement  
5986
5987    Returns the new IF_STMT or SWITCH_STMT.  */
5988
5989 static tree
5990 cp_parser_selection_statement (parser)
5991      cp_parser *parser;
5992 {
5993   cp_token *token;
5994   enum rid keyword;
5995
5996   /* Peek at the next token.  */
5997   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5998
5999   /* See what kind of keyword it is.  */
6000   keyword = token->keyword;
6001   switch (keyword)
6002     {
6003     case RID_IF:
6004     case RID_SWITCH:
6005       {
6006         tree statement;
6007         tree condition;
6008
6009         /* Look for the `('.  */
6010         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6011           {
6012             cp_parser_skip_to_end_of_statement (parser);
6013             return error_mark_node;
6014           }
6015
6016         /* Begin the selection-statement.  */
6017         if (keyword == RID_IF)
6018           statement = begin_if_stmt ();
6019         else
6020           statement = begin_switch_stmt ();
6021
6022         /* Parse the condition.  */
6023         condition = cp_parser_condition (parser);
6024         /* Look for the `)'.  */
6025         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6026           cp_parser_skip_to_closing_parenthesis (parser);
6027
6028         if (keyword == RID_IF)
6029           {
6030             tree then_stmt;
6031
6032             /* Add the condition.  */
6033             finish_if_stmt_cond (condition, statement);
6034
6035             /* Parse the then-clause.  */
6036             then_stmt = cp_parser_implicitly_scoped_statement (parser);
6037             finish_then_clause (statement);
6038
6039             /* If the next token is `else', parse the else-clause.  */
6040             if (cp_lexer_next_token_is_keyword (parser->lexer,
6041                                                 RID_ELSE))
6042               {
6043                 tree else_stmt;
6044
6045                 /* Consume the `else' keyword.  */
6046                 cp_lexer_consume_token (parser->lexer);
6047                 /* Parse the else-clause.  */
6048                 else_stmt 
6049                   = cp_parser_implicitly_scoped_statement (parser);
6050                 finish_else_clause (statement);
6051               }
6052
6053             /* Now we're all done with the if-statement.  */
6054             finish_if_stmt ();
6055           }
6056         else
6057           {
6058             tree body;
6059
6060             /* Add the condition.  */
6061             finish_switch_cond (condition, statement);
6062
6063             /* Parse the body of the switch-statement.  */
6064             body = cp_parser_implicitly_scoped_statement (parser);
6065
6066             /* Now we're all done with the switch-statement.  */
6067             finish_switch_stmt (statement);
6068           }
6069
6070         return statement;
6071       }
6072       break;
6073
6074     default:
6075       cp_parser_error (parser, "expected selection-statement");
6076       return error_mark_node;
6077     }
6078 }
6079
6080 /* Parse a condition. 
6081
6082    condition:
6083      expression
6084      type-specifier-seq declarator = assignment-expression  
6085
6086    GNU Extension:
6087    
6088    condition:
6089      type-specifier-seq declarator asm-specification [opt] 
6090        attributes [opt] = assignment-expression
6091  
6092    Returns the expression that should be tested.  */
6093
6094 static tree
6095 cp_parser_condition (parser)
6096      cp_parser *parser;
6097 {
6098   tree type_specifiers;
6099   const char *saved_message;
6100
6101   /* Try the declaration first.  */
6102   cp_parser_parse_tentatively (parser);
6103   /* New types are not allowed in the type-specifier-seq for a
6104      condition.  */
6105   saved_message = parser->type_definition_forbidden_message;
6106   parser->type_definition_forbidden_message
6107     = "types may not be defined in conditions";
6108   /* Parse the type-specifier-seq.  */
6109   type_specifiers = cp_parser_type_specifier_seq (parser);
6110   /* Restore the saved message.  */
6111   parser->type_definition_forbidden_message = saved_message;
6112   /* If all is well, we might be looking at a declaration.  */
6113   if (!cp_parser_error_occurred (parser))
6114     {
6115       tree decl;
6116       tree asm_specification;
6117       tree attributes;
6118       tree declarator;
6119       tree initializer = NULL_TREE;
6120       
6121       /* Parse the declarator.  */
6122       declarator = cp_parser_declarator (parser, 
6123                                          /*abstract_p=*/false,
6124                                          /*ctor_dtor_or_conv_p=*/NULL);
6125       /* Parse the attributes.  */
6126       attributes = cp_parser_attributes_opt (parser);
6127       /* Parse the asm-specification.  */
6128       asm_specification = cp_parser_asm_specification_opt (parser);
6129       /* If the next token is not an `=', then we might still be
6130          looking at an expression.  For example:
6131          
6132            if (A(a).x)
6133           
6134          looks like a decl-specifier-seq and a declarator -- but then
6135          there is no `=', so this is an expression.  */
6136       cp_parser_require (parser, CPP_EQ, "`='");
6137       /* If we did see an `=', then we are looking at a declaration
6138          for sure.  */
6139       if (cp_parser_parse_definitely (parser))
6140         {
6141           /* Create the declaration.  */
6142           decl = start_decl (declarator, type_specifiers, 
6143                              /*initialized_p=*/true,
6144                              attributes, /*prefix_attributes=*/NULL_TREE);
6145           /* Parse the assignment-expression.  */
6146           initializer = cp_parser_assignment_expression (parser);
6147           
6148           /* Process the initializer.  */
6149           cp_finish_decl (decl, 
6150                           initializer, 
6151                           asm_specification, 
6152                           LOOKUP_ONLYCONVERTING);
6153           
6154           return convert_from_reference (decl);
6155         }
6156     }
6157   /* If we didn't even get past the declarator successfully, we are
6158      definitely not looking at a declaration.  */
6159   else
6160     cp_parser_abort_tentative_parse (parser);
6161
6162   /* Otherwise, we are looking at an expression.  */
6163   return cp_parser_expression (parser);
6164 }
6165
6166 /* Parse an iteration-statement.
6167
6168    iteration-statement:
6169      while ( condition ) statement
6170      do statement while ( expression ) ;
6171      for ( for-init-statement condition [opt] ; expression [opt] )
6172        statement
6173
6174    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6175
6176 static tree
6177 cp_parser_iteration_statement (parser)
6178      cp_parser *parser;
6179 {
6180   cp_token *token;
6181   enum rid keyword;
6182   tree statement;
6183
6184   /* Peek at the next token.  */
6185   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6186   if (!token)
6187     return error_mark_node;
6188
6189   /* See what kind of keyword it is.  */
6190   keyword = token->keyword;
6191   switch (keyword)
6192     {
6193     case RID_WHILE:
6194       {
6195         tree condition;
6196
6197         /* Begin the while-statement.  */
6198         statement = begin_while_stmt ();
6199         /* Look for the `('.  */
6200         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6201         /* Parse the condition.  */
6202         condition = cp_parser_condition (parser);
6203         finish_while_stmt_cond (condition, statement);
6204         /* Look for the `)'.  */
6205         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6206         /* Parse the dependent statement.  */
6207         cp_parser_already_scoped_statement (parser);
6208         /* We're done with the while-statement.  */
6209         finish_while_stmt (statement);
6210       }
6211       break;
6212
6213     case RID_DO:
6214       {
6215         tree expression;
6216
6217         /* Begin the do-statement.  */
6218         statement = begin_do_stmt ();
6219         /* Parse the body of the do-statement.  */
6220         cp_parser_implicitly_scoped_statement (parser);
6221         finish_do_body (statement);
6222         /* Look for the `while' keyword.  */
6223         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6224         /* Look for the `('.  */
6225         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6226         /* Parse the expression.  */
6227         expression = cp_parser_expression (parser);
6228         /* We're done with the do-statement.  */
6229         finish_do_stmt (expression, statement);
6230         /* Look for the `)'.  */
6231         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6232         /* Look for the `;'.  */
6233         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6234       }
6235       break;
6236
6237     case RID_FOR:
6238       {
6239         tree condition = NULL_TREE;
6240         tree expression = NULL_TREE;
6241
6242         /* Begin the for-statement.  */
6243         statement = begin_for_stmt ();
6244         /* Look for the `('.  */
6245         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6246         /* Parse the initialization.  */
6247         cp_parser_for_init_statement (parser);
6248         finish_for_init_stmt (statement);
6249
6250         /* If there's a condition, process it.  */
6251         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6252           condition = cp_parser_condition (parser);
6253         finish_for_cond (condition, statement);
6254         /* Look for the `;'.  */
6255         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6256
6257         /* If there's an expression, process it.  */
6258         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6259           expression = cp_parser_expression (parser);
6260         finish_for_expr (expression, statement);
6261         /* Look for the `)'.  */
6262         cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6263
6264         /* Parse the body of the for-statement.  */
6265         cp_parser_already_scoped_statement (parser);
6266
6267         /* We're done with the for-statement.  */
6268         finish_for_stmt (statement);
6269       }
6270       break;
6271
6272     default:
6273       cp_parser_error (parser, "expected iteration-statement");
6274       statement = error_mark_node;
6275       break;
6276     }
6277
6278   return statement;
6279 }
6280
6281 /* Parse a for-init-statement.
6282
6283    for-init-statement:
6284      expression-statement
6285      simple-declaration  */
6286
6287 static void
6288 cp_parser_for_init_statement (parser)
6289      cp_parser *parser;
6290 {
6291   /* If the next token is a `;', then we have an empty
6292      expression-statement.  Gramatically, this is also a
6293      simple-declaration, but an invalid one, because it does not
6294      declare anything.  Therefore, if we did not handle this case
6295      specially, we would issue an error message about an invalid
6296      declaration.  */
6297   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6298     {
6299       /* We're going to speculatively look for a declaration, falling back
6300          to an expression, if necessary.  */
6301       cp_parser_parse_tentatively (parser);
6302       /* Parse the declaration.  */
6303       cp_parser_simple_declaration (parser,
6304                                     /*function_definition_allowed_p=*/false);
6305       /* If the tentative parse failed, then we shall need to look for an
6306          expression-statement.  */
6307       if (cp_parser_parse_definitely (parser))
6308         return;
6309     }
6310
6311   cp_parser_expression_statement (parser);
6312 }
6313
6314 /* Parse a jump-statement.
6315
6316    jump-statement:
6317      break ;
6318      continue ;
6319      return expression [opt] ;
6320      goto identifier ;  
6321
6322    GNU extension:
6323
6324    jump-statement:
6325      goto * expression ;
6326
6327    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6328    GOTO_STMT.  */
6329
6330 static tree
6331 cp_parser_jump_statement (parser)
6332      cp_parser *parser;
6333 {
6334   tree statement = error_mark_node;
6335   cp_token *token;
6336   enum rid keyword;
6337
6338   /* Peek at the next token.  */
6339   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6340   if (!token)
6341     return error_mark_node;
6342
6343   /* See what kind of keyword it is.  */
6344   keyword = token->keyword;
6345   switch (keyword)
6346     {
6347     case RID_BREAK:
6348       statement = finish_break_stmt ();
6349       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6350       break;
6351
6352     case RID_CONTINUE:
6353       statement = finish_continue_stmt ();
6354       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6355       break;
6356
6357     case RID_RETURN:
6358       {
6359         tree expr;
6360
6361         /* If the next token is a `;', then there is no 
6362            expression.  */
6363         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6364           expr = cp_parser_expression (parser);
6365         else
6366           expr = NULL_TREE;
6367         /* Build the return-statement.  */
6368         statement = finish_return_stmt (expr);
6369         /* Look for the final `;'.  */
6370         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6371       }
6372       break;
6373
6374     case RID_GOTO:
6375       /* Create the goto-statement.  */
6376       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6377         {
6378           /* Issue a warning about this use of a GNU extension.  */
6379           if (pedantic)
6380             pedwarn ("ISO C++ forbids computed gotos");
6381           /* Consume the '*' token.  */
6382           cp_lexer_consume_token (parser->lexer);
6383           /* Parse the dependent expression.  */
6384           finish_goto_stmt (cp_parser_expression (parser));
6385         }
6386       else
6387         finish_goto_stmt (cp_parser_identifier (parser));
6388       /* Look for the final `;'.  */
6389       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6390       break;
6391
6392     default:
6393       cp_parser_error (parser, "expected jump-statement");
6394       break;
6395     }
6396
6397   return statement;
6398 }
6399
6400 /* Parse a declaration-statement.
6401
6402    declaration-statement:
6403      block-declaration  */
6404
6405 static void
6406 cp_parser_declaration_statement (parser)
6407      cp_parser *parser;
6408 {
6409   /* Parse the block-declaration.  */
6410   cp_parser_block_declaration (parser, /*statement_p=*/true);
6411
6412   /* Finish off the statement.  */
6413   finish_stmt ();
6414 }
6415
6416 /* Some dependent statements (like `if (cond) statement'), are
6417    implicitly in their own scope.  In other words, if the statement is
6418    a single statement (as opposed to a compound-statement), it is
6419    none-the-less treated as if it were enclosed in braces.  Any
6420    declarations appearing in the dependent statement are out of scope
6421    after control passes that point.  This function parses a statement,
6422    but ensures that is in its own scope, even if it is not a
6423    compound-statement.  
6424
6425    Returns the new statement.  */
6426
6427 static tree
6428 cp_parser_implicitly_scoped_statement (parser)
6429      cp_parser *parser;
6430 {
6431   tree statement;
6432
6433   /* If the token is not a `{', then we must take special action.  */
6434   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6435     {
6436       /* Create a compound-statement.  */
6437       statement = begin_compound_stmt (/*has_no_scope=*/0);
6438       /* Parse the dependent-statement.  */
6439       cp_parser_statement (parser);
6440       /* Finish the dummy compound-statement.  */
6441       finish_compound_stmt (/*has_no_scope=*/0, statement);
6442     }
6443   /* Otherwise, we simply parse the statement directly.  */
6444   else
6445     statement = cp_parser_compound_statement (parser);
6446
6447   /* Return the statement.  */
6448   return statement;
6449 }
6450
6451 /* For some dependent statements (like `while (cond) statement'), we
6452    have already created a scope.  Therefore, even if the dependent
6453    statement is a compound-statement, we do not want to create another
6454    scope.  */
6455
6456 static void
6457 cp_parser_already_scoped_statement (parser)
6458      cp_parser *parser;
6459 {
6460   /* If the token is not a `{', then we must take special action.  */
6461   if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6462     {
6463       tree statement;
6464
6465       /* Create a compound-statement.  */
6466       statement = begin_compound_stmt (/*has_no_scope=*/1);
6467       /* Parse the dependent-statement.  */
6468       cp_parser_statement (parser);
6469       /* Finish the dummy compound-statement.  */
6470       finish_compound_stmt (/*has_no_scope=*/1, statement);
6471     }
6472   /* Otherwise, we simply parse the statement directly.  */
6473   else
6474     cp_parser_statement (parser);
6475 }
6476
6477 /* Declarations [gram.dcl.dcl] */
6478
6479 /* Parse an optional declaration-sequence.
6480
6481    declaration-seq:
6482      declaration
6483      declaration-seq declaration  */
6484
6485 static void
6486 cp_parser_declaration_seq_opt (parser)
6487      cp_parser *parser;
6488 {
6489   while (true)
6490     {
6491       cp_token *token;
6492
6493       token = cp_lexer_peek_token (parser->lexer);
6494
6495       if (token->type == CPP_CLOSE_BRACE
6496           || token->type == CPP_EOF)
6497         break;
6498
6499       if (token->type == CPP_SEMICOLON) 
6500         {
6501           /* A declaration consisting of a single semicolon is
6502              invalid.  Allow it unless we're being pedantic.  */
6503           if (pedantic)
6504             pedwarn ("extra `;'");
6505           cp_lexer_consume_token (parser->lexer);
6506           continue;
6507         }
6508
6509       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6510          parser to enter or exit implict `extern "C"' blocks.  */
6511       while (pending_lang_change > 0)
6512         {
6513           push_lang_context (lang_name_c);
6514           --pending_lang_change;
6515         }
6516       while (pending_lang_change < 0)
6517         {
6518           pop_lang_context ();
6519           ++pending_lang_change;
6520         }
6521
6522       /* Parse the declaration itself.  */
6523       cp_parser_declaration (parser);
6524     }
6525 }
6526
6527 /* Parse a declaration.
6528
6529    declaration:
6530      block-declaration
6531      function-definition
6532      template-declaration
6533      explicit-instantiation
6534      explicit-specialization
6535      linkage-specification
6536      namespace-definition    
6537
6538    GNU extension:
6539
6540    declaration:
6541       __extension__ declaration */
6542
6543 static void
6544 cp_parser_declaration (parser)
6545      cp_parser *parser;
6546 {
6547   cp_token token1;
6548   cp_token token2;
6549   int saved_pedantic;
6550
6551   /* Check for the `__extension__' keyword.  */
6552   if (cp_parser_extension_opt (parser, &saved_pedantic))
6553     {
6554       /* Parse the qualified declaration.  */
6555       cp_parser_declaration (parser);
6556       /* Restore the PEDANTIC flag.  */
6557       pedantic = saved_pedantic;
6558
6559       return;
6560     }
6561
6562   /* Try to figure out what kind of declaration is present.  */
6563   token1 = *cp_lexer_peek_token (parser->lexer);
6564   if (token1.type != CPP_EOF)
6565     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6566
6567   /* If the next token is `extern' and the following token is a string
6568      literal, then we have a linkage specification.  */
6569   if (token1.keyword == RID_EXTERN
6570       && cp_parser_is_string_literal (&token2))
6571     cp_parser_linkage_specification (parser);
6572   /* If the next token is `template', then we have either a template
6573      declaration, an explicit instantiation, or an explicit
6574      specialization.  */
6575   else if (token1.keyword == RID_TEMPLATE)
6576     {
6577       /* `template <>' indicates a template specialization.  */
6578       if (token2.type == CPP_LESS
6579           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6580         cp_parser_explicit_specialization (parser);
6581       /* `template <' indicates a template declaration.  */
6582       else if (token2.type == CPP_LESS)
6583         cp_parser_template_declaration (parser, /*member_p=*/false);
6584       /* Anything else must be an explicit instantiation.  */
6585       else
6586         cp_parser_explicit_instantiation (parser);
6587     }
6588   /* If the next token is `export', then we have a template
6589      declaration.  */
6590   else if (token1.keyword == RID_EXPORT)
6591     cp_parser_template_declaration (parser, /*member_p=*/false);
6592   /* If the next token is `extern', 'static' or 'inline' and the one
6593      after that is `template', we have a GNU extended explicit
6594      instantiation directive.  */
6595   else if (cp_parser_allow_gnu_extensions_p (parser)
6596            && (token1.keyword == RID_EXTERN
6597                || token1.keyword == RID_STATIC
6598                || token1.keyword == RID_INLINE)
6599            && token2.keyword == RID_TEMPLATE)
6600     cp_parser_explicit_instantiation (parser);
6601   /* If the next token is `namespace', check for a named or unnamed
6602      namespace definition.  */
6603   else if (token1.keyword == RID_NAMESPACE
6604            && (/* A named namespace definition.  */
6605                (token2.type == CPP_NAME
6606                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
6607                     == CPP_OPEN_BRACE))
6608                /* An unnamed namespace definition.  */
6609                || token2.type == CPP_OPEN_BRACE))
6610     cp_parser_namespace_definition (parser);
6611   /* We must have either a block declaration or a function
6612      definition.  */
6613   else
6614     /* Try to parse a block-declaration, or a function-definition.  */
6615     cp_parser_block_declaration (parser, /*statement_p=*/false);
6616 }
6617
6618 /* Parse a block-declaration.  
6619
6620    block-declaration:
6621      simple-declaration
6622      asm-definition
6623      namespace-alias-definition
6624      using-declaration
6625      using-directive  
6626
6627    GNU Extension:
6628
6629    block-declaration:
6630      __extension__ block-declaration 
6631      label-declaration
6632
6633    If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6634    part of a declaration-statement.  */
6635
6636 static void
6637 cp_parser_block_declaration (cp_parser *parser, 
6638                              bool      statement_p)
6639 {
6640   cp_token *token1;
6641   int saved_pedantic;
6642
6643   /* Check for the `__extension__' keyword.  */
6644   if (cp_parser_extension_opt (parser, &saved_pedantic))
6645     {
6646       /* Parse the qualified declaration.  */
6647       cp_parser_block_declaration (parser, statement_p);
6648       /* Restore the PEDANTIC flag.  */
6649       pedantic = saved_pedantic;
6650
6651       return;
6652     }
6653
6654   /* Peek at the next token to figure out which kind of declaration is
6655      present.  */
6656   token1 = cp_lexer_peek_token (parser->lexer);
6657
6658   /* If the next keyword is `asm', we have an asm-definition.  */
6659   if (token1->keyword == RID_ASM)
6660     {
6661       if (statement_p)
6662         cp_parser_commit_to_tentative_parse (parser);
6663       cp_parser_asm_definition (parser);
6664     }
6665   /* If the next keyword is `namespace', we have a
6666      namespace-alias-definition.  */
6667   else if (token1->keyword == RID_NAMESPACE)
6668     cp_parser_namespace_alias_definition (parser);
6669   /* If the next keyword is `using', we have either a
6670      using-declaration or a using-directive.  */
6671   else if (token1->keyword == RID_USING)
6672     {
6673       cp_token *token2;
6674
6675       if (statement_p)
6676         cp_parser_commit_to_tentative_parse (parser);
6677       /* If the token after `using' is `namespace', then we have a
6678          using-directive.  */
6679       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6680       if (token2->keyword == RID_NAMESPACE)
6681         cp_parser_using_directive (parser);
6682       /* Otherwise, it's a using-declaration.  */
6683       else
6684         cp_parser_using_declaration (parser);
6685     }
6686   /* If the next keyword is `__label__' we have a label declaration.  */
6687   else if (token1->keyword == RID_LABEL)
6688     {
6689       if (statement_p)
6690         cp_parser_commit_to_tentative_parse (parser);
6691       cp_parser_label_declaration (parser);
6692     }
6693   /* Anything else must be a simple-declaration.  */
6694   else
6695     cp_parser_simple_declaration (parser, !statement_p);
6696 }
6697
6698 /* Parse a simple-declaration.
6699
6700    simple-declaration:
6701      decl-specifier-seq [opt] init-declarator-list [opt] ;  
6702
6703    init-declarator-list:
6704      init-declarator
6705      init-declarator-list , init-declarator 
6706
6707    If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6708    function-definition as a simple-declaration.   */
6709
6710 static void
6711 cp_parser_simple_declaration (parser, function_definition_allowed_p)
6712      cp_parser *parser;
6713      bool function_definition_allowed_p;
6714 {
6715   tree decl_specifiers;
6716   tree attributes;
6717   tree access_checks;
6718   bool declares_class_or_enum;
6719   bool saw_declarator;
6720
6721   /* Defer access checks until we know what is being declared; the
6722      checks for names appearing in the decl-specifier-seq should be
6723      done as if we were in the scope of the thing being declared.  */
6724   cp_parser_start_deferring_access_checks (parser);
6725   /* Parse the decl-specifier-seq.  We have to keep track of whether
6726      or not the decl-specifier-seq declares a named class or
6727      enumeration type, since that is the only case in which the
6728      init-declarator-list is allowed to be empty.  
6729
6730      [dcl.dcl]
6731
6732      In a simple-declaration, the optional init-declarator-list can be
6733      omitted only when declaring a class or enumeration, that is when
6734      the decl-specifier-seq contains either a class-specifier, an
6735      elaborated-type-specifier, or an enum-specifier.  */
6736   decl_specifiers
6737     = cp_parser_decl_specifier_seq (parser, 
6738                                     CP_PARSER_FLAGS_OPTIONAL,
6739                                     &attributes,
6740                                     &declares_class_or_enum);
6741   /* We no longer need to defer access checks.  */
6742   access_checks = cp_parser_stop_deferring_access_checks (parser);
6743
6744   /* Keep going until we hit the `;' at the end of the simple
6745      declaration.  */
6746   saw_declarator = false;
6747   while (cp_lexer_next_token_is_not (parser->lexer, 
6748                                      CPP_SEMICOLON))
6749     {
6750       cp_token *token;
6751       bool function_definition_p;
6752
6753       saw_declarator = true;
6754       /* Parse the init-declarator.  */
6755       cp_parser_init_declarator (parser, decl_specifiers, attributes,
6756                                  access_checks,
6757                                  function_definition_allowed_p,
6758                                  /*member_p=*/false,
6759                                  &function_definition_p);
6760       /* Handle function definitions specially.  */
6761       if (function_definition_p)
6762         {
6763           /* If the next token is a `,', then we are probably
6764              processing something like:
6765
6766                void f() {}, *p;
6767
6768              which is erroneous.  */
6769           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6770             error ("mixing declarations and function-definitions is forbidden");
6771           /* Otherwise, we're done with the list of declarators.  */
6772           else
6773             return;
6774         }
6775       /* The next token should be either a `,' or a `;'.  */
6776       token = cp_lexer_peek_token (parser->lexer);
6777       /* If it's a `,', there are more declarators to come.  */
6778       if (token->type == CPP_COMMA)
6779         cp_lexer_consume_token (parser->lexer);
6780       /* If it's a `;', we are done.  */
6781       else if (token->type == CPP_SEMICOLON)
6782         break;
6783       /* Anything else is an error.  */
6784       else
6785         {
6786           cp_parser_error (parser, "expected `,' or `;'");
6787           /* Skip tokens until we reach the end of the statement.  */
6788           cp_parser_skip_to_end_of_statement (parser);
6789           return;
6790         }
6791       /* After the first time around, a function-definition is not
6792          allowed -- even if it was OK at first.  For example:
6793
6794            int i, f() {}
6795
6796          is not valid.  */
6797       function_definition_allowed_p = false;
6798     }
6799
6800   /* Issue an error message if no declarators are present, and the
6801      decl-specifier-seq does not itself declare a class or
6802      enumeration.  */
6803   if (!saw_declarator)
6804     {
6805       if (cp_parser_declares_only_class_p (parser))
6806         shadow_tag (decl_specifiers);
6807       /* Perform any deferred access checks.  */
6808       cp_parser_perform_deferred_access_checks (access_checks);
6809     }
6810
6811   /* Consume the `;'.  */
6812   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6813
6814   /* Mark all the classes that appeared in the decl-specifier-seq as
6815      having received a `;'.  */
6816   note_list_got_semicolon (decl_specifiers);
6817 }
6818
6819 /* Parse a decl-specifier-seq.
6820
6821    decl-specifier-seq:
6822      decl-specifier-seq [opt] decl-specifier
6823
6824    decl-specifier:
6825      storage-class-specifier
6826      type-specifier
6827      function-specifier
6828      friend
6829      typedef  
6830
6831    GNU Extension:
6832
6833    decl-specifier-seq:
6834      decl-specifier-seq [opt] attributes
6835
6836    Returns a TREE_LIST, giving the decl-specifiers in the order they
6837    appear in the source code.  The TREE_VALUE of each node is the
6838    decl-specifier.  For a keyword (such as `auto' or `friend'), the
6839    TREE_VALUE is simply the correspoding TREE_IDENTIFIER.  For the
6840    representation of a type-specifier, see cp_parser_type_specifier.  
6841
6842    If there are attributes, they will be stored in *ATTRIBUTES,
6843    represented as described above cp_parser_attributes.  
6844
6845    If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6846    appears, and the entity that will be a friend is not going to be a
6847    class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
6848    even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6849    friendship is granted might not be a class.  */
6850
6851 static tree
6852 cp_parser_decl_specifier_seq (parser, flags, attributes,
6853                               declares_class_or_enum)
6854      cp_parser *parser;
6855      cp_parser_flags flags;
6856      tree *attributes;
6857      bool *declares_class_or_enum;
6858 {
6859   tree decl_specs = NULL_TREE;
6860   bool friend_p = false;
6861
6862   /* Assume no class or enumeration type is declared.  */
6863   *declares_class_or_enum = false;
6864
6865   /* Assume there are no attributes.  */
6866   *attributes = NULL_TREE;
6867
6868   /* Keep reading specifiers until there are no more to read.  */
6869   while (true)
6870     {
6871       tree decl_spec = NULL_TREE;
6872       bool constructor_p;
6873       cp_token *token;
6874
6875       /* Peek at the next token.  */
6876       token = cp_lexer_peek_token (parser->lexer);
6877       /* Handle attributes.  */
6878       if (token->keyword == RID_ATTRIBUTE)
6879         {
6880           /* Parse the attributes.  */
6881           decl_spec = cp_parser_attributes_opt (parser);
6882           /* Add them to the list.  */
6883           *attributes = chainon (*attributes, decl_spec);
6884           continue;
6885         }
6886       /* If the next token is an appropriate keyword, we can simply
6887          add it to the list.  */
6888       switch (token->keyword)
6889         {
6890         case RID_FRIEND:
6891           /* decl-specifier:
6892                friend  */
6893           friend_p = true;
6894           /* The representation of the specifier is simply the
6895              appropriate TREE_IDENTIFIER node.  */
6896           decl_spec = token->value;
6897           /* Consume the token.  */
6898           cp_lexer_consume_token (parser->lexer);
6899           break;
6900
6901           /* function-specifier:
6902                inline
6903                virtual
6904                explicit  */
6905         case RID_INLINE:
6906         case RID_VIRTUAL:
6907         case RID_EXPLICIT:
6908           decl_spec = cp_parser_function_specifier_opt (parser);
6909           break;
6910           
6911           /* decl-specifier:
6912                typedef  */
6913         case RID_TYPEDEF:
6914           /* The representation of the specifier is simply the
6915              appropriate TREE_IDENTIFIER node.  */
6916           decl_spec = token->value;
6917           /* Consume the token.  */
6918           cp_lexer_consume_token (parser->lexer);
6919           break;
6920
6921           /* storage-class-specifier:
6922                auto
6923                register
6924                static
6925                extern
6926                mutable  
6927
6928              GNU Extension:
6929                thread  */
6930         case RID_AUTO:
6931         case RID_REGISTER:
6932         case RID_STATIC:
6933         case RID_EXTERN:
6934         case RID_MUTABLE:
6935         case RID_THREAD:
6936           decl_spec = cp_parser_storage_class_specifier_opt (parser);
6937           break;
6938           
6939         default:
6940           break;
6941         }
6942
6943       /* Constructors are a special case.  The `S' in `S()' is not a
6944          decl-specifier; it is the beginning of the declarator.  */
6945       constructor_p = (!decl_spec 
6946                        && cp_parser_constructor_declarator_p (parser,
6947                                                               friend_p));
6948
6949       /* If we don't have a DECL_SPEC yet, then we must be looking at
6950          a type-specifier.  */
6951       if (!decl_spec && !constructor_p)
6952         {
6953           bool decl_spec_declares_class_or_enum;
6954           bool is_cv_qualifier;
6955
6956           decl_spec
6957             = cp_parser_type_specifier (parser, flags,
6958                                         friend_p,
6959                                         /*is_declaration=*/true,
6960                                         &decl_spec_declares_class_or_enum,
6961                                         &is_cv_qualifier);
6962
6963           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6964
6965           /* If this type-specifier referenced a user-defined type
6966              (a typedef, class-name, etc.), then we can't allow any
6967              more such type-specifiers henceforth.
6968
6969              [dcl.spec]
6970
6971              The longest sequence of decl-specifiers that could
6972              possibly be a type name is taken as the
6973              decl-specifier-seq of a declaration.  The sequence shall
6974              be self-consistent as described below.
6975
6976              [dcl.type]
6977
6978              As a general rule, at most one type-specifier is allowed
6979              in the complete decl-specifier-seq of a declaration.  The
6980              only exceptions are the following:
6981
6982              -- const or volatile can be combined with any other
6983                 type-specifier. 
6984
6985              -- signed or unsigned can be combined with char, long,
6986                 short, or int.
6987
6988              -- ..
6989
6990              Example:
6991
6992                typedef char* Pc;
6993                void g (const int Pc);
6994
6995              Here, Pc is *not* part of the decl-specifier seq; it's
6996              the declarator.  Therefore, once we see a type-specifier
6997              (other than a cv-qualifier), we forbid any additional
6998              user-defined types.  We *do* still allow things like `int
6999              int' to be considered a decl-specifier-seq, and issue the
7000              error message later.  */
7001           if (decl_spec && !is_cv_qualifier)
7002             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7003         }
7004
7005       /* If we still do not have a DECL_SPEC, then there are no more
7006          decl-specifiers.  */
7007       if (!decl_spec)
7008         {
7009           /* Issue an error message, unless the entire construct was
7010              optional.  */
7011           if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
7012             {
7013               cp_parser_error (parser, "expected decl specifier");
7014               return error_mark_node;
7015             }
7016
7017           break;
7018         }
7019
7020       /* Add the DECL_SPEC to the list of specifiers.  */
7021       decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
7022
7023       /* After we see one decl-specifier, further decl-specifiers are
7024          always optional.  */
7025       flags |= CP_PARSER_FLAGS_OPTIONAL;
7026     }
7027
7028   /* We have built up the DECL_SPECS in reverse order.  Return them in
7029      the correct order.  */
7030   return nreverse (decl_specs);
7031 }
7032
7033 /* Parse an (optional) storage-class-specifier. 
7034
7035    storage-class-specifier:
7036      auto
7037      register
7038      static
7039      extern
7040      mutable  
7041
7042    GNU Extension:
7043
7044    storage-class-specifier:
7045      thread
7046
7047    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7048    
7049 static tree
7050 cp_parser_storage_class_specifier_opt (parser)
7051      cp_parser *parser;
7052 {
7053   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7054     {
7055     case RID_AUTO:
7056     case RID_REGISTER:
7057     case RID_STATIC:
7058     case RID_EXTERN:
7059     case RID_MUTABLE:
7060     case RID_THREAD:
7061       /* Consume the token.  */
7062       return cp_lexer_consume_token (parser->lexer)->value;
7063
7064     default:
7065       return NULL_TREE;
7066     }
7067 }
7068
7069 /* Parse an (optional) function-specifier. 
7070
7071    function-specifier:
7072      inline
7073      virtual
7074      explicit
7075
7076    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7077    
7078 static tree
7079 cp_parser_function_specifier_opt (parser)
7080      cp_parser *parser;
7081 {
7082   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7083     {
7084     case RID_INLINE:
7085     case RID_VIRTUAL:
7086     case RID_EXPLICIT:
7087       /* Consume the token.  */
7088       return cp_lexer_consume_token (parser->lexer)->value;
7089
7090     default:
7091       return NULL_TREE;
7092     }
7093 }
7094
7095 /* Parse a linkage-specification.
7096
7097    linkage-specification:
7098      extern string-literal { declaration-seq [opt] }
7099      extern string-literal declaration  */
7100
7101 static void
7102 cp_parser_linkage_specification (parser)
7103      cp_parser *parser;
7104 {
7105   cp_token *token;
7106   tree linkage;
7107
7108   /* Look for the `extern' keyword.  */
7109   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7110
7111   /* Peek at the next token.  */
7112   token = cp_lexer_peek_token (parser->lexer);
7113   /* If it's not a string-literal, then there's a problem.  */
7114   if (!cp_parser_is_string_literal (token))
7115     {
7116       cp_parser_error (parser, "expected language-name");
7117       return;
7118     }
7119   /* Consume the token.  */
7120   cp_lexer_consume_token (parser->lexer);
7121
7122   /* Transform the literal into an identifier.  If the literal is a
7123      wide-character string, or contains embedded NULs, then we can't
7124      handle it as the user wants.  */
7125   if (token->type == CPP_WSTRING
7126       || (strlen (TREE_STRING_POINTER (token->value))
7127           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7128     {
7129       cp_parser_error (parser, "invalid linkage-specification");
7130       /* Assume C++ linkage.  */
7131       linkage = get_identifier ("c++");
7132     }
7133   /* If it's a simple string constant, things are easier.  */
7134   else
7135     linkage = get_identifier (TREE_STRING_POINTER (token->value));
7136
7137   /* We're now using the new linkage.  */
7138   push_lang_context (linkage);
7139
7140   /* If the next token is a `{', then we're using the first
7141      production.  */
7142   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7143     {
7144       /* Consume the `{' token.  */
7145       cp_lexer_consume_token (parser->lexer);
7146       /* Parse the declarations.  */
7147       cp_parser_declaration_seq_opt (parser);
7148       /* Look for the closing `}'.  */
7149       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7150     }
7151   /* Otherwise, there's just one declaration.  */
7152   else
7153     {
7154       bool saved_in_unbraced_linkage_specification_p;
7155
7156       saved_in_unbraced_linkage_specification_p 
7157         = parser->in_unbraced_linkage_specification_p;
7158       parser->in_unbraced_linkage_specification_p = true;
7159       have_extern_spec = true;
7160       cp_parser_declaration (parser);
7161       have_extern_spec = false;
7162       parser->in_unbraced_linkage_specification_p 
7163         = saved_in_unbraced_linkage_specification_p;
7164     }
7165
7166   /* We're done with the linkage-specification.  */
7167   pop_lang_context ();
7168 }
7169
7170 /* Special member functions [gram.special] */
7171
7172 /* Parse a conversion-function-id.
7173
7174    conversion-function-id:
7175      operator conversion-type-id  
7176
7177    Returns an IDENTIFIER_NODE representing the operator.  */
7178
7179 static tree 
7180 cp_parser_conversion_function_id (parser)
7181      cp_parser *parser;
7182 {
7183   tree type;
7184   tree saved_scope;
7185   tree saved_qualifying_scope;
7186   tree saved_object_scope;
7187
7188   /* Look for the `operator' token.  */
7189   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7190     return error_mark_node;
7191   /* When we parse the conversion-type-id, the current scope will be
7192      reset.  However, we need that information in able to look up the
7193      conversion function later, so we save it here.  */
7194   saved_scope = parser->scope;
7195   saved_qualifying_scope = parser->qualifying_scope;
7196   saved_object_scope = parser->object_scope;
7197   /* We must enter the scope of the class so that the names of
7198      entities declared within the class are available in the
7199      conversion-type-id.  For example, consider:
7200
7201        struct S { 
7202          typedef int I;
7203          operator I();
7204        };
7205
7206        S::operator I() { ... }
7207
7208      In order to see that `I' is a type-name in the definition, we
7209      must be in the scope of `S'.  */
7210   if (saved_scope)
7211     push_scope (saved_scope);
7212   /* Parse the conversion-type-id.  */
7213   type = cp_parser_conversion_type_id (parser);
7214   /* Leave the scope of the class, if any.  */
7215   if (saved_scope)
7216     pop_scope (saved_scope);
7217   /* Restore the saved scope.  */
7218   parser->scope = saved_scope;
7219   parser->qualifying_scope = saved_qualifying_scope;
7220   parser->object_scope = saved_object_scope;
7221   /* If the TYPE is invalid, indicate failure.  */
7222   if (type == error_mark_node)
7223     return error_mark_node;
7224   return mangle_conv_op_name_for_type (type);
7225 }
7226
7227 /* Parse a conversion-type-id:
7228
7229    conversion-type-id:
7230      type-specifier-seq conversion-declarator [opt]
7231
7232    Returns the TYPE specified.  */
7233
7234 static tree
7235 cp_parser_conversion_type_id (parser)
7236      cp_parser *parser;
7237 {
7238   tree attributes;
7239   tree type_specifiers;
7240   tree declarator;
7241
7242   /* Parse the attributes.  */
7243   attributes = cp_parser_attributes_opt (parser);
7244   /* Parse the type-specifiers.  */
7245   type_specifiers = cp_parser_type_specifier_seq (parser);
7246   /* If that didn't work, stop.  */
7247   if (type_specifiers == error_mark_node)
7248     return error_mark_node;
7249   /* Parse the conversion-declarator.  */
7250   declarator = cp_parser_conversion_declarator_opt (parser);
7251
7252   return grokdeclarator (declarator, type_specifiers, TYPENAME,
7253                          /*initialized=*/0, &attributes);
7254 }
7255
7256 /* Parse an (optional) conversion-declarator.
7257
7258    conversion-declarator:
7259      ptr-operator conversion-declarator [opt]  
7260
7261    Returns a representation of the declarator.  See
7262    cp_parser_declarator for details.  */
7263
7264 static tree
7265 cp_parser_conversion_declarator_opt (parser)
7266      cp_parser *parser;
7267 {
7268   enum tree_code code;
7269   tree class_type;
7270   tree cv_qualifier_seq;
7271
7272   /* We don't know if there's a ptr-operator next, or not.  */
7273   cp_parser_parse_tentatively (parser);
7274   /* Try the ptr-operator.  */
7275   code = cp_parser_ptr_operator (parser, &class_type, 
7276                                  &cv_qualifier_seq);
7277   /* If it worked, look for more conversion-declarators.  */
7278   if (cp_parser_parse_definitely (parser))
7279     {
7280      tree declarator;
7281
7282      /* Parse another optional declarator.  */
7283      declarator = cp_parser_conversion_declarator_opt (parser);
7284
7285      /* Create the representation of the declarator.  */
7286      if (code == INDIRECT_REF)
7287        declarator = make_pointer_declarator (cv_qualifier_seq,
7288                                              declarator);
7289      else
7290        declarator =  make_reference_declarator (cv_qualifier_seq,
7291                                                 declarator);
7292
7293      /* Handle the pointer-to-member case.  */
7294      if (class_type)
7295        declarator = build_nt (SCOPE_REF, class_type, declarator);
7296
7297      return declarator;
7298    }
7299
7300   return NULL_TREE;
7301 }
7302
7303 /* Parse an (optional) ctor-initializer.
7304
7305    ctor-initializer:
7306      : mem-initializer-list  
7307
7308    Returns TRUE iff the ctor-initializer was actually present.  */
7309
7310 static bool
7311 cp_parser_ctor_initializer_opt (parser)
7312      cp_parser *parser;
7313 {
7314   /* If the next token is not a `:', then there is no
7315      ctor-initializer.  */
7316   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7317     {
7318       /* Do default initialization of any bases and members.  */
7319       if (DECL_CONSTRUCTOR_P (current_function_decl))
7320         finish_mem_initializers (NULL_TREE);
7321
7322       return false;
7323     }
7324
7325   /* Consume the `:' token.  */
7326   cp_lexer_consume_token (parser->lexer);
7327   /* And the mem-initializer-list.  */
7328   cp_parser_mem_initializer_list (parser);
7329
7330   return true;
7331 }
7332
7333 /* Parse a mem-initializer-list.
7334
7335    mem-initializer-list:
7336      mem-initializer
7337      mem-initializer , mem-initializer-list  */
7338
7339 static void
7340 cp_parser_mem_initializer_list (parser)
7341      cp_parser *parser;
7342 {
7343   tree mem_initializer_list = NULL_TREE;
7344
7345   /* Let the semantic analysis code know that we are starting the
7346      mem-initializer-list.  */
7347   begin_mem_initializers ();
7348
7349   /* Loop through the list.  */
7350   while (true)
7351     {
7352       tree mem_initializer;
7353
7354       /* Parse the mem-initializer.  */
7355       mem_initializer = cp_parser_mem_initializer (parser);
7356       /* Add it to the list, unless it was erroneous.  */
7357       if (mem_initializer)
7358         {
7359           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7360           mem_initializer_list = mem_initializer;
7361         }
7362       /* If the next token is not a `,', we're done.  */
7363       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7364         break;
7365       /* Consume the `,' token.  */
7366       cp_lexer_consume_token (parser->lexer);
7367     }
7368
7369   /* Perform semantic analysis.  */
7370   finish_mem_initializers (mem_initializer_list);
7371 }
7372
7373 /* Parse a mem-initializer.
7374
7375    mem-initializer:
7376      mem-initializer-id ( expression-list [opt] )  
7377
7378    GNU extension:
7379   
7380    mem-initializer:
7381      ( expresion-list [opt] )
7382
7383    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7384    class) or FIELD_DECL (for a non-static data member) to initialize;
7385    the TREE_VALUE is the expression-list.  */
7386
7387 static tree
7388 cp_parser_mem_initializer (parser)
7389      cp_parser *parser;
7390 {
7391   tree mem_initializer_id;
7392   tree expression_list;
7393
7394   /* Find out what is being initialized.  */
7395   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7396     {
7397       pedwarn ("anachronistic old-style base class initializer");
7398       mem_initializer_id = NULL_TREE;
7399     }
7400   else
7401     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7402   /* Look for the opening `('.  */
7403   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7404   /* Parse the expression-list.  */
7405   if (cp_lexer_next_token_is_not (parser->lexer,
7406                                   CPP_CLOSE_PAREN))
7407     expression_list = cp_parser_expression_list (parser);
7408   else
7409     expression_list = void_type_node;
7410   /* Look for the closing `)'.  */
7411   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7412
7413   return expand_member_init (mem_initializer_id,
7414                              expression_list);
7415 }
7416
7417 /* Parse a mem-initializer-id.
7418
7419    mem-initializer-id:
7420      :: [opt] nested-name-specifier [opt] class-name
7421      identifier  
7422
7423    Returns a TYPE indicating the class to be initializer for the first
7424    production.  Returns an IDENTIFIER_NODE indicating the data member
7425    to be initialized for the second production.  */
7426
7427 static tree
7428 cp_parser_mem_initializer_id (parser)
7429      cp_parser *parser;
7430 {
7431   bool global_scope_p;
7432   bool nested_name_specifier_p;
7433   tree id;
7434
7435   /* Look for the optional `::' operator.  */
7436   global_scope_p 
7437     = (cp_parser_global_scope_opt (parser, 
7438                                    /*current_scope_valid_p=*/false) 
7439        != NULL_TREE);
7440   /* Look for the optional nested-name-specifier.  The simplest way to
7441      implement:
7442
7443        [temp.res]
7444
7445        The keyword `typename' is not permitted in a base-specifier or
7446        mem-initializer; in these contexts a qualified name that
7447        depends on a template-parameter is implicitly assumed to be a
7448        type name.
7449
7450      is to assume that we have seen the `typename' keyword at this
7451      point.  */
7452   nested_name_specifier_p 
7453     = (cp_parser_nested_name_specifier_opt (parser,
7454                                             /*typename_keyword_p=*/true,
7455                                             /*check_dependency_p=*/true,
7456                                             /*type_p=*/true)
7457        != NULL_TREE);
7458   /* If there is a `::' operator or a nested-name-specifier, then we
7459      are definitely looking for a class-name.  */
7460   if (global_scope_p || nested_name_specifier_p)
7461     return cp_parser_class_name (parser,
7462                                  /*typename_keyword_p=*/true,
7463                                  /*template_keyword_p=*/false,
7464                                  /*type_p=*/false,
7465                                  /*check_access_p=*/true,
7466                                  /*check_dependency_p=*/true,
7467                                  /*class_head_p=*/false);
7468   /* Otherwise, we could also be looking for an ordinary identifier.  */
7469   cp_parser_parse_tentatively (parser);
7470   /* Try a class-name.  */
7471   id = cp_parser_class_name (parser, 
7472                              /*typename_keyword_p=*/true,
7473                              /*template_keyword_p=*/false,
7474                              /*type_p=*/false,
7475                              /*check_access_p=*/true,
7476                              /*check_dependency_p=*/true,
7477                              /*class_head_p=*/false);
7478   /* If we found one, we're done.  */
7479   if (cp_parser_parse_definitely (parser))
7480     return id;
7481   /* Otherwise, look for an ordinary identifier.  */
7482   return cp_parser_identifier (parser);
7483 }
7484
7485 /* Overloading [gram.over] */
7486
7487 /* Parse an operator-function-id.
7488
7489    operator-function-id:
7490      operator operator  
7491
7492    Returns an IDENTIFIER_NODE for the operator which is a
7493    human-readable spelling of the identifier, e.g., `operator +'.  */
7494
7495 static tree 
7496 cp_parser_operator_function_id (parser)
7497      cp_parser *parser;
7498 {
7499   /* Look for the `operator' keyword.  */
7500   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7501     return error_mark_node;
7502   /* And then the name of the operator itself.  */
7503   return cp_parser_operator (parser);
7504 }
7505
7506 /* Parse an operator.
7507
7508    operator:
7509      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7510      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7511      || ++ -- , ->* -> () []
7512
7513    GNU Extensions:
7514    
7515    operator:
7516      <? >? <?= >?=
7517
7518    Returns an IDENTIFIER_NODE for the operator which is a
7519    human-readable spelling of the identifier, e.g., `operator +'.  */
7520    
7521 static tree
7522 cp_parser_operator (parser)
7523      cp_parser *parser;
7524 {
7525   tree id = NULL_TREE;
7526   cp_token *token;
7527
7528   /* Peek at the next token.  */
7529   token = cp_lexer_peek_token (parser->lexer);
7530   /* Figure out which operator we have.  */
7531   switch (token->type)
7532     {
7533     case CPP_KEYWORD:
7534       {
7535         enum tree_code op;
7536
7537         /* The keyword should be either `new' or `delete'.  */
7538         if (token->keyword == RID_NEW)
7539           op = NEW_EXPR;
7540         else if (token->keyword == RID_DELETE)
7541           op = DELETE_EXPR;
7542         else
7543           break;
7544
7545         /* Consume the `new' or `delete' token.  */
7546         cp_lexer_consume_token (parser->lexer);
7547
7548         /* Peek at the next token.  */
7549         token = cp_lexer_peek_token (parser->lexer);
7550         /* If it's a `[' token then this is the array variant of the
7551            operator.  */
7552         if (token->type == CPP_OPEN_SQUARE)
7553           {
7554             /* Consume the `[' token.  */
7555             cp_lexer_consume_token (parser->lexer);
7556             /* Look for the `]' token.  */
7557             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7558             id = ansi_opname (op == NEW_EXPR 
7559                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7560           }
7561         /* Otherwise, we have the non-array variant.  */
7562         else
7563           id = ansi_opname (op);
7564
7565         return id;
7566       }
7567
7568     case CPP_PLUS:
7569       id = ansi_opname (PLUS_EXPR);
7570       break;
7571
7572     case CPP_MINUS:
7573       id = ansi_opname (MINUS_EXPR);
7574       break;
7575
7576     case CPP_MULT:
7577       id = ansi_opname (MULT_EXPR);
7578       break;
7579
7580     case CPP_DIV:
7581       id = ansi_opname (TRUNC_DIV_EXPR);
7582       break;
7583
7584     case CPP_MOD:
7585       id = ansi_opname (TRUNC_MOD_EXPR);
7586       break;
7587
7588     case CPP_XOR:
7589       id = ansi_opname (BIT_XOR_EXPR);
7590       break;
7591
7592     case CPP_AND:
7593       id = ansi_opname (BIT_AND_EXPR);
7594       break;
7595
7596     case CPP_OR:
7597       id = ansi_opname (BIT_IOR_EXPR);
7598       break;
7599
7600     case CPP_COMPL:
7601       id = ansi_opname (BIT_NOT_EXPR);
7602       break;
7603       
7604     case CPP_NOT:
7605       id = ansi_opname (TRUTH_NOT_EXPR);
7606       break;
7607
7608     case CPP_EQ:
7609       id = ansi_assopname (NOP_EXPR);
7610       break;
7611
7612     case CPP_LESS:
7613       id = ansi_opname (LT_EXPR);
7614       break;
7615
7616     case CPP_GREATER:
7617       id = ansi_opname (GT_EXPR);
7618       break;
7619
7620     case CPP_PLUS_EQ:
7621       id = ansi_assopname (PLUS_EXPR);
7622       break;
7623
7624     case CPP_MINUS_EQ:
7625       id = ansi_assopname (MINUS_EXPR);
7626       break;
7627
7628     case CPP_MULT_EQ:
7629       id = ansi_assopname (MULT_EXPR);
7630       break;
7631
7632     case CPP_DIV_EQ:
7633       id = ansi_assopname (TRUNC_DIV_EXPR);
7634       break;
7635
7636     case CPP_MOD_EQ:
7637       id = ansi_assopname (TRUNC_MOD_EXPR);
7638       break;
7639
7640     case CPP_XOR_EQ:
7641       id = ansi_assopname (BIT_XOR_EXPR);
7642       break;
7643
7644     case CPP_AND_EQ:
7645       id = ansi_assopname (BIT_AND_EXPR);
7646       break;
7647
7648     case CPP_OR_EQ:
7649       id = ansi_assopname (BIT_IOR_EXPR);
7650       break;
7651
7652     case CPP_LSHIFT:
7653       id = ansi_opname (LSHIFT_EXPR);
7654       break;
7655
7656     case CPP_RSHIFT:
7657       id = ansi_opname (RSHIFT_EXPR);
7658       break;
7659
7660     case CPP_LSHIFT_EQ:
7661       id = ansi_assopname (LSHIFT_EXPR);
7662       break;
7663
7664     case CPP_RSHIFT_EQ:
7665       id = ansi_assopname (RSHIFT_EXPR);
7666       break;
7667
7668     case CPP_EQ_EQ:
7669       id = ansi_opname (EQ_EXPR);
7670       break;
7671
7672     case CPP_NOT_EQ:
7673       id = ansi_opname (NE_EXPR);
7674       break;
7675
7676     case CPP_LESS_EQ:
7677       id = ansi_opname (LE_EXPR);
7678       break;
7679
7680     case CPP_GREATER_EQ:
7681       id = ansi_opname (GE_EXPR);
7682       break;
7683
7684     case CPP_AND_AND:
7685       id = ansi_opname (TRUTH_ANDIF_EXPR);
7686       break;
7687
7688     case CPP_OR_OR:
7689       id = ansi_opname (TRUTH_ORIF_EXPR);
7690       break;
7691       
7692     case CPP_PLUS_PLUS:
7693       id = ansi_opname (POSTINCREMENT_EXPR);
7694       break;
7695
7696     case CPP_MINUS_MINUS:
7697       id = ansi_opname (PREDECREMENT_EXPR);
7698       break;
7699
7700     case CPP_COMMA:
7701       id = ansi_opname (COMPOUND_EXPR);
7702       break;
7703
7704     case CPP_DEREF_STAR:
7705       id = ansi_opname (MEMBER_REF);
7706       break;
7707
7708     case CPP_DEREF:
7709       id = ansi_opname (COMPONENT_REF);
7710       break;
7711
7712     case CPP_OPEN_PAREN:
7713       /* Consume the `('.  */
7714       cp_lexer_consume_token (parser->lexer);
7715       /* Look for the matching `)'.  */
7716       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7717       return ansi_opname (CALL_EXPR);
7718
7719     case CPP_OPEN_SQUARE:
7720       /* Consume the `['.  */
7721       cp_lexer_consume_token (parser->lexer);
7722       /* Look for the matching `]'.  */
7723       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7724       return ansi_opname (ARRAY_REF);
7725
7726       /* Extensions.  */
7727     case CPP_MIN:
7728       id = ansi_opname (MIN_EXPR);
7729       break;
7730
7731     case CPP_MAX:
7732       id = ansi_opname (MAX_EXPR);
7733       break;
7734
7735     case CPP_MIN_EQ:
7736       id = ansi_assopname (MIN_EXPR);
7737       break;
7738
7739     case CPP_MAX_EQ:
7740       id = ansi_assopname (MAX_EXPR);
7741       break;
7742
7743     default:
7744       /* Anything else is an error.  */
7745       break;
7746     }
7747
7748   /* If we have selected an identifier, we need to consume the
7749      operator token.  */
7750   if (id)
7751     cp_lexer_consume_token (parser->lexer);
7752   /* Otherwise, no valid operator name was present.  */
7753   else
7754     {
7755       cp_parser_error (parser, "expected operator");
7756       id = error_mark_node;
7757     }
7758
7759   return id;
7760 }
7761
7762 /* Parse a template-declaration.
7763
7764    template-declaration:
7765      export [opt] template < template-parameter-list > declaration  
7766
7767    If MEMBER_P is TRUE, this template-declaration occurs within a
7768    class-specifier.  
7769
7770    The grammar rule given by the standard isn't correct.  What
7771    is really meant is:
7772
7773    template-declaration:
7774      export [opt] template-parameter-list-seq 
7775        decl-specifier-seq [opt] init-declarator [opt] ;
7776      export [opt] template-parameter-list-seq 
7777        function-definition
7778
7779    template-parameter-list-seq:
7780      template-parameter-list-seq [opt]
7781      template < template-parameter-list >  */
7782
7783 static void
7784 cp_parser_template_declaration (parser, member_p)
7785      cp_parser *parser;
7786      bool member_p;
7787 {
7788   /* Check for `export'.  */
7789   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7790     {
7791       /* Consume the `export' token.  */
7792       cp_lexer_consume_token (parser->lexer);
7793       /* Warn that we do not support `export'.  */
7794       warning ("keyword `export' not implemented, and will be ignored");
7795     }
7796
7797   cp_parser_template_declaration_after_export (parser, member_p);
7798 }
7799
7800 /* Parse a template-parameter-list.
7801
7802    template-parameter-list:
7803      template-parameter
7804      template-parameter-list , template-parameter
7805
7806    Returns a TREE_LIST.  Each node represents a template parameter.
7807    The nodes are connected via their TREE_CHAINs.  */
7808
7809 static tree
7810 cp_parser_template_parameter_list (parser)
7811      cp_parser *parser;
7812 {
7813   tree parameter_list = NULL_TREE;
7814
7815   while (true)
7816     {
7817       tree parameter;
7818       cp_token *token;
7819
7820       /* Parse the template-parameter.  */
7821       parameter = cp_parser_template_parameter (parser);
7822       /* Add it to the list.  */
7823       parameter_list = process_template_parm (parameter_list,
7824                                               parameter);
7825
7826       /* Peek at the next token.  */
7827       token = cp_lexer_peek_token (parser->lexer);
7828       /* If it's not a `,', we're done.  */
7829       if (token->type != CPP_COMMA)
7830         break;
7831       /* Otherwise, consume the `,' token.  */
7832       cp_lexer_consume_token (parser->lexer);
7833     }
7834
7835   return parameter_list;
7836 }
7837
7838 /* Parse a template-parameter.
7839
7840    template-parameter:
7841      type-parameter
7842      parameter-declaration
7843
7844    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
7845    TREE_PURPOSE is the default value, if any.  */
7846
7847 static tree
7848 cp_parser_template_parameter (parser)
7849      cp_parser *parser;
7850 {
7851   cp_token *token;
7852
7853   /* Peek at the next token.  */
7854   token = cp_lexer_peek_token (parser->lexer);
7855   /* If it is `class' or `template', we have a type-parameter.  */
7856   if (token->keyword == RID_TEMPLATE)
7857     return cp_parser_type_parameter (parser);
7858   /* If it is `class' or `typename' we do not know yet whether it is a
7859      type parameter or a non-type parameter.  Consider:
7860
7861        template <typename T, typename T::X X> ...
7862
7863      or:
7864      
7865        template <class C, class D*> ...
7866
7867      Here, the first parameter is a type parameter, and the second is
7868      a non-type parameter.  We can tell by looking at the token after
7869      the identifier -- if it is a `,', `=', or `>' then we have a type
7870      parameter.  */
7871   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7872     {
7873       /* Peek at the token after `class' or `typename'.  */
7874       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7875       /* If it's an identifier, skip it.  */
7876       if (token->type == CPP_NAME)
7877         token = cp_lexer_peek_nth_token (parser->lexer, 3);
7878       /* Now, see if the token looks like the end of a template
7879          parameter.  */
7880       if (token->type == CPP_COMMA 
7881           || token->type == CPP_EQ
7882           || token->type == CPP_GREATER)
7883         return cp_parser_type_parameter (parser);
7884     }
7885
7886   /* Otherwise, it is a non-type parameter.  
7887
7888      [temp.param]
7889
7890      When parsing a default template-argument for a non-type
7891      template-parameter, the first non-nested `>' is taken as the end
7892      of the template parameter-list rather than a greater-than
7893      operator.  */
7894   return 
7895     cp_parser_parameter_declaration (parser,
7896                                      /*greater_than_is_operator_p=*/false);
7897 }
7898
7899 /* Parse a type-parameter.
7900
7901    type-parameter:
7902      class identifier [opt]
7903      class identifier [opt] = type-id
7904      typename identifier [opt]
7905      typename identifier [opt] = type-id
7906      template < template-parameter-list > class identifier [opt]
7907      template < template-parameter-list > class identifier [opt] 
7908        = id-expression  
7909
7910    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
7911    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
7912    the declaration of the parameter.  */
7913
7914 static tree
7915 cp_parser_type_parameter (parser)
7916      cp_parser *parser;
7917 {
7918   cp_token *token;
7919   tree parameter;
7920
7921   /* Look for a keyword to tell us what kind of parameter this is.  */
7922   token = cp_parser_require (parser, CPP_KEYWORD, 
7923                              "expected `class', `typename', or `template'");
7924   if (!token)
7925     return error_mark_node;
7926
7927   switch (token->keyword)
7928     {
7929     case RID_CLASS:
7930     case RID_TYPENAME:
7931       {
7932         tree identifier;
7933         tree default_argument;
7934
7935         /* If the next token is an identifier, then it names the
7936            parameter.  */
7937         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7938           identifier = cp_parser_identifier (parser);
7939         else
7940           identifier = NULL_TREE;
7941
7942         /* Create the parameter.  */
7943         parameter = finish_template_type_parm (class_type_node, identifier);
7944
7945         /* If the next token is an `=', we have a default argument.  */
7946         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7947           {
7948             /* Consume the `=' token.  */
7949             cp_lexer_consume_token (parser->lexer);
7950             /* Parse the default-argumen.  */
7951             default_argument = cp_parser_type_id (parser);
7952           }
7953         else
7954           default_argument = NULL_TREE;
7955
7956         /* Create the combined representation of the parameter and the
7957            default argument.  */
7958         parameter = build_tree_list (default_argument, 
7959                                      parameter);
7960       }
7961       break;
7962
7963     case RID_TEMPLATE:
7964       {
7965         tree parameter_list;
7966         tree identifier;
7967         tree default_argument;
7968
7969         /* Look for the `<'.  */
7970         cp_parser_require (parser, CPP_LESS, "`<'");
7971         /* Parse the template-parameter-list.  */
7972         begin_template_parm_list ();
7973         parameter_list 
7974           = cp_parser_template_parameter_list (parser);
7975         parameter_list = end_template_parm_list (parameter_list);
7976         /* Look for the `>'.  */
7977         cp_parser_require (parser, CPP_GREATER, "`>'");
7978         /* Look for the `class' keyword.  */
7979         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7980         /* If the next token is an `=', then there is a
7981            default-argument.  If the next token is a `>', we are at
7982            the end of the parameter-list.  If the next token is a `,',
7983            then we are at the end of this parameter.  */
7984         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7985             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7986             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7987           identifier = cp_parser_identifier (parser);
7988         else
7989           identifier = NULL_TREE;
7990         /* Create the template parameter.  */
7991         parameter = finish_template_template_parm (class_type_node,
7992                                                    identifier);
7993                                                    
7994         /* If the next token is an `=', then there is a
7995            default-argument.  */
7996         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7997           {
7998             /* Consume the `='.  */
7999             cp_lexer_consume_token (parser->lexer);
8000             /* Parse the id-expression.  */
8001             default_argument 
8002               = cp_parser_id_expression (parser,
8003                                          /*template_keyword_p=*/false,
8004                                          /*check_dependency_p=*/true,
8005                                          /*template_p=*/NULL);
8006             /* Look up the name.  */
8007             default_argument 
8008               = cp_parser_lookup_name_simple (parser, default_argument);
8009             /* See if the default argument is valid.  */
8010             default_argument
8011               = check_template_template_default_arg (default_argument);
8012           }
8013         else
8014           default_argument = NULL_TREE;
8015
8016         /* Create the combined representation of the parameter and the
8017            default argument.  */
8018         parameter =  build_tree_list (default_argument, 
8019                                       parameter);
8020       }
8021       break;
8022
8023     default:
8024       /* Anything else is an error.  */
8025       cp_parser_error (parser,
8026                        "expected `class', `typename', or `template'");
8027       parameter = error_mark_node;
8028     }
8029   
8030   return parameter;
8031 }
8032
8033 /* Parse a template-id.
8034
8035    template-id:
8036      template-name < template-argument-list [opt] >
8037
8038    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8039    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8040    returned.  Otherwise, if the template-name names a function, or set
8041    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8042    names a class, returns a TYPE_DECL for the specialization.  
8043
8044    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8045    uninstantiated templates.  */
8046
8047 static tree
8048 cp_parser_template_id (cp_parser *parser, 
8049                        bool template_keyword_p, 
8050                        bool check_dependency_p)
8051 {
8052   tree template;
8053   tree arguments;
8054   tree saved_scope;
8055   tree saved_qualifying_scope;
8056   tree saved_object_scope;
8057   tree template_id;
8058   bool saved_greater_than_is_operator_p;
8059   ptrdiff_t start_of_id;
8060   tree access_check = NULL_TREE;
8061
8062   /* If the next token corresponds to a template-id, there is no need
8063      to reparse it.  */
8064   if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
8065     {
8066       tree value;
8067       tree check;
8068
8069       /* Get the stored value.  */
8070       value = cp_lexer_consume_token (parser->lexer)->value;
8071       /* Perform any access checks that were deferred.  */
8072       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8073         cp_parser_defer_access_check (parser, 
8074                                       TREE_PURPOSE (check),
8075                                       TREE_VALUE (check));
8076       /* Return the stored value.  */
8077       return TREE_VALUE (value);
8078     }
8079
8080   /* Remember where the template-id starts.  */
8081   if (cp_parser_parsing_tentatively (parser)
8082       && !cp_parser_committed_to_tentative_parse (parser))
8083     {
8084       cp_token *next_token = cp_lexer_peek_token (parser->lexer);
8085       start_of_id = cp_lexer_token_difference (parser->lexer,
8086                                                parser->lexer->first_token,
8087                                                next_token);
8088       access_check = parser->context->deferred_access_checks;
8089     }
8090   else
8091     start_of_id = -1;
8092
8093   /* Parse the template-name.  */
8094   template = cp_parser_template_name (parser, template_keyword_p,
8095                                       check_dependency_p);
8096   if (template == error_mark_node)
8097     return error_mark_node;
8098
8099   /* Look for the `<' that starts the template-argument-list.  */
8100   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8101     return error_mark_node;
8102
8103   /* [temp.names]
8104
8105      When parsing a template-id, the first non-nested `>' is taken as
8106      the end of the template-argument-list rather than a greater-than
8107      operator.  */
8108   saved_greater_than_is_operator_p 
8109     = parser->greater_than_is_operator_p;
8110   parser->greater_than_is_operator_p = false;
8111   /* Parsing the argument list may modify SCOPE, so we save it
8112      here.  */
8113   saved_scope = parser->scope;
8114   saved_qualifying_scope = parser->qualifying_scope;
8115   saved_object_scope = parser->object_scope;
8116   /* Parse the template-argument-list itself.  */
8117   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8118     arguments = NULL_TREE;
8119   else
8120     arguments = cp_parser_template_argument_list (parser);
8121   /* Look for the `>' that ends the template-argument-list.  */
8122   cp_parser_require (parser, CPP_GREATER, "`>'");
8123   /* The `>' token might be a greater-than operator again now.  */
8124   parser->greater_than_is_operator_p 
8125     = saved_greater_than_is_operator_p;
8126   /* Restore the SAVED_SCOPE.  */
8127   parser->scope = saved_scope;
8128   parser->qualifying_scope = saved_qualifying_scope;
8129   parser->object_scope = saved_object_scope;
8130
8131   /* Build a representation of the specialization.  */
8132   if (TREE_CODE (template) == IDENTIFIER_NODE)
8133     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8134   else if (DECL_CLASS_TEMPLATE_P (template)
8135            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8136     template_id 
8137       = finish_template_type (template, arguments, 
8138                               cp_lexer_next_token_is (parser->lexer, 
8139                                                       CPP_SCOPE));
8140   else
8141     {
8142       /* If it's not a class-template or a template-template, it should be
8143          a function-template.  */
8144       my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8145                            || TREE_CODE (template) == OVERLOAD
8146                            || BASELINK_P (template)),
8147                           20010716);
8148       
8149       template_id = lookup_template_function (template, arguments);
8150     }
8151   
8152   /* If parsing tentatively, replace the sequence of tokens that makes
8153      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8154      should we re-parse the token stream, we will not have to repeat
8155      the effort required to do the parse, nor will we issue duplicate
8156      error messages about problems during instantiation of the
8157      template.  */
8158   if (start_of_id >= 0)
8159     {
8160       cp_token *token;
8161       tree c;
8162
8163       /* Find the token that corresponds to the start of the
8164          template-id.  */
8165       token = cp_lexer_advance_token (parser->lexer, 
8166                                       parser->lexer->first_token,
8167                                       start_of_id);
8168
8169       /* Remember the access checks associated with this
8170          nested-name-specifier.  */
8171       c = parser->context->deferred_access_checks;
8172       if (c == access_check)
8173         access_check = NULL_TREE;
8174       else
8175         {
8176           while (TREE_CHAIN (c) != access_check)
8177             c = TREE_CHAIN (c);
8178           access_check = parser->context->deferred_access_checks;
8179           parser->context->deferred_access_checks = TREE_CHAIN (c);
8180           TREE_CHAIN (c) = NULL_TREE;
8181         }
8182
8183       /* Reset the contents of the START_OF_ID token.  */
8184       token->type = CPP_TEMPLATE_ID;
8185       token->value = build_tree_list (access_check, template_id);
8186       token->keyword = RID_MAX;
8187       /* Purge all subsequent tokens.  */
8188       cp_lexer_purge_tokens_after (parser->lexer, token);
8189     }
8190
8191   return template_id;
8192 }
8193
8194 /* Parse a template-name.
8195
8196    template-name:
8197      identifier
8198  
8199    The standard should actually say:
8200
8201    template-name:
8202      identifier
8203      operator-function-id
8204      conversion-function-id
8205
8206    A defect report has been filed about this issue.
8207
8208    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8209    `template' keyword, in a construction like:
8210
8211      T::template f<3>()
8212
8213    In that case `f' is taken to be a template-name, even though there
8214    is no way of knowing for sure.
8215
8216    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8217    name refers to a set of overloaded functions, at least one of which
8218    is a template, or an IDENTIFIER_NODE with the name of the template,
8219    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8220    names are looked up inside uninstantiated templates.  */
8221
8222 static tree
8223 cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8224      cp_parser *parser;
8225      bool template_keyword_p;
8226      bool check_dependency_p;
8227 {
8228   tree identifier;
8229   tree decl;
8230   tree fns;
8231
8232   /* If the next token is `operator', then we have either an
8233      operator-function-id or a conversion-function-id.  */
8234   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8235     {
8236       /* We don't know whether we're looking at an
8237          operator-function-id or a conversion-function-id.  */
8238       cp_parser_parse_tentatively (parser);
8239       /* Try an operator-function-id.  */
8240       identifier = cp_parser_operator_function_id (parser);
8241       /* If that didn't work, try a conversion-function-id.  */
8242       if (!cp_parser_parse_definitely (parser))
8243         identifier = cp_parser_conversion_function_id (parser);
8244     }
8245   /* Look for the identifier.  */
8246   else
8247     identifier = cp_parser_identifier (parser);
8248   
8249   /* If we didn't find an identifier, we don't have a template-id.  */
8250   if (identifier == error_mark_node)
8251     return error_mark_node;
8252
8253   /* If the name immediately followed the `template' keyword, then it
8254      is a template-name.  However, if the next token is not `<', then
8255      we do not treat it as a template-name, since it is not being used
8256      as part of a template-id.  This enables us to handle constructs
8257      like:
8258
8259        template <typename T> struct S { S(); };
8260        template <typename T> S<T>::S();
8261
8262      correctly.  We would treat `S' as a template -- if it were `S<T>'
8263      -- but we do not if there is no `<'.  */
8264   if (template_keyword_p && processing_template_decl
8265       && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8266     return identifier;
8267
8268   /* Look up the name.  */
8269   decl = cp_parser_lookup_name (parser, identifier,
8270                                 /*check_access=*/true,
8271                                 /*is_type=*/false,
8272                                 /*is_namespace=*/false,
8273                                 check_dependency_p);
8274   decl = maybe_get_template_decl_from_type_decl (decl);
8275
8276   /* If DECL is a template, then the name was a template-name.  */
8277   if (TREE_CODE (decl) == TEMPLATE_DECL)
8278     ;
8279   else 
8280     {
8281       /* The standard does not explicitly indicate whether a name that
8282          names a set of overloaded declarations, some of which are
8283          templates, is a template-name.  However, such a name should
8284          be a template-name; otherwise, there is no way to form a
8285          template-id for the overloaded templates.  */
8286       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8287       if (TREE_CODE (fns) == OVERLOAD)
8288         {
8289           tree fn;
8290           
8291           for (fn = fns; fn; fn = OVL_NEXT (fn))
8292             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8293               break;
8294         }
8295       else
8296         {
8297           /* Otherwise, the name does not name a template.  */
8298           cp_parser_error (parser, "expected template-name");
8299           return error_mark_node;
8300         }
8301     }
8302
8303   /* If DECL is dependent, and refers to a function, then just return
8304      its name; we will look it up again during template instantiation.  */
8305   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8306     {
8307       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8308       if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8309         return identifier;
8310     }
8311
8312   return decl;
8313 }
8314
8315 /* Parse a template-argument-list.
8316
8317    template-argument-list:
8318      template-argument
8319      template-argument-list , template-argument
8320
8321    Returns a TREE_LIST representing the arguments, in the order they
8322    appeared.  The TREE_VALUE of each node is a representation of the
8323    argument.  */
8324
8325 static tree
8326 cp_parser_template_argument_list (parser)
8327      cp_parser *parser;
8328 {
8329   tree arguments = NULL_TREE;
8330
8331   while (true)
8332     {
8333       tree argument;
8334
8335       /* Parse the template-argument.  */
8336       argument = cp_parser_template_argument (parser);
8337       /* Add it to the list.  */
8338       arguments = tree_cons (NULL_TREE, argument, arguments);
8339       /* If it is not a `,', then there are no more arguments.  */
8340       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8341         break;
8342       /* Otherwise, consume the ','.  */
8343       cp_lexer_consume_token (parser->lexer);
8344     }
8345
8346   /* We built up the arguments in reverse order.  */
8347   return nreverse (arguments);
8348 }
8349
8350 /* Parse a template-argument.
8351
8352    template-argument:
8353      assignment-expression
8354      type-id
8355      id-expression
8356
8357    The representation is that of an assignment-expression, type-id, or
8358    id-expression -- except that the qualified id-expression is
8359    evaluated, so that the value returned is either a DECL or an
8360    OVERLOAD.  */
8361
8362 static tree
8363 cp_parser_template_argument (parser)
8364      cp_parser *parser;
8365 {
8366   tree argument;
8367   bool template_p;
8368
8369   /* There's really no way to know what we're looking at, so we just
8370      try each alternative in order.  
8371
8372        [temp.arg]
8373
8374        In a template-argument, an ambiguity between a type-id and an
8375        expression is resolved to a type-id, regardless of the form of
8376        the corresponding template-parameter.  
8377
8378      Therefore, we try a type-id first.  */
8379   cp_parser_parse_tentatively (parser);
8380   argument = cp_parser_type_id (parser);
8381   /* If the next token isn't a `,' or a `>', then this argument wasn't
8382      really finished.  */
8383   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8384       && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8385     cp_parser_error (parser, "expected template-argument");
8386   /* If that worked, we're done.  */
8387   if (cp_parser_parse_definitely (parser))
8388     return argument;
8389   /* We're still not sure what the argument will be.  */
8390   cp_parser_parse_tentatively (parser);
8391   /* Try a template.  */
8392   argument = cp_parser_id_expression (parser, 
8393                                       /*template_keyword_p=*/false,
8394                                       /*check_dependency_p=*/true,
8395                                       &template_p);
8396   /* If the next token isn't a `,' or a `>', then this argument wasn't
8397      really finished.  */
8398   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8399       && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8400     cp_parser_error (parser, "expected template-argument");
8401   if (!cp_parser_error_occurred (parser))
8402     {
8403       /* Figure out what is being referred to.  */
8404       argument = cp_parser_lookup_name_simple (parser, argument);
8405       if (template_p)
8406         argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8407                                                 TREE_OPERAND (argument, 1),
8408                                                 tf_error | tf_parsing);
8409       else if (TREE_CODE (argument) != TEMPLATE_DECL)
8410         cp_parser_error (parser, "expected template-name");
8411     }
8412   if (cp_parser_parse_definitely (parser))
8413     return argument;
8414   /* It must be an assignment-expression.  */
8415   return cp_parser_assignment_expression (parser);
8416 }
8417
8418 /* Parse an explicit-instantiation.
8419
8420    explicit-instantiation:
8421      template declaration  
8422
8423    Although the standard says `declaration', what it really means is:
8424
8425    explicit-instantiation:
8426      template decl-specifier-seq [opt] declarator [opt] ; 
8427
8428    Things like `template int S<int>::i = 5, int S<double>::j;' are not
8429    supposed to be allowed.  A defect report has been filed about this
8430    issue.  
8431
8432    GNU Extension:
8433   
8434    explicit-instantiation:
8435      storage-class-specifier template 
8436        decl-specifier-seq [opt] declarator [opt] ;
8437      function-specifier template 
8438        decl-specifier-seq [opt] declarator [opt] ;  */
8439
8440 static void
8441 cp_parser_explicit_instantiation (parser)
8442      cp_parser *parser;
8443 {
8444   bool declares_class_or_enum;
8445   tree decl_specifiers;
8446   tree attributes;
8447   tree extension_specifier = NULL_TREE;
8448
8449   /* Look for an (optional) storage-class-specifier or
8450      function-specifier.  */
8451   if (cp_parser_allow_gnu_extensions_p (parser))
8452     {
8453       extension_specifier 
8454         = cp_parser_storage_class_specifier_opt (parser);
8455       if (!extension_specifier)
8456         extension_specifier = cp_parser_function_specifier_opt (parser);
8457     }
8458
8459   /* Look for the `template' keyword.  */
8460   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8461   /* Let the front end know that we are processing an explicit
8462      instantiation.  */
8463   begin_explicit_instantiation ();
8464   /* [temp.explicit] says that we are supposed to ignore access
8465      control while processing explicit instantiation directives.  */
8466   scope_chain->check_access = 0;
8467   /* Parse a decl-specifier-seq.  */
8468   decl_specifiers 
8469     = cp_parser_decl_specifier_seq (parser,
8470                                     CP_PARSER_FLAGS_OPTIONAL,
8471                                     &attributes,
8472                                     &declares_class_or_enum);
8473   /* If there was exactly one decl-specifier, and it declared a class,
8474      and there's no declarator, then we have an explicit type
8475      instantiation.  */
8476   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8477     {
8478       tree type;
8479
8480       type = check_tag_decl (decl_specifiers);
8481       if (type)
8482         do_type_instantiation (type, extension_specifier, /*complain=*/1);
8483     }
8484   else
8485     {
8486       tree declarator;
8487       tree decl;
8488
8489       /* Parse the declarator.  */
8490       declarator 
8491         = cp_parser_declarator (parser, 
8492                                 /*abstract_p=*/false, 
8493                                 /*ctor_dtor_or_conv_p=*/NULL);
8494       decl = grokdeclarator (declarator, decl_specifiers, 
8495                              NORMAL, 0, NULL);
8496       /* Do the explicit instantiation.  */
8497       do_decl_instantiation (decl, extension_specifier);
8498     }
8499   /* We're done with the instantiation.  */
8500   end_explicit_instantiation ();
8501   /* Trun access control back on.  */
8502   scope_chain->check_access = flag_access_control;
8503
8504   /* Look for the trailing `;'.  */
8505   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8506 }
8507
8508 /* Parse an explicit-specialization.
8509
8510    explicit-specialization:
8511      template < > declaration  
8512
8513    Although the standard says `declaration', what it really means is:
8514
8515    explicit-specialization:
8516      template <> decl-specifier [opt] init-declarator [opt] ;
8517      template <> function-definition 
8518      template <> explicit-specialization
8519      template <> template-declaration  */
8520
8521 static void
8522 cp_parser_explicit_specialization (parser)
8523      cp_parser *parser;
8524 {
8525   /* Look for the `template' keyword.  */
8526   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8527   /* Look for the `<'.  */
8528   cp_parser_require (parser, CPP_LESS, "`<'");
8529   /* Look for the `>'.  */
8530   cp_parser_require (parser, CPP_GREATER, "`>'");
8531   /* We have processed another parameter list.  */
8532   ++parser->num_template_parameter_lists;
8533   /* Let the front end know that we are beginning a specialization.  */
8534   begin_specialization ();
8535
8536   /* If the next keyword is `template', we need to figure out whether
8537      or not we're looking a template-declaration.  */
8538   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8539     {
8540       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8541           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8542         cp_parser_template_declaration_after_export (parser,
8543                                                      /*member_p=*/false);
8544       else
8545         cp_parser_explicit_specialization (parser);
8546     }
8547   else
8548     /* Parse the dependent declaration.  */
8549     cp_parser_single_declaration (parser, 
8550                                   /*member_p=*/false,
8551                                   /*friend_p=*/NULL);
8552
8553   /* We're done with the specialization.  */
8554   end_specialization ();
8555   /* We're done with this parameter list.  */
8556   --parser->num_template_parameter_lists;
8557 }
8558
8559 /* Parse a type-specifier.
8560
8561    type-specifier:
8562      simple-type-specifier
8563      class-specifier
8564      enum-specifier
8565      elaborated-type-specifier
8566      cv-qualifier
8567
8568    GNU Extension:
8569
8570    type-specifier:
8571      __complex__
8572
8573    Returns a representation of the type-specifier.  If the
8574    type-specifier is a keyword (like `int' or `const', or
8575    `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8576    For a class-specifier, enum-specifier, or elaborated-type-specifier
8577    a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8578
8579    If IS_FRIEND is TRUE then this type-specifier is being declared a
8580    `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
8581    appearing in a decl-specifier-seq.
8582
8583    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8584    class-specifier, enum-specifier, or elaborated-type-specifier, then
8585    *DECLARES_CLASS_OR_ENUM is set to TRUE.  Otherwise, it is set to
8586    FALSE.
8587
8588    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8589    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
8590    is set to FALSE.  */
8591
8592 static tree
8593 cp_parser_type_specifier (parser, 
8594                           flags, 
8595                           is_friend,
8596                           is_declaration,
8597                           declares_class_or_enum,
8598                           is_cv_qualifier)
8599      cp_parser *parser;
8600      cp_parser_flags flags;
8601      bool is_friend;
8602      bool is_declaration;
8603      bool *declares_class_or_enum;
8604      bool *is_cv_qualifier;
8605 {
8606   tree type_spec = NULL_TREE;
8607   cp_token *token;
8608   enum rid keyword;
8609
8610   /* Assume this type-specifier does not declare a new type.  */
8611   if (declares_class_or_enum)
8612     *declares_class_or_enum = false;
8613   /* And that it does not specify a cv-qualifier.  */
8614   if (is_cv_qualifier)
8615     *is_cv_qualifier = false;
8616   /* Peek at the next token.  */
8617   token = cp_lexer_peek_token (parser->lexer);
8618
8619   /* If we're looking at a keyword, we can use that to guide the
8620      production we choose.  */
8621   keyword = token->keyword;
8622   switch (keyword)
8623     {
8624       /* Any of these indicate either a class-specifier, or an
8625          elaborated-type-specifier.  */
8626     case RID_CLASS:
8627     case RID_STRUCT:
8628     case RID_UNION:
8629     case RID_ENUM:
8630       /* Parse tentatively so that we can back up if we don't find a
8631          class-specifier or enum-specifier.  */
8632       cp_parser_parse_tentatively (parser);
8633       /* Look for the class-specifier or enum-specifier.  */
8634       if (keyword == RID_ENUM)
8635         type_spec = cp_parser_enum_specifier (parser);
8636       else
8637         type_spec = cp_parser_class_specifier (parser);
8638
8639       /* If that worked, we're done.  */
8640       if (cp_parser_parse_definitely (parser))
8641         {
8642           if (declares_class_or_enum)
8643             *declares_class_or_enum = true;
8644           return type_spec;
8645         }
8646
8647       /* Fall through.  */
8648
8649     case RID_TYPENAME:
8650       /* Look for an elaborated-type-specifier.  */
8651       type_spec = cp_parser_elaborated_type_specifier (parser,
8652                                                        is_friend,
8653                                                        is_declaration);
8654       /* We're declaring a class or enum -- unless we're using
8655          `typename'.  */
8656       if (declares_class_or_enum && keyword != RID_TYPENAME)
8657         *declares_class_or_enum = true;
8658       return type_spec;
8659
8660     case RID_CONST:
8661     case RID_VOLATILE:
8662     case RID_RESTRICT:
8663       type_spec = cp_parser_cv_qualifier_opt (parser);
8664       /* Even though we call a routine that looks for an optional
8665          qualifier, we know that there should be one.  */
8666       my_friendly_assert (type_spec != NULL, 20000328);
8667       /* This type-specifier was a cv-qualified.  */
8668       if (is_cv_qualifier)
8669         *is_cv_qualifier = true;
8670
8671       return type_spec;
8672
8673     case RID_COMPLEX:
8674       /* The `__complex__' keyword is a GNU extension.  */
8675       return cp_lexer_consume_token (parser->lexer)->value;
8676
8677     default:
8678       break;
8679     }
8680
8681   /* If we do not already have a type-specifier, assume we are looking
8682      at a simple-type-specifier.  */
8683   type_spec = cp_parser_simple_type_specifier (parser, flags);
8684
8685   /* If we didn't find a type-specifier, and a type-specifier was not
8686      optional in this context, issue an error message.  */
8687   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8688     {
8689       cp_parser_error (parser, "expected type specifier");
8690       return error_mark_node;
8691     }
8692
8693   return type_spec;
8694 }
8695
8696 /* Parse a simple-type-specifier.
8697
8698    simple-type-specifier:
8699      :: [opt] nested-name-specifier [opt] type-name
8700      :: [opt] nested-name-specifier template template-id
8701      char
8702      wchar_t
8703      bool
8704      short
8705      int
8706      long
8707      signed
8708      unsigned
8709      float
8710      double
8711      void  
8712
8713    GNU Extension:
8714
8715    simple-type-specifier:
8716      __typeof__ unary-expression
8717      __typeof__ ( type-id )
8718
8719    For the various keywords, the value returned is simply the
8720    TREE_IDENTIFIER representing the keyword.  For the first two
8721    productions, the value returned is the indicated TYPE_DECL.  */
8722
8723 static tree
8724 cp_parser_simple_type_specifier (parser, flags)
8725      cp_parser *parser;
8726      cp_parser_flags flags;
8727 {
8728   tree type = NULL_TREE;
8729   cp_token *token;
8730
8731   /* Peek at the next token.  */
8732   token = cp_lexer_peek_token (parser->lexer);
8733
8734   /* If we're looking at a keyword, things are easy.  */
8735   switch (token->keyword)
8736     {
8737     case RID_CHAR:
8738     case RID_WCHAR:
8739     case RID_BOOL:
8740     case RID_SHORT:
8741     case RID_INT:
8742     case RID_LONG:
8743     case RID_SIGNED:
8744     case RID_UNSIGNED:
8745     case RID_FLOAT:
8746     case RID_DOUBLE:
8747     case RID_VOID:
8748       /* Consume the token.  */
8749       return cp_lexer_consume_token (parser->lexer)->value;
8750
8751     case RID_TYPEOF:
8752       {
8753         tree operand;
8754
8755         /* Consume the `typeof' token.  */
8756         cp_lexer_consume_token (parser->lexer);
8757         /* Parse the operand to `typeof'  */
8758         operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8759         /* If it is not already a TYPE, take its type.  */
8760         if (!TYPE_P (operand))
8761           operand = finish_typeof (operand);
8762
8763         return operand;
8764       }
8765
8766     default:
8767       break;
8768     }
8769
8770   /* The type-specifier must be a user-defined type.  */
8771   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) 
8772     {
8773       /* Don't gobble tokens or issue error messages if this is an
8774          optional type-specifier.  */
8775       if (flags & CP_PARSER_FLAGS_OPTIONAL)
8776         cp_parser_parse_tentatively (parser);
8777
8778       /* Look for the optional `::' operator.  */
8779       cp_parser_global_scope_opt (parser,
8780                                   /*current_scope_valid_p=*/false);
8781       /* Look for the nested-name specifier.  */
8782       cp_parser_nested_name_specifier_opt (parser,
8783                                            /*typename_keyword_p=*/false,
8784                                            /*check_dependency_p=*/true,
8785                                            /*type_p=*/false);
8786       /* If we have seen a nested-name-specifier, and the next token
8787          is `template', then we are using the template-id production.  */
8788       if (parser->scope 
8789           && cp_parser_optional_template_keyword (parser))
8790         {
8791           /* Look for the template-id.  */
8792           type = cp_parser_template_id (parser, 
8793                                         /*template_keyword_p=*/true,
8794                                         /*check_dependency_p=*/true);
8795           /* If the template-id did not name a type, we are out of
8796              luck.  */
8797           if (TREE_CODE (type) != TYPE_DECL)
8798             {
8799               cp_parser_error (parser, "expected template-id for type");
8800               type = NULL_TREE;
8801             }
8802         }
8803       /* Otherwise, look for a type-name.  */
8804       else
8805         {
8806           type = cp_parser_type_name (parser);
8807           if (type == error_mark_node)
8808             type = NULL_TREE;
8809         }
8810
8811       /* If it didn't work out, we don't have a TYPE.  */
8812       if ((flags & CP_PARSER_FLAGS_OPTIONAL) 
8813           && !cp_parser_parse_definitely (parser))
8814         type = NULL_TREE;
8815     }
8816
8817   /* If we didn't get a type-name, issue an error message.  */
8818   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8819     {
8820       cp_parser_error (parser, "expected type-name");
8821       return error_mark_node;
8822     }
8823
8824   return type;
8825 }
8826
8827 /* Parse a type-name.
8828
8829    type-name:
8830      class-name
8831      enum-name
8832      typedef-name  
8833
8834    enum-name:
8835      identifier
8836
8837    typedef-name:
8838      identifier 
8839
8840    Returns a TYPE_DECL for the the type.  */
8841
8842 static tree
8843 cp_parser_type_name (parser)
8844      cp_parser *parser;
8845 {
8846   tree type_decl;
8847   tree identifier;
8848
8849   /* We can't know yet whether it is a class-name or not.  */
8850   cp_parser_parse_tentatively (parser);
8851   /* Try a class-name.  */
8852   type_decl = cp_parser_class_name (parser, 
8853                                     /*typename_keyword_p=*/false,
8854                                     /*template_keyword_p=*/false,
8855                                     /*type_p=*/false,
8856                                     /*check_access_p=*/true,
8857                                     /*check_dependency_p=*/true,
8858                                     /*class_head_p=*/false);
8859   /* If it's not a class-name, keep looking.  */
8860   if (!cp_parser_parse_definitely (parser))
8861     {
8862       /* It must be a typedef-name or an enum-name.  */
8863       identifier = cp_parser_identifier (parser);
8864       if (identifier == error_mark_node)
8865         return error_mark_node;
8866       
8867       /* Look up the type-name.  */
8868       type_decl = cp_parser_lookup_name_simple (parser, identifier);
8869       /* Issue an error if we did not find a type-name.  */
8870       if (TREE_CODE (type_decl) != TYPE_DECL)
8871         {
8872           cp_parser_error (parser, "expected type-name");
8873           type_decl = error_mark_node;
8874         }
8875       /* Remember that the name was used in the definition of the
8876          current class so that we can check later to see if the
8877          meaning would have been different after the class was
8878          entirely defined.  */
8879       else if (type_decl != error_mark_node
8880                && !parser->scope)
8881         maybe_note_name_used_in_class (identifier, type_decl);
8882     }
8883   
8884   return type_decl;
8885 }
8886
8887
8888 /* Parse an elaborated-type-specifier.  Note that the grammar given
8889    here incorporates the resolution to DR68.
8890
8891    elaborated-type-specifier:
8892      class-key :: [opt] nested-name-specifier [opt] identifier
8893      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8894      enum :: [opt] nested-name-specifier [opt] identifier
8895      typename :: [opt] nested-name-specifier identifier
8896      typename :: [opt] nested-name-specifier template [opt] 
8897        template-id 
8898
8899    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8900    declared `friend'.  If IS_DECLARATION is TRUE, then this
8901    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8902    something is being declared.
8903
8904    Returns the TYPE specified.  */
8905
8906 static tree
8907 cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8908      cp_parser *parser;
8909      bool is_friend;
8910      bool is_declaration;
8911 {
8912   enum tag_types tag_type;
8913   tree identifier;
8914   tree type = NULL_TREE;
8915
8916   /* See if we're looking at the `enum' keyword.  */
8917   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8918     {
8919       /* Consume the `enum' token.  */
8920       cp_lexer_consume_token (parser->lexer);
8921       /* Remember that it's an enumeration type.  */
8922       tag_type = enum_type;
8923     }
8924   /* Or, it might be `typename'.  */
8925   else if (cp_lexer_next_token_is_keyword (parser->lexer,
8926                                            RID_TYPENAME))
8927     {
8928       /* Consume the `typename' token.  */
8929       cp_lexer_consume_token (parser->lexer);
8930       /* Remember that it's a `typename' type.  */
8931       tag_type = typename_type;
8932       /* The `typename' keyword is only allowed in templates.  */
8933       if (!processing_template_decl)
8934         pedwarn ("using `typename' outside of template");
8935     }
8936   /* Otherwise it must be a class-key.  */
8937   else
8938     {
8939       tag_type = cp_parser_class_key (parser);
8940       if (tag_type == none_type)
8941         return error_mark_node;
8942     }
8943
8944   /* Look for the `::' operator.  */
8945   cp_parser_global_scope_opt (parser, 
8946                               /*current_scope_valid_p=*/false);
8947   /* Look for the nested-name-specifier.  */
8948   if (tag_type == typename_type)
8949     cp_parser_nested_name_specifier (parser,
8950                                      /*typename_keyword_p=*/true,
8951                                      /*check_dependency_p=*/true,
8952                                      /*type_p=*/true);
8953   else
8954     /* Even though `typename' is not present, the proposed resolution
8955        to Core Issue 180 says that in `class A<T>::B', `B' should be
8956        considered a type-name, even if `A<T>' is dependent.  */
8957     cp_parser_nested_name_specifier_opt (parser,
8958                                          /*typename_keyword_p=*/true,
8959                                          /*check_dependency_p=*/true,
8960                                          /*type_p=*/true);
8961   /* For everything but enumeration types, consider a template-id.  */
8962   if (tag_type != enum_type)
8963     {
8964       bool template_p = false;
8965       tree decl;
8966
8967       /* Allow the `template' keyword.  */
8968       template_p = cp_parser_optional_template_keyword (parser);
8969       /* If we didn't see `template', we don't know if there's a
8970          template-id or not.  */
8971       if (!template_p)
8972         cp_parser_parse_tentatively (parser);
8973       /* Parse the template-id.  */
8974       decl = cp_parser_template_id (parser, template_p,
8975                                     /*check_dependency_p=*/true);
8976       /* If we didn't find a template-id, look for an ordinary
8977          identifier.  */
8978       if (!template_p && !cp_parser_parse_definitely (parser))
8979         ;
8980       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8981          in effect, then we must assume that, upon instantiation, the
8982          template will correspond to a class.  */
8983       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8984                && tag_type == typename_type)
8985         type = make_typename_type (parser->scope, decl,
8986                                    /*complain=*/1);
8987       else 
8988         type = TREE_TYPE (decl);
8989     }
8990
8991   /* For an enumeration type, consider only a plain identifier.  */
8992   if (!type)
8993     {
8994       identifier = cp_parser_identifier (parser);
8995
8996       if (identifier == error_mark_node)
8997         return error_mark_node;
8998
8999       /* For a `typename', we needn't call xref_tag.  */
9000       if (tag_type == typename_type)
9001         return make_typename_type (parser->scope, identifier, 
9002                                    /*complain=*/1);
9003       /* Look up a qualified name in the usual way.  */
9004       if (parser->scope)
9005         {
9006           tree decl;
9007
9008           /* In an elaborated-type-specifier, names are assumed to name
9009              types, so we set IS_TYPE to TRUE when calling
9010              cp_parser_lookup_name.  */
9011           decl = cp_parser_lookup_name (parser, identifier, 
9012                                         /*check_access=*/true,
9013                                         /*is_type=*/true,
9014                                         /*is_namespace=*/false,
9015                                         /*check_dependency=*/true);
9016           decl = (cp_parser_maybe_treat_template_as_class 
9017                   (decl, /*tag_name_p=*/is_friend));
9018
9019           if (TREE_CODE (decl) != TYPE_DECL)
9020             {
9021               error ("expected type-name");
9022               return error_mark_node;
9023             }
9024           else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
9025                    && tag_type != enum_type)
9026             error ("`%T' referred to as `%s'", TREE_TYPE (decl),
9027                    tag_type == record_type ? "struct" : "class");
9028           else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
9029                    && tag_type == enum_type)
9030             error ("`%T' referred to as enum", TREE_TYPE (decl));
9031
9032           type = TREE_TYPE (decl);
9033         }
9034       else 
9035         {
9036           /* An elaborated-type-specifier sometimes introduces a new type and
9037              sometimes names an existing type.  Normally, the rule is that it
9038              introduces a new type only if there is not an existing type of
9039              the same name already in scope.  For example, given:
9040
9041                struct S {};
9042                void f() { struct S s; }
9043
9044              the `struct S' in the body of `f' is the same `struct S' as in
9045              the global scope; the existing definition is used.  However, if
9046              there were no global declaration, this would introduce a new 
9047              local class named `S'.
9048
9049              An exception to this rule applies to the following code:
9050
9051                namespace N { struct S; }
9052
9053              Here, the elaborated-type-specifier names a new type
9054              unconditionally; even if there is already an `S' in the
9055              containing scope this declaration names a new type.
9056              This exception only applies if the elaborated-type-specifier
9057              forms the complete declaration:
9058
9059                [class.name] 
9060
9061                A declaration consisting solely of `class-key identifier ;' is
9062                either a redeclaration of the name in the current scope or a
9063                forward declaration of the identifier as a class name.  It
9064                introduces the name into the current scope.
9065
9066              We are in this situation precisely when the next token is a `;'.
9067
9068              An exception to the exception is that a `friend' declaration does
9069              *not* name a new type; i.e., given:
9070
9071                struct S { friend struct T; };
9072
9073              `T' is not a new type in the scope of `S'.  
9074
9075              Also, `new struct S' or `sizeof (struct S)' never results in the
9076              definition of a new type; a new type can only be declared in a
9077              declaration context.   */
9078
9079           type = xref_tag (tag_type, identifier, 
9080                            /*attributes=*/NULL_TREE,
9081                            (is_friend 
9082                             || !is_declaration
9083                             || cp_lexer_next_token_is_not (parser->lexer, 
9084                                                            CPP_SEMICOLON)));
9085         }
9086     }
9087   if (tag_type != enum_type)
9088     cp_parser_check_class_key (tag_type, type);
9089   return type;
9090 }
9091
9092 /* Parse an enum-specifier.
9093
9094    enum-specifier:
9095      enum identifier [opt] { enumerator-list [opt] }
9096
9097    Returns an ENUM_TYPE representing the enumeration.  */
9098
9099 static tree
9100 cp_parser_enum_specifier (parser)
9101      cp_parser *parser;
9102 {
9103   cp_token *token;
9104   tree identifier = NULL_TREE;
9105   tree type;
9106
9107   /* Look for the `enum' keyword.  */
9108   if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9109     return error_mark_node;
9110   /* Peek at the next token.  */
9111   token = cp_lexer_peek_token (parser->lexer);
9112
9113   /* See if it is an identifier.  */
9114   if (token->type == CPP_NAME)
9115     identifier = cp_parser_identifier (parser);
9116
9117   /* Look for the `{'.  */
9118   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9119     return error_mark_node;
9120
9121   /* At this point, we're going ahead with the enum-specifier, even
9122      if some other problem occurs.  */
9123   cp_parser_commit_to_tentative_parse (parser);
9124
9125   /* Issue an error message if type-definitions are forbidden here.  */
9126   cp_parser_check_type_definition (parser);
9127
9128   /* Create the new type.  */
9129   type = start_enum (identifier ? identifier : make_anon_name ());
9130
9131   /* Peek at the next token.  */
9132   token = cp_lexer_peek_token (parser->lexer);
9133   /* If it's not a `}', then there are some enumerators.  */
9134   if (token->type != CPP_CLOSE_BRACE)
9135     cp_parser_enumerator_list (parser, type);
9136   /* Look for the `}'.  */
9137   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9138
9139   /* Finish up the enumeration.  */
9140   finish_enum (type);
9141
9142   return type;
9143 }
9144
9145 /* Parse an enumerator-list.  The enumerators all have the indicated
9146    TYPE.  
9147
9148    enumerator-list:
9149      enumerator-definition
9150      enumerator-list , enumerator-definition  */
9151
9152 static void
9153 cp_parser_enumerator_list (parser, type)
9154      cp_parser *parser;
9155      tree type;
9156 {
9157   while (true)
9158     {
9159       cp_token *token;
9160
9161       /* Parse an enumerator-definition.  */
9162       cp_parser_enumerator_definition (parser, type);
9163       /* Peek at the next token.  */
9164       token = cp_lexer_peek_token (parser->lexer);
9165       /* If it's not a `,', then we've reached the end of the 
9166          list.  */
9167       if (token->type != CPP_COMMA)
9168         break;
9169       /* Otherwise, consume the `,' and keep going.  */
9170       cp_lexer_consume_token (parser->lexer);
9171       /* If the next token is a `}', there is a trailing comma.  */
9172       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9173         {
9174           if (pedantic && !in_system_header)
9175             pedwarn ("comma at end of enumerator list");
9176           break;
9177         }
9178     }
9179 }
9180
9181 /* Parse an enumerator-definition.  The enumerator has the indicated
9182    TYPE.
9183
9184    enumerator-definition:
9185      enumerator
9186      enumerator = constant-expression
9187     
9188    enumerator:
9189      identifier  */
9190
9191 static void
9192 cp_parser_enumerator_definition (parser, type)
9193      cp_parser *parser;
9194      tree type;
9195 {
9196   cp_token *token;
9197   tree identifier;
9198   tree value;
9199
9200   /* Look for the identifier.  */
9201   identifier = cp_parser_identifier (parser);
9202   if (identifier == error_mark_node)
9203     return;
9204   
9205   /* Peek at the next token.  */
9206   token = cp_lexer_peek_token (parser->lexer);
9207   /* If it's an `=', then there's an explicit value.  */
9208   if (token->type == CPP_EQ)
9209     {
9210       /* Consume the `=' token.  */
9211       cp_lexer_consume_token (parser->lexer);
9212       /* Parse the value.  */
9213       value = cp_parser_constant_expression (parser);
9214     }
9215   else
9216     value = NULL_TREE;
9217
9218   /* Create the enumerator.  */
9219   build_enumerator (identifier, value, type);
9220 }
9221
9222 /* Parse a namespace-name.
9223
9224    namespace-name:
9225      original-namespace-name
9226      namespace-alias
9227
9228    Returns the NAMESPACE_DECL for the namespace.  */
9229
9230 static tree
9231 cp_parser_namespace_name (parser)
9232      cp_parser *parser;
9233 {
9234   tree identifier;
9235   tree namespace_decl;
9236
9237   /* Get the name of the namespace.  */
9238   identifier = cp_parser_identifier (parser);
9239   if (identifier == error_mark_node)
9240     return error_mark_node;
9241
9242   /* Look up the identifier in the currently active scope.  Look only
9243      for namespaces, due to:
9244
9245        [basic.lookup.udir]
9246
9247        When looking up a namespace-name in a using-directive or alias
9248        definition, only namespace names are considered.  
9249
9250      And:
9251
9252        [basic.lookup.qual]
9253
9254        During the lookup of a name preceding the :: scope resolution
9255        operator, object, function, and enumerator names are ignored.  
9256
9257      (Note that cp_parser_class_or_namespace_name only calls this
9258      function if the token after the name is the scope resolution
9259      operator.)  */
9260   namespace_decl = cp_parser_lookup_name (parser, identifier,
9261                                           /*check_access=*/true,
9262                                           /*is_type=*/false,
9263                                           /*is_namespace=*/true,
9264                                           /*check_dependency=*/true);
9265   /* If it's not a namespace, issue an error.  */
9266   if (namespace_decl == error_mark_node
9267       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9268     {
9269       cp_parser_error (parser, "expected namespace-name");
9270       namespace_decl = error_mark_node;
9271     }
9272   
9273   return namespace_decl;
9274 }
9275
9276 /* Parse a namespace-definition.
9277
9278    namespace-definition:
9279      named-namespace-definition
9280      unnamed-namespace-definition  
9281
9282    named-namespace-definition:
9283      original-namespace-definition
9284      extension-namespace-definition
9285
9286    original-namespace-definition:
9287      namespace identifier { namespace-body }
9288    
9289    extension-namespace-definition:
9290      namespace original-namespace-name { namespace-body }
9291  
9292    unnamed-namespace-definition:
9293      namespace { namespace-body } */
9294
9295 static void
9296 cp_parser_namespace_definition (parser)
9297      cp_parser *parser;
9298 {
9299   tree identifier;
9300
9301   /* Look for the `namespace' keyword.  */
9302   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9303
9304   /* Get the name of the namespace.  We do not attempt to distinguish
9305      between an original-namespace-definition and an
9306      extension-namespace-definition at this point.  The semantic
9307      analysis routines are responsible for that.  */
9308   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9309     identifier = cp_parser_identifier (parser);
9310   else
9311     identifier = NULL_TREE;
9312
9313   /* Look for the `{' to start the namespace.  */
9314   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9315   /* Start the namespace.  */
9316   push_namespace (identifier);
9317   /* Parse the body of the namespace.  */
9318   cp_parser_namespace_body (parser);
9319   /* Finish the namespace.  */
9320   pop_namespace ();
9321   /* Look for the final `}'.  */
9322   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9323 }
9324
9325 /* Parse a namespace-body.
9326
9327    namespace-body:
9328      declaration-seq [opt]  */
9329
9330 static void
9331 cp_parser_namespace_body (parser)
9332      cp_parser *parser;
9333 {
9334   cp_parser_declaration_seq_opt (parser);
9335 }
9336
9337 /* Parse a namespace-alias-definition.
9338
9339    namespace-alias-definition:
9340      namespace identifier = qualified-namespace-specifier ;  */
9341
9342 static void
9343 cp_parser_namespace_alias_definition (parser)
9344      cp_parser *parser;
9345 {
9346   tree identifier;
9347   tree namespace_specifier;
9348
9349   /* Look for the `namespace' keyword.  */
9350   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9351   /* Look for the identifier.  */
9352   identifier = cp_parser_identifier (parser);
9353   if (identifier == error_mark_node)
9354     return;
9355   /* Look for the `=' token.  */
9356   cp_parser_require (parser, CPP_EQ, "`='");
9357   /* Look for the qualified-namespace-specifier.  */
9358   namespace_specifier 
9359     = cp_parser_qualified_namespace_specifier (parser);
9360   /* Look for the `;' token.  */
9361   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9362
9363   /* Register the alias in the symbol table.  */
9364   do_namespace_alias (identifier, namespace_specifier);
9365 }
9366
9367 /* Parse a qualified-namespace-specifier.
9368
9369    qualified-namespace-specifier:
9370      :: [opt] nested-name-specifier [opt] namespace-name
9371
9372    Returns a NAMESPACE_DECL corresponding to the specified
9373    namespace.  */
9374
9375 static tree
9376 cp_parser_qualified_namespace_specifier (parser)
9377      cp_parser *parser;
9378 {
9379   /* Look for the optional `::'.  */
9380   cp_parser_global_scope_opt (parser, 
9381                               /*current_scope_valid_p=*/false);
9382
9383   /* Look for the optional nested-name-specifier.  */
9384   cp_parser_nested_name_specifier_opt (parser,
9385                                        /*typename_keyword_p=*/false,
9386                                        /*check_dependency_p=*/true,
9387                                        /*type_p=*/false);
9388
9389   return cp_parser_namespace_name (parser);
9390 }
9391
9392 /* Parse a using-declaration.
9393
9394    using-declaration:
9395      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9396      using :: unqualified-id ;  */
9397
9398 static void
9399 cp_parser_using_declaration (parser)
9400      cp_parser *parser;
9401 {
9402   cp_token *token;
9403   bool typename_p = false;
9404   bool global_scope_p;
9405   tree decl;
9406   tree identifier;
9407   tree scope;
9408
9409   /* Look for the `using' keyword.  */
9410   cp_parser_require_keyword (parser, RID_USING, "`using'");
9411   
9412   /* Peek at the next token.  */
9413   token = cp_lexer_peek_token (parser->lexer);
9414   /* See if it's `typename'.  */
9415   if (token->keyword == RID_TYPENAME)
9416     {
9417       /* Remember that we've seen it.  */
9418       typename_p = true;
9419       /* Consume the `typename' token.  */
9420       cp_lexer_consume_token (parser->lexer);
9421     }
9422
9423   /* Look for the optional global scope qualification.  */
9424   global_scope_p 
9425     = (cp_parser_global_scope_opt (parser,
9426                                    /*current_scope_valid_p=*/false) 
9427        != NULL_TREE);
9428
9429   /* If we saw `typename', or didn't see `::', then there must be a
9430      nested-name-specifier present.  */
9431   if (typename_p || !global_scope_p)
9432     cp_parser_nested_name_specifier (parser, typename_p, 
9433                                      /*check_dependency_p=*/true,
9434                                      /*type_p=*/false);
9435   /* Otherwise, we could be in either of the two productions.  In that
9436      case, treat the nested-name-specifier as optional.  */
9437   else
9438     cp_parser_nested_name_specifier_opt (parser,
9439                                          /*typename_keyword_p=*/false,
9440                                          /*check_dependency_p=*/true,
9441                                          /*type_p=*/false);
9442
9443   /* Parse the unqualified-id.  */
9444   identifier = cp_parser_unqualified_id (parser, 
9445                                          /*template_keyword_p=*/false,
9446                                          /*check_dependency_p=*/true);
9447
9448   /* The function we call to handle a using-declaration is different
9449      depending on what scope we are in.  */
9450   scope = current_scope ();
9451   if (scope && TYPE_P (scope))
9452     {
9453       /* Create the USING_DECL.  */
9454       decl = do_class_using_decl (build_nt (SCOPE_REF,
9455                                             parser->scope,
9456                                             identifier));
9457       /* Add it to the list of members in this class.  */
9458       finish_member_declaration (decl);
9459     }
9460   else
9461     {
9462       decl = cp_parser_lookup_name_simple (parser, identifier);
9463       if (scope)
9464         do_local_using_decl (decl);
9465       else
9466         do_toplevel_using_decl (decl);
9467     }
9468
9469   /* Look for the final `;'.  */
9470   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9471 }
9472
9473 /* Parse a using-directive.  
9474  
9475    using-directive:
9476      using namespace :: [opt] nested-name-specifier [opt]
9477        namespace-name ;  */
9478
9479 static void
9480 cp_parser_using_directive (parser)
9481      cp_parser *parser;
9482 {
9483   tree namespace_decl;
9484
9485   /* Look for the `using' keyword.  */
9486   cp_parser_require_keyword (parser, RID_USING, "`using'");
9487   /* And the `namespace' keyword.  */
9488   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9489   /* Look for the optional `::' operator.  */
9490   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9491   /* And the optional nested-name-sepcifier.  */
9492   cp_parser_nested_name_specifier_opt (parser,
9493                                        /*typename_keyword_p=*/false,
9494                                        /*check_dependency_p=*/true,
9495                                        /*type_p=*/false);
9496   /* Get the namespace being used.  */
9497   namespace_decl = cp_parser_namespace_name (parser);
9498   /* Update the symbol table.  */
9499   do_using_directive (namespace_decl);
9500   /* Look for the final `;'.  */
9501   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9502 }
9503
9504 /* Parse an asm-definition.
9505
9506    asm-definition:
9507      asm ( string-literal ) ;  
9508
9509    GNU Extension:
9510
9511    asm-definition:
9512      asm volatile [opt] ( string-literal ) ;
9513      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9514      asm volatile [opt] ( string-literal : asm-operand-list [opt]
9515                           : asm-operand-list [opt] ) ;
9516      asm volatile [opt] ( string-literal : asm-operand-list [opt] 
9517                           : asm-operand-list [opt] 
9518                           : asm-operand-list [opt] ) ;  */
9519
9520 static void
9521 cp_parser_asm_definition (parser)
9522      cp_parser *parser;
9523 {
9524   cp_token *token;
9525   tree string;
9526   tree outputs = NULL_TREE;
9527   tree inputs = NULL_TREE;
9528   tree clobbers = NULL_TREE;
9529   tree asm_stmt;
9530   bool volatile_p = false;
9531   bool extended_p = false;
9532
9533   /* Look for the `asm' keyword.  */
9534   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9535   /* See if the next token is `volatile'.  */
9536   if (cp_parser_allow_gnu_extensions_p (parser)
9537       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9538     {
9539       /* Remember that we saw the `volatile' keyword.  */
9540       volatile_p = true;
9541       /* Consume the token.  */
9542       cp_lexer_consume_token (parser->lexer);
9543     }
9544   /* Look for the opening `('.  */
9545   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9546   /* Look for the string.  */
9547   token = cp_parser_require (parser, CPP_STRING, "asm body");
9548   if (!token)
9549     return;
9550   string = token->value;
9551   /* If we're allowing GNU extensions, check for the extended assembly
9552      syntax.  Unfortunately, the `:' tokens need not be separated by 
9553      a space in C, and so, for compatibility, we tolerate that here
9554      too.  Doing that means that we have to treat the `::' operator as
9555      two `:' tokens.  */
9556   if (cp_parser_allow_gnu_extensions_p (parser)
9557       && at_function_scope_p ()
9558       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9559           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9560     {
9561       bool inputs_p = false;
9562       bool clobbers_p = false;
9563
9564       /* The extended syntax was used.  */
9565       extended_p = true;
9566
9567       /* Look for outputs.  */
9568       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9569         {
9570           /* Consume the `:'.  */
9571           cp_lexer_consume_token (parser->lexer);
9572           /* Parse the output-operands.  */
9573           if (cp_lexer_next_token_is_not (parser->lexer, 
9574                                           CPP_COLON)
9575               && cp_lexer_next_token_is_not (parser->lexer,
9576                                              CPP_SCOPE))
9577             outputs = cp_parser_asm_operand_list (parser);
9578         }
9579       /* If the next token is `::', there are no outputs, and the
9580          next token is the beginning of the inputs.  */
9581       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9582         {
9583           /* Consume the `::' token.  */
9584           cp_lexer_consume_token (parser->lexer);
9585           /* The inputs are coming next.  */
9586           inputs_p = true;
9587         }
9588
9589       /* Look for inputs.  */
9590       if (inputs_p
9591           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9592         {
9593           if (!inputs_p)
9594             /* Consume the `:'.  */
9595             cp_lexer_consume_token (parser->lexer);
9596           /* Parse the output-operands.  */
9597           if (cp_lexer_next_token_is_not (parser->lexer, 
9598                                           CPP_COLON)
9599               && cp_lexer_next_token_is_not (parser->lexer,
9600                                              CPP_SCOPE))
9601             inputs = cp_parser_asm_operand_list (parser);
9602         }
9603       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9604         /* The clobbers are coming next.  */
9605         clobbers_p = true;
9606
9607       /* Look for clobbers.  */
9608       if (clobbers_p 
9609           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9610         {
9611           if (!clobbers_p)
9612             /* Consume the `:'.  */
9613             cp_lexer_consume_token (parser->lexer);
9614           /* Parse the clobbers.  */
9615           clobbers = cp_parser_asm_clobber_list (parser);
9616         }
9617     }
9618   /* Look for the closing `)'.  */
9619   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9620     cp_parser_skip_to_closing_parenthesis (parser);
9621   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9622
9623   /* Create the ASM_STMT.  */
9624   if (at_function_scope_p ())
9625     {
9626       asm_stmt = 
9627         finish_asm_stmt (volatile_p 
9628                          ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9629                          string, outputs, inputs, clobbers);
9630       /* If the extended syntax was not used, mark the ASM_STMT.  */
9631       if (!extended_p)
9632         ASM_INPUT_P (asm_stmt) = 1;
9633     }
9634   else
9635     assemble_asm (string);
9636 }
9637
9638 /* Declarators [gram.dcl.decl] */
9639
9640 /* Parse an init-declarator.
9641
9642    init-declarator:
9643      declarator initializer [opt]
9644
9645    GNU Extension:
9646
9647    init-declarator:
9648      declarator asm-specification [opt] attributes [opt] initializer [opt]
9649
9650    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9651    Returns a reprsentation of the entity declared.  The ACCESS_CHECKS
9652    represent deferred access checks from the decl-specifier-seq.  If
9653    MEMBER_P is TRUE, then this declarator appears in a class scope.
9654    The new DECL created by this declarator is returned.
9655
9656    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9657    for a function-definition here as well.  If the declarator is a
9658    declarator for a function-definition, *FUNCTION_DEFINITION_P will
9659    be TRUE upon return.  By that point, the function-definition will
9660    have been completely parsed.
9661
9662    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9663    is FALSE.  */
9664
9665 static tree
9666 cp_parser_init_declarator (parser, 
9667                            decl_specifiers, 
9668                            prefix_attributes,
9669                            access_checks,
9670                            function_definition_allowed_p,
9671                            member_p,
9672                            function_definition_p)
9673      cp_parser *parser;
9674      tree decl_specifiers;
9675      tree prefix_attributes;
9676      tree access_checks;
9677      bool function_definition_allowed_p;
9678      bool member_p;
9679      bool *function_definition_p;
9680 {
9681   cp_token *token;
9682   tree declarator;
9683   tree attributes;
9684   tree asm_specification;
9685   tree initializer;
9686   tree decl = NULL_TREE;
9687   tree scope;
9688   tree declarator_access_checks;
9689   bool is_initialized;
9690   bool is_parenthesized_init;
9691   bool ctor_dtor_or_conv_p;
9692   bool friend_p;
9693
9694   /* Assume that this is not the declarator for a function
9695      definition.  */
9696   if (function_definition_p)
9697     *function_definition_p = false;
9698
9699   /* Defer access checks while parsing the declarator; we cannot know
9700      what names are accessible until we know what is being 
9701      declared.  */
9702   cp_parser_start_deferring_access_checks (parser);
9703   /* Parse the declarator.  */
9704   declarator 
9705     = cp_parser_declarator (parser,
9706                             /*abstract_p=*/false,
9707                             &ctor_dtor_or_conv_p);
9708   /* Gather up the deferred checks.  */
9709   declarator_access_checks 
9710     = cp_parser_stop_deferring_access_checks (parser);
9711
9712   /* If the DECLARATOR was erroneous, there's no need to go
9713      further.  */
9714   if (declarator == error_mark_node)
9715     return error_mark_node;
9716
9717   /* Figure out what scope the entity declared by the DECLARATOR is
9718      located in.  `grokdeclarator' sometimes changes the scope, so
9719      we compute it now.  */
9720   scope = get_scope_of_declarator (declarator);
9721
9722   /* If we're allowing GNU extensions, look for an asm-specification
9723      and attributes.  */
9724   if (cp_parser_allow_gnu_extensions_p (parser))
9725     {
9726       /* Look for an asm-specification.  */
9727       asm_specification = cp_parser_asm_specification_opt (parser);
9728       /* And attributes.  */
9729       attributes = cp_parser_attributes_opt (parser);
9730     }
9731   else
9732     {
9733       asm_specification = NULL_TREE;
9734       attributes = NULL_TREE;
9735     }
9736
9737   /* Peek at the next token.  */
9738   token = cp_lexer_peek_token (parser->lexer);
9739   /* Check to see if the token indicates the start of a
9740      function-definition.  */
9741   if (cp_parser_token_starts_function_definition_p (token))
9742     {
9743       if (!function_definition_allowed_p)
9744         {
9745           /* If a function-definition should not appear here, issue an
9746              error message.  */
9747           cp_parser_error (parser,
9748                            "a function-definition is not allowed here");
9749           return error_mark_node;
9750         }
9751       else
9752         {
9753           tree *ac;
9754
9755           /* Neither attributes nor an asm-specification are allowed
9756              on a function-definition.  */
9757           if (asm_specification)
9758             error ("an asm-specification is not allowed on a function-definition");
9759           if (attributes)
9760             error ("attributes are not allowed on a function-definition");
9761           /* This is a function-definition.  */
9762           *function_definition_p = true;
9763
9764           /* Thread the access checks together.  */
9765           ac = &access_checks;
9766           while (*ac)
9767             ac = &TREE_CHAIN (*ac);
9768           *ac = declarator_access_checks;
9769
9770           /* Parse the function definition.  */
9771           decl = (cp_parser_function_definition_from_specifiers_and_declarator
9772                   (parser, decl_specifiers, prefix_attributes, declarator,
9773                    access_checks));
9774
9775           /* Pull the access-checks apart again.  */
9776           *ac = NULL_TREE;
9777
9778           return decl;
9779         }
9780     }
9781
9782   /* [dcl.dcl]
9783
9784      Only in function declarations for constructors, destructors, and
9785      type conversions can the decl-specifier-seq be omitted.  
9786
9787      We explicitly postpone this check past the point where we handle
9788      function-definitions because we tolerate function-definitions
9789      that are missing their return types in some modes.  */
9790   if (!decl_specifiers && !ctor_dtor_or_conv_p)
9791     {
9792       cp_parser_error (parser, 
9793                        "expected constructor, destructor, or type conversion");
9794       return error_mark_node;
9795     }
9796
9797   /* An `=' or an `(' indicates an initializer.  */
9798   is_initialized = (token->type == CPP_EQ 
9799                      || token->type == CPP_OPEN_PAREN);
9800   /* If the init-declarator isn't initialized and isn't followed by a
9801      `,' or `;', it's not a valid init-declarator.  */
9802   if (!is_initialized 
9803       && token->type != CPP_COMMA
9804       && token->type != CPP_SEMICOLON)
9805     {
9806       cp_parser_error (parser, "expected init-declarator");
9807       return error_mark_node;
9808     }
9809
9810   /* Because start_decl has side-effects, we should only call it if we
9811      know we're going ahead.  By this point, we know that we cannot
9812      possibly be looking at any other construct.  */
9813   cp_parser_commit_to_tentative_parse (parser);
9814
9815   /* Check to see whether or not this declaration is a friend.  */
9816   friend_p = cp_parser_friend_p (decl_specifiers);
9817
9818   /* Check that the number of template-parameter-lists is OK.  */
9819   if (!cp_parser_check_declarator_template_parameters (parser, 
9820                                                        declarator))
9821     return error_mark_node;
9822
9823   /* Enter the newly declared entry in the symbol table.  If we're
9824      processing a declaration in a class-specifier, we wait until
9825      after processing the initializer.  */
9826   if (!member_p)
9827     {
9828       if (parser->in_unbraced_linkage_specification_p)
9829         {
9830           decl_specifiers = tree_cons (error_mark_node,
9831                                        get_identifier ("extern"),
9832                                        decl_specifiers);
9833           have_extern_spec = false;
9834         }
9835       decl = start_decl (declarator,
9836                          decl_specifiers,
9837                          is_initialized,
9838                          attributes,
9839                          prefix_attributes);
9840     }
9841
9842   /* Enter the SCOPE.  That way unqualified names appearing in the
9843      initializer will be looked up in SCOPE.  */
9844   if (scope)
9845     push_scope (scope);
9846
9847   /* Perform deferred access control checks, now that we know in which
9848      SCOPE the declared entity resides.  */
9849   if (!member_p && decl) 
9850     {
9851       tree saved_current_function_decl = NULL_TREE;
9852
9853       /* If the entity being declared is a function, pretend that we
9854          are in its scope.  If it is a `friend', it may have access to
9855          things that would not otherwise be accessible. */
9856       if (TREE_CODE (decl) == FUNCTION_DECL)
9857         {
9858           saved_current_function_decl = current_function_decl;
9859           current_function_decl = decl;
9860         }
9861         
9862       /* Perform the access control checks for the decl-specifiers.  */
9863       cp_parser_perform_deferred_access_checks (access_checks);
9864       /* And for the declarator.  */
9865       cp_parser_perform_deferred_access_checks (declarator_access_checks);
9866
9867       /* Restore the saved value.  */
9868       if (TREE_CODE (decl) == FUNCTION_DECL)
9869         current_function_decl = saved_current_function_decl;
9870     }
9871
9872   /* Parse the initializer.  */
9873   if (is_initialized)
9874     initializer = cp_parser_initializer (parser, 
9875                                          &is_parenthesized_init);
9876   else
9877     {
9878       initializer = NULL_TREE;
9879       is_parenthesized_init = false;
9880     }
9881
9882   /* The old parser allows attributes to appear after a parenthesized
9883      initializer.  Mark Mitchell proposed removing this functionality
9884      on the GCC mailing lists on 2002-08-13.  This parser accepts the
9885      attributes -- but ignores them.  */
9886   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9887     if (cp_parser_attributes_opt (parser))
9888       warning ("attributes after parenthesized initializer ignored");
9889
9890   /* Leave the SCOPE, now that we have processed the initializer.  It
9891      is important to do this before calling cp_finish_decl because it
9892      makes decisions about whether to create DECL_STMTs or not based
9893      on the current scope.  */
9894   if (scope)
9895     pop_scope (scope);
9896
9897   /* For an in-class declaration, use `grokfield' to create the
9898      declaration.  */
9899   if (member_p)
9900     decl = grokfield (declarator, decl_specifiers,
9901                       initializer, /*asmspec=*/NULL_TREE,
9902                         /*attributes=*/NULL_TREE);
9903
9904   /* Finish processing the declaration.  But, skip friend
9905      declarations.  */
9906   if (!friend_p && decl)
9907     cp_finish_decl (decl, 
9908                     initializer, 
9909                     asm_specification,
9910                     /* If the initializer is in parentheses, then this is
9911                        a direct-initialization, which means that an
9912                        `explicit' constructor is OK.  Otherwise, an
9913                        `explicit' constructor cannot be used.  */
9914                     ((is_parenthesized_init || !is_initialized)
9915                      ? 0 : LOOKUP_ONLYCONVERTING));
9916
9917   return decl;
9918 }
9919
9920 /* Parse a declarator.
9921    
9922    declarator:
9923      direct-declarator
9924      ptr-operator declarator  
9925
9926    abstract-declarator:
9927      ptr-operator abstract-declarator [opt]
9928      direct-abstract-declarator
9929
9930    GNU Extensions:
9931
9932    declarator:
9933      attributes [opt] direct-declarator
9934      attributes [opt] ptr-operator declarator  
9935
9936    abstract-declarator:
9937      attributes [opt] ptr-operator abstract-declarator [opt]
9938      attributes [opt] direct-abstract-declarator
9939      
9940    Returns a representation of the declarator.  If the declarator has
9941    the form `* declarator', then an INDIRECT_REF is returned, whose
9942    only operand is the sub-declarator.  Analagously, `& declarator' is
9943    represented as an ADDR_EXPR.  For `X::* declarator', a SCOPE_REF is
9944    used.  The first operand is the TYPE for `X'.  The second operand
9945    is an INDIRECT_REF whose operand is the sub-declarator.
9946
9947    Otherwise, the reprsentation is as for a direct-declarator.
9948
9949    (It would be better to define a structure type to represent
9950    declarators, rather than abusing `tree' nodes to represent
9951    declarators.  That would be much clearer and save some memory.
9952    There is no reason for declarators to be garbage-collected, for
9953    example; they are created during parser and no longer needed after
9954    `grokdeclarator' has been called.)
9955
9956    For a ptr-operator that has the optional cv-qualifier-seq,
9957    cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9958    node.
9959
9960    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9961    true if this declarator represents a constructor, destructor, or
9962    type conversion operator.  Otherwise, it is set to false.  
9963
9964    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9965    a decl-specifier-seq unless it declares a constructor, destructor,
9966    or conversion.  It might seem that we could check this condition in
9967    semantic analysis, rather than parsing, but that makes it difficult
9968    to handle something like `f()'.  We want to notice that there are
9969    no decl-specifiers, and therefore realize that this is an
9970    expression, not a declaration.)  */
9971
9972 static tree
9973 cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
9974      cp_parser *parser;
9975      bool abstract_p;
9976      bool *ctor_dtor_or_conv_p;
9977 {
9978   cp_token *token;
9979   tree declarator;
9980   enum tree_code code;
9981   tree cv_qualifier_seq;
9982   tree class_type;
9983   tree attributes = NULL_TREE;
9984
9985   /* Assume this is not a constructor, destructor, or type-conversion
9986      operator.  */
9987   if (ctor_dtor_or_conv_p)
9988     *ctor_dtor_or_conv_p = false;
9989
9990   if (cp_parser_allow_gnu_extensions_p (parser))
9991     attributes = cp_parser_attributes_opt (parser);
9992   
9993   /* Peek at the next token.  */
9994   token = cp_lexer_peek_token (parser->lexer);
9995   
9996   /* Check for the ptr-operator production.  */
9997   cp_parser_parse_tentatively (parser);
9998   /* Parse the ptr-operator.  */
9999   code = cp_parser_ptr_operator (parser, 
10000                                  &class_type, 
10001                                  &cv_qualifier_seq);
10002   /* If that worked, then we have a ptr-operator.  */
10003   if (cp_parser_parse_definitely (parser))
10004     {
10005       /* The dependent declarator is optional if we are parsing an
10006          abstract-declarator.  */
10007       if (abstract_p)
10008         cp_parser_parse_tentatively (parser);
10009
10010       /* Parse the dependent declarator.  */
10011       declarator = cp_parser_declarator (parser, abstract_p,
10012                                          /*ctor_dtor_or_conv_p=*/NULL);
10013
10014       /* If we are parsing an abstract-declarator, we must handle the
10015          case where the dependent declarator is absent.  */
10016       if (abstract_p && !cp_parser_parse_definitely (parser))
10017         declarator = NULL_TREE;
10018         
10019       /* Build the representation of the ptr-operator.  */
10020       if (code == INDIRECT_REF)
10021         declarator = make_pointer_declarator (cv_qualifier_seq, 
10022                                               declarator);
10023       else
10024         declarator = make_reference_declarator (cv_qualifier_seq,
10025                                                 declarator);
10026       /* Handle the pointer-to-member case.  */
10027       if (class_type)
10028         declarator = build_nt (SCOPE_REF, class_type, declarator);
10029     }
10030   /* Everything else is a direct-declarator.  */
10031   else
10032     declarator = cp_parser_direct_declarator (parser, 
10033                                               abstract_p,
10034                                               ctor_dtor_or_conv_p);
10035
10036   if (attributes && declarator != error_mark_node)
10037     declarator = tree_cons (attributes, declarator, NULL_TREE);
10038   
10039   return declarator;
10040 }
10041
10042 /* Parse a direct-declarator or direct-abstract-declarator.
10043
10044    direct-declarator:
10045      declarator-id
10046      direct-declarator ( parameter-declaration-clause )
10047        cv-qualifier-seq [opt] 
10048        exception-specification [opt]
10049      direct-declarator [ constant-expression [opt] ]
10050      ( declarator )  
10051
10052    direct-abstract-declarator:
10053      direct-abstract-declarator [opt]
10054        ( parameter-declaration-clause ) 
10055        cv-qualifier-seq [opt]
10056        exception-specification [opt]
10057      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10058      ( abstract-declarator )
10059
10060    Returns a representation of the declarator.  ABSTRACT_P is TRUE if
10061    we are parsing a direct-abstract-declarator; FALSE if we are
10062    parsing a direct-declarator.  CTOR_DTOR_OR_CONV_P is as for 
10063    cp_parser_declarator.
10064
10065    For the declarator-id production, the representation is as for an
10066    id-expression, except that a qualified name is represented as a
10067    SCOPE_REF.  A function-declarator is represented as a CALL_EXPR;
10068    see the documentation of the FUNCTION_DECLARATOR_* macros for
10069    information about how to find the various declarator components.
10070    An array-declarator is represented as an ARRAY_REF.  The
10071    direct-declarator is the first operand; the constant-expression
10072    indicating the size of the array is the second operand.  */
10073
10074 static tree
10075 cp_parser_direct_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
10076      cp_parser *parser;
10077      bool abstract_p;
10078      bool *ctor_dtor_or_conv_p;
10079 {
10080   cp_token *token;
10081   tree declarator;
10082   tree scope = NULL_TREE;
10083   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10084   bool saved_in_declarator_p = parser->in_declarator_p;
10085
10086   /* Peek at the next token.  */
10087   token = cp_lexer_peek_token (parser->lexer);
10088   /* Find the initial direct-declarator.  It might be a parenthesized
10089      declarator.  */
10090   if (token->type == CPP_OPEN_PAREN)
10091     {
10092       bool error_p;
10093
10094       /* For an abstract declarator we do not know whether we are
10095          looking at the beginning of a parameter-declaration-clause,
10096          or at a parenthesized abstract declarator.  For example, if
10097          we see `(int)', we are looking at a
10098          parameter-declaration-clause, and the
10099          direct-abstract-declarator has been omitted.  If, on the
10100          other hand we are looking at `((*))' then we are looking at a
10101          parenthesized abstract-declarator.  There is no easy way to
10102          tell which situation we are in.  */
10103       if (abstract_p)
10104         cp_parser_parse_tentatively (parser);
10105
10106       /* Consume the `('.  */
10107       cp_lexer_consume_token (parser->lexer);
10108       /* Parse the nested declarator.  */
10109       declarator 
10110         = cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p);
10111       /* Expect a `)'.  */
10112       error_p = !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10113
10114       /* If parsing a parenthesized abstract declarator didn't work,
10115          try a parameter-declaration-clause.  */
10116       if (abstract_p && !cp_parser_parse_definitely (parser))
10117         declarator = NULL_TREE;
10118       /* If we were not parsing an abstract declarator, but failed to
10119          find a satisfactory nested declarator, then an error has
10120          occurred.  */
10121       else if (!abstract_p 
10122                && (declarator == error_mark_node || error_p))
10123         return error_mark_node;
10124       /* Default args cannot appear in an abstract decl.  */
10125       parser->default_arg_ok_p = false;
10126     }
10127   /* Otherwise, for a non-abstract declarator, there should be a
10128      declarator-id.  */
10129   else if (!abstract_p)
10130     {
10131       declarator = cp_parser_declarator_id (parser);
10132       
10133       if (TREE_CODE (declarator) == SCOPE_REF)
10134         {
10135           scope = TREE_OPERAND (declarator, 0);
10136           
10137           /* In the declaration of a member of a template class
10138              outside of the class itself, the SCOPE will sometimes be
10139              a TYPENAME_TYPE.  For example, given:
10140              
10141                template <typename T>
10142                int S<T>::R::i = 3;
10143
10144              the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In this
10145              context, we must resolve S<T>::R to an ordinary type,
10146              rather than a typename type.
10147
10148              The reason we normally avoid resolving TYPENAME_TYPEs is
10149              that a specialization of `S' might render `S<T>::R' not a
10150              type.  However, if `S' is specialized, then this `i' will
10151              not be used, so there is no harm in resolving the types
10152              here.  */
10153           if (TREE_CODE (scope) == TYPENAME_TYPE)
10154             {
10155               /* Resolve the TYPENAME_TYPE.  */
10156               scope = cp_parser_resolve_typename_type (parser, scope);
10157               /* If that failed, the declarator is invalid.  */
10158               if (scope == error_mark_node)
10159                 return error_mark_node;
10160               /* Build a new DECLARATOR.  */
10161               declarator = build_nt (SCOPE_REF, 
10162                                      scope,
10163                                      TREE_OPERAND (declarator, 1));
10164             }
10165         }
10166       else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10167         /* Default args can only appear for a function decl.  */
10168         parser->default_arg_ok_p = false;
10169       
10170       /* Check to see whether the declarator-id names a constructor, 
10171          destructor, or conversion.  */
10172       if (ctor_dtor_or_conv_p 
10173           && ((TREE_CODE (declarator) == SCOPE_REF 
10174                && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10175               || (TREE_CODE (declarator) != SCOPE_REF
10176                   && at_class_scope_p ())))
10177         {
10178           tree unqualified_name;
10179           tree class_type;
10180
10181           /* Get the unqualified part of the name.  */
10182           if (TREE_CODE (declarator) == SCOPE_REF)
10183             {
10184               class_type = TREE_OPERAND (declarator, 0);
10185               unqualified_name = TREE_OPERAND (declarator, 1);
10186             }
10187           else
10188             {
10189               class_type = current_class_type;
10190               unqualified_name = declarator;
10191             }
10192
10193           /* See if it names ctor, dtor or conv.  */
10194           if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10195               || IDENTIFIER_TYPENAME_P (unqualified_name)
10196               || constructor_name_p (unqualified_name, class_type))
10197             {
10198               *ctor_dtor_or_conv_p = true;
10199               /* We would have cleared the default arg flag above, but
10200                  they are ok.  */
10201               parser->default_arg_ok_p = saved_default_arg_ok_p;
10202             }
10203         }
10204     }
10205   /* But for an abstract declarator, the initial direct-declarator can
10206      be omitted.  */
10207   else
10208     {
10209       declarator = NULL_TREE;
10210       parser->default_arg_ok_p = false;
10211     }
10212
10213   scope = get_scope_of_declarator (declarator);
10214   if (scope)
10215     /* Any names that appear after the declarator-id for a member
10216        are looked up in the containing scope.  */
10217     push_scope (scope);
10218   else
10219     scope = NULL_TREE;
10220   parser->in_declarator_p = true;
10221
10222   /* Now, parse function-declarators and array-declarators until there
10223      are no more.  */
10224   while (true)
10225     {
10226       /* Peek at the next token.  */
10227       token = cp_lexer_peek_token (parser->lexer);
10228       /* If it's a `[', we're looking at an array-declarator.  */
10229       if (token->type == CPP_OPEN_SQUARE)
10230         {
10231           tree bounds;
10232
10233           /* Consume the `['.  */
10234           cp_lexer_consume_token (parser->lexer);
10235           /* Peek at the next token.  */
10236           token = cp_lexer_peek_token (parser->lexer);
10237           /* If the next token is `]', then there is no
10238              constant-expression.  */
10239           if (token->type != CPP_CLOSE_SQUARE)
10240             bounds = cp_parser_constant_expression (parser);
10241           else
10242             bounds = NULL_TREE;
10243           /* Look for the closing `]'.  */
10244           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
10245
10246           declarator = build_nt (ARRAY_REF, declarator, bounds);
10247         }
10248       /* If it's a `(', we're looking at a function-declarator.  */
10249       else if (token->type == CPP_OPEN_PAREN)
10250         {
10251           /* A function-declarator.  Or maybe not.  Consider, for
10252              example:
10253
10254                int i (int);
10255                int i (3);
10256
10257              The first is the declaration of a function while the
10258              second is a the definition of a variable, including its
10259              initializer.
10260
10261              Having seen only the parenthesis, we cannot know which of
10262              these two alternatives should be selected.  Even more
10263              complex are examples like:
10264
10265                int i (int (a));
10266                int i (int (3));
10267
10268              The former is a function-declaration; the latter is a
10269              variable initialization.  
10270
10271              First, we attempt to parse a parameter-declaration
10272              clause.  If this works, then we continue; otherwise, we
10273              replace the tokens consumed in the process and continue.  */
10274           tree params;
10275
10276           /* We are now parsing tentatively.  */
10277           cp_parser_parse_tentatively (parser);
10278           
10279           /* Consume the `('.  */
10280           cp_lexer_consume_token (parser->lexer);
10281           /* Parse the parameter-declaration-clause.  */
10282           params = cp_parser_parameter_declaration_clause (parser);
10283           
10284           /* If all went well, parse the cv-qualifier-seq and the
10285              exception-specification.  */
10286           if (cp_parser_parse_definitely (parser))
10287             {
10288               tree cv_qualifiers;
10289               tree exception_specification;
10290
10291               /* Consume the `)'.  */
10292               cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10293
10294               /* Parse the cv-qualifier-seq.  */
10295               cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10296               /* And the exception-specification.  */
10297               exception_specification 
10298                 = cp_parser_exception_specification_opt (parser);
10299
10300               /* Create the function-declarator.  */
10301               declarator = make_call_declarator (declarator,
10302                                                  params,
10303                                                  cv_qualifiers,
10304                                                  exception_specification);
10305             }
10306           /* Otherwise, we must be done with the declarator.  */
10307           else
10308             break;
10309         }
10310       /* Otherwise, we're done with the declarator.  */
10311       else
10312         break;
10313       /* Any subsequent parameter lists are to do with return type, so
10314          are not those of the declared function.  */
10315       parser->default_arg_ok_p = false;
10316     }
10317
10318   /* For an abstract declarator, we might wind up with nothing at this
10319      point.  That's an error; the declarator is not optional.  */
10320   if (!declarator)
10321     cp_parser_error (parser, "expected declarator");
10322
10323   /* If we entered a scope, we must exit it now.  */
10324   if (scope)
10325     pop_scope (scope);
10326
10327   parser->default_arg_ok_p = saved_default_arg_ok_p;
10328   parser->in_declarator_p = saved_in_declarator_p;
10329   
10330   return declarator;
10331 }
10332
10333 /* Parse a ptr-operator.  
10334
10335    ptr-operator:
10336      * cv-qualifier-seq [opt]
10337      &
10338      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10339
10340    GNU Extension:
10341
10342    ptr-operator:
10343      & cv-qualifier-seq [opt]
10344
10345    Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10346    used.  Returns ADDR_EXPR if a reference was used.  In the
10347    case of a pointer-to-member, *TYPE is filled in with the 
10348    TYPE containing the member.  *CV_QUALIFIER_SEQ is filled in
10349    with the cv-qualifier-seq, or NULL_TREE, if there are no
10350    cv-qualifiers.  Returns ERROR_MARK if an error occurred.  */
10351    
10352 static enum tree_code
10353 cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10354      cp_parser *parser;
10355      tree *type;
10356      tree *cv_qualifier_seq;
10357 {
10358   enum tree_code code = ERROR_MARK;
10359   cp_token *token;
10360
10361   /* Assume that it's not a pointer-to-member.  */
10362   *type = NULL_TREE;
10363   /* And that there are no cv-qualifiers.  */
10364   *cv_qualifier_seq = NULL_TREE;
10365
10366   /* Peek at the next token.  */
10367   token = cp_lexer_peek_token (parser->lexer);
10368   /* If it's a `*' or `&' we have a pointer or reference.  */
10369   if (token->type == CPP_MULT || token->type == CPP_AND)
10370     {
10371       /* Remember which ptr-operator we were processing.  */
10372       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10373
10374       /* Consume the `*' or `&'.  */
10375       cp_lexer_consume_token (parser->lexer);
10376
10377       /* A `*' can be followed by a cv-qualifier-seq, and so can a
10378          `&', if we are allowing GNU extensions.  (The only qualifier
10379          that can legally appear after `&' is `restrict', but that is
10380          enforced during semantic analysis.  */
10381       if (code == INDIRECT_REF 
10382           || cp_parser_allow_gnu_extensions_p (parser))
10383         *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10384     }
10385   else
10386     {
10387       /* Try the pointer-to-member case.  */
10388       cp_parser_parse_tentatively (parser);
10389       /* Look for the optional `::' operator.  */
10390       cp_parser_global_scope_opt (parser,
10391                                   /*current_scope_valid_p=*/false);
10392       /* Look for the nested-name specifier.  */
10393       cp_parser_nested_name_specifier (parser,
10394                                        /*typename_keyword_p=*/false,
10395                                        /*check_dependency_p=*/true,
10396                                        /*type_p=*/false);
10397       /* If we found it, and the next token is a `*', then we are
10398          indeed looking at a pointer-to-member operator.  */
10399       if (!cp_parser_error_occurred (parser)
10400           && cp_parser_require (parser, CPP_MULT, "`*'"))
10401         {
10402           /* The type of which the member is a member is given by the
10403              current SCOPE.  */
10404           *type = parser->scope;
10405           /* The next name will not be qualified.  */
10406           parser->scope = NULL_TREE;
10407           parser->qualifying_scope = NULL_TREE;
10408           parser->object_scope = NULL_TREE;
10409           /* Indicate that the `*' operator was used.  */
10410           code = INDIRECT_REF;
10411           /* Look for the optional cv-qualifier-seq.  */
10412           *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10413         }
10414       /* If that didn't work we don't have a ptr-operator.  */
10415       if (!cp_parser_parse_definitely (parser))
10416         cp_parser_error (parser, "expected ptr-operator");
10417     }
10418
10419   return code;
10420 }
10421
10422 /* Parse an (optional) cv-qualifier-seq.
10423
10424    cv-qualifier-seq:
10425      cv-qualifier cv-qualifier-seq [opt]  
10426
10427    Returns a TREE_LIST.  The TREE_VALUE of each node is the
10428    representation of a cv-qualifier.  */
10429
10430 static tree
10431 cp_parser_cv_qualifier_seq_opt (parser)
10432      cp_parser *parser;
10433 {
10434   tree cv_qualifiers = NULL_TREE;
10435   
10436   while (true)
10437     {
10438       tree cv_qualifier;
10439
10440       /* Look for the next cv-qualifier.  */
10441       cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10442       /* If we didn't find one, we're done.  */
10443       if (!cv_qualifier)
10444         break;
10445
10446       /* Add this cv-qualifier to the list.  */
10447       cv_qualifiers 
10448         = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10449     }
10450
10451   /* We built up the list in reverse order.  */
10452   return nreverse (cv_qualifiers);
10453 }
10454
10455 /* Parse an (optional) cv-qualifier.
10456
10457    cv-qualifier:
10458      const
10459      volatile  
10460
10461    GNU Extension:
10462
10463    cv-qualifier:
10464      __restrict__ */
10465
10466 static tree
10467 cp_parser_cv_qualifier_opt (parser)
10468      cp_parser *parser;
10469 {
10470   cp_token *token;
10471   tree cv_qualifier = NULL_TREE;
10472
10473   /* Peek at the next token.  */
10474   token = cp_lexer_peek_token (parser->lexer);
10475   /* See if it's a cv-qualifier.  */
10476   switch (token->keyword)
10477     {
10478     case RID_CONST:
10479     case RID_VOLATILE:
10480     case RID_RESTRICT:
10481       /* Save the value of the token.  */
10482       cv_qualifier = token->value;
10483       /* Consume the token.  */
10484       cp_lexer_consume_token (parser->lexer);
10485       break;
10486
10487     default:
10488       break;
10489     }
10490
10491   return cv_qualifier;
10492 }
10493
10494 /* Parse a declarator-id.
10495
10496    declarator-id:
10497      id-expression
10498      :: [opt] nested-name-specifier [opt] type-name  
10499
10500    In the `id-expression' case, the value returned is as for
10501    cp_parser_id_expression if the id-expression was an unqualified-id.
10502    If the id-expression was a qualified-id, then a SCOPE_REF is
10503    returned.  The first operand is the scope (either a NAMESPACE_DECL
10504    or TREE_TYPE), but the second is still just a representation of an
10505    unqualified-id.  */
10506
10507 static tree
10508 cp_parser_declarator_id (parser)
10509      cp_parser *parser;
10510 {
10511   tree id_expression;
10512
10513   /* The expression must be an id-expression.  Assume that qualified
10514      names are the names of types so that:
10515
10516        template <class T>
10517        int S<T>::R::i = 3;
10518
10519      will work; we must treat `S<T>::R' as the name of a type.
10520      Similarly, assume that qualified names are templates, where
10521      required, so that:
10522
10523        template <class T>
10524        int S<T>::R<T>::i = 3;
10525
10526      will work, too.  */
10527   id_expression = cp_parser_id_expression (parser,
10528                                            /*template_keyword_p=*/false,
10529                                            /*check_dependency_p=*/false,
10530                                            /*template_p=*/NULL);
10531   /* If the name was qualified, create a SCOPE_REF to represent 
10532      that.  */
10533   if (parser->scope)
10534     id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10535
10536   return id_expression;
10537 }
10538
10539 /* Parse a type-id.
10540
10541    type-id:
10542      type-specifier-seq abstract-declarator [opt]
10543
10544    Returns the TYPE specified.  */
10545
10546 static tree
10547 cp_parser_type_id (parser)
10548      cp_parser *parser;
10549 {
10550   tree type_specifier_seq;
10551   tree abstract_declarator;
10552
10553   /* Parse the type-specifier-seq.  */
10554   type_specifier_seq 
10555     = cp_parser_type_specifier_seq (parser);
10556   if (type_specifier_seq == error_mark_node)
10557     return error_mark_node;
10558
10559   /* There might or might not be an abstract declarator.  */
10560   cp_parser_parse_tentatively (parser);
10561   /* Look for the declarator.  */
10562   abstract_declarator 
10563     = cp_parser_declarator (parser, /*abstract_p=*/true, NULL);
10564   /* Check to see if there really was a declarator.  */
10565   if (!cp_parser_parse_definitely (parser))
10566     abstract_declarator = NULL_TREE;
10567
10568   return groktypename (build_tree_list (type_specifier_seq,
10569                                         abstract_declarator));
10570 }
10571
10572 /* Parse a type-specifier-seq.
10573
10574    type-specifier-seq:
10575      type-specifier type-specifier-seq [opt]
10576
10577    GNU extension:
10578
10579    type-specifier-seq:
10580      attributes type-specifier-seq [opt]
10581
10582    Returns a TREE_LIST.  Either the TREE_VALUE of each node is a
10583    type-specifier, or the TREE_PURPOSE is a list of attributes.  */
10584
10585 static tree
10586 cp_parser_type_specifier_seq (parser)
10587      cp_parser *parser;
10588 {
10589   bool seen_type_specifier = false;
10590   tree type_specifier_seq = NULL_TREE;
10591
10592   /* Parse the type-specifiers and attributes.  */
10593   while (true)
10594     {
10595       tree type_specifier;
10596
10597       /* Check for attributes first.  */
10598       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10599         {
10600           type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10601                                           NULL_TREE,
10602                                           type_specifier_seq);
10603           continue;
10604         }
10605
10606       /* After the first type-specifier, others are optional.  */
10607       if (seen_type_specifier)
10608         cp_parser_parse_tentatively (parser);
10609       /* Look for the type-specifier.  */
10610       type_specifier = cp_parser_type_specifier (parser, 
10611                                                  CP_PARSER_FLAGS_NONE,
10612                                                  /*is_friend=*/false,
10613                                                  /*is_declaration=*/false,
10614                                                  NULL,
10615                                                  NULL);
10616       /* If the first type-specifier could not be found, this is not a
10617          type-specifier-seq at all.  */
10618       if (!seen_type_specifier && type_specifier == error_mark_node)
10619         return error_mark_node;
10620       /* If subsequent type-specifiers could not be found, the
10621          type-specifier-seq is complete.  */
10622       else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10623         break;
10624
10625       /* Add the new type-specifier to the list.  */
10626       type_specifier_seq 
10627         = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10628       seen_type_specifier = true;
10629     }
10630
10631   /* We built up the list in reverse order.  */
10632   return nreverse (type_specifier_seq);
10633 }
10634
10635 /* Parse a parameter-declaration-clause.
10636
10637    parameter-declaration-clause:
10638      parameter-declaration-list [opt] ... [opt]
10639      parameter-declaration-list , ...
10640
10641    Returns a representation for the parameter declarations.  Each node
10642    is a TREE_LIST.  (See cp_parser_parameter_declaration for the exact
10643    representation.)  If the parameter-declaration-clause ends with an
10644    ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10645    list.  A return value of NULL_TREE indicates a
10646    parameter-declaration-clause consisting only of an ellipsis.  */
10647
10648 static tree
10649 cp_parser_parameter_declaration_clause (parser)
10650      cp_parser *parser;
10651 {
10652   tree parameters;
10653   cp_token *token;
10654   bool ellipsis_p;
10655
10656   /* Peek at the next token.  */
10657   token = cp_lexer_peek_token (parser->lexer);
10658   /* Check for trivial parameter-declaration-clauses.  */
10659   if (token->type == CPP_ELLIPSIS)
10660     {
10661       /* Consume the `...' token.  */
10662       cp_lexer_consume_token (parser->lexer);
10663       return NULL_TREE;
10664     }
10665   else if (token->type == CPP_CLOSE_PAREN)
10666     /* There are no parameters.  */
10667     {
10668 #ifndef NO_IMPLICIT_EXTERN_C
10669       if (in_system_header && current_class_type == NULL
10670           && current_lang_name == lang_name_c)
10671         return NULL_TREE;
10672       else
10673 #endif
10674         return void_list_node;
10675     }
10676   /* Check for `(void)', too, which is a special case.  */
10677   else if (token->keyword == RID_VOID
10678            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
10679                == CPP_CLOSE_PAREN))
10680     {
10681       /* Consume the `void' token.  */
10682       cp_lexer_consume_token (parser->lexer);
10683       /* There are no parameters.  */
10684       return void_list_node;
10685     }
10686   
10687   /* Parse the parameter-declaration-list.  */
10688   parameters = cp_parser_parameter_declaration_list (parser);
10689   /* If a parse error occurred while parsing the
10690      parameter-declaration-list, then the entire
10691      parameter-declaration-clause is erroneous.  */
10692   if (parameters == error_mark_node)
10693     return error_mark_node;
10694
10695   /* Peek at the next token.  */
10696   token = cp_lexer_peek_token (parser->lexer);
10697   /* If it's a `,', the clause should terminate with an ellipsis.  */
10698   if (token->type == CPP_COMMA)
10699     {
10700       /* Consume the `,'.  */
10701       cp_lexer_consume_token (parser->lexer);
10702       /* Expect an ellipsis.  */
10703       ellipsis_p 
10704         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10705     }
10706   /* It might also be `...' if the optional trailing `,' was 
10707      omitted.  */
10708   else if (token->type == CPP_ELLIPSIS)
10709     {
10710       /* Consume the `...' token.  */
10711       cp_lexer_consume_token (parser->lexer);
10712       /* And remember that we saw it.  */
10713       ellipsis_p = true;
10714     }
10715   else
10716     ellipsis_p = false;
10717
10718   /* Finish the parameter list.  */
10719   return finish_parmlist (parameters, ellipsis_p);
10720 }
10721
10722 /* Parse a parameter-declaration-list.
10723
10724    parameter-declaration-list:
10725      parameter-declaration
10726      parameter-declaration-list , parameter-declaration
10727
10728    Returns a representation of the parameter-declaration-list, as for
10729    cp_parser_parameter_declaration_clause.  However, the
10730    `void_list_node' is never appended to the list.  */
10731
10732 static tree
10733 cp_parser_parameter_declaration_list (parser)
10734      cp_parser *parser;
10735 {
10736   tree parameters = NULL_TREE;
10737
10738   /* Look for more parameters.  */
10739   while (true)
10740     {
10741       tree parameter;
10742       /* Parse the parameter.  */
10743       parameter 
10744         = cp_parser_parameter_declaration (parser,
10745                                            /*greater_than_is_operator_p=*/true);
10746       /* If a parse error ocurred parsing the parameter declaration,
10747          then the entire parameter-declaration-list is erroneous.  */
10748       if (parameter == error_mark_node)
10749         {
10750           parameters = error_mark_node;
10751           break;
10752         }
10753       /* Add the new parameter to the list.  */
10754       TREE_CHAIN (parameter) = parameters;
10755       parameters = parameter;
10756
10757       /* Peek at the next token.  */
10758       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10759           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10760         /* The parameter-declaration-list is complete.  */
10761         break;
10762       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10763         {
10764           cp_token *token;
10765
10766           /* Peek at the next token.  */
10767           token = cp_lexer_peek_nth_token (parser->lexer, 2);
10768           /* If it's an ellipsis, then the list is complete.  */
10769           if (token->type == CPP_ELLIPSIS)
10770             break;
10771           /* Otherwise, there must be more parameters.  Consume the
10772              `,'.  */
10773           cp_lexer_consume_token (parser->lexer);
10774         }
10775       else
10776         {
10777           cp_parser_error (parser, "expected `,' or `...'");
10778           break;
10779         }
10780     }
10781
10782   /* We built up the list in reverse order; straighten it out now.  */
10783   return nreverse (parameters);
10784 }
10785
10786 /* Parse a parameter declaration.
10787
10788    parameter-declaration:
10789      decl-specifier-seq declarator
10790      decl-specifier-seq declarator = assignment-expression
10791      decl-specifier-seq abstract-declarator [opt]
10792      decl-specifier-seq abstract-declarator [opt] = assignment-expression
10793
10794    If GREATER_THAN_IS_OPERATOR_P is FALSE, then a non-nested `>' token
10795    encountered during the parsing of the assignment-expression is not
10796    interpreted as a greater-than operator.
10797
10798    Returns a TREE_LIST representing the parameter-declaration.  The
10799    TREE_VALUE is a representation of the decl-specifier-seq and
10800    declarator.  In particular, the TREE_VALUE will be a TREE_LIST
10801    whose TREE_PURPOSE represents the decl-specifier-seq and whose
10802    TREE_VALUE represents the declarator.  */
10803
10804 static tree
10805 cp_parser_parameter_declaration (parser, greater_than_is_operator_p)
10806      cp_parser *parser;
10807      bool greater_than_is_operator_p;
10808 {
10809   bool declares_class_or_enum;
10810   tree decl_specifiers;
10811   tree attributes;
10812   tree declarator;
10813   tree default_argument;
10814   tree parameter;
10815   cp_token *token;
10816   const char *saved_message;
10817
10818   /* Type definitions may not appear in parameter types.  */
10819   saved_message = parser->type_definition_forbidden_message;
10820   parser->type_definition_forbidden_message 
10821     = "types may not be defined in parameter types";
10822
10823   /* Parse the declaration-specifiers.  */
10824   decl_specifiers 
10825     = cp_parser_decl_specifier_seq (parser,
10826                                     CP_PARSER_FLAGS_NONE,
10827                                     &attributes,
10828                                     &declares_class_or_enum);
10829   /* If an error occurred, there's no reason to attempt to parse the
10830      rest of the declaration.  */
10831   if (cp_parser_error_occurred (parser))
10832     {
10833       parser->type_definition_forbidden_message = saved_message;
10834       return error_mark_node;
10835     }
10836
10837   /* Peek at the next token.  */
10838   token = cp_lexer_peek_token (parser->lexer);
10839   /* If the next token is a `)', `,', `=', `>', or `...', then there
10840      is no declarator.  */
10841   if (token->type == CPP_CLOSE_PAREN 
10842       || token->type == CPP_COMMA
10843       || token->type == CPP_EQ
10844       || token->type == CPP_ELLIPSIS
10845       || token->type == CPP_GREATER)
10846     declarator = NULL_TREE;
10847   /* Otherwise, there should be a declarator.  */
10848   else
10849     {
10850       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10851       parser->default_arg_ok_p = false;
10852   
10853       /* We don't know whether the declarator will be abstract or
10854          not.  So, first we try an ordinary declarator.  */
10855       cp_parser_parse_tentatively (parser);
10856       declarator = cp_parser_declarator (parser,
10857                                          /*abstract_p=*/false,
10858                                          /*ctor_dtor_or_conv_p=*/NULL);
10859       /* If that didn't work, look for an abstract declarator.  */
10860       if (!cp_parser_parse_definitely (parser))
10861         declarator = cp_parser_declarator (parser,
10862                                            /*abstract_p=*/true,
10863                                            /*ctor_dtor_or_conv_p=*/NULL);
10864       parser->default_arg_ok_p = saved_default_arg_ok_p;
10865       /* After the declarator, allow more attributes.  */
10866       attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10867     }
10868
10869   /* The restriction on definining new types applies only to the type
10870      of the parameter, not to the default argument.  */
10871   parser->type_definition_forbidden_message = saved_message;
10872
10873   /* If the next token is `=', then process a default argument.  */
10874   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10875     {
10876       bool saved_greater_than_is_operator_p;
10877       /* Consume the `='.  */
10878       cp_lexer_consume_token (parser->lexer);
10879
10880       /* If we are defining a class, then the tokens that make up the
10881          default argument must be saved and processed later.  */
10882       if (at_class_scope_p () && TYPE_BEING_DEFINED (current_class_type))
10883         {
10884           unsigned depth = 0;
10885
10886           /* Create a DEFAULT_ARG to represented the unparsed default
10887              argument.  */
10888           default_argument = make_node (DEFAULT_ARG);
10889           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10890
10891           /* Add tokens until we have processed the entire default
10892              argument.  */
10893           while (true)
10894             {
10895               bool done = false;
10896               cp_token *token;
10897
10898               /* Peek at the next token.  */
10899               token = cp_lexer_peek_token (parser->lexer);
10900               /* What we do depends on what token we have.  */
10901               switch (token->type)
10902                 {
10903                   /* In valid code, a default argument must be
10904                      immediately followed by a `,' `)', or `...'.  */
10905                 case CPP_COMMA:
10906                 case CPP_CLOSE_PAREN:
10907                 case CPP_ELLIPSIS:
10908                   /* If we run into a non-nested `;', `}', or `]',
10909                      then the code is invalid -- but the default
10910                      argument is certainly over.  */
10911                 case CPP_SEMICOLON:
10912                 case CPP_CLOSE_BRACE:
10913                 case CPP_CLOSE_SQUARE:
10914                   if (depth == 0)
10915                     done = true;
10916                   /* Update DEPTH, if necessary.  */
10917                   else if (token->type == CPP_CLOSE_PAREN
10918                            || token->type == CPP_CLOSE_BRACE
10919                            || token->type == CPP_CLOSE_SQUARE)
10920                     --depth;
10921                   break;
10922
10923                 case CPP_OPEN_PAREN:
10924                 case CPP_OPEN_SQUARE:
10925                 case CPP_OPEN_BRACE:
10926                   ++depth;
10927                   break;
10928
10929                 case CPP_GREATER:
10930                   /* If we see a non-nested `>', and `>' is not an
10931                      operator, then it marks the end of the default
10932                      argument.  */
10933                   if (!depth && !greater_than_is_operator_p)
10934                     done = true;
10935                   break;
10936
10937                   /* If we run out of tokens, issue an error message.  */
10938                 case CPP_EOF:
10939                   error ("file ends in default argument");
10940                   done = true;
10941                   break;
10942
10943                 case CPP_NAME:
10944                 case CPP_SCOPE:
10945                   /* In these cases, we should look for template-ids.
10946                      For example, if the default argument is 
10947                      `X<int, double>()', we need to do name lookup to
10948                      figure out whether or not `X' is a template; if
10949                      so, the `,' does not end the deault argument.
10950
10951                      That is not yet done.  */
10952                   break;
10953
10954                 default:
10955                   break;
10956                 }
10957
10958               /* If we've reached the end, stop.  */
10959               if (done)
10960                 break;
10961               
10962               /* Add the token to the token block.  */
10963               token = cp_lexer_consume_token (parser->lexer);
10964               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10965                                          token);
10966             }
10967         }
10968       /* Outside of a class definition, we can just parse the
10969          assignment-expression.  */
10970       else
10971         {
10972           bool saved_local_variables_forbidden_p;
10973
10974           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10975              set correctly.  */
10976           saved_greater_than_is_operator_p 
10977             = parser->greater_than_is_operator_p;
10978           parser->greater_than_is_operator_p = greater_than_is_operator_p;
10979           /* Local variable names (and the `this' keyword) may not
10980              appear in a default argument.  */
10981           saved_local_variables_forbidden_p 
10982             = parser->local_variables_forbidden_p;
10983           parser->local_variables_forbidden_p = true;
10984           /* Parse the assignment-expression.  */
10985           default_argument = cp_parser_assignment_expression (parser);
10986           /* Restore saved state.  */
10987           parser->greater_than_is_operator_p 
10988             = saved_greater_than_is_operator_p;
10989           parser->local_variables_forbidden_p 
10990             = saved_local_variables_forbidden_p; 
10991         }
10992       if (!parser->default_arg_ok_p)
10993         {
10994           pedwarn ("default arguments are only permitted on functions");
10995           if (flag_pedantic_errors)
10996             default_argument = NULL_TREE;
10997         }
10998     }
10999   else
11000     default_argument = NULL_TREE;
11001   
11002   /* Create the representation of the parameter.  */
11003   if (attributes)
11004     decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11005   parameter = build_tree_list (default_argument, 
11006                                build_tree_list (decl_specifiers,
11007                                                 declarator));
11008
11009   return parameter;
11010 }
11011
11012 /* Parse a function-definition.  
11013
11014    function-definition:
11015      decl-specifier-seq [opt] declarator ctor-initializer [opt]
11016        function-body 
11017      decl-specifier-seq [opt] declarator function-try-block  
11018
11019    GNU Extension:
11020
11021    function-definition:
11022      __extension__ function-definition 
11023
11024    Returns the FUNCTION_DECL for the function.  If FRIEND_P is
11025    non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
11026    be a `friend'.  */
11027
11028 static tree
11029 cp_parser_function_definition (parser, friend_p)
11030      cp_parser *parser;
11031      bool *friend_p;
11032 {
11033   tree decl_specifiers;
11034   tree attributes;
11035   tree declarator;
11036   tree fn;
11037   tree access_checks;
11038   cp_token *token;
11039   bool declares_class_or_enum;
11040   bool member_p;
11041   /* The saved value of the PEDANTIC flag.  */
11042   int saved_pedantic;
11043
11044   /* Any pending qualification must be cleared by our caller.  It is
11045      more robust to force the callers to clear PARSER->SCOPE than to
11046      do it here since if the qualification is in effect here, it might
11047      also end up in effect elsewhere that it is not intended.  */
11048   my_friendly_assert (!parser->scope, 20010821);
11049
11050   /* Handle `__extension__'.  */
11051   if (cp_parser_extension_opt (parser, &saved_pedantic))
11052     {
11053       /* Parse the function-definition.  */
11054       fn = cp_parser_function_definition (parser, friend_p);
11055       /* Restore the PEDANTIC flag.  */
11056       pedantic = saved_pedantic;
11057
11058       return fn;
11059     }
11060
11061   /* Check to see if this definition appears in a class-specifier.  */
11062   member_p = (at_class_scope_p () 
11063               && TYPE_BEING_DEFINED (current_class_type));
11064   /* Defer access checks in the decl-specifier-seq until we know what
11065      function is being defined.  There is no need to do this for the
11066      definition of member functions; we cannot be defining a member
11067      from another class.  */
11068   if (!member_p)
11069     cp_parser_start_deferring_access_checks (parser);
11070   /* Parse the decl-specifier-seq.  */
11071   decl_specifiers 
11072     = cp_parser_decl_specifier_seq (parser,
11073                                     CP_PARSER_FLAGS_OPTIONAL,
11074                                     &attributes,
11075                                     &declares_class_or_enum);
11076   /* Figure out whether this declaration is a `friend'.  */
11077   if (friend_p)
11078     *friend_p = cp_parser_friend_p (decl_specifiers);
11079
11080   /* Parse the declarator.  */
11081   declarator = cp_parser_declarator (parser, 
11082                                      /*abstract_p=*/false,
11083                                      /*ctor_dtor_or_conv_p=*/NULL);
11084
11085   /* Gather up any access checks that occurred.  */
11086   if (!member_p)
11087     access_checks = cp_parser_stop_deferring_access_checks (parser);
11088   else
11089     access_checks = NULL_TREE;
11090
11091   /* If something has already gone wrong, we may as well stop now.  */
11092   if (declarator == error_mark_node)
11093     {
11094       /* Skip to the end of the function, or if this wasn't anything
11095          like a function-definition, to a `;' in the hopes of finding
11096          a sensible place from which to continue parsing.  */
11097       cp_parser_skip_to_end_of_block_or_statement (parser);
11098       return error_mark_node;
11099     }
11100
11101   /* The next character should be a `{' (for a simple function
11102      definition), a `:' (for a ctor-initializer), or `try' (for a
11103      function-try block).  */
11104   token = cp_lexer_peek_token (parser->lexer);
11105   if (!cp_parser_token_starts_function_definition_p (token))
11106     {
11107       /* Issue the error-message.  */
11108       cp_parser_error (parser, "expected function-definition");
11109       /* Skip to the next `;'.  */
11110       cp_parser_skip_to_end_of_block_or_statement (parser);
11111
11112       return error_mark_node;
11113     }
11114
11115   /* If we are in a class scope, then we must handle
11116      function-definitions specially.  In particular, we save away the
11117      tokens that make up the function body, and parse them again
11118      later, in order to handle code like:
11119
11120        struct S {
11121          int f () { return i; }
11122          int i;
11123        }; 
11124  
11125      Here, we cannot parse the body of `f' until after we have seen
11126      the declaration of `i'.  */
11127   if (member_p)
11128     {
11129       cp_token_cache *cache;
11130
11131       /* Create the function-declaration.  */
11132       fn = start_method (decl_specifiers, declarator, attributes);
11133       /* If something went badly wrong, bail out now.  */
11134       if (fn == error_mark_node)
11135         {
11136           /* If there's a function-body, skip it.  */
11137           if (cp_parser_token_starts_function_definition_p 
11138               (cp_lexer_peek_token (parser->lexer)))
11139             cp_parser_skip_to_end_of_block_or_statement (parser);
11140           return error_mark_node;
11141         }
11142
11143       /* Create a token cache.  */
11144       cache = cp_token_cache_new ();
11145       /* Save away the tokens that make up the body of the 
11146          function.  */
11147       cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11148       /* Handle function try blocks.  */
11149       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11150         cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11151
11152       /* Save away the inline definition; we will process it when the
11153          class is complete.  */
11154       DECL_PENDING_INLINE_INFO (fn) = cache;
11155       DECL_PENDING_INLINE_P (fn) = 1;
11156
11157       /* We're done with the inline definition.  */
11158       finish_method (fn);
11159
11160       /* Add FN to the queue of functions to be parsed later.  */
11161       TREE_VALUE (parser->unparsed_functions_queues)
11162         = tree_cons (current_class_type, fn, 
11163                      TREE_VALUE (parser->unparsed_functions_queues));
11164
11165       return fn;
11166     }
11167
11168   /* Check that the number of template-parameter-lists is OK.  */
11169   if (!cp_parser_check_declarator_template_parameters (parser, 
11170                                                        declarator))
11171     {
11172       cp_parser_skip_to_end_of_block_or_statement (parser);
11173       return error_mark_node;
11174     }
11175
11176   return (cp_parser_function_definition_from_specifiers_and_declarator
11177           (parser, decl_specifiers, attributes, declarator, access_checks));
11178 }
11179
11180 /* Parse a function-body.
11181
11182    function-body:
11183      compound_statement  */
11184
11185 static void
11186 cp_parser_function_body (cp_parser *parser)
11187 {
11188   cp_parser_compound_statement (parser);
11189 }
11190
11191 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11192    true if a ctor-initializer was present.  */
11193
11194 static bool
11195 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11196 {
11197   tree body;
11198   bool ctor_initializer_p;
11199
11200   /* Begin the function body.  */
11201   body = begin_function_body ();
11202   /* Parse the optional ctor-initializer.  */
11203   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11204   /* Parse the function-body.  */
11205   cp_parser_function_body (parser);
11206   /* Finish the function body.  */
11207   finish_function_body (body);
11208
11209   return ctor_initializer_p;
11210 }
11211
11212 /* Parse an initializer.
11213
11214    initializer:
11215      = initializer-clause
11216      ( expression-list )  
11217
11218    Returns a expression representing the initializer.  If no
11219    initializer is present, NULL_TREE is returned.  
11220
11221    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11222    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11223    set to FALSE if there is no initializer present.  */
11224
11225 static tree
11226 cp_parser_initializer (parser, is_parenthesized_init)
11227      cp_parser *parser;
11228      bool *is_parenthesized_init;
11229 {
11230   cp_token *token;
11231   tree init;
11232
11233   /* Peek at the next token.  */
11234   token = cp_lexer_peek_token (parser->lexer);
11235
11236   /* Let our caller know whether or not this initializer was
11237      parenthesized.  */
11238   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11239
11240   if (token->type == CPP_EQ)
11241     {
11242       /* Consume the `='.  */
11243       cp_lexer_consume_token (parser->lexer);
11244       /* Parse the initializer-clause.  */
11245       init = cp_parser_initializer_clause (parser);
11246     }
11247   else if (token->type == CPP_OPEN_PAREN)
11248     {
11249       /* Consume the `('.  */
11250       cp_lexer_consume_token (parser->lexer);
11251       /* Parse the expression-list.  */
11252       init = cp_parser_expression_list (parser);
11253       /* Consume the `)' token.  */
11254       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11255         cp_parser_skip_to_closing_parenthesis (parser);
11256     }
11257   else
11258     {
11259       /* Anything else is an error.  */
11260       cp_parser_error (parser, "expected initializer");
11261       init = error_mark_node;
11262     }
11263
11264   return init;
11265 }
11266
11267 /* Parse an initializer-clause.  
11268
11269    initializer-clause:
11270      assignment-expression
11271      { initializer-list , [opt] }
11272      { }
11273
11274    Returns an expression representing the initializer.  
11275
11276    If the `assignment-expression' production is used the value
11277    returned is simply a reprsentation for the expression.  
11278
11279    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11280    the elements of the initializer-list (or NULL_TREE, if the last
11281    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11282    NULL_TREE.  There is no way to detect whether or not the optional
11283    trailing `,' was provided.  */
11284
11285 static tree
11286 cp_parser_initializer_clause (parser)
11287      cp_parser *parser;
11288 {
11289   tree initializer;
11290
11291   /* If it is not a `{', then we are looking at an
11292      assignment-expression.  */
11293   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11294     initializer = cp_parser_assignment_expression (parser);
11295   else
11296     {
11297       /* Consume the `{' token.  */
11298       cp_lexer_consume_token (parser->lexer);
11299       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11300       initializer = make_node (CONSTRUCTOR);
11301       /* Mark it with TREE_HAS_CONSTRUCTOR.  This should not be
11302          necessary, but check_initializer depends upon it, for 
11303          now.  */
11304       TREE_HAS_CONSTRUCTOR (initializer) = 1;
11305       /* If it's not a `}', then there is a non-trivial initializer.  */
11306       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11307         {
11308           /* Parse the initializer list.  */
11309           CONSTRUCTOR_ELTS (initializer)
11310             = cp_parser_initializer_list (parser);
11311           /* A trailing `,' token is allowed.  */
11312           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11313             cp_lexer_consume_token (parser->lexer);
11314         }
11315
11316       /* Now, there should be a trailing `}'.  */
11317       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11318     }
11319
11320   return initializer;
11321 }
11322
11323 /* Parse an initializer-list.
11324
11325    initializer-list:
11326      initializer-clause
11327      initializer-list , initializer-clause
11328
11329    GNU Extension:
11330    
11331    initializer-list:
11332      identifier : initializer-clause
11333      initializer-list, identifier : initializer-clause
11334
11335    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
11336    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
11337    IDENTIFIER_NODE naming the field to initialize.   */
11338
11339 static tree
11340 cp_parser_initializer_list (parser)
11341      cp_parser *parser;
11342 {
11343   tree initializers = NULL_TREE;
11344
11345   /* Parse the rest of the list.  */
11346   while (true)
11347     {
11348       cp_token *token;
11349       tree identifier;
11350       tree initializer;
11351       
11352       /* If the next token is an identifier and the following one is a
11353          colon, we are looking at the GNU designated-initializer
11354          syntax.  */
11355       if (cp_parser_allow_gnu_extensions_p (parser)
11356           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11357           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11358         {
11359           /* Consume the identifier.  */
11360           identifier = cp_lexer_consume_token (parser->lexer)->value;
11361           /* Consume the `:'.  */
11362           cp_lexer_consume_token (parser->lexer);
11363         }
11364       else
11365         identifier = NULL_TREE;
11366
11367       /* Parse the initializer.  */
11368       initializer = cp_parser_initializer_clause (parser);
11369
11370       /* Add it to the list.  */
11371       initializers = tree_cons (identifier, initializer, initializers);
11372
11373       /* If the next token is not a comma, we have reached the end of
11374          the list.  */
11375       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11376         break;
11377
11378       /* Peek at the next token.  */
11379       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11380       /* If the next token is a `}', then we're still done.  An
11381          initializer-clause can have a trailing `,' after the
11382          initializer-list and before the closing `}'.  */
11383       if (token->type == CPP_CLOSE_BRACE)
11384         break;
11385
11386       /* Consume the `,' token.  */
11387       cp_lexer_consume_token (parser->lexer);
11388     }
11389
11390   /* The initializers were built up in reverse order, so we need to
11391      reverse them now.  */
11392   return nreverse (initializers);
11393 }
11394
11395 /* Classes [gram.class] */
11396
11397 /* Parse a class-name.
11398
11399    class-name:
11400      identifier
11401      template-id
11402
11403    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11404    to indicate that names looked up in dependent types should be
11405    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
11406    keyword has been used to indicate that the name that appears next
11407    is a template.  TYPE_P is true iff the next name should be treated
11408    as class-name, even if it is declared to be some other kind of name
11409    as well.  The accessibility of the class-name is checked iff
11410    CHECK_ACCESS_P is true.  If CHECK_DEPENDENCY_P is FALSE, names are
11411    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
11412    is the class being defined in a class-head.
11413
11414    Returns the TYPE_DECL representing the class.  */
11415
11416 static tree
11417 cp_parser_class_name (cp_parser *parser, 
11418                       bool typename_keyword_p, 
11419                       bool template_keyword_p, 
11420                       bool type_p,
11421                       bool check_access_p,
11422                       bool check_dependency_p,
11423                       bool class_head_p)
11424 {
11425   tree decl;
11426   tree scope;
11427   bool typename_p;
11428   cp_token *token;
11429
11430   /* All class-names start with an identifier.  */
11431   token = cp_lexer_peek_token (parser->lexer);
11432   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11433     {
11434       cp_parser_error (parser, "expected class-name");
11435       return error_mark_node;
11436     }
11437     
11438   /* PARSER->SCOPE can be cleared when parsing the template-arguments
11439      to a template-id, so we save it here.  */
11440   scope = parser->scope;
11441   /* Any name names a type if we're following the `typename' keyword
11442      in a qualified name where the enclosing scope is type-dependent.  */
11443   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11444                 && cp_parser_dependent_type_p (scope));
11445   /* Handle the common case (an identifier, but not a template-id)
11446      efficiently.  */
11447   if (token->type == CPP_NAME 
11448       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11449     {
11450       tree identifier;
11451
11452       /* Look for the identifier.  */
11453       identifier = cp_parser_identifier (parser);
11454       /* If the next token isn't an identifier, we are certainly not
11455          looking at a class-name.  */
11456       if (identifier == error_mark_node)
11457         decl = error_mark_node;
11458       /* If we know this is a type-name, there's no need to look it
11459          up.  */
11460       else if (typename_p)
11461         decl = identifier;
11462       else
11463         {
11464           /* If the next token is a `::', then the name must be a type
11465              name.
11466
11467              [basic.lookup.qual]
11468
11469              During the lookup for a name preceding the :: scope
11470              resolution operator, object, function, and enumerator
11471              names are ignored.  */
11472           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11473             type_p = true;
11474           /* Look up the name.  */
11475           decl = cp_parser_lookup_name (parser, identifier, 
11476                                         check_access_p,
11477                                         type_p,
11478                                         /*is_namespace=*/false,
11479                                         check_dependency_p);
11480         }
11481     }
11482   else
11483     {
11484       /* Try a template-id.  */
11485       decl = cp_parser_template_id (parser, template_keyword_p,
11486                                     check_dependency_p);
11487       if (decl == error_mark_node)
11488         return error_mark_node;
11489     }
11490
11491   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11492
11493   /* If this is a typename, create a TYPENAME_TYPE.  */
11494   if (typename_p && decl != error_mark_node)
11495     decl = TYPE_NAME (make_typename_type (scope, decl,
11496                                           /*complain=*/1));
11497
11498   /* Check to see that it is really the name of a class.  */
11499   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 
11500       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11501       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11502     /* Situations like this:
11503
11504          template <typename T> struct A {
11505            typename T::template X<int>::I i; 
11506          };
11507
11508        are problematic.  Is `T::template X<int>' a class-name?  The
11509        standard does not seem to be definitive, but there is no other
11510        valid interpretation of the following `::'.  Therefore, those
11511        names are considered class-names.  */
11512     decl = TYPE_NAME (make_typename_type (scope, decl, 
11513                                           tf_error | tf_parsing));
11514   else if (decl == error_mark_node
11515            || TREE_CODE (decl) != TYPE_DECL
11516            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11517     {
11518       cp_parser_error (parser, "expected class-name");
11519       return error_mark_node;
11520     }
11521
11522   return decl;
11523 }
11524
11525 /* Parse a class-specifier.
11526
11527    class-specifier:
11528      class-head { member-specification [opt] }
11529
11530    Returns the TREE_TYPE representing the class.  */
11531
11532 static tree
11533 cp_parser_class_specifier (parser)
11534      cp_parser *parser;
11535 {
11536   cp_token *token;
11537   tree type;
11538   tree attributes = NULL_TREE;
11539   int has_trailing_semicolon;
11540   bool nested_name_specifier_p;
11541   bool deferring_access_checks_p;
11542   tree saved_access_checks;
11543   unsigned saved_num_template_parameter_lists;
11544
11545   /* Parse the class-head.  */
11546   type = cp_parser_class_head (parser,
11547                                &nested_name_specifier_p,
11548                                &deferring_access_checks_p,
11549                                &saved_access_checks);
11550   /* If the class-head was a semantic disaster, skip the entire body
11551      of the class.  */
11552   if (!type)
11553     {
11554       cp_parser_skip_to_end_of_block_or_statement (parser);
11555       return error_mark_node;
11556     }
11557   /* Look for the `{'.  */
11558   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11559     return error_mark_node;
11560   /* Issue an error message if type-definitions are forbidden here.  */
11561   cp_parser_check_type_definition (parser);
11562   /* Remember that we are defining one more class.  */
11563   ++parser->num_classes_being_defined;
11564   /* Inside the class, surrounding template-parameter-lists do not
11565      apply.  */
11566   saved_num_template_parameter_lists 
11567     = parser->num_template_parameter_lists; 
11568   parser->num_template_parameter_lists = 0;
11569   /* Start the class.  */
11570   type = begin_class_definition (type);
11571   if (type == error_mark_node)
11572     /* If the type is erroneous, skip the entire body of the class. */
11573     cp_parser_skip_to_closing_brace (parser);
11574   else
11575     /* Parse the member-specification.  */
11576     cp_parser_member_specification_opt (parser);
11577   /* Look for the trailing `}'.  */
11578   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11579   /* We get better error messages by noticing a common problem: a
11580      missing trailing `;'.  */
11581   token = cp_lexer_peek_token (parser->lexer);
11582   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11583   /* Look for attributes to apply to this class.  */
11584   if (cp_parser_allow_gnu_extensions_p (parser))
11585     attributes = cp_parser_attributes_opt (parser);
11586   /* Finish the class definition.  */
11587   type = finish_class_definition (type, 
11588                                   attributes,
11589                                   has_trailing_semicolon,
11590                                   nested_name_specifier_p);
11591   /* If this class is not itself within the scope of another class,
11592      then we need to parse the bodies of all of the queued function
11593      definitions.  Note that the queued functions defined in a class
11594      are not always processed immediately following the
11595      class-specifier for that class.  Consider:
11596
11597        struct A {
11598          struct B { void f() { sizeof (A); } };
11599        };
11600
11601      If `f' were processed before the processing of `A' were
11602      completed, there would be no way to compute the size of `A'.
11603      Note that the nesting we are interested in here is lexical --
11604      not the semantic nesting given by TYPE_CONTEXT.  In particular,
11605      for:
11606
11607        struct A { struct B; };
11608        struct A::B { void f() { } };
11609
11610      there is no need to delay the parsing of `A::B::f'.  */
11611   if (--parser->num_classes_being_defined == 0) 
11612     {
11613       tree last_scope = NULL_TREE;
11614
11615       /* Process non FUNCTION_DECL related DEFAULT_ARGs.  */
11616       for (parser->default_arg_types = nreverse (parser->default_arg_types);
11617            parser->default_arg_types;
11618            parser->default_arg_types = TREE_CHAIN (parser->default_arg_types))
11619         cp_parser_late_parsing_default_args
11620           (parser, TREE_PURPOSE (parser->default_arg_types), NULL_TREE);
11621       
11622       /* Reverse the queue, so that we process it in the order the
11623          functions were declared.  */
11624       TREE_VALUE (parser->unparsed_functions_queues)
11625         = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11626       /* Loop through all of the functions.  */
11627       while (TREE_VALUE (parser->unparsed_functions_queues))
11628
11629         {
11630           tree fn;
11631           tree fn_scope;
11632           tree queue_entry;
11633
11634           /* Figure out which function we need to process.  */
11635           queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11636           fn_scope = TREE_PURPOSE (queue_entry);
11637           fn = TREE_VALUE (queue_entry);
11638
11639           /* Parse the function.  */
11640           cp_parser_late_parsing_for_member (parser, fn);
11641
11642           TREE_VALUE (parser->unparsed_functions_queues)
11643             = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11644         }
11645
11646       /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11647          more time than we have popped, so me must pop here.  */
11648       if (last_scope)
11649         pop_scope (last_scope);
11650     }
11651
11652   /* Put back any saved access checks.  */
11653   if (deferring_access_checks_p)
11654     {
11655       cp_parser_start_deferring_access_checks (parser);
11656       parser->context->deferred_access_checks = saved_access_checks;
11657     }
11658
11659   /* Restore the count of active template-parameter-lists.  */
11660   parser->num_template_parameter_lists
11661     = saved_num_template_parameter_lists;
11662
11663   return type;
11664 }
11665
11666 /* Parse a class-head.
11667
11668    class-head:
11669      class-key identifier [opt] base-clause [opt]
11670      class-key nested-name-specifier identifier base-clause [opt]
11671      class-key nested-name-specifier [opt] template-id 
11672        base-clause [opt]  
11673
11674    GNU Extensions:
11675      class-key attributes identifier [opt] base-clause [opt]
11676      class-key attributes nested-name-specifier identifier base-clause [opt]
11677      class-key attributes nested-name-specifier [opt] template-id 
11678        base-clause [opt]  
11679
11680    Returns the TYPE of the indicated class.  Sets
11681    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11682    involving a nested-name-specifier was used, and FALSE otherwise.
11683    Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11684    access checks before this class-head.  In that case,
11685    *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11686    checks.  
11687
11688    Returns NULL_TREE if the class-head is syntactically valid, but
11689    semantically invalid in a way that means we should skip the entire
11690    body of the class.  */
11691
11692 static tree
11693 cp_parser_class_head (parser, 
11694                       nested_name_specifier_p,
11695                       deferring_access_checks_p,
11696                       saved_access_checks)
11697      cp_parser *parser;
11698      bool *nested_name_specifier_p;
11699      bool *deferring_access_checks_p;
11700      tree *saved_access_checks;
11701 {
11702   cp_token *token;
11703   tree nested_name_specifier;
11704   enum tag_types class_key;
11705   tree id = NULL_TREE;
11706   tree type = NULL_TREE;
11707   tree attributes;
11708   bool template_id_p = false;
11709   bool qualified_p = false;
11710   bool invalid_nested_name_p = false;
11711   unsigned num_templates;
11712
11713   /* Assume no nested-name-specifier will be present.  */
11714   *nested_name_specifier_p = false;
11715   /* Assume no template parameter lists will be used in defining the
11716      type.  */
11717   num_templates = 0;
11718
11719   /* Look for the class-key.  */
11720   class_key = cp_parser_class_key (parser);
11721   if (class_key == none_type)
11722     return error_mark_node;
11723
11724   /* Parse the attributes.  */
11725   attributes = cp_parser_attributes_opt (parser);
11726
11727   /* If the next token is `::', that is invalid -- but sometimes
11728      people do try to write:
11729
11730        struct ::S {};  
11731
11732      Handle this gracefully by accepting the extra qualifier, and then
11733      issuing an error about it later if this really is a
11734      class-header.  If it turns out just to be an elaborated type
11735      specifier, remain silent.  */
11736   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11737     qualified_p = true;
11738
11739   /* Determine the name of the class.  Begin by looking for an
11740      optional nested-name-specifier.  */
11741   nested_name_specifier 
11742     = cp_parser_nested_name_specifier_opt (parser,
11743                                            /*typename_keyword_p=*/false,
11744                                            /*check_dependency_p=*/true,
11745                                            /*type_p=*/false);
11746   /* If there was a nested-name-specifier, then there *must* be an
11747      identifier.  */
11748   if (nested_name_specifier)
11749     {
11750       /* Although the grammar says `identifier', it really means
11751          `class-name' or `template-name'.  You are only allowed to
11752          define a class that has already been declared with this
11753          syntax.  
11754
11755          The proposed resolution for Core Issue 180 says that whever
11756          you see `class T::X' you should treat `X' as a type-name.
11757          
11758          It is OK to define an inaccessible class; for example:
11759          
11760            class A { class B; };
11761            class A::B {};
11762          
11763          So, we ask cp_parser_class_name not to check accessibility.  
11764
11765          We do not know if we will see a class-name, or a
11766          template-name.  We look for a class-name first, in case the
11767          class-name is a template-id; if we looked for the
11768          template-name first we would stop after the template-name.  */
11769       cp_parser_parse_tentatively (parser);
11770       type = cp_parser_class_name (parser,
11771                                    /*typename_keyword_p=*/false,
11772                                    /*template_keyword_p=*/false,
11773                                    /*type_p=*/true,
11774                                    /*check_access_p=*/false,
11775                                    /*check_dependency_p=*/false,
11776                                    /*class_head_p=*/true);
11777       /* If that didn't work, ignore the nested-name-specifier.  */
11778       if (!cp_parser_parse_definitely (parser))
11779         {
11780           invalid_nested_name_p = true;
11781           id = cp_parser_identifier (parser);
11782           if (id == error_mark_node)
11783             id = NULL_TREE;
11784         }
11785       /* If we could not find a corresponding TYPE, treat this
11786          declaration like an unqualified declaration.  */
11787       if (type == error_mark_node)
11788         nested_name_specifier = NULL_TREE;
11789       /* Otherwise, count the number of templates used in TYPE and its
11790          containing scopes.  */
11791       else 
11792         {
11793           tree scope;
11794
11795           for (scope = TREE_TYPE (type); 
11796                scope && TREE_CODE (scope) != NAMESPACE_DECL;
11797                scope = (TYPE_P (scope) 
11798                         ? TYPE_CONTEXT (scope)
11799                         : DECL_CONTEXT (scope))) 
11800             if (TYPE_P (scope) 
11801                 && CLASS_TYPE_P (scope)
11802                 && CLASSTYPE_TEMPLATE_INFO (scope)
11803                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
11804               ++num_templates;
11805         }
11806     }
11807   /* Otherwise, the identifier is optional.  */
11808   else
11809     {
11810       /* We don't know whether what comes next is a template-id,
11811          an identifier, or nothing at all.  */
11812       cp_parser_parse_tentatively (parser);
11813       /* Check for a template-id.  */
11814       id = cp_parser_template_id (parser, 
11815                                   /*template_keyword_p=*/false,
11816                                   /*check_dependency_p=*/true);
11817       /* If that didn't work, it could still be an identifier.  */
11818       if (!cp_parser_parse_definitely (parser))
11819         {
11820           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11821             id = cp_parser_identifier (parser);
11822           else
11823             id = NULL_TREE;
11824         }
11825       else
11826         {
11827           template_id_p = true;
11828           ++num_templates;
11829         }
11830     }
11831
11832   /* If it's not a `:' or a `{' then we can't really be looking at a
11833      class-head, since a class-head only appears as part of a
11834      class-specifier.  We have to detect this situation before calling
11835      xref_tag, since that has irreversible side-effects.  */
11836   if (!cp_parser_next_token_starts_class_definition_p (parser))
11837     {
11838       cp_parser_error (parser, "expected `{' or `:'");
11839       return error_mark_node;
11840     }
11841
11842   /* At this point, we're going ahead with the class-specifier, even
11843      if some other problem occurs.  */
11844   cp_parser_commit_to_tentative_parse (parser);
11845   /* Issue the error about the overly-qualified name now.  */
11846   if (qualified_p)
11847     cp_parser_error (parser,
11848                      "global qualification of class name is invalid");
11849   else if (invalid_nested_name_p)
11850     cp_parser_error (parser,
11851                      "qualified name does not name a class");
11852   /* Make sure that the right number of template parameters were
11853      present.  */
11854   if (!cp_parser_check_template_parameters (parser, num_templates))
11855     /* If something went wrong, there is no point in even trying to
11856        process the class-definition.  */
11857     return NULL_TREE;
11858
11859   /* We do not need to defer access checks for entities declared
11860      within the class.  But, we do need to save any access checks that
11861      are currently deferred and restore them later, in case we are in
11862      the middle of something else.  */
11863   *deferring_access_checks_p = parser->context->deferring_access_checks_p;
11864   if (*deferring_access_checks_p)
11865     *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
11866
11867   /* Look up the type.  */
11868   if (template_id_p)
11869     {
11870       type = TREE_TYPE (id);
11871       maybe_process_partial_specialization (type);
11872     }
11873   else if (!nested_name_specifier)
11874     {
11875       /* If the class was unnamed, create a dummy name.  */
11876       if (!id)
11877         id = make_anon_name ();
11878       type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11879     }
11880   else
11881     {
11882       bool new_type_p;
11883       tree class_type;
11884
11885       /* Given:
11886
11887             template <typename T> struct S { struct T };
11888             template <typename T> struct S::T { };
11889
11890          we will get a TYPENAME_TYPE when processing the definition of
11891          `S::T'.  We need to resolve it to the actual type before we
11892          try to define it.  */
11893       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11894         {
11895           type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
11896           if (type != error_mark_node)
11897             type = TYPE_NAME (type);
11898         }
11899
11900       maybe_process_partial_specialization (TREE_TYPE (type));
11901       class_type = current_class_type;
11902       type = TREE_TYPE (handle_class_head (class_key, 
11903                                            nested_name_specifier,
11904                                            type,
11905                                            attributes,
11906                                            /*defn_p=*/true,
11907                                            &new_type_p));
11908       if (type != error_mark_node)
11909         {
11910           if (!class_type && TYPE_CONTEXT (type))
11911             *nested_name_specifier_p = true;
11912           else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11913                                                class_type))
11914             *nested_name_specifier_p = true;
11915         }
11916     }
11917   /* Indicate whether this class was declared as a `class' or as a
11918      `struct'.  */
11919   if (TREE_CODE (type) == RECORD_TYPE)
11920     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11921   cp_parser_check_class_key (class_key, type);
11922
11923   /* Enter the scope containing the class; the names of base classes
11924      should be looked up in that context.  For example, given:
11925
11926        struct A { struct B {}; struct C; };
11927        struct A::C : B {};
11928
11929      is valid.  */
11930   if (nested_name_specifier)
11931     push_scope (nested_name_specifier);
11932   /* Now, look for the base-clause.  */
11933   token = cp_lexer_peek_token (parser->lexer);
11934   if (token->type == CPP_COLON)
11935     {
11936       tree bases;
11937
11938       /* Get the list of base-classes.  */
11939       bases = cp_parser_base_clause (parser);
11940       /* Process them.  */
11941       xref_basetypes (type, bases);
11942     }
11943   /* Leave the scope given by the nested-name-specifier.  We will
11944      enter the class scope itself while processing the members.  */
11945   if (nested_name_specifier)
11946     pop_scope (nested_name_specifier);
11947
11948   return type;
11949 }
11950
11951 /* Parse a class-key.
11952
11953    class-key:
11954      class
11955      struct
11956      union
11957
11958    Returns the kind of class-key specified, or none_type to indicate
11959    error.  */
11960
11961 static enum tag_types
11962 cp_parser_class_key (parser)
11963      cp_parser *parser;
11964 {
11965   cp_token *token;
11966   enum tag_types tag_type;
11967
11968   /* Look for the class-key.  */
11969   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11970   if (!token)
11971     return none_type;
11972
11973   /* Check to see if the TOKEN is a class-key.  */
11974   tag_type = cp_parser_token_is_class_key (token);
11975   if (!tag_type)
11976     cp_parser_error (parser, "expected class-key");
11977   return tag_type;
11978 }
11979
11980 /* Parse an (optional) member-specification.
11981
11982    member-specification:
11983      member-declaration member-specification [opt]
11984      access-specifier : member-specification [opt]  */
11985
11986 static void
11987 cp_parser_member_specification_opt (parser)
11988      cp_parser *parser;
11989 {
11990   while (true)
11991     {
11992       cp_token *token;
11993       enum rid keyword;
11994
11995       /* Peek at the next token.  */
11996       token = cp_lexer_peek_token (parser->lexer);
11997       /* If it's a `}', or EOF then we've seen all the members.  */
11998       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11999         break;
12000
12001       /* See if this token is a keyword.  */
12002       keyword = token->keyword;
12003       switch (keyword)
12004         {
12005         case RID_PUBLIC:
12006         case RID_PROTECTED:
12007         case RID_PRIVATE:
12008           /* Consume the access-specifier.  */
12009           cp_lexer_consume_token (parser->lexer);
12010           /* Remember which access-specifier is active.  */
12011           current_access_specifier = token->value;
12012           /* Look for the `:'.  */
12013           cp_parser_require (parser, CPP_COLON, "`:'");
12014           break;
12015
12016         default:
12017           /* Otherwise, the next construction must be a
12018              member-declaration.  */
12019           cp_parser_member_declaration (parser);
12020           reset_type_access_control ();
12021         }
12022     }
12023 }
12024
12025 /* Parse a member-declaration.  
12026
12027    member-declaration:
12028      decl-specifier-seq [opt] member-declarator-list [opt] ;
12029      function-definition ; [opt]
12030      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12031      using-declaration
12032      template-declaration 
12033
12034    member-declarator-list:
12035      member-declarator
12036      member-declarator-list , member-declarator
12037
12038    member-declarator:
12039      declarator pure-specifier [opt] 
12040      declarator constant-initializer [opt]
12041      identifier [opt] : constant-expression 
12042
12043    GNU Extensions:
12044
12045    member-declaration:
12046      __extension__ member-declaration
12047
12048    member-declarator:
12049      declarator attributes [opt] pure-specifier [opt]
12050      declarator attributes [opt] constant-initializer [opt]
12051      identifier [opt] attributes [opt] : constant-expression  */
12052
12053 static void
12054 cp_parser_member_declaration (parser)
12055      cp_parser *parser;
12056 {
12057   tree decl_specifiers;
12058   tree prefix_attributes;
12059   tree decl;
12060   bool declares_class_or_enum;
12061   bool friend_p;
12062   cp_token *token;
12063   int saved_pedantic;
12064
12065   /* Check for the `__extension__' keyword.  */
12066   if (cp_parser_extension_opt (parser, &saved_pedantic))
12067     {
12068       /* Recurse.  */
12069       cp_parser_member_declaration (parser);
12070       /* Restore the old value of the PEDANTIC flag.  */
12071       pedantic = saved_pedantic;
12072
12073       return;
12074     }
12075
12076   /* Check for a template-declaration.  */
12077   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12078     {
12079       /* Parse the template-declaration.  */
12080       cp_parser_template_declaration (parser, /*member_p=*/true);
12081
12082       return;
12083     }
12084
12085   /* Check for a using-declaration.  */
12086   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12087     {
12088       /* Parse the using-declaration.  */
12089       cp_parser_using_declaration (parser);
12090
12091       return;
12092     }
12093   
12094   /* We can't tell whether we're looking at a declaration or a
12095      function-definition.  */
12096   cp_parser_parse_tentatively (parser);
12097
12098   /* Parse the decl-specifier-seq.  */
12099   decl_specifiers 
12100     = cp_parser_decl_specifier_seq (parser,
12101                                     CP_PARSER_FLAGS_OPTIONAL,
12102                                     &prefix_attributes,
12103                                     &declares_class_or_enum);
12104   /* If there is no declarator, then the decl-specifier-seq should
12105      specify a type.  */
12106   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12107     {
12108       /* If there was no decl-specifier-seq, and the next token is a
12109          `;', then we have something like:
12110
12111            struct S { ; };
12112
12113          [class.mem]
12114
12115          Each member-declaration shall declare at least one member
12116          name of the class.  */
12117       if (!decl_specifiers)
12118         {
12119           if (pedantic)
12120             pedwarn ("extra semicolon");
12121         }
12122       else 
12123         {
12124           tree type;
12125           
12126           /* See if this declaration is a friend.  */
12127           friend_p = cp_parser_friend_p (decl_specifiers);
12128           /* If there were decl-specifiers, check to see if there was
12129              a class-declaration.  */
12130           type = check_tag_decl (decl_specifiers);
12131           /* Nested classes have already been added to the class, but
12132              a `friend' needs to be explicitly registered.  */
12133           if (friend_p)
12134             {
12135               /* If the `friend' keyword was present, the friend must
12136                  be introduced with a class-key.  */
12137                if (!declares_class_or_enum)
12138                  error ("a class-key must be used when declaring a friend");
12139                /* In this case:
12140
12141                     template <typename T> struct A { 
12142                       friend struct A<T>::B; 
12143                     };
12144  
12145                   A<T>::B will be represented by a TYPENAME_TYPE, and
12146                   therefore not recognized by check_tag_decl.  */
12147                if (!type)
12148                  {
12149                    tree specifier;
12150
12151                    for (specifier = decl_specifiers; 
12152                         specifier;
12153                         specifier = TREE_CHAIN (specifier))
12154                      {
12155                        tree s = TREE_VALUE (specifier);
12156
12157                        if (TREE_CODE (s) == IDENTIFIER_NODE
12158                            && IDENTIFIER_GLOBAL_VALUE (s))
12159                          type = IDENTIFIER_GLOBAL_VALUE (s);
12160                        if (TREE_CODE (s) == TYPE_DECL)
12161                          s = TREE_TYPE (s);
12162                        if (TYPE_P (s))
12163                          {
12164                            type = s;
12165                            break;
12166                          }
12167                      }
12168                  }
12169                if (!type)
12170                  error ("friend declaration does not name a class or "
12171                         "function");
12172                else
12173                  make_friend_class (current_class_type, type);
12174             }
12175           /* If there is no TYPE, an error message will already have
12176              been issued.  */
12177           else if (!type)
12178             ;
12179           /* An anonymous aggregate has to be handled specially; such
12180              a declaration really declares a data member (with a
12181              particular type), as opposed to a nested class.  */
12182           else if (ANON_AGGR_TYPE_P (type))
12183             {
12184               /* Remove constructors and such from TYPE, now that we
12185                  know it is an anoymous aggregate.  */
12186               fixup_anonymous_aggr (type);
12187               /* And make the corresponding data member.  */
12188               decl = build_decl (FIELD_DECL, NULL_TREE, type);
12189               /* Add it to the class.  */
12190               finish_member_declaration (decl);
12191             }
12192         }
12193     }
12194   else
12195     {
12196       /* See if these declarations will be friends.  */
12197       friend_p = cp_parser_friend_p (decl_specifiers);
12198
12199       /* Keep going until we hit the `;' at the end of the 
12200          declaration.  */
12201       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12202         {
12203           tree attributes = NULL_TREE;
12204           tree first_attribute;
12205
12206           /* Peek at the next token.  */
12207           token = cp_lexer_peek_token (parser->lexer);
12208
12209           /* Check for a bitfield declaration.  */
12210           if (token->type == CPP_COLON
12211               || (token->type == CPP_NAME
12212                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type 
12213                   == CPP_COLON))
12214             {
12215               tree identifier;
12216               tree width;
12217
12218               /* Get the name of the bitfield.  Note that we cannot just
12219                  check TOKEN here because it may have been invalidated by
12220                  the call to cp_lexer_peek_nth_token above.  */
12221               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12222                 identifier = cp_parser_identifier (parser);
12223               else
12224                 identifier = NULL_TREE;
12225
12226               /* Consume the `:' token.  */
12227               cp_lexer_consume_token (parser->lexer);
12228               /* Get the width of the bitfield.  */
12229               width = cp_parser_constant_expression (parser);
12230
12231               /* Look for attributes that apply to the bitfield.  */
12232               attributes = cp_parser_attributes_opt (parser);
12233               /* Remember which attributes are prefix attributes and
12234                  which are not.  */
12235               first_attribute = attributes;
12236               /* Combine the attributes.  */
12237               attributes = chainon (prefix_attributes, attributes);
12238
12239               /* Create the bitfield declaration.  */
12240               decl = grokbitfield (identifier, 
12241                                    decl_specifiers,
12242                                    width);
12243               /* Apply the attributes.  */
12244               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12245             }
12246           else
12247             {
12248               tree declarator;
12249               tree initializer;
12250               tree asm_specification;
12251               bool ctor_dtor_or_conv_p;
12252
12253               /* Parse the declarator.  */
12254               declarator 
12255                 = cp_parser_declarator (parser,
12256                                         /*abstract_p=*/false,
12257                                         &ctor_dtor_or_conv_p);
12258
12259               /* If something went wrong parsing the declarator, make sure
12260                  that we at least consume some tokens.  */
12261               if (declarator == error_mark_node)
12262                 {
12263                   /* Skip to the end of the statement.  */
12264                   cp_parser_skip_to_end_of_statement (parser);
12265                   break;
12266                 }
12267
12268               /* Look for an asm-specification.  */
12269               asm_specification = cp_parser_asm_specification_opt (parser);
12270               /* Look for attributes that apply to the declaration.  */
12271               attributes = cp_parser_attributes_opt (parser);
12272               /* Remember which attributes are prefix attributes and
12273                  which are not.  */
12274               first_attribute = attributes;
12275               /* Combine the attributes.  */
12276               attributes = chainon (prefix_attributes, attributes);
12277
12278               /* If it's an `=', then we have a constant-initializer or a
12279                  pure-specifier.  It is not correct to parse the
12280                  initializer before registering the member declaration
12281                  since the member declaration should be in scope while
12282                  its initializer is processed.  However, the rest of the
12283                  front end does not yet provide an interface that allows
12284                  us to handle this correctly.  */
12285               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12286                 {
12287                   /* In [class.mem]:
12288
12289                      A pure-specifier shall be used only in the declaration of
12290                      a virtual function.  
12291
12292                      A member-declarator can contain a constant-initializer
12293                      only if it declares a static member of integral or
12294                      enumeration type.  
12295
12296                      Therefore, if the DECLARATOR is for a function, we look
12297                      for a pure-specifier; otherwise, we look for a
12298                      constant-initializer.  When we call `grokfield', it will
12299                      perform more stringent semantics checks.  */
12300                   if (TREE_CODE (declarator) == CALL_EXPR)
12301                     initializer = cp_parser_pure_specifier (parser);
12302                   else
12303                     {
12304                       /* This declaration cannot be a function
12305                          definition.  */
12306                       cp_parser_commit_to_tentative_parse (parser);
12307                       /* Parse the initializer.  */
12308                       initializer = cp_parser_constant_initializer (parser);
12309                     }
12310                 }
12311               /* Otherwise, there is no initializer.  */
12312               else
12313                 initializer = NULL_TREE;
12314
12315               /* See if we are probably looking at a function
12316                  definition.  We are certainly not looking at at a
12317                  member-declarator.  Calling `grokfield' has
12318                  side-effects, so we must not do it unless we are sure
12319                  that we are looking at a member-declarator.  */
12320               if (cp_parser_token_starts_function_definition_p 
12321                   (cp_lexer_peek_token (parser->lexer)))
12322                 decl = error_mark_node;
12323               else
12324                 /* Create the declaration.  */
12325                 decl = grokfield (declarator, 
12326                                   decl_specifiers, 
12327                                   initializer,
12328                                   asm_specification,
12329                                   attributes);
12330             }
12331
12332           /* Reset PREFIX_ATTRIBUTES.  */
12333           while (attributes && TREE_CHAIN (attributes) != first_attribute)
12334             attributes = TREE_CHAIN (attributes);
12335           if (attributes)
12336             TREE_CHAIN (attributes) = NULL_TREE;
12337
12338           /* If there is any qualification still in effect, clear it
12339              now; we will be starting fresh with the next declarator.  */
12340           parser->scope = NULL_TREE;
12341           parser->qualifying_scope = NULL_TREE;
12342           parser->object_scope = NULL_TREE;
12343           /* If it's a `,', then there are more declarators.  */
12344           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12345             cp_lexer_consume_token (parser->lexer);
12346           /* If the next token isn't a `;', then we have a parse error.  */
12347           else if (cp_lexer_next_token_is_not (parser->lexer,
12348                                                CPP_SEMICOLON))
12349             {
12350               cp_parser_error (parser, "expected `;'");
12351               /* Skip tokens until we find a `;'  */
12352               cp_parser_skip_to_end_of_statement (parser);
12353
12354               break;
12355             }
12356
12357           if (decl)
12358             {
12359               /* Add DECL to the list of members.  */
12360               if (!friend_p)
12361                 finish_member_declaration (decl);
12362
12363               /* If DECL is a function, we must return
12364                  to parse it later.  (Even though there is no definition,
12365                  there might be default arguments that need handling.)  */
12366               if (TREE_CODE (decl) == FUNCTION_DECL)
12367                 TREE_VALUE (parser->unparsed_functions_queues)
12368                   = tree_cons (current_class_type, decl, 
12369                                TREE_VALUE (parser->unparsed_functions_queues));
12370             }
12371         }
12372     }
12373
12374   /* If everything went well, look for the `;'.  */
12375   if (cp_parser_parse_definitely (parser))
12376     {
12377       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12378       return;
12379     }
12380
12381   /* Parse the function-definition.  */
12382   decl = cp_parser_function_definition (parser, &friend_p);
12383   /* If the member was not a friend, declare it here.  */
12384   if (!friend_p)
12385     finish_member_declaration (decl);
12386   /* Peek at the next token.  */
12387   token = cp_lexer_peek_token (parser->lexer);
12388   /* If the next token is a semicolon, consume it.  */
12389   if (token->type == CPP_SEMICOLON)
12390     cp_lexer_consume_token (parser->lexer);
12391 }
12392
12393 /* Parse a pure-specifier.
12394
12395    pure-specifier:
12396      = 0
12397
12398    Returns INTEGER_ZERO_NODE if a pure specifier is found.
12399    Otherwiser, ERROR_MARK_NODE is returned.  */
12400
12401 static tree
12402 cp_parser_pure_specifier (parser)
12403      cp_parser *parser;
12404 {
12405   cp_token *token;
12406
12407   /* Look for the `=' token.  */
12408   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12409     return error_mark_node;
12410   /* Look for the `0' token.  */
12411   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12412   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
12413      to get information from the lexer about how the number was
12414      spelled in order to fix this problem.  */
12415   if (!token || !integer_zerop (token->value))
12416     return error_mark_node;
12417
12418   return integer_zero_node;
12419 }
12420
12421 /* Parse a constant-initializer.
12422
12423    constant-initializer:
12424      = constant-expression
12425
12426    Returns a representation of the constant-expression.  */
12427
12428 static tree
12429 cp_parser_constant_initializer (parser)
12430      cp_parser *parser;
12431 {
12432   /* Look for the `=' token.  */
12433   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12434     return error_mark_node;
12435
12436   /* It is invalid to write:
12437
12438        struct S { static const int i = { 7 }; };
12439
12440      */
12441   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12442     {
12443       cp_parser_error (parser,
12444                        "a brace-enclosed initializer is not allowed here");
12445       /* Consume the opening brace.  */
12446       cp_lexer_consume_token (parser->lexer);
12447       /* Skip the initializer.  */
12448       cp_parser_skip_to_closing_brace (parser);
12449       /* Look for the trailing `}'.  */
12450       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12451       
12452       return error_mark_node;
12453     }
12454
12455   return cp_parser_constant_expression (parser);
12456 }
12457
12458 /* Derived classes [gram.class.derived] */
12459
12460 /* Parse a base-clause.
12461
12462    base-clause:
12463      : base-specifier-list  
12464
12465    base-specifier-list:
12466      base-specifier
12467      base-specifier-list , base-specifier
12468
12469    Returns a TREE_LIST representing the base-classes, in the order in
12470    which they were declared.  The representation of each node is as
12471    described by cp_parser_base_specifier.  
12472
12473    In the case that no bases are specified, this function will return
12474    NULL_TREE, not ERROR_MARK_NODE.  */
12475
12476 static tree
12477 cp_parser_base_clause (parser)
12478      cp_parser *parser;
12479 {
12480   tree bases = NULL_TREE;
12481
12482   /* Look for the `:' that begins the list.  */
12483   cp_parser_require (parser, CPP_COLON, "`:'");
12484
12485   /* Scan the base-specifier-list.  */
12486   while (true)
12487     {
12488       cp_token *token;
12489       tree base;
12490
12491       /* Look for the base-specifier.  */
12492       base = cp_parser_base_specifier (parser);
12493       /* Add BASE to the front of the list.  */
12494       if (base != error_mark_node)
12495         {
12496           TREE_CHAIN (base) = bases;
12497           bases = base;
12498         }
12499       /* Peek at the next token.  */
12500       token = cp_lexer_peek_token (parser->lexer);
12501       /* If it's not a comma, then the list is complete.  */
12502       if (token->type != CPP_COMMA)
12503         break;
12504       /* Consume the `,'.  */
12505       cp_lexer_consume_token (parser->lexer);
12506     }
12507
12508   /* PARSER->SCOPE may still be non-NULL at this point, if the last
12509      base class had a qualified name.  However, the next name that
12510      appears is certainly not qualified.  */
12511   parser->scope = NULL_TREE;
12512   parser->qualifying_scope = NULL_TREE;
12513   parser->object_scope = NULL_TREE;
12514
12515   return nreverse (bases);
12516 }
12517
12518 /* Parse a base-specifier.
12519
12520    base-specifier:
12521      :: [opt] nested-name-specifier [opt] class-name
12522      virtual access-specifier [opt] :: [opt] nested-name-specifier
12523        [opt] class-name
12524      access-specifier virtual [opt] :: [opt] nested-name-specifier
12525        [opt] class-name
12526
12527    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
12528    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12529    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
12530    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
12531        
12532 static tree
12533 cp_parser_base_specifier (parser)
12534      cp_parser *parser;
12535 {
12536   cp_token *token;
12537   bool done = false;
12538   bool virtual_p = false;
12539   bool duplicate_virtual_error_issued_p = false;
12540   bool duplicate_access_error_issued_p = false;
12541   bool class_scope_p;
12542   access_kind access = ak_none;
12543   tree access_node;
12544   tree type;
12545
12546   /* Process the optional `virtual' and `access-specifier'.  */
12547   while (!done)
12548     {
12549       /* Peek at the next token.  */
12550       token = cp_lexer_peek_token (parser->lexer);
12551       /* Process `virtual'.  */
12552       switch (token->keyword)
12553         {
12554         case RID_VIRTUAL:
12555           /* If `virtual' appears more than once, issue an error.  */
12556           if (virtual_p && !duplicate_virtual_error_issued_p)
12557             {
12558               cp_parser_error (parser,
12559                                "`virtual' specified more than once in base-specified");
12560               duplicate_virtual_error_issued_p = true;
12561             }
12562
12563           virtual_p = true;
12564
12565           /* Consume the `virtual' token.  */
12566           cp_lexer_consume_token (parser->lexer);
12567
12568           break;
12569
12570         case RID_PUBLIC:
12571         case RID_PROTECTED:
12572         case RID_PRIVATE:
12573           /* If more than one access specifier appears, issue an
12574              error.  */
12575           if (access != ak_none && !duplicate_access_error_issued_p)
12576             {
12577               cp_parser_error (parser,
12578                                "more than one access specifier in base-specified");
12579               duplicate_access_error_issued_p = true;
12580             }
12581
12582           access = ((access_kind) 
12583                     tree_low_cst (ridpointers[(int) token->keyword],
12584                                   /*pos=*/1));
12585
12586           /* Consume the access-specifier.  */
12587           cp_lexer_consume_token (parser->lexer);
12588
12589           break;
12590
12591         default:
12592           done = true;
12593           break;
12594         }
12595     }
12596
12597   /* Map `virtual_p' and `access' onto one of the access 
12598      tree-nodes.  */
12599   if (!virtual_p)
12600     switch (access)
12601       {
12602       case ak_none:
12603         access_node = access_default_node;
12604         break;
12605       case ak_public:
12606         access_node = access_public_node;
12607         break;
12608       case ak_protected:
12609         access_node = access_protected_node;
12610         break;
12611       case ak_private:
12612         access_node = access_private_node;
12613         break;
12614       default:
12615         abort ();
12616       }
12617   else
12618     switch (access)
12619       {
12620       case ak_none:
12621         access_node = access_default_virtual_node;
12622         break;
12623       case ak_public:
12624         access_node = access_public_virtual_node;
12625         break;
12626       case ak_protected:
12627         access_node = access_protected_virtual_node;
12628         break;
12629       case ak_private:
12630         access_node = access_private_virtual_node;
12631         break;
12632       default:
12633         abort ();
12634       }
12635
12636   /* Look for the optional `::' operator.  */
12637   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12638   /* Look for the nested-name-specifier.  The simplest way to
12639      implement:
12640
12641        [temp.res]
12642
12643        The keyword `typename' is not permitted in a base-specifier or
12644        mem-initializer; in these contexts a qualified name that
12645        depends on a template-parameter is implicitly assumed to be a
12646        type name.
12647
12648      is to pretend that we have seen the `typename' keyword at this
12649      point.  */ 
12650   cp_parser_nested_name_specifier_opt (parser,
12651                                        /*typename_keyword_p=*/true,
12652                                        /*check_dependency_p=*/true,
12653                                        /*type_p=*/true);
12654   /* If the base class is given by a qualified name, assume that names
12655      we see are type names or templates, as appropriate.  */
12656   class_scope_p = (parser->scope && TYPE_P (parser->scope));
12657   /* Finally, look for the class-name.  */
12658   type = cp_parser_class_name (parser, 
12659                                class_scope_p,
12660                                class_scope_p,
12661                                /*type_p=*/true,
12662                                /*check_access=*/true,
12663                                /*check_dependency_p=*/true,
12664                                /*class_head_p=*/false);
12665
12666   if (type == error_mark_node)
12667     return error_mark_node;
12668
12669   return finish_base_specifier (access_node, TREE_TYPE (type));
12670 }
12671
12672 /* Exception handling [gram.exception] */
12673
12674 /* Parse an (optional) exception-specification.
12675
12676    exception-specification:
12677      throw ( type-id-list [opt] )
12678
12679    Returns a TREE_LIST representing the exception-specification.  The
12680    TREE_VALUE of each node is a type.  */
12681
12682 static tree
12683 cp_parser_exception_specification_opt (parser)
12684      cp_parser *parser;
12685 {
12686   cp_token *token;
12687   tree type_id_list;
12688
12689   /* Peek at the next token.  */
12690   token = cp_lexer_peek_token (parser->lexer);
12691   /* If it's not `throw', then there's no exception-specification.  */
12692   if (!cp_parser_is_keyword (token, RID_THROW))
12693     return NULL_TREE;
12694
12695   /* Consume the `throw'.  */
12696   cp_lexer_consume_token (parser->lexer);
12697
12698   /* Look for the `('.  */
12699   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12700
12701   /* Peek at the next token.  */
12702   token = cp_lexer_peek_token (parser->lexer);
12703   /* If it's not a `)', then there is a type-id-list.  */
12704   if (token->type != CPP_CLOSE_PAREN)
12705     {
12706       const char *saved_message;
12707
12708       /* Types may not be defined in an exception-specification.  */
12709       saved_message = parser->type_definition_forbidden_message;
12710       parser->type_definition_forbidden_message
12711         = "types may not be defined in an exception-specification";
12712       /* Parse the type-id-list.  */
12713       type_id_list = cp_parser_type_id_list (parser);
12714       /* Restore the saved message.  */
12715       parser->type_definition_forbidden_message = saved_message;
12716     }
12717   else
12718     type_id_list = empty_except_spec;
12719
12720   /* Look for the `)'.  */
12721   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12722
12723   return type_id_list;
12724 }
12725
12726 /* Parse an (optional) type-id-list.
12727
12728    type-id-list:
12729      type-id
12730      type-id-list , type-id
12731
12732    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
12733    in the order that the types were presented.  */
12734
12735 static tree
12736 cp_parser_type_id_list (parser)
12737      cp_parser *parser;
12738 {
12739   tree types = NULL_TREE;
12740
12741   while (true)
12742     {
12743       cp_token *token;
12744       tree type;
12745
12746       /* Get the next type-id.  */
12747       type = cp_parser_type_id (parser);
12748       /* Add it to the list.  */
12749       types = add_exception_specifier (types, type, /*complain=*/1);
12750       /* Peek at the next token.  */
12751       token = cp_lexer_peek_token (parser->lexer);
12752       /* If it is not a `,', we are done.  */
12753       if (token->type != CPP_COMMA)
12754         break;
12755       /* Consume the `,'.  */
12756       cp_lexer_consume_token (parser->lexer);
12757     }
12758
12759   return nreverse (types);
12760 }
12761
12762 /* Parse a try-block.
12763
12764    try-block:
12765      try compound-statement handler-seq  */
12766
12767 static tree
12768 cp_parser_try_block (parser)
12769      cp_parser *parser;
12770 {
12771   tree try_block;
12772
12773   cp_parser_require_keyword (parser, RID_TRY, "`try'");
12774   try_block = begin_try_block ();
12775   cp_parser_compound_statement (parser);
12776   finish_try_block (try_block);
12777   cp_parser_handler_seq (parser);
12778   finish_handler_sequence (try_block);
12779
12780   return try_block;
12781 }
12782
12783 /* Parse a function-try-block.
12784
12785    function-try-block:
12786      try ctor-initializer [opt] function-body handler-seq  */
12787
12788 static bool
12789 cp_parser_function_try_block (parser)
12790      cp_parser *parser;
12791 {
12792   tree try_block;
12793   bool ctor_initializer_p;
12794
12795   /* Look for the `try' keyword.  */
12796   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12797     return false;
12798   /* Let the rest of the front-end know where we are.  */
12799   try_block = begin_function_try_block ();
12800   /* Parse the function-body.  */
12801   ctor_initializer_p 
12802     = cp_parser_ctor_initializer_opt_and_function_body (parser);
12803   /* We're done with the `try' part.  */
12804   finish_function_try_block (try_block);
12805   /* Parse the handlers.  */
12806   cp_parser_handler_seq (parser);
12807   /* We're done with the handlers.  */
12808   finish_function_handler_sequence (try_block);
12809
12810   return ctor_initializer_p;
12811 }
12812
12813 /* Parse a handler-seq.
12814
12815    handler-seq:
12816      handler handler-seq [opt]  */
12817
12818 static void
12819 cp_parser_handler_seq (parser)
12820      cp_parser *parser;
12821 {
12822   while (true)
12823     {
12824       cp_token *token;
12825
12826       /* Parse the handler.  */
12827       cp_parser_handler (parser);
12828       /* Peek at the next token.  */
12829       token = cp_lexer_peek_token (parser->lexer);
12830       /* If it's not `catch' then there are no more handlers.  */
12831       if (!cp_parser_is_keyword (token, RID_CATCH))
12832         break;
12833     }
12834 }
12835
12836 /* Parse a handler.
12837
12838    handler:
12839      catch ( exception-declaration ) compound-statement  */
12840
12841 static void
12842 cp_parser_handler (parser)
12843      cp_parser *parser;
12844 {
12845   tree handler;
12846   tree declaration;
12847
12848   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12849   handler = begin_handler ();
12850   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12851   declaration = cp_parser_exception_declaration (parser);
12852   finish_handler_parms (declaration, handler);
12853   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12854   cp_parser_compound_statement (parser);
12855   finish_handler (handler);
12856 }
12857
12858 /* Parse an exception-declaration.
12859
12860    exception-declaration:
12861      type-specifier-seq declarator
12862      type-specifier-seq abstract-declarator
12863      type-specifier-seq
12864      ...  
12865
12866    Returns a VAR_DECL for the declaration, or NULL_TREE if the
12867    ellipsis variant is used.  */
12868
12869 static tree
12870 cp_parser_exception_declaration (parser)
12871      cp_parser *parser;
12872 {
12873   tree type_specifiers;
12874   tree declarator;
12875   const char *saved_message;
12876
12877   /* If it's an ellipsis, it's easy to handle.  */
12878   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12879     {
12880       /* Consume the `...' token.  */
12881       cp_lexer_consume_token (parser->lexer);
12882       return NULL_TREE;
12883     }
12884
12885   /* Types may not be defined in exception-declarations.  */
12886   saved_message = parser->type_definition_forbidden_message;
12887   parser->type_definition_forbidden_message
12888     = "types may not be defined in exception-declarations";
12889
12890   /* Parse the type-specifier-seq.  */
12891   type_specifiers = cp_parser_type_specifier_seq (parser);
12892   /* If it's a `)', then there is no declarator.  */
12893   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12894     declarator = NULL_TREE;
12895   else
12896     {
12897       /* Otherwise, we can't be sure whether we are looking at a
12898          direct, or an abstract, declarator.  */
12899       cp_parser_parse_tentatively (parser);
12900       /* Try an ordinary declarator.  */
12901       declarator = cp_parser_declarator (parser,
12902                                          /*abstract_p=*/false,
12903                                          /*ctor_dtor_or_conv_p=*/NULL);
12904       /* If that didn't work, try an abstract declarator.  */
12905       if (!cp_parser_parse_definitely (parser))
12906         declarator = cp_parser_declarator (parser,
12907                                            /*abstract_p=*/true,
12908                                            /*ctor_dtor_or_conv_p=*/NULL);
12909     }
12910
12911   /* Restore the saved message.  */
12912   parser->type_definition_forbidden_message = saved_message;
12913
12914   return start_handler_parms (type_specifiers, declarator);
12915 }
12916
12917 /* Parse a throw-expression. 
12918
12919    throw-expression:
12920      throw assignment-expresion [opt]
12921
12922    Returns a THROW_EXPR representing the throw-expression.  */
12923
12924 static tree
12925 cp_parser_throw_expression (parser)
12926      cp_parser *parser;
12927 {
12928   tree expression;
12929
12930   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12931   /* We can't be sure if there is an assignment-expression or not.  */
12932   cp_parser_parse_tentatively (parser);
12933   /* Try it.  */
12934   expression = cp_parser_assignment_expression (parser);
12935   /* If it didn't work, this is just a rethrow.  */
12936   if (!cp_parser_parse_definitely (parser))
12937     expression = NULL_TREE;
12938
12939   return build_throw (expression);
12940 }
12941
12942 /* GNU Extensions */
12943
12944 /* Parse an (optional) asm-specification.
12945
12946    asm-specification:
12947      asm ( string-literal )
12948
12949    If the asm-specification is present, returns a STRING_CST
12950    corresponding to the string-literal.  Otherwise, returns
12951    NULL_TREE.  */
12952
12953 static tree
12954 cp_parser_asm_specification_opt (parser)
12955      cp_parser *parser;
12956 {
12957   cp_token *token;
12958   tree asm_specification;
12959
12960   /* Peek at the next token.  */
12961   token = cp_lexer_peek_token (parser->lexer);
12962   /* If the next token isn't the `asm' keyword, then there's no 
12963      asm-specification.  */
12964   if (!cp_parser_is_keyword (token, RID_ASM))
12965     return NULL_TREE;
12966
12967   /* Consume the `asm' token.  */
12968   cp_lexer_consume_token (parser->lexer);
12969   /* Look for the `('.  */
12970   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12971
12972   /* Look for the string-literal.  */
12973   token = cp_parser_require (parser, CPP_STRING, "string-literal");
12974   if (token)
12975     asm_specification = token->value;
12976   else
12977     asm_specification = NULL_TREE;
12978
12979   /* Look for the `)'.  */
12980   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12981
12982   return asm_specification;
12983 }
12984
12985 /* Parse an asm-operand-list.  
12986
12987    asm-operand-list:
12988      asm-operand
12989      asm-operand-list , asm-operand
12990      
12991    asm-operand:
12992      string-literal ( expression )  
12993      [ string-literal ] string-literal ( expression )
12994
12995    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
12996    each node is the expression.  The TREE_PURPOSE is itself a
12997    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12998    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12999    is a STRING_CST for the string literal before the parenthesis.  */
13000
13001 static tree
13002 cp_parser_asm_operand_list (parser)
13003      cp_parser *parser;
13004 {
13005   tree asm_operands = NULL_TREE;
13006
13007   while (true)
13008     {
13009       tree string_literal;
13010       tree expression;
13011       tree name;
13012       cp_token *token;
13013       
13014       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 
13015         {
13016           /* Consume the `[' token.  */
13017           cp_lexer_consume_token (parser->lexer);
13018           /* Read the operand name.  */
13019           name = cp_parser_identifier (parser);
13020           if (name != error_mark_node) 
13021             name = build_string (IDENTIFIER_LENGTH (name),
13022                                  IDENTIFIER_POINTER (name));
13023           /* Look for the closing `]'.  */
13024           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13025         }
13026       else
13027         name = NULL_TREE;
13028       /* Look for the string-literal.  */
13029       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13030       string_literal = token ? token->value : error_mark_node;
13031       /* Look for the `('.  */
13032       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13033       /* Parse the expression.  */
13034       expression = cp_parser_expression (parser);
13035       /* Look for the `)'.  */
13036       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13037       /* Add this operand to the list.  */
13038       asm_operands = tree_cons (build_tree_list (name, string_literal),
13039                                 expression, 
13040                                 asm_operands);
13041       /* If the next token is not a `,', there are no more 
13042          operands.  */
13043       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13044         break;
13045       /* Consume the `,'.  */
13046       cp_lexer_consume_token (parser->lexer);
13047     }
13048
13049   return nreverse (asm_operands);
13050 }
13051
13052 /* Parse an asm-clobber-list.  
13053
13054    asm-clobber-list:
13055      string-literal
13056      asm-clobber-list , string-literal  
13057
13058    Returns a TREE_LIST, indicating the clobbers in the order that they
13059    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13060
13061 static tree
13062 cp_parser_asm_clobber_list (parser)
13063      cp_parser *parser;
13064 {
13065   tree clobbers = NULL_TREE;
13066
13067   while (true)
13068     {
13069       cp_token *token;
13070       tree string_literal;
13071
13072       /* Look for the string literal.  */
13073       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13074       string_literal = token ? token->value : error_mark_node;
13075       /* Add it to the list.  */
13076       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13077       /* If the next token is not a `,', then the list is 
13078          complete.  */
13079       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13080         break;
13081       /* Consume the `,' token.  */
13082       cp_lexer_consume_token (parser->lexer);
13083     }
13084
13085   return clobbers;
13086 }
13087
13088 /* Parse an (optional) series of attributes.
13089
13090    attributes:
13091      attributes attribute
13092
13093    attribute:
13094      __attribute__ (( attribute-list [opt] ))  
13095
13096    The return value is as for cp_parser_attribute_list.  */
13097      
13098 static tree
13099 cp_parser_attributes_opt (parser)
13100      cp_parser *parser;
13101 {
13102   tree attributes = NULL_TREE;
13103
13104   while (true)
13105     {
13106       cp_token *token;
13107       tree attribute_list;
13108
13109       /* Peek at the next token.  */
13110       token = cp_lexer_peek_token (parser->lexer);
13111       /* If it's not `__attribute__', then we're done.  */
13112       if (token->keyword != RID_ATTRIBUTE)
13113         break;
13114
13115       /* Consume the `__attribute__' keyword.  */
13116       cp_lexer_consume_token (parser->lexer);
13117       /* Look for the two `(' tokens.  */
13118       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13119       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13120
13121       /* Peek at the next token.  */
13122       token = cp_lexer_peek_token (parser->lexer);
13123       if (token->type != CPP_CLOSE_PAREN)
13124         /* Parse the attribute-list.  */
13125         attribute_list = cp_parser_attribute_list (parser);
13126       else
13127         /* If the next token is a `)', then there is no attribute
13128            list.  */
13129         attribute_list = NULL;
13130
13131       /* Look for the two `)' tokens.  */
13132       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13133       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13134
13135       /* Add these new attributes to the list.  */
13136       attributes = chainon (attributes, attribute_list);
13137     }
13138
13139   return attributes;
13140 }
13141
13142 /* Parse an attribute-list.  
13143
13144    attribute-list:  
13145      attribute 
13146      attribute-list , attribute
13147
13148    attribute:
13149      identifier     
13150      identifier ( identifier )
13151      identifier ( identifier , expression-list )
13152      identifier ( expression-list ) 
13153
13154    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13155    TREE_PURPOSE of each node is the identifier indicating which
13156    attribute is in use.  The TREE_VALUE represents the arguments, if
13157    any.  */
13158
13159 static tree
13160 cp_parser_attribute_list (parser)
13161      cp_parser *parser;
13162 {
13163   tree attribute_list = NULL_TREE;
13164
13165   while (true)
13166     {
13167       cp_token *token;
13168       tree identifier;
13169       tree attribute;
13170
13171       /* Look for the identifier.  We also allow keywords here; for
13172          example `__attribute__ ((const))' is legal.  */
13173       token = cp_lexer_peek_token (parser->lexer);
13174       if (token->type != CPP_NAME 
13175           && token->type != CPP_KEYWORD)
13176         return error_mark_node;
13177       /* Consume the token.  */
13178       token = cp_lexer_consume_token (parser->lexer);
13179       
13180       /* Save away the identifier that indicates which attribute this is.  */
13181       identifier = token->value;
13182       attribute = build_tree_list (identifier, NULL_TREE);
13183
13184       /* Peek at the next token.  */
13185       token = cp_lexer_peek_token (parser->lexer);
13186       /* If it's an `(', then parse the attribute arguments.  */
13187       if (token->type == CPP_OPEN_PAREN)
13188         {
13189           tree arguments;
13190           int arguments_allowed_p = 1;
13191
13192           /* Consume the `('.  */
13193           cp_lexer_consume_token (parser->lexer);
13194           /* Peek at the next token.  */
13195           token = cp_lexer_peek_token (parser->lexer);
13196           /* Check to see if the next token is an identifier.  */
13197           if (token->type == CPP_NAME)
13198             {
13199               /* Save the identifier.  */
13200               identifier = token->value;
13201               /* Consume the identifier.  */
13202               cp_lexer_consume_token (parser->lexer);
13203               /* Peek at the next token.  */
13204               token = cp_lexer_peek_token (parser->lexer);
13205               /* If the next token is a `,', then there are some other
13206                  expressions as well.  */
13207               if (token->type == CPP_COMMA)
13208                 /* Consume the comma.  */
13209                 cp_lexer_consume_token (parser->lexer);
13210               else
13211                 arguments_allowed_p = 0;
13212             }
13213           else
13214             identifier = NULL_TREE;
13215
13216           /* If there are arguments, parse them too.  */
13217           if (arguments_allowed_p)
13218             arguments = cp_parser_expression_list (parser);
13219           else
13220             arguments = NULL_TREE;
13221
13222           /* Combine the identifier and the arguments.  */
13223           if (identifier)
13224             arguments = tree_cons (NULL_TREE, identifier, arguments);
13225
13226           /* Save the identifier and arguments away.  */
13227           TREE_VALUE (attribute) = arguments;
13228
13229           /* Look for the closing `)'.  */
13230           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13231         }
13232
13233       /* Add this attribute to the list.  */
13234       TREE_CHAIN (attribute) = attribute_list;
13235       attribute_list = attribute;
13236
13237       /* Now, look for more attributes.  */
13238       token = cp_lexer_peek_token (parser->lexer);
13239       /* If the next token isn't a `,', we're done.  */
13240       if (token->type != CPP_COMMA)
13241         break;
13242
13243       /* Consume the commma and keep going.  */
13244       cp_lexer_consume_token (parser->lexer);
13245     }
13246
13247   /* We built up the list in reverse order.  */
13248   return nreverse (attribute_list);
13249 }
13250
13251 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13252    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13253    current value of the PEDANTIC flag, regardless of whether or not
13254    the `__extension__' keyword is present.  The caller is responsible
13255    for restoring the value of the PEDANTIC flag.  */
13256
13257 static bool
13258 cp_parser_extension_opt (parser, saved_pedantic)
13259      cp_parser *parser;
13260      int *saved_pedantic;
13261 {
13262   /* Save the old value of the PEDANTIC flag.  */
13263   *saved_pedantic = pedantic;
13264
13265   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13266     {
13267       /* Consume the `__extension__' token.  */
13268       cp_lexer_consume_token (parser->lexer);
13269       /* We're not being pedantic while the `__extension__' keyword is
13270          in effect.  */
13271       pedantic = 0;
13272
13273       return true;
13274     }
13275
13276   return false;
13277 }
13278
13279 /* Parse a label declaration.
13280
13281    label-declaration:
13282      __label__ label-declarator-seq ;
13283
13284    label-declarator-seq:
13285      identifier , label-declarator-seq
13286      identifier  */
13287
13288 static void
13289 cp_parser_label_declaration (parser)
13290      cp_parser *parser;
13291 {
13292   /* Look for the `__label__' keyword.  */
13293   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13294
13295   while (true)
13296     {
13297       tree identifier;
13298
13299       /* Look for an identifier.  */
13300       identifier = cp_parser_identifier (parser);
13301       /* Declare it as a lobel.  */
13302       finish_label_decl (identifier);
13303       /* If the next token is a `;', stop.  */
13304       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13305         break;
13306       /* Look for the `,' separating the label declarations.  */
13307       cp_parser_require (parser, CPP_COMMA, "`,'");
13308     }
13309
13310   /* Look for the final `;'.  */
13311   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13312 }
13313
13314 /* Support Functions */
13315
13316 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13317    NAME should have one of the representations used for an
13318    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13319    is returned.  If PARSER->SCOPE is a dependent type, then a
13320    SCOPE_REF is returned.
13321
13322    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13323    returned; the name was already resolved when the TEMPLATE_ID_EXPR
13324    was formed.  Abstractly, such entities should not be passed to this
13325    function, because they do not need to be looked up, but it is
13326    simpler to check for this special case here, rather than at the
13327    call-sites.
13328
13329    In cases not explicitly covered above, this function returns a
13330    DECL, OVERLOAD, or baselink representing the result of the lookup.
13331    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13332    is returned.
13333
13334    If CHECK_ACCESS is TRUE, then access control is performed on the
13335    declaration to which the name resolves, and an error message is
13336    issued if the declaration is inaccessible.
13337
13338    If IS_TYPE is TRUE, bindings that do not refer to types are
13339    ignored.
13340
13341    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13342    are ignored.
13343
13344    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13345    types.  */
13346
13347 static tree
13348 cp_parser_lookup_name (cp_parser *parser, tree name, bool check_access, 
13349                        bool is_type, bool is_namespace, bool check_dependency)
13350 {
13351   tree decl;
13352   tree object_type = parser->context->object_type;
13353
13354   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13355      no longer valid.  Note that if we are parsing tentatively, and
13356      the parse fails, OBJECT_TYPE will be automatically restored.  */
13357   parser->context->object_type = NULL_TREE;
13358
13359   if (name == error_mark_node)
13360     return error_mark_node;
13361
13362   /* A template-id has already been resolved; there is no lookup to
13363      do.  */
13364   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13365     return name;
13366   if (BASELINK_P (name))
13367     {
13368       my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13369                            == TEMPLATE_ID_EXPR),
13370                           20020909);
13371       return name;
13372     }
13373
13374   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
13375      it should already have been checked to make sure that the name
13376      used matches the type being destroyed.  */
13377   if (TREE_CODE (name) == BIT_NOT_EXPR)
13378     {
13379       tree type;
13380
13381       /* Figure out to which type this destructor applies.  */
13382       if (parser->scope)
13383         type = parser->scope;
13384       else if (object_type)
13385         type = object_type;
13386       else
13387         type = current_class_type;
13388       /* If that's not a class type, there is no destructor.  */
13389       if (!type || !CLASS_TYPE_P (type))
13390         return error_mark_node;
13391       /* If it was a class type, return the destructor.  */
13392       return CLASSTYPE_DESTRUCTORS (type);
13393     }
13394
13395   /* By this point, the NAME should be an ordinary identifier.  If
13396      the id-expression was a qualified name, the qualifying scope is
13397      stored in PARSER->SCOPE at this point.  */
13398   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13399                       20000619);
13400   
13401   /* Perform the lookup.  */
13402   if (parser->scope)
13403     { 
13404       bool dependent_type_p;
13405
13406       if (parser->scope == error_mark_node)
13407         return error_mark_node;
13408
13409       /* If the SCOPE is dependent, the lookup must be deferred until
13410          the template is instantiated -- unless we are explicitly
13411          looking up names in uninstantiated templates.  Even then, we
13412          cannot look up the name if the scope is not a class type; it
13413          might, for example, be a template type parameter.  */
13414       dependent_type_p = (TYPE_P (parser->scope)
13415                           && !(parser->in_declarator_p
13416                                && currently_open_class (parser->scope))
13417                           && cp_parser_dependent_type_p (parser->scope));
13418       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13419            && dependent_type_p)
13420         {
13421           if (!is_type)
13422             decl = build_nt (SCOPE_REF, parser->scope, name);
13423           else
13424             /* The resolution to Core Issue 180 says that `struct A::B'
13425                should be considered a type-name, even if `A' is
13426                dependent.  */
13427             decl = TYPE_NAME (make_typename_type (parser->scope,
13428                                                   name,
13429                                                   /*complain=*/1));
13430         }
13431       else
13432         {
13433           /* If PARSER->SCOPE is a dependent type, then it must be a
13434              class type, and we must not be checking dependencies;
13435              otherwise, we would have processed this lookup above.  So
13436              that PARSER->SCOPE is not considered a dependent base by
13437              lookup_member, we must enter the scope here.  */
13438           if (dependent_type_p)
13439             push_scope (parser->scope);
13440           /* If the PARSER->SCOPE is a a template specialization, it
13441              may be instantiated during name lookup.  In that case,
13442              errors may be issued.  Even if we rollback the current
13443              tentative parse, those errors are valid.  */
13444           decl = lookup_qualified_name (parser->scope, name, is_type,
13445                                         /*flags=*/0);
13446           if (dependent_type_p)
13447             pop_scope (parser->scope);
13448         }
13449       parser->qualifying_scope = parser->scope;
13450       parser->object_scope = NULL_TREE;
13451     }
13452   else if (object_type)
13453     {
13454       tree object_decl = NULL_TREE;
13455       /* Look up the name in the scope of the OBJECT_TYPE, unless the
13456          OBJECT_TYPE is not a class.  */
13457       if (CLASS_TYPE_P (object_type))
13458         /* If the OBJECT_TYPE is a template specialization, it may
13459            be instantiated during name lookup.  In that case, errors
13460            may be issued.  Even if we rollback the current tentative
13461            parse, those errors are valid.  */
13462         object_decl = lookup_member (object_type,
13463                                      name,
13464                                      /*protect=*/0, is_type);
13465       /* Look it up in the enclosing context, too.  */
13466       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13467                                is_namespace,
13468                                /*flags=*/0);
13469       parser->object_scope = object_type;
13470       parser->qualifying_scope = NULL_TREE;
13471       if (object_decl)
13472         decl = object_decl;
13473     }
13474   else
13475     {
13476       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13477                                is_namespace,
13478                                /*flags=*/0);
13479       parser->qualifying_scope = NULL_TREE;
13480       parser->object_scope = NULL_TREE;
13481     }
13482
13483   /* If the lookup failed, let our caller know.  */
13484   if (!decl 
13485       || decl == error_mark_node
13486       || (TREE_CODE (decl) == FUNCTION_DECL 
13487           && DECL_ANTICIPATED (decl)))
13488     return error_mark_node;
13489
13490   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
13491   if (TREE_CODE (decl) == TREE_LIST)
13492     {
13493       /* The error message we have to print is too complicated for
13494          cp_parser_error, so we incorporate its actions directly.  */
13495       if (!cp_parser_simulate_error (parser))
13496         {
13497           error ("reference to `%D' is ambiguous", name);
13498           print_candidates (decl);
13499         }
13500       return error_mark_node;
13501     }
13502
13503   my_friendly_assert (DECL_P (decl) 
13504                       || TREE_CODE (decl) == OVERLOAD
13505                       || TREE_CODE (decl) == SCOPE_REF
13506                       || BASELINK_P (decl),
13507                       20000619);
13508
13509   /* If we have resolved the name of a member declaration, check to
13510      see if the declaration is accessible.  When the name resolves to
13511      set of overloaded functions, accesibility is checked when
13512      overload resolution is done.  
13513
13514      During an explicit instantiation, access is not checked at all,
13515      as per [temp.explicit].  */
13516   if (check_access && scope_chain->check_access && DECL_P (decl))
13517     {
13518       tree qualifying_type;
13519       
13520       /* Figure out the type through which DECL is being
13521          accessed.  */
13522       qualifying_type 
13523         = cp_parser_scope_through_which_access_occurs (decl,
13524                                                        object_type,
13525                                                        parser->scope);
13526       if (qualifying_type)
13527         {
13528           /* If we are supposed to defer access checks, just record
13529              the information for later.  */
13530           if (parser->context->deferring_access_checks_p)
13531             cp_parser_defer_access_check (parser, qualifying_type, decl);
13532           /* Otherwise, check accessibility now.  */
13533           else
13534             enforce_access (qualifying_type, decl);
13535         }
13536     }
13537
13538   return decl;
13539 }
13540
13541 /* Like cp_parser_lookup_name, but for use in the typical case where
13542    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13543    TRUE.  */
13544
13545 static tree
13546 cp_parser_lookup_name_simple (parser, name)
13547      cp_parser *parser;
13548      tree name;
13549 {
13550   return cp_parser_lookup_name (parser, name, 
13551                                 /*check_access=*/true,
13552                                 /*is_type=*/false,
13553                                 /*is_namespace=*/false,
13554                                 /*check_dependency=*/true);
13555 }
13556
13557 /* TYPE is a TYPENAME_TYPE.  Returns the ordinary TYPE to which the
13558    TYPENAME_TYPE corresponds.  Note that this function peers inside
13559    uninstantiated templates and therefore should be used only in
13560    extremely limited situations.  */
13561
13562 static tree
13563 cp_parser_resolve_typename_type (parser, type)
13564      cp_parser *parser;
13565      tree type;
13566 {
13567   tree scope;
13568   tree name;
13569   tree decl;
13570
13571   my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13572                       20010702);
13573
13574   scope = TYPE_CONTEXT (type);
13575   name = DECL_NAME (TYPE_NAME (type));
13576
13577   /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13578      it first before we can figure out what NAME refers to.  */
13579   if (TREE_CODE (scope) == TYPENAME_TYPE)
13580     scope = cp_parser_resolve_typename_type (parser, scope);
13581   /* If we don't know what SCOPE refers to, then we cannot resolve the
13582      TYPENAME_TYPE.  */
13583   if (scope == error_mark_node)
13584     return error_mark_node;
13585   /* If the SCOPE is a template type parameter, we have no way of
13586      resolving the name.  */
13587   if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13588     return type;
13589   /* Enter the SCOPE so that name lookup will be resolved as if we
13590      were in the class definition.  In particular, SCOPE will no
13591      longer be considered a dependent type.  */
13592   push_scope (scope);
13593   /* Look up the declaration.  */
13594   decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13595   /* If all went well, we got a TYPE_DECL for a non-typename.  */
13596   if (!decl 
13597       || TREE_CODE (decl) != TYPE_DECL 
13598       || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13599     {
13600       cp_parser_error (parser, "could not resolve typename type");
13601       type = error_mark_node;
13602     }
13603   else
13604     type = TREE_TYPE (decl);
13605   /* Leave the SCOPE.  */
13606   pop_scope (scope);
13607
13608   return type;
13609 }
13610
13611 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13612    the current context, return the TYPE_DECL.  If TAG_NAME_P is
13613    true, the DECL indicates the class being defined in a class-head,
13614    or declared in an elaborated-type-specifier.
13615
13616    Otherwise, return DECL.  */
13617
13618 static tree
13619 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13620 {
13621   /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13622      the scope of the class, then treat the TEMPLATE_DECL as a
13623      class-name.  For example, in:
13624
13625        template <class T> struct S {
13626          S s;
13627        };
13628
13629      is OK.  
13630
13631      If the TEMPLATE_DECL is being declared as part of a class-head,
13632      the same translation occurs:
13633
13634        struct A { 
13635          template <typename T> struct B;
13636        };
13637
13638        template <typename T> struct A::B {}; 
13639    
13640      Similarly, in a elaborated-type-specifier:
13641
13642        namespace N { struct X{}; }
13643
13644        struct A {
13645          template <typename T> friend struct N::X;
13646        };
13647
13648      */
13649   if (DECL_CLASS_TEMPLATE_P (decl)
13650       && (tag_name_p
13651           || (current_class_type
13652               && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13653                               current_class_type))))
13654     return DECL_TEMPLATE_RESULT (decl);
13655
13656   return decl;
13657 }
13658
13659 /* If too many, or too few, template-parameter lists apply to the
13660    declarator, issue an error message.  Returns TRUE if all went well,
13661    and FALSE otherwise.  */
13662
13663 static bool
13664 cp_parser_check_declarator_template_parameters (parser, declarator)
13665      cp_parser *parser;
13666      tree declarator;
13667 {
13668   unsigned num_templates;
13669
13670   /* We haven't seen any classes that involve template parameters yet.  */
13671   num_templates = 0;
13672
13673   switch (TREE_CODE (declarator))
13674     {
13675     case CALL_EXPR:
13676     case ARRAY_REF:
13677     case INDIRECT_REF:
13678     case ADDR_EXPR:
13679       {
13680         tree main_declarator = TREE_OPERAND (declarator, 0);
13681         return
13682           cp_parser_check_declarator_template_parameters (parser, 
13683                                                           main_declarator);
13684       }
13685
13686     case SCOPE_REF:
13687       {
13688         tree scope;
13689         tree member;
13690
13691         scope = TREE_OPERAND (declarator, 0);
13692         member = TREE_OPERAND (declarator, 1);
13693
13694         /* If this is a pointer-to-member, then we are not interested
13695            in the SCOPE, because it does not qualify the thing that is
13696            being declared.  */
13697         if (TREE_CODE (member) == INDIRECT_REF)
13698           return (cp_parser_check_declarator_template_parameters
13699                   (parser, member));
13700
13701         while (scope && CLASS_TYPE_P (scope))
13702           {
13703             /* You're supposed to have one `template <...>'
13704                for every template class, but you don't need one
13705                for a full specialization.  For example:
13706                
13707                template <class T> struct S{};
13708                template <> struct S<int> { void f(); };
13709                void S<int>::f () {}
13710                
13711                is correct; there shouldn't be a `template <>' for
13712                the definition of `S<int>::f'.  */
13713             if (CLASSTYPE_TEMPLATE_INFO (scope)
13714                 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13715                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13716                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13717               ++num_templates;
13718
13719             scope = TYPE_CONTEXT (scope);
13720           }
13721       }
13722
13723       /* Fall through.  */
13724
13725     default:
13726       /* If the DECLARATOR has the form `X<y>' then it uses one
13727          additional level of template parameters.  */
13728       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13729         ++num_templates;
13730
13731       return cp_parser_check_template_parameters (parser, 
13732                                                   num_templates);
13733     }
13734 }
13735
13736 /* NUM_TEMPLATES were used in the current declaration.  If that is
13737    invalid, return FALSE and issue an error messages.  Otherwise,
13738    return TRUE.  */
13739
13740 static bool
13741 cp_parser_check_template_parameters (parser, num_templates)
13742      cp_parser *parser;
13743      unsigned num_templates;
13744 {
13745   /* If there are more template classes than parameter lists, we have
13746      something like:
13747      
13748        template <class T> void S<T>::R<T>::f ();  */
13749   if (parser->num_template_parameter_lists < num_templates)
13750     {
13751       error ("too few template-parameter-lists");
13752       return false;
13753     }
13754   /* If there are the same number of template classes and parameter
13755      lists, that's OK.  */
13756   if (parser->num_template_parameter_lists == num_templates)
13757     return true;
13758   /* If there are more, but only one more, then we are referring to a
13759      member template.  That's OK too.  */
13760   if (parser->num_template_parameter_lists == num_templates + 1)
13761       return true;
13762   /* Otherwise, there are too many template parameter lists.  We have
13763      something like:
13764
13765      template <class T> template <class U> void S::f();  */
13766   error ("too many template-parameter-lists");
13767   return false;
13768 }
13769
13770 /* Parse a binary-expression of the general form:
13771
13772    binary-expression:
13773      <expr>
13774      binary-expression <token> <expr>
13775
13776    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
13777    to parser the <expr>s.  If the first production is used, then the
13778    value returned by FN is returned directly.  Otherwise, a node with
13779    the indicated EXPR_TYPE is returned, with operands corresponding to
13780    the two sub-expressions.  */
13781
13782 static tree
13783 cp_parser_binary_expression (parser, token_tree_map, fn)
13784      cp_parser *parser;
13785      cp_parser_token_tree_map token_tree_map;
13786      cp_parser_expression_fn fn;
13787 {
13788   tree lhs;
13789
13790   /* Parse the first expression.  */
13791   lhs = (*fn) (parser);
13792   /* Now, look for more expressions.  */
13793   while (true)
13794     {
13795       cp_token *token;
13796       cp_parser_token_tree_map_node *map_node;
13797       tree rhs;
13798
13799       /* Peek at the next token.  */
13800       token = cp_lexer_peek_token (parser->lexer);
13801       /* If the token is `>', and that's not an operator at the
13802          moment, then we're done.  */
13803       if (token->type == CPP_GREATER
13804           && !parser->greater_than_is_operator_p)
13805         break;
13806       /* If we find one of the tokens we want, build the correspoding
13807          tree representation.  */
13808       for (map_node = token_tree_map; 
13809            map_node->token_type != CPP_EOF;
13810            ++map_node)
13811         if (map_node->token_type == token->type)
13812           {
13813             /* Consume the operator token.  */
13814             cp_lexer_consume_token (parser->lexer);
13815             /* Parse the right-hand side of the expression.  */
13816             rhs = (*fn) (parser);
13817             /* Build the binary tree node.  */
13818             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13819             break;
13820           }
13821
13822       /* If the token wasn't one of the ones we want, we're done.  */
13823       if (map_node->token_type == CPP_EOF)
13824         break;
13825     }
13826
13827   return lhs;
13828 }
13829
13830 /* Parse an optional `::' token indicating that the following name is
13831    from the global namespace.  If so, PARSER->SCOPE is set to the
13832    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13833    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13834    Returns the new value of PARSER->SCOPE, if the `::' token is
13835    present, and NULL_TREE otherwise.  */
13836
13837 static tree
13838 cp_parser_global_scope_opt (parser, current_scope_valid_p)
13839      cp_parser *parser;
13840      bool current_scope_valid_p;
13841 {
13842   cp_token *token;
13843
13844   /* Peek at the next token.  */
13845   token = cp_lexer_peek_token (parser->lexer);
13846   /* If we're looking at a `::' token then we're starting from the
13847      global namespace, not our current location.  */
13848   if (token->type == CPP_SCOPE)
13849     {
13850       /* Consume the `::' token.  */
13851       cp_lexer_consume_token (parser->lexer);
13852       /* Set the SCOPE so that we know where to start the lookup.  */
13853       parser->scope = global_namespace;
13854       parser->qualifying_scope = global_namespace;
13855       parser->object_scope = NULL_TREE;
13856
13857       return parser->scope;
13858     }
13859   else if (!current_scope_valid_p)
13860     {
13861       parser->scope = NULL_TREE;
13862       parser->qualifying_scope = NULL_TREE;
13863       parser->object_scope = NULL_TREE;
13864     }
13865
13866   return NULL_TREE;
13867 }
13868
13869 /* Returns TRUE if the upcoming token sequence is the start of a
13870    constructor declarator.  If FRIEND_P is true, the declarator is
13871    preceded by the `friend' specifier.  */
13872
13873 static bool
13874 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13875 {
13876   bool constructor_p;
13877   tree type_decl = NULL_TREE;
13878   bool nested_name_p;
13879
13880   /* Parse tentatively; we are going to roll back all of the tokens
13881      consumed here.  */
13882   cp_parser_parse_tentatively (parser);
13883   /* Assume that we are looking at a constructor declarator.  */
13884   constructor_p = true;
13885   /* Look for the optional `::' operator.  */
13886   cp_parser_global_scope_opt (parser,
13887                               /*current_scope_valid_p=*/false);
13888   /* Look for the nested-name-specifier.  */
13889   nested_name_p 
13890     = (cp_parser_nested_name_specifier_opt (parser,
13891                                             /*typename_keyword_p=*/false,
13892                                             /*check_dependency_p=*/false,
13893                                             /*type_p=*/false)
13894        != NULL_TREE);
13895   /* Outside of a class-specifier, there must be a
13896      nested-name-specifier.  */
13897   if (!nested_name_p && 
13898       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13899        || friend_p))
13900     constructor_p = false;
13901   /* If we still think that this might be a constructor-declarator,
13902      look for a class-name.  */
13903   if (constructor_p)
13904     {
13905       /* If we have:
13906
13907            template <typename T> struct S { S(); }
13908            template <typename T> S<T>::S ();
13909
13910          we must recognize that the nested `S' names a class.
13911          Similarly, for:
13912
13913            template <typename T> S<T>::S<T> ();
13914
13915          we must recognize that the nested `S' names a template.  */
13916       type_decl = cp_parser_class_name (parser,
13917                                         /*typename_keyword_p=*/false,
13918                                         /*template_keyword_p=*/false,
13919                                         /*type_p=*/false,
13920                                         /*check_access_p=*/false,
13921                                         /*check_dependency_p=*/false,
13922                                         /*class_head_p=*/false);
13923       /* If there was no class-name, then this is not a constructor.  */
13924       constructor_p = !cp_parser_error_occurred (parser);
13925     }
13926   /* If we're still considering a constructor, we have to see a `(',
13927      to begin the parameter-declaration-clause, followed by either a
13928      `)', an `...', or a decl-specifier.  We need to check for a
13929      type-specifier to avoid being fooled into thinking that:
13930
13931        S::S (f) (int);
13932
13933      is a constructor.  (It is actually a function named `f' that
13934      takes one parameter (of type `int') and returns a value of type
13935      `S::S'.  */
13936   if (constructor_p 
13937       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13938     {
13939       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13940           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13941           && !cp_parser_storage_class_specifier_opt (parser))
13942         {
13943           if (current_class_type 
13944               && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
13945             /* The constructor for one class cannot be declared inside
13946                another.  */
13947             constructor_p = false;
13948           else
13949             {
13950               tree type;
13951
13952               /* Names appearing in the type-specifier should be looked up
13953                  in the scope of the class.  */
13954               if (current_class_type)
13955                 type = NULL_TREE;
13956               else
13957                 {
13958                   type = TREE_TYPE (type_decl);
13959                   if (TREE_CODE (type) == TYPENAME_TYPE)
13960                     type = cp_parser_resolve_typename_type (parser, type);
13961                   push_scope (type);
13962                 }
13963               /* Look for the type-specifier.  */
13964               cp_parser_type_specifier (parser,
13965                                         CP_PARSER_FLAGS_NONE,
13966                                         /*is_friend=*/false,
13967                                         /*is_declarator=*/true,
13968                                         /*declares_class_or_enum=*/NULL,
13969                                         /*is_cv_qualifier=*/NULL);
13970               /* Leave the scope of the class.  */
13971               if (type)
13972                 pop_scope (type);
13973
13974               constructor_p = !cp_parser_error_occurred (parser);
13975             }
13976         }
13977     }
13978   else
13979     constructor_p = false;
13980   /* We did not really want to consume any tokens.  */
13981   cp_parser_abort_tentative_parse (parser);
13982
13983   return constructor_p;
13984 }
13985
13986 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13987    ATTRIBUTES, and DECLARATOR.  The ACCESS_CHECKS have been deferred;
13988    they must be performed once we are in the scope of the function.
13989
13990    Returns the function defined.  */
13991
13992 static tree
13993 cp_parser_function_definition_from_specifiers_and_declarator
13994   (parser, decl_specifiers, attributes, declarator, access_checks)
13995      cp_parser *parser;
13996      tree decl_specifiers;
13997      tree attributes;
13998      tree declarator;
13999      tree access_checks;
14000 {
14001   tree fn;
14002   bool success_p;
14003
14004   /* Begin the function-definition.  */
14005   success_p = begin_function_definition (decl_specifiers, 
14006                                          attributes, 
14007                                          declarator);
14008
14009   /* If there were names looked up in the decl-specifier-seq that we
14010      did not check, check them now.  We must wait until we are in the
14011      scope of the function to perform the checks, since the function
14012      might be a friend.  */
14013   cp_parser_perform_deferred_access_checks (access_checks);
14014
14015   if (!success_p)
14016     {
14017       /* If begin_function_definition didn't like the definition, skip
14018          the entire function.  */
14019       error ("invalid function declaration");
14020       cp_parser_skip_to_end_of_block_or_statement (parser);
14021       fn = error_mark_node;
14022     }
14023   else
14024     fn = cp_parser_function_definition_after_declarator (parser,
14025                                                          /*inline_p=*/false);
14026
14027   return fn;
14028 }
14029
14030 /* Parse the part of a function-definition that follows the
14031    declarator.  INLINE_P is TRUE iff this function is an inline
14032    function defined with a class-specifier.
14033
14034    Returns the function defined.  */
14035
14036 static tree 
14037 cp_parser_function_definition_after_declarator (parser, 
14038                                                 inline_p)
14039      cp_parser *parser;
14040      bool inline_p;
14041 {
14042   tree fn;
14043   bool ctor_initializer_p = false;
14044   bool saved_in_unbraced_linkage_specification_p;
14045   unsigned saved_num_template_parameter_lists;
14046
14047   /* If the next token is `return', then the code may be trying to
14048      make use of the "named return value" extension that G++ used to
14049      support.  */
14050   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14051     {
14052       /* Consume the `return' keyword.  */
14053       cp_lexer_consume_token (parser->lexer);
14054       /* Look for the identifier that indicates what value is to be
14055          returned.  */
14056       cp_parser_identifier (parser);
14057       /* Issue an error message.  */
14058       error ("named return values are no longer supported");
14059       /* Skip tokens until we reach the start of the function body.  */
14060       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14061         cp_lexer_consume_token (parser->lexer);
14062     }
14063   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14064      anything declared inside `f'.  */
14065   saved_in_unbraced_linkage_specification_p 
14066     = parser->in_unbraced_linkage_specification_p;
14067   parser->in_unbraced_linkage_specification_p = false;
14068   /* Inside the function, surrounding template-parameter-lists do not
14069      apply.  */
14070   saved_num_template_parameter_lists 
14071     = parser->num_template_parameter_lists; 
14072   parser->num_template_parameter_lists = 0;
14073   /* If the next token is `try', then we are looking at a
14074      function-try-block.  */
14075   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14076     ctor_initializer_p = cp_parser_function_try_block (parser);
14077   /* A function-try-block includes the function-body, so we only do
14078      this next part if we're not processing a function-try-block.  */
14079   else
14080     ctor_initializer_p 
14081       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14082
14083   /* Finish the function.  */
14084   fn = finish_function ((ctor_initializer_p ? 1 : 0) | 
14085                         (inline_p ? 2 : 0));
14086   /* Generate code for it, if necessary.  */
14087   expand_body (fn);
14088   /* Restore the saved values.  */
14089   parser->in_unbraced_linkage_specification_p 
14090     = saved_in_unbraced_linkage_specification_p;
14091   parser->num_template_parameter_lists 
14092     = saved_num_template_parameter_lists;
14093
14094   return fn;
14095 }
14096
14097 /* Parse a template-declaration, assuming that the `export' (and
14098    `extern') keywords, if present, has already been scanned.  MEMBER_P
14099    is as for cp_parser_template_declaration.  */
14100
14101 static void
14102 cp_parser_template_declaration_after_export (parser, member_p)
14103      cp_parser *parser;
14104      bool member_p;
14105 {
14106   tree decl = NULL_TREE;
14107   tree parameter_list;
14108   bool friend_p = false;
14109
14110   /* Look for the `template' keyword.  */
14111   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14112     return;
14113       
14114   /* And the `<'.  */
14115   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14116     return;
14117       
14118   /* Parse the template parameters.  */
14119   begin_template_parm_list ();
14120   /* If the next token is `>', then we have an invalid
14121      specialization.  Rather than complain about an invalid template
14122      parameter, issue an error message here.  */
14123   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14124     {
14125       cp_parser_error (parser, "invalid explicit specialization");
14126       parameter_list = NULL_TREE;
14127     }
14128   else
14129     parameter_list = cp_parser_template_parameter_list (parser);
14130   parameter_list = end_template_parm_list (parameter_list);
14131   /* Look for the `>'.  */
14132   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14133   /* We just processed one more parameter list.  */
14134   ++parser->num_template_parameter_lists;
14135   /* If the next token is `template', there are more template
14136      parameters.  */
14137   if (cp_lexer_next_token_is_keyword (parser->lexer, 
14138                                       RID_TEMPLATE))
14139     cp_parser_template_declaration_after_export (parser, member_p);
14140   else
14141     {
14142       decl = cp_parser_single_declaration (parser,
14143                                            member_p,
14144                                            &friend_p);
14145
14146       /* If this is a member template declaration, let the front
14147          end know.  */
14148       if (member_p && !friend_p && decl)
14149         decl = finish_member_template_decl (decl);
14150       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14151         make_friend_class (current_class_type, TREE_TYPE (decl));
14152     }
14153   /* We are done with the current parameter list.  */
14154   --parser->num_template_parameter_lists;
14155
14156   /* Finish up.  */
14157   finish_template_decl (parameter_list);
14158
14159   /* Register member declarations.  */
14160   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14161     finish_member_declaration (decl);
14162
14163   /* If DECL is a function template, we must return to parse it later.
14164      (Even though there is no definition, there might be default
14165      arguments that need handling.)  */
14166   if (member_p && decl 
14167       && (TREE_CODE (decl) == FUNCTION_DECL
14168           || DECL_FUNCTION_TEMPLATE_P (decl)))
14169     TREE_VALUE (parser->unparsed_functions_queues)
14170       = tree_cons (current_class_type, decl, 
14171                    TREE_VALUE (parser->unparsed_functions_queues));
14172 }
14173
14174 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14175    `function-definition' sequence.  MEMBER_P is true, this declaration
14176    appears in a class scope.
14177
14178    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14179    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14180
14181 static tree
14182 cp_parser_single_declaration (parser, 
14183                               member_p,
14184                               friend_p)
14185      cp_parser *parser;
14186      bool member_p;
14187      bool *friend_p;
14188 {
14189   bool declares_class_or_enum;
14190   tree decl = NULL_TREE;
14191   tree decl_specifiers;
14192   tree attributes;
14193   tree access_checks;
14194
14195   /* Parse the dependent declaration.  We don't know yet
14196      whether it will be a function-definition.  */
14197   cp_parser_parse_tentatively (parser);
14198   /* Defer access checks until we know what is being declared.  */
14199   cp_parser_start_deferring_access_checks (parser);
14200   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14201      alternative.  */
14202   decl_specifiers 
14203     = cp_parser_decl_specifier_seq (parser,
14204                                     CP_PARSER_FLAGS_OPTIONAL,
14205                                     &attributes,
14206                                     &declares_class_or_enum);
14207   /* Gather up the access checks that occurred the
14208      decl-specifier-seq.  */
14209   access_checks = cp_parser_stop_deferring_access_checks (parser);
14210   /* Check for the declaration of a template class.  */
14211   if (declares_class_or_enum)
14212     {
14213       if (cp_parser_declares_only_class_p (parser))
14214         {
14215           decl = shadow_tag (decl_specifiers);
14216           if (decl)
14217             decl = TYPE_NAME (decl);
14218           else
14219             decl = error_mark_node;
14220         }
14221     }
14222   else
14223     decl = NULL_TREE;
14224   /* If it's not a template class, try for a template function.  If
14225      the next token is a `;', then this declaration does not declare
14226      anything.  But, if there were errors in the decl-specifiers, then
14227      the error might well have come from an attempted class-specifier.
14228      In that case, there's no need to warn about a missing declarator.  */
14229   if (!decl
14230       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14231           || !value_member (error_mark_node, decl_specifiers)))
14232     decl = cp_parser_init_declarator (parser, 
14233                                       decl_specifiers,
14234                                       attributes,
14235                                       access_checks,
14236                                       /*function_definition_allowed_p=*/false,
14237                                       member_p,
14238                                       /*function_definition_p=*/NULL);
14239   /* Clear any current qualification; whatever comes next is the start
14240      of something new.  */
14241   parser->scope = NULL_TREE;
14242   parser->qualifying_scope = NULL_TREE;
14243   parser->object_scope = NULL_TREE;
14244   /* Look for a trailing `;' after the declaration.  */
14245   if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14246       && cp_parser_committed_to_tentative_parse (parser))
14247     cp_parser_skip_to_end_of_block_or_statement (parser);
14248   /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS.  */
14249   if (cp_parser_parse_definitely (parser))
14250     {
14251       if (friend_p)
14252         *friend_p = cp_parser_friend_p (decl_specifiers);
14253     }
14254   /* Otherwise, try a function-definition.  */
14255   else
14256     decl = cp_parser_function_definition (parser, friend_p);
14257
14258   return decl;
14259 }
14260
14261 /* Parse a functional cast to TYPE.  Returns an expression
14262    representing the cast.  */
14263
14264 static tree
14265 cp_parser_functional_cast (parser, type)
14266      cp_parser *parser;
14267      tree type;
14268 {
14269   tree expression_list;
14270
14271   /* Look for the opening `('.  */
14272   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14273     return error_mark_node;
14274   /* If the next token is not an `)', there are arguments to the
14275      cast.  */
14276   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14277     expression_list = cp_parser_expression_list (parser);
14278   else
14279     expression_list = NULL_TREE;
14280   /* Look for the closing `)'.  */
14281   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14282
14283   return build_functional_cast (type, expression_list);
14284 }
14285
14286 /* MEMBER_FUNCTION is a member function, or a friend.  If default
14287    arguments, or the body of the function have not yet been parsed,
14288    parse them now.  */
14289
14290 static void
14291 cp_parser_late_parsing_for_member (parser, member_function)
14292      cp_parser *parser;
14293      tree member_function;
14294 {
14295   cp_lexer *saved_lexer;
14296
14297   /* If this member is a template, get the underlying
14298      FUNCTION_DECL.  */
14299   if (DECL_FUNCTION_TEMPLATE_P (member_function))
14300     member_function = DECL_TEMPLATE_RESULT (member_function);
14301
14302   /* There should not be any class definitions in progress at this
14303      point; the bodies of members are only parsed outside of all class
14304      definitions.  */
14305   my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14306   /* While we're parsing the member functions we might encounter more
14307      classes.  We want to handle them right away, but we don't want
14308      them getting mixed up with functions that are currently in the
14309      queue.  */
14310   parser->unparsed_functions_queues
14311     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14312
14313   /* Make sure that any template parameters are in scope.  */
14314   maybe_begin_member_template_processing (member_function);
14315
14316   /* If there are default arguments that have not yet been processed,
14317      take care of them now.  */
14318   cp_parser_late_parsing_default_args (parser, TREE_TYPE (member_function),
14319                                        DECL_FUNCTION_MEMBER_P (member_function)
14320                                        ? DECL_CONTEXT (member_function)
14321                                        : NULL_TREE);
14322
14323   /* If the body of the function has not yet been parsed, parse it
14324      now.  */
14325   if (DECL_PENDING_INLINE_P (member_function))
14326     {
14327       tree function_scope;
14328       cp_token_cache *tokens;
14329
14330       /* The function is no longer pending; we are processing it.  */
14331       tokens = DECL_PENDING_INLINE_INFO (member_function);
14332       DECL_PENDING_INLINE_INFO (member_function) = NULL;
14333       DECL_PENDING_INLINE_P (member_function) = 0;
14334       /* If this was an inline function in a local class, enter the scope
14335          of the containing function.  */
14336       function_scope = decl_function_context (member_function);
14337       if (function_scope)
14338         push_function_context_to (function_scope);
14339       
14340       /* Save away the current lexer.  */
14341       saved_lexer = parser->lexer;
14342       /* Make a new lexer to feed us the tokens saved for this function.  */
14343       parser->lexer = cp_lexer_new_from_tokens (tokens);
14344       parser->lexer->next = saved_lexer;
14345       
14346       /* Set the current source position to be the location of the first
14347          token in the saved inline body.  */
14348       cp_lexer_set_source_position_from_token 
14349         (parser->lexer,
14350          cp_lexer_peek_token (parser->lexer));
14351       
14352       /* Let the front end know that we going to be defining this
14353          function.  */
14354       start_function (NULL_TREE, member_function, NULL_TREE,
14355                       SF_PRE_PARSED | SF_INCLASS_INLINE);
14356       
14357       /* Now, parse the body of the function.  */
14358       cp_parser_function_definition_after_declarator (parser,
14359                                                       /*inline_p=*/true);
14360       
14361       /* Leave the scope of the containing function.  */
14362       if (function_scope)
14363         pop_function_context_from (function_scope);
14364       /* Restore the lexer.  */
14365       parser->lexer = saved_lexer;
14366     }
14367
14368   /* Remove any template parameters from the symbol table.  */
14369   maybe_end_member_template_processing ();
14370
14371   /* Restore the queue.  */
14372   parser->unparsed_functions_queues 
14373     = TREE_CHAIN (parser->unparsed_functions_queues);
14374 }
14375
14376 /* TYPE is a FUNCTION_TYPE or METHOD_TYPE which contains a parameter
14377    with an unparsed DEFAULT_ARG.  If non-NULL, SCOPE is the class in
14378    whose context name lookups in the default argument should occur.
14379    Parse the default args now.  */
14380
14381 static void
14382 cp_parser_late_parsing_default_args (cp_parser *parser, tree type, tree scope)
14383 {
14384   cp_lexer *saved_lexer;
14385   cp_token_cache *tokens;
14386   bool saved_local_variables_forbidden_p;
14387   tree parameters;
14388   
14389   for (parameters = TYPE_ARG_TYPES (type);
14390        parameters;
14391        parameters = TREE_CHAIN (parameters))
14392     {
14393       if (!TREE_PURPOSE (parameters)
14394           || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14395         continue;
14396   
14397        /* Save away the current lexer.  */
14398       saved_lexer = parser->lexer;
14399        /* Create a new one, using the tokens we have saved.  */
14400       tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
14401       parser->lexer = cp_lexer_new_from_tokens (tokens);
14402
14403        /* Set the current source position to be the location of the
14404           first token in the default argument.  */
14405       cp_lexer_set_source_position_from_token 
14406         (parser->lexer, cp_lexer_peek_token (parser->lexer));
14407
14408        /* Local variable names (and the `this' keyword) may not appear
14409           in a default argument.  */
14410       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14411       parser->local_variables_forbidden_p = true;
14412        /* Parse the assignment-expression.  */
14413       if (scope)
14414         push_nested_class (scope, 1);
14415       TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14416       if (scope)
14417         pop_nested_class ();
14418
14419        /* Restore saved state.  */
14420       parser->lexer = saved_lexer;
14421       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14422     }
14423 }
14424
14425 /* Parse the operand of `sizeof' (or a similar operator).  Returns
14426    either a TYPE or an expression, depending on the form of the
14427    input.  The KEYWORD indicates which kind of expression we have
14428    encountered.  */
14429
14430 static tree
14431 cp_parser_sizeof_operand (parser, keyword)
14432      cp_parser *parser;
14433      enum rid keyword;
14434 {
14435   static const char *format;
14436   tree expr = NULL_TREE;
14437   const char *saved_message;
14438   bool saved_constant_expression_p;
14439
14440   /* Initialize FORMAT the first time we get here.  */
14441   if (!format)
14442     format = "types may not be defined in `%s' expressions";
14443
14444   /* Types cannot be defined in a `sizeof' expression.  Save away the
14445      old message.  */
14446   saved_message = parser->type_definition_forbidden_message;
14447   /* And create the new one.  */
14448   parser->type_definition_forbidden_message 
14449     = ((const char *) 
14450        xmalloc (strlen (format) 
14451                 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14452                 + 1 /* `\0' */));
14453   sprintf ((char *) parser->type_definition_forbidden_message,
14454            format, IDENTIFIER_POINTER (ridpointers[keyword]));
14455
14456   /* The restrictions on constant-expressions do not apply inside
14457      sizeof expressions.  */
14458   saved_constant_expression_p = parser->constant_expression_p;
14459   parser->constant_expression_p = false;
14460
14461   /* Do not actually evaluate the expression.  */
14462   ++skip_evaluation;
14463   /* If it's a `(', then we might be looking at the type-id
14464      construction.  */
14465   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14466     {
14467       tree type;
14468
14469       /* We can't be sure yet whether we're looking at a type-id or an
14470          expression.  */
14471       cp_parser_parse_tentatively (parser);
14472       /* Consume the `('.  */
14473       cp_lexer_consume_token (parser->lexer);
14474       /* Parse the type-id.  */
14475       type = cp_parser_type_id (parser);
14476       /* Now, look for the trailing `)'.  */
14477       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14478       /* If all went well, then we're done.  */
14479       if (cp_parser_parse_definitely (parser))
14480         {
14481           /* Build a list of decl-specifiers; right now, we have only
14482              a single type-specifier.  */
14483           type = build_tree_list (NULL_TREE,
14484                                   type);
14485
14486           /* Call grokdeclarator to figure out what type this is.  */
14487           expr = grokdeclarator (NULL_TREE,
14488                                  type,
14489                                  TYPENAME,
14490                                  /*initialized=*/0,
14491                                  /*attrlist=*/NULL);
14492         }
14493     }
14494
14495   /* If the type-id production did not work out, then we must be
14496      looking at the unary-expression production.  */
14497   if (!expr)
14498     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14499   /* Go back to evaluating expressions.  */
14500   --skip_evaluation;
14501
14502   /* Free the message we created.  */
14503   free ((char *) parser->type_definition_forbidden_message);
14504   /* And restore the old one.  */
14505   parser->type_definition_forbidden_message = saved_message;
14506   parser->constant_expression_p = saved_constant_expression_p;
14507
14508   return expr;
14509 }
14510
14511 /* If the current declaration has no declarator, return true.  */
14512
14513 static bool
14514 cp_parser_declares_only_class_p (cp_parser *parser)
14515 {
14516   /* If the next token is a `;' or a `,' then there is no 
14517      declarator.  */
14518   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14519           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14520 }
14521
14522 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14523    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
14524
14525 static bool
14526 cp_parser_friend_p (decl_specifiers)
14527      tree decl_specifiers;
14528 {
14529   while (decl_specifiers)
14530     {
14531       /* See if this decl-specifier is `friend'.  */
14532       if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14533           && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14534         return true;
14535
14536       /* Go on to the next decl-specifier.  */
14537       decl_specifiers = TREE_CHAIN (decl_specifiers);
14538     }
14539
14540   return false;
14541 }
14542
14543 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
14544    issue an error message indicating that TOKEN_DESC was expected.
14545    
14546    Returns the token consumed, if the token had the appropriate type.
14547    Otherwise, returns NULL.  */
14548
14549 static cp_token *
14550 cp_parser_require (parser, type, token_desc)
14551      cp_parser *parser;
14552      enum cpp_ttype type;
14553      const char *token_desc;
14554 {
14555   if (cp_lexer_next_token_is (parser->lexer, type))
14556     return cp_lexer_consume_token (parser->lexer);
14557   else
14558     {
14559       /* Output the MESSAGE -- unless we're parsing tentatively.  */
14560       if (!cp_parser_simulate_error (parser))
14561         error ("expected %s", token_desc);
14562       return NULL;
14563     }
14564 }
14565
14566 /* Like cp_parser_require, except that tokens will be skipped until
14567    the desired token is found.  An error message is still produced if
14568    the next token is not as expected.  */
14569
14570 static void
14571 cp_parser_skip_until_found (parser, type, token_desc)
14572      cp_parser *parser;
14573      enum cpp_ttype type;
14574      const char *token_desc;
14575 {
14576   cp_token *token;
14577   unsigned nesting_depth = 0;
14578
14579   if (cp_parser_require (parser, type, token_desc))
14580     return;
14581
14582   /* Skip tokens until the desired token is found.  */
14583   while (true)
14584     {
14585       /* Peek at the next token.  */
14586       token = cp_lexer_peek_token (parser->lexer);
14587       /* If we've reached the token we want, consume it and 
14588          stop.  */
14589       if (token->type == type && !nesting_depth)
14590         {
14591           cp_lexer_consume_token (parser->lexer);
14592           return;
14593         }
14594       /* If we've run out of tokens, stop.  */
14595       if (token->type == CPP_EOF)
14596         return;
14597       if (token->type == CPP_OPEN_BRACE 
14598           || token->type == CPP_OPEN_PAREN
14599           || token->type == CPP_OPEN_SQUARE)
14600         ++nesting_depth;
14601       else if (token->type == CPP_CLOSE_BRACE 
14602                || token->type == CPP_CLOSE_PAREN
14603                || token->type == CPP_CLOSE_SQUARE)
14604         {
14605           if (nesting_depth-- == 0)
14606             return;
14607         }
14608       /* Consume this token.  */
14609       cp_lexer_consume_token (parser->lexer);
14610     }
14611 }
14612
14613 /* If the next token is the indicated keyword, consume it.  Otherwise,
14614    issue an error message indicating that TOKEN_DESC was expected.
14615    
14616    Returns the token consumed, if the token had the appropriate type.
14617    Otherwise, returns NULL.  */
14618
14619 static cp_token *
14620 cp_parser_require_keyword (parser, keyword, token_desc)
14621      cp_parser *parser;
14622      enum rid keyword;
14623      const char *token_desc;
14624 {
14625   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14626
14627   if (token && token->keyword != keyword)
14628     {
14629       dyn_string_t error_msg;
14630
14631       /* Format the error message.  */
14632       error_msg = dyn_string_new (0);
14633       dyn_string_append_cstr (error_msg, "expected ");
14634       dyn_string_append_cstr (error_msg, token_desc);
14635       cp_parser_error (parser, error_msg->s);
14636       dyn_string_delete (error_msg);
14637       return NULL;
14638     }
14639
14640   return token;
14641 }
14642
14643 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14644    function-definition.  */
14645
14646 static bool 
14647 cp_parser_token_starts_function_definition_p (token)
14648      cp_token *token;
14649 {
14650   return (/* An ordinary function-body begins with an `{'.  */
14651           token->type == CPP_OPEN_BRACE
14652           /* A ctor-initializer begins with a `:'.  */
14653           || token->type == CPP_COLON
14654           /* A function-try-block begins with `try'.  */
14655           || token->keyword == RID_TRY
14656           /* The named return value extension begins with `return'.  */
14657           || token->keyword == RID_RETURN);
14658 }
14659
14660 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14661    definition.  */
14662
14663 static bool
14664 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14665 {
14666   cp_token *token;
14667
14668   token = cp_lexer_peek_token (parser->lexer);
14669   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14670 }
14671
14672 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14673    or none_type otherwise.  */
14674
14675 static enum tag_types
14676 cp_parser_token_is_class_key (token)
14677      cp_token *token;
14678 {
14679   switch (token->keyword)
14680     {
14681     case RID_CLASS:
14682       return class_type;
14683     case RID_STRUCT:
14684       return record_type;
14685     case RID_UNION:
14686       return union_type;
14687       
14688     default:
14689       return none_type;
14690     }
14691 }
14692
14693 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
14694
14695 static void
14696 cp_parser_check_class_key (enum tag_types class_key, tree type)
14697 {
14698   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14699     pedwarn ("`%s' tag used in naming `%#T'",
14700             class_key == union_type ? "union"
14701              : class_key == record_type ? "struct" : "class", 
14702              type);
14703 }
14704                            
14705 /* Look for the `template' keyword, as a syntactic disambiguator.
14706    Return TRUE iff it is present, in which case it will be 
14707    consumed.  */
14708
14709 static bool
14710 cp_parser_optional_template_keyword (cp_parser *parser)
14711 {
14712   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14713     {
14714       /* The `template' keyword can only be used within templates;
14715          outside templates the parser can always figure out what is a
14716          template and what is not.  */
14717       if (!processing_template_decl)
14718         {
14719           error ("`template' (as a disambiguator) is only allowed "
14720                  "within templates");
14721           /* If this part of the token stream is rescanned, the same
14722              error message would be generated.  So, we purge the token
14723              from the stream.  */
14724           cp_lexer_purge_token (parser->lexer);
14725           return false;
14726         }
14727       else
14728         {
14729           /* Consume the `template' keyword.  */
14730           cp_lexer_consume_token (parser->lexer);
14731           return true;
14732         }
14733     }
14734
14735   return false;
14736 }
14737
14738 /* Add tokens to CACHE until an non-nested END token appears.  */
14739
14740 static void
14741 cp_parser_cache_group (cp_parser *parser, 
14742                        cp_token_cache *cache,
14743                        enum cpp_ttype end,
14744                        unsigned depth)
14745 {
14746   while (true)
14747     {
14748       cp_token *token;
14749
14750       /* Abort a parenthesized expression if we encounter a brace.  */
14751       if ((end == CPP_CLOSE_PAREN || depth == 0)
14752           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14753         return;
14754       /* Consume the next token.  */
14755       token = cp_lexer_consume_token (parser->lexer);
14756       /* If we've reached the end of the file, stop.  */
14757       if (token->type == CPP_EOF)
14758         return;
14759       /* Add this token to the tokens we are saving.  */
14760       cp_token_cache_push_token (cache, token);
14761       /* See if it starts a new group.  */
14762       if (token->type == CPP_OPEN_BRACE)
14763         {
14764           cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14765           if (depth == 0)
14766             return;
14767         }
14768       else if (token->type == CPP_OPEN_PAREN)
14769         cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14770       else if (token->type == end)
14771         return;
14772     }
14773 }
14774
14775 /* Begin parsing tentatively.  We always save tokens while parsing
14776    tentatively so that if the tentative parsing fails we can restore the
14777    tokens.  */
14778
14779 static void
14780 cp_parser_parse_tentatively (parser)
14781      cp_parser *parser;
14782 {
14783   /* Enter a new parsing context.  */
14784   parser->context = cp_parser_context_new (parser->context);
14785   /* Begin saving tokens.  */
14786   cp_lexer_save_tokens (parser->lexer);
14787   /* In order to avoid repetitive access control error messages,
14788      access checks are queued up until we are no longer parsing
14789      tentatively.  */
14790   cp_parser_start_deferring_access_checks (parser);
14791 }
14792
14793 /* Commit to the currently active tentative parse.  */
14794
14795 static void
14796 cp_parser_commit_to_tentative_parse (parser)
14797      cp_parser *parser;
14798 {
14799   cp_parser_context *context;
14800   cp_lexer *lexer;
14801
14802   /* Mark all of the levels as committed.  */
14803   lexer = parser->lexer;
14804   for (context = parser->context; context->next; context = context->next)
14805     {
14806       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14807         break;
14808       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14809       while (!cp_lexer_saving_tokens (lexer))
14810         lexer = lexer->next;
14811       cp_lexer_commit_tokens (lexer);
14812     }
14813 }
14814
14815 /* Abort the currently active tentative parse.  All consumed tokens
14816    will be rolled back, and no diagnostics will be issued.  */
14817
14818 static void
14819 cp_parser_abort_tentative_parse (parser)
14820      cp_parser *parser;
14821 {
14822   cp_parser_simulate_error (parser);
14823   /* Now, pretend that we want to see if the construct was
14824      successfully parsed.  */
14825   cp_parser_parse_definitely (parser);
14826 }
14827
14828 /* Stop parsing tentatively.  If a parse error has ocurred, restore the
14829    token stream.  Otherwise, commit to the tokens we have consumed.
14830    Returns true if no error occurred; false otherwise.  */
14831
14832 static bool
14833 cp_parser_parse_definitely (parser)
14834      cp_parser *parser;
14835 {
14836   bool error_occurred;
14837   cp_parser_context *context;
14838
14839   /* Remember whether or not an error ocurred, since we are about to
14840      destroy that information.  */
14841   error_occurred = cp_parser_error_occurred (parser);
14842   /* Remove the topmost context from the stack.  */
14843   context = parser->context;
14844   parser->context = context->next;
14845   /* If no parse errors occurred, commit to the tentative parse.  */
14846   if (!error_occurred)
14847     {
14848       /* Commit to the tokens read tentatively, unless that was
14849          already done.  */
14850       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14851         cp_lexer_commit_tokens (parser->lexer);
14852       if (!parser->context->deferring_access_checks_p)
14853         /* If in the parent context we are not deferring checks, then
14854            these perform these checks now.  */
14855         (cp_parser_perform_deferred_access_checks 
14856          (context->deferred_access_checks));
14857       else
14858         /* Any lookups that were deferred during the tentative parse are
14859            still deferred.  */
14860         parser->context->deferred_access_checks 
14861           = chainon (parser->context->deferred_access_checks,
14862                      context->deferred_access_checks);
14863     }
14864   /* Otherwise, if errors occurred, roll back our state so that things
14865      are just as they were before we began the tentative parse.  */
14866   else
14867     cp_lexer_rollback_tokens (parser->lexer);
14868   /* Add the context to the front of the free list.  */
14869   context->next = cp_parser_context_free_list;
14870   cp_parser_context_free_list = context;
14871
14872   return !error_occurred;
14873 }
14874
14875 /* Returns true if we are parsing tentatively -- but have decided that
14876    we will stick with this tentative parse, even if errors occur.  */
14877
14878 static bool
14879 cp_parser_committed_to_tentative_parse (parser)
14880      cp_parser *parser;
14881 {
14882   return (cp_parser_parsing_tentatively (parser)
14883           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14884 }
14885
14886 /* Returns non-zero iff an error has occurred during the most recent
14887    tentative parse.  */
14888    
14889 static bool
14890 cp_parser_error_occurred (parser)
14891      cp_parser *parser;
14892 {
14893   return (cp_parser_parsing_tentatively (parser)
14894           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14895 }
14896
14897 /* Returns non-zero if GNU extensions are allowed.  */
14898
14899 static bool
14900 cp_parser_allow_gnu_extensions_p (parser)
14901      cp_parser *parser;
14902 {
14903   return parser->allow_gnu_extensions_p;
14904 }
14905
14906 \f
14907
14908 /* The parser.  */
14909
14910 static GTY (()) cp_parser *the_parser;
14911
14912 /* External interface.  */
14913
14914 /* Parse the entire translation unit.  */
14915
14916 int
14917 yyparse ()
14918 {
14919   bool error_occurred;
14920
14921   the_parser = cp_parser_new ();
14922   error_occurred = cp_parser_translation_unit (the_parser);
14923   the_parser = NULL;
14924
14925   return error_occurred;
14926 }
14927
14928 /* Clean up after parsing the entire translation unit.  */
14929
14930 void
14931 free_parser_stacks ()
14932 {
14933   /* Nothing to do.  */
14934 }
14935
14936 /* This variable must be provided by every front end.  */
14937
14938 int yydebug;
14939
14940 #include "gt-cp-parser.h"