OSDN Git Service

* Make-lang.in (po-generated): Remove parse.c.
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>.
4
5    This file is part of GNU CC.
6
7    GNU CC 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    GNU CC 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 GNU CC; 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 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 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 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 /* Non-zero if we are presently saving tokens.  */
372
373 static int
374 cp_lexer_saving_tokens (lexer)
375      const cp_lexer *lexer;
376 {
377   return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
378 }
379
380 /* TOKEN points into the circular token buffer.  Return a pointer to
381    the next token in the buffer.  */
382
383 static cp_token *
384 cp_lexer_next_token (lexer, token)
385      cp_lexer *lexer;
386      cp_token *token;
387 {
388   token++;
389   if (token == lexer->buffer_end)
390     token = lexer->buffer;
391   return token;
392 }
393
394 /* Return a pointer to the token that is N tokens beyond TOKEN in the
395    buffer.  */
396
397 static cp_token *
398 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
399 {
400   token += n;
401   if (token >= lexer->buffer_end)
402     token = lexer->buffer + (token - lexer->buffer_end);
403   return token;
404 }
405
406 /* Returns the number of times that START would have to be incremented
407    to reach FINISH.  If START and FINISH are the same, returns zero.  */
408
409 static ptrdiff_t
410 cp_lexer_token_difference (lexer, start, finish)
411      cp_lexer *lexer;
412      cp_token *start;
413      cp_token *finish;
414 {
415   if (finish >= start)
416     return finish - start;
417   else
418     return ((lexer->buffer_end - lexer->buffer)
419             - (start - finish));
420 }
421
422 /* Obtain another token from the C preprocessor and add it to the
423    token buffer.  Returns the newly read token.  */
424
425 static cp_token *
426 cp_lexer_read_token (lexer)
427      cp_lexer *lexer;
428 {
429   cp_token *token;
430
431   /* Make sure there is room in the buffer.  */
432   cp_lexer_maybe_grow_buffer (lexer);
433
434   /* If there weren't any tokens, then this one will be the first.  */
435   if (!lexer->first_token)
436     lexer->first_token = lexer->last_token;
437   /* Similarly, if there were no available tokens, there is one now.  */
438   if (!lexer->next_token)
439     lexer->next_token = lexer->last_token;
440
441   /* Figure out where we're going to store the new token.  */
442   token = lexer->last_token;
443
444   /* Get a new token from the preprocessor.  */
445   cp_lexer_get_preprocessor_token (lexer, token);
446
447   /* Increment LAST_TOKEN.  */
448   lexer->last_token = cp_lexer_next_token (lexer, token);
449
450   /* The preprocessor does not yet do translation phase six, i.e., the
451      combination of adjacent string literals.  Therefore, we do it
452      here.  */
453   if (token->type == CPP_STRING || token->type == CPP_WSTRING)
454     {
455       ptrdiff_t delta;
456       int i;
457
458       /* When we grow the buffer, we may invalidate TOKEN.  So, save
459          the distance from the beginning of the BUFFER so that we can
460          recaulate it.  */
461       delta = cp_lexer_token_difference (lexer, lexer->buffer, token);
462       /* Make sure there is room in the buffer for another token.  */
463       cp_lexer_maybe_grow_buffer (lexer);
464       /* Restore TOKEN.  */
465       token = lexer->buffer;
466       for (i = 0; i < delta; ++i)
467         token = cp_lexer_next_token (lexer, token);
468
469       VARRAY_PUSH_TREE (lexer->string_tokens, token->value);
470       while (true)
471         {
472           /* Read the token after TOKEN.  */
473           cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
474           /* See whether it's another string constant.  */
475           if (lexer->last_token->type != token->type)
476             {
477               /* If not, then it will be the next real token.  */
478               lexer->last_token = cp_lexer_next_token (lexer, 
479                                                        lexer->last_token);
480               break;
481             }
482
483           /* Chain the strings together.  */
484           VARRAY_PUSH_TREE (lexer->string_tokens, 
485                             lexer->last_token->value);
486         }
487
488       /* Create a single STRING_CST.  Curiously we have to call
489          combine_strings even if there is only a single string in
490          order to get the type set correctly.  */
491       token->value = combine_strings (lexer->string_tokens);
492       VARRAY_CLEAR (lexer->string_tokens);
493       token->value = fix_string_type (token->value);
494       /* Strings should have type `const char []'.  Right now, we will
495          have an ARRAY_TYPE that is constant rather than an array of
496          constant elements.  */
497       if (flag_const_strings)
498         {
499           tree type;
500
501           /* Get the current type.  It will be an ARRAY_TYPE.  */
502           type = TREE_TYPE (token->value);
503           /* Use build_cplus_array_type to rebuild the array, thereby
504              getting the right type.  */
505           type = build_cplus_array_type (TREE_TYPE (type),
506                                          TYPE_DOMAIN (type));
507           /* Reset the type of the token.  */
508           TREE_TYPE (token->value) = type;
509         }
510     }
511
512   return token;
513 }
514
515 /* If the circular buffer is full, make it bigger.  */
516
517 static void
518 cp_lexer_maybe_grow_buffer (lexer)
519      cp_lexer *lexer;
520 {
521   /* If the buffer is full, enlarge it.  */
522   if (lexer->last_token == lexer->first_token)
523     {
524       cp_token *new_buffer;
525       cp_token *old_buffer;
526       cp_token *new_first_token;
527       ptrdiff_t buffer_length;
528       size_t num_tokens_to_copy;
529
530       /* Remember the current buffer pointer.  It will become invalid,
531          but we will need to do pointer arithmetic involving this
532          value.  */
533       old_buffer = lexer->buffer;
534       /* Compute the current buffer size.  */
535       buffer_length = lexer->buffer_end - lexer->buffer;
536       /* Allocate a buffer twice as big.  */
537       new_buffer = ((cp_token *)
538                     ggc_realloc (lexer->buffer, 
539                                  2 * buffer_length * sizeof (cp_token)));
540       
541       /* Because the buffer is circular, logically consecutive tokens
542          are not necessarily placed consecutively in memory.
543          Therefore, we must keep move the tokens that were before
544          FIRST_TOKEN to the second half of the newly allocated
545          buffer.  */
546       num_tokens_to_copy = (lexer->first_token - old_buffer);
547       memcpy (new_buffer + buffer_length,
548               new_buffer,
549               num_tokens_to_copy * sizeof (cp_token));
550       /* Clear the rest of the buffer.  We never look at this storage,
551          but the garbage collector may.  */
552       memset (new_buffer + buffer_length + num_tokens_to_copy, 0, 
553               (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
554
555       /* Now recompute all of the buffer pointers.  */
556       new_first_token 
557         = new_buffer + (lexer->first_token - old_buffer);
558       if (lexer->next_token != NULL)
559         {
560           ptrdiff_t next_token_delta;
561
562           if (lexer->next_token > lexer->first_token)
563             next_token_delta = lexer->next_token - lexer->first_token;
564           else
565             next_token_delta = 
566               buffer_length - (lexer->first_token - lexer->next_token);
567           lexer->next_token = new_first_token + next_token_delta;
568         }
569       lexer->last_token = new_first_token + buffer_length;
570       lexer->buffer = new_buffer;
571       lexer->buffer_end = new_buffer + buffer_length * 2;
572       lexer->first_token = new_first_token;
573     }
574 }
575
576 /* Store the next token from the preprocessor in *TOKEN.  */
577
578 static void 
579 cp_lexer_get_preprocessor_token (lexer, token)
580      cp_lexer *lexer ATTRIBUTE_UNUSED;
581      cp_token *token;
582 {
583   bool done;
584
585   /* If this not the main lexer, return a terminating CPP_EOF token.  */
586   if (!lexer->main_lexer_p)
587     {
588       token->type = CPP_EOF;
589       token->line_number = 0;
590       token->file_name = NULL;
591       token->value = NULL_TREE;
592       token->keyword = RID_MAX;
593
594       return;
595     }
596
597   done = false;
598   /* Keep going until we get a token we like.  */
599   while (!done)
600     {
601       /* Get a new token from the preprocessor.  */
602       token->type = c_lex (&token->value);
603       /* Issue messages about tokens we cannot process.  */
604       switch (token->type)
605         {
606         case CPP_ATSIGN:
607         case CPP_HASH:
608         case CPP_PASTE:
609           error ("invalid token");
610           break;
611
612         case CPP_OTHER:
613           /* These tokens are already warned about by c_lex.  */
614           break;
615
616         default:
617           /* This is a good token, so we exit the loop.  */
618           done = true;
619           break;
620         }
621     }
622   /* Now we've got our token.  */
623   token->line_number = lineno;
624   token->file_name = input_filename;
625
626   /* Check to see if this token is a keyword.  */
627   if (token->type == CPP_NAME 
628       && C_IS_RESERVED_WORD (token->value))
629     {
630       /* Mark this token as a keyword.  */
631       token->type = CPP_KEYWORD;
632       /* Record which keyword.  */
633       token->keyword = C_RID_CODE (token->value);
634       /* Update the value.  Some keywords are mapped to particular
635          entities, rather than simply having the value of the
636          corresponding IDENTIFIER_NODE.  For example, `__const' is
637          mapped to `const'.  */
638       token->value = ridpointers[token->keyword];
639     }
640   else
641     token->keyword = RID_MAX;
642 }
643
644 /* Return a pointer to the next token in the token stream, but do not
645    consume it.  */
646
647 static cp_token *
648 cp_lexer_peek_token (lexer)
649      cp_lexer *lexer;
650 {
651   cp_token *token;
652
653   /* If there are no tokens, read one now.  */
654   if (!lexer->next_token)
655     cp_lexer_read_token (lexer);
656
657   /* Provide debugging output.  */
658   if (cp_lexer_debugging_p (lexer))
659     {
660       fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
661       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
662       fprintf (cp_lexer_debug_stream, "\n");
663     }
664
665   token = lexer->next_token;
666   cp_lexer_set_source_position_from_token (lexer, token);
667   return token;
668 }
669
670 /* Return true if the next token has the indicated TYPE.  */
671
672 static bool
673 cp_lexer_next_token_is (lexer, type)
674      cp_lexer *lexer;
675      enum cpp_ttype type;
676 {
677   cp_token *token;
678
679   /* Peek at the next token.  */
680   token = cp_lexer_peek_token (lexer);
681   /* Check to see if it has the indicated TYPE.  */
682   return token->type == type;
683 }
684
685 /* Return true if the next token does not have the indicated TYPE.  */
686
687 static bool
688 cp_lexer_next_token_is_not (lexer, type)
689      cp_lexer *lexer;
690      enum cpp_ttype type;
691 {
692   return !cp_lexer_next_token_is (lexer, type);
693 }
694
695 /* Return true if the next token is the indicated KEYWORD.  */
696
697 static bool
698 cp_lexer_next_token_is_keyword (lexer, keyword)
699      cp_lexer *lexer;
700      enum rid keyword;
701 {
702   cp_token *token;
703
704   /* Peek at the next token.  */
705   token = cp_lexer_peek_token (lexer);
706   /* Check to see if it is the indicated keyword.  */
707   return token->keyword == keyword;
708 }
709
710 /* Return a pointer to the Nth token in the token stream.  If N is 1,
711    then this is precisely equivalent to cp_lexer_peek_token.  */
712
713 static cp_token *
714 cp_lexer_peek_nth_token (lexer, n)
715      cp_lexer *lexer;
716      size_t n;
717 {
718   cp_token *token;
719
720   /* N is 1-based, not zero-based.  */
721   my_friendly_assert (n > 0, 20000224);
722
723   /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary.  */
724   token = lexer->next_token;
725   /* If there are no tokens in the buffer, get one now.  */
726   if (!token)
727     {
728       cp_lexer_read_token (lexer);
729       token = lexer->next_token;
730     }
731
732   /* Now, read tokens until we have enough.  */
733   while (--n > 0)
734     {
735       /* Advance to the next token.  */
736       token = cp_lexer_next_token (lexer, token);
737       /* If that's all the tokens we have, read a new one.  */
738       if (token == lexer->last_token)
739         token = cp_lexer_read_token (lexer);
740     }
741
742   return token;
743 }
744
745 /* Consume the next token.  The pointer returned is valid only until
746    another token is read.  Callers should preserve copy the token
747    explicitly if they will need its value for a longer period of
748    time.  */
749
750 static cp_token *
751 cp_lexer_consume_token (lexer)
752      cp_lexer *lexer;
753 {
754   cp_token *token;
755
756   /* If there are no tokens, read one now.  */
757   if (!lexer->next_token)
758     cp_lexer_read_token (lexer);
759
760   /* Remember the token we'll be returning.  */
761   token = lexer->next_token;
762
763   /* Increment NEXT_TOKEN.  */
764   lexer->next_token = cp_lexer_next_token (lexer, 
765                                            lexer->next_token);
766   /* Check to see if we're all out of tokens.  */
767   if (lexer->next_token == lexer->last_token)
768     lexer->next_token = NULL;
769
770   /* If we're not saving tokens, then move FIRST_TOKEN too.  */
771   if (!cp_lexer_saving_tokens (lexer))
772     {
773       /* If there are no tokens available, set FIRST_TOKEN to NULL.  */
774       if (!lexer->next_token)
775         lexer->first_token = NULL;
776       else
777         lexer->first_token = lexer->next_token;
778     }
779
780   /* Provide debugging output.  */
781   if (cp_lexer_debugging_p (lexer))
782     {
783       fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
784       cp_lexer_print_token (cp_lexer_debug_stream, token);
785       fprintf (cp_lexer_debug_stream, "\n");
786     }
787
788   return token;
789 }
790
791 /* Permanently remove the next token from the token stream.  There
792    must be a valid next token already; this token never reads
793    additional tokens from the preprocessor.  */
794
795 static void
796 cp_lexer_purge_token (cp_lexer *lexer)
797 {
798   cp_token *token;
799   cp_token *next_token;
800
801   token = lexer->next_token;
802   while (true) 
803     {
804       next_token = cp_lexer_next_token (lexer, token);
805       if (next_token == lexer->last_token)
806         break;
807       *token = *next_token;
808       token = next_token;
809     }
810
811   lexer->last_token = token;
812   /* The token purged may have been the only token remaining; if so,
813      clear NEXT_TOKEN.  */
814   if (lexer->next_token == token)
815     lexer->next_token = NULL;
816 }
817
818 /* Permanently remove all tokens after TOKEN, up to, but not
819    including, the token that will be returned next by
820    cp_lexer_peek_token.  */
821
822 static void
823 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
824 {
825   cp_token *peek;
826   cp_token *t1;
827   cp_token *t2;
828
829   if (lexer->next_token)
830     {
831       /* Copy the tokens that have not yet been read to the location
832          immediately following TOKEN.  */
833       t1 = cp_lexer_next_token (lexer, token);
834       t2 = peek = cp_lexer_peek_token (lexer);
835       /* Move tokens into the vacant area between TOKEN and PEEK.  */
836       while (t2 != lexer->last_token)
837         {
838           *t1 = *t2;
839           t1 = cp_lexer_next_token (lexer, t1);
840           t2 = cp_lexer_next_token (lexer, t2);
841         }
842       /* Now, the next available token is right after TOKEN.  */
843       lexer->next_token = cp_lexer_next_token (lexer, token);
844       /* And the last token is wherever we ended up.  */
845       lexer->last_token = t1;
846     }
847   else
848     {
849       /* There are no tokens in the buffer, so there is nothing to
850          copy.  The last token in the buffer is TOKEN itself.  */
851       lexer->last_token = cp_lexer_next_token (lexer, token);
852     }
853 }
854
855 /* Begin saving tokens.  All tokens consumed after this point will be
856    preserved.  */
857
858 static void
859 cp_lexer_save_tokens (lexer)
860      cp_lexer *lexer;
861 {
862   /* Provide debugging output.  */
863   if (cp_lexer_debugging_p (lexer))
864     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
865
866   /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
867      restore the tokens if required.  */
868   if (!lexer->next_token)
869     cp_lexer_read_token (lexer);
870
871   VARRAY_PUSH_INT (lexer->saved_tokens,
872                    cp_lexer_token_difference (lexer,
873                                               lexer->first_token,
874                                               lexer->next_token));
875 }
876
877 /* Commit to the portion of the token stream most recently saved.  */
878
879 static void
880 cp_lexer_commit_tokens (lexer)
881      cp_lexer *lexer;
882 {
883   /* Provide debugging output.  */
884   if (cp_lexer_debugging_p (lexer))
885     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
886
887   VARRAY_POP (lexer->saved_tokens);
888 }
889
890 /* Return all tokens saved since the last call to cp_lexer_save_tokens
891    to the token stream.  Stop saving tokens.  */
892
893 static void
894 cp_lexer_rollback_tokens (lexer)
895      cp_lexer *lexer;
896 {
897   size_t delta;
898
899   /* Provide debugging output.  */
900   if (cp_lexer_debugging_p (lexer))
901     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
902
903   /* Find the token that was the NEXT_TOKEN when we started saving
904      tokens.  */
905   delta = VARRAY_TOP_INT(lexer->saved_tokens);
906   /* Make it the next token again now.  */
907   lexer->next_token = cp_lexer_advance_token (lexer,
908                                               lexer->first_token, 
909                                               delta);
910   /* It might be the case that there wer no tokens when we started
911      saving tokens, but that there are some tokens now.  */
912   if (!lexer->next_token && lexer->first_token)
913     lexer->next_token = lexer->first_token;
914
915   /* Stop saving tokens.  */
916   VARRAY_POP (lexer->saved_tokens);
917 }
918
919 /* Set the current source position from the information stored in
920    TOKEN.  */
921
922 static void
923 cp_lexer_set_source_position_from_token (lexer, token)
924      cp_lexer *lexer ATTRIBUTE_UNUSED;
925      const cp_token *token;
926 {
927   /* Ideally, the source position information would not be a global
928      variable, but it is.  */
929
930   /* Update the line number.  */
931   if (token->type != CPP_EOF)
932     {
933       lineno = token->line_number;
934       input_filename = token->file_name;
935     }
936 }
937
938 /* Print a representation of the TOKEN on the STREAM.  */
939
940 static void
941 cp_lexer_print_token (stream, token)
942      FILE *stream;
943      cp_token *token;
944 {
945   const char *token_type = NULL;
946
947   /* Figure out what kind of token this is.  */
948   switch (token->type)
949     {
950     case CPP_EQ:
951       token_type = "EQ";
952       break;
953
954     case CPP_COMMA:
955       token_type = "COMMA";
956       break;
957
958     case CPP_OPEN_PAREN:
959       token_type = "OPEN_PAREN";
960       break;
961
962     case CPP_CLOSE_PAREN:
963       token_type = "CLOSE_PAREN";
964       break;
965
966     case CPP_OPEN_BRACE:
967       token_type = "OPEN_BRACE";
968       break;
969
970     case CPP_CLOSE_BRACE:
971       token_type = "CLOSE_BRACE";
972       break;
973
974     case CPP_SEMICOLON:
975       token_type = "SEMICOLON";
976       break;
977
978     case CPP_NAME:
979       token_type = "NAME";
980       break;
981
982     case CPP_EOF:
983       token_type = "EOF";
984       break;
985
986     case CPP_KEYWORD:
987       token_type = "keyword";
988       break;
989
990       /* This is not a token that we know how to handle yet.  */
991     default:
992       break;
993     }
994
995   /* If we have a name for the token, print it out.  Otherwise, we
996      simply give the numeric code.  */
997   if (token_type)
998     fprintf (stream, "%s", token_type);
999   else
1000     fprintf (stream, "%d", token->type);
1001   /* And, for an identifier, print the identifier name.  */
1002   if (token->type == CPP_NAME 
1003       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1004          For example, `struct' is mapped to an INTEGER_CST.  */
1005       || (token->type == CPP_KEYWORD 
1006           && TREE_CODE (token->value) == IDENTIFIER_NODE))
1007     fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
1008 }
1009
1010 /* Returns non-zero if debugging information should be output.  */
1011
1012 static bool
1013 cp_lexer_debugging_p (lexer)
1014      cp_lexer *lexer;
1015 {
1016   return lexer->debugging_p;
1017 }
1018
1019 /* Start emitting debugging information.  */
1020
1021 static void
1022 cp_lexer_start_debugging (lexer)
1023      cp_lexer *lexer;
1024 {
1025   ++lexer->debugging_p;
1026 }
1027   
1028 /* Stop emitting debugging information.  */
1029
1030 static void
1031 cp_lexer_stop_debugging (lexer)
1032      cp_lexer *lexer;
1033 {
1034   --lexer->debugging_p;
1035 }
1036
1037 \f
1038 /* The parser.  */
1039
1040 /* Overview
1041    --------
1042
1043    A cp_parser parses the token stream as specified by the C++
1044    grammar.  Its job is purely parsing, not semantic analysis.  For
1045    example, the parser breaks the token stream into declarators,
1046    expressions, statements, and other similar syntactic constructs.
1047    It does not check that the types of the expressions on either side
1048    of an assignment-statement are compatible, or that a function is
1049    not declared with a parameter of type `void'.
1050
1051    The parser invokes routines elsewhere in the compiler to perform
1052    semantic analysis and to build up the abstract syntax tree for the
1053    code processed.  
1054
1055    The parser (and the template instantiation code, which is, in a
1056    way, a close relative of parsing) are the only parts of the
1057    compiler that should be calling push_scope and pop_scope, or
1058    related functions.  The parser (and template instantiation code)
1059    keeps track of what scope is presently active; everything else
1060    should simply honor that.  (The code that generates static
1061    initializers may also need to set the scope, in order to check
1062    access control correctly when emitting the initializers.)
1063
1064    Methodology
1065    -----------
1066    
1067    The parser is of the standard recursive-descent variety.  Upcoming
1068    tokens in the token stream are examined in order to determine which
1069    production to use when parsing a non-terminal.  Some C++ constructs
1070    require arbitrary look ahead to disambiguate.  For example, it is
1071    impossible, in the general case, to tell whether a statement is an
1072    expression or declaration without scanning the entire statement.
1073    Therefore, the parser is capable of "parsing tentatively."  When the
1074    parser is not sure what construct comes next, it enters this mode.
1075    Then, while we attempt to parse the construct, the parser queues up
1076    error messages, rather than issuing them immediately, and saves the
1077    tokens it consumes.  If the construct is parsed successfully, the
1078    parser "commits", i.e., it issues any queued error messages and
1079    the tokens that were being preserved are permanently discarded.
1080    If, however, the construct is not parsed successfully, the parser
1081    rolls back its state completely so that it can resume parsing using
1082    a different alternative.
1083
1084    Future Improvements
1085    -------------------
1086    
1087    The performance of the parser could probably be improved
1088    substantially.  Some possible improvements include:
1089
1090      - The expression parser recurses through the various levels of
1091        precedence as specified in the grammar, rather than using an
1092        operator-precedence technique.  Therefore, parsing a simple
1093        identifier requires multiple recursive calls.
1094
1095      - We could often eliminate the need to parse tentatively by
1096        looking ahead a little bit.  In some places, this approach
1097        might not entirely eliminate the need to parse tentatively, but
1098        it might still speed up the average case.  */
1099
1100 /* Flags that are passed to some parsing functions.  These values can
1101    be bitwise-ored together.  */
1102
1103 typedef enum cp_parser_flags
1104 {
1105   /* No flags.  */
1106   CP_PARSER_FLAGS_NONE = 0x0,
1107   /* The construct is optional.  If it is not present, then no error
1108      should be issued.  */
1109   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1110   /* When parsing a type-specifier, do not allow user-defined types.  */
1111   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1112 } cp_parser_flags;
1113
1114 /* The different kinds of ids that we ecounter.  */
1115
1116 typedef enum cp_parser_id_kind
1117 {
1118   /* Not an id at all.  */
1119   CP_PARSER_ID_KIND_NONE,
1120   /* An unqualified-id that is not a template-id.  */
1121   CP_PARSER_ID_KIND_UNQUALIFIED,
1122   /* An unqualified template-id.  */
1123   CP_PARSER_ID_KIND_TEMPLATE_ID,
1124   /* A qualified-id.  */
1125   CP_PARSER_ID_KIND_QUALIFIED
1126 } cp_parser_id_kind;
1127
1128 /* A mapping from a token type to a corresponding tree node type.  */
1129
1130 typedef struct cp_parser_token_tree_map_node
1131 {
1132   /* The token type.  */
1133   enum cpp_ttype token_type;
1134   /* The corresponding tree code.  */
1135   enum tree_code tree_type;
1136 } cp_parser_token_tree_map_node;
1137
1138 /* A complete map consists of several ordinary entries, followed by a
1139    terminator.  The terminating entry has a token_type of CPP_EOF.  */
1140
1141 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1142
1143 /* The status of a tentative parse.  */
1144
1145 typedef enum cp_parser_status_kind
1146 {
1147   /* No errors have occurred.  */
1148   CP_PARSER_STATUS_KIND_NO_ERROR,
1149   /* An error has occurred.  */
1150   CP_PARSER_STATUS_KIND_ERROR,
1151   /* We are committed to this tentative parse, whether or not an error
1152      has occurred.  */
1153   CP_PARSER_STATUS_KIND_COMMITTED
1154 } cp_parser_status_kind;
1155
1156 /* Context that is saved and restored when parsing tentatively.  */
1157
1158 typedef struct cp_parser_context GTY (())
1159 {
1160   /* If this is a tentative parsing context, the status of the
1161      tentative parse.  */
1162   enum cp_parser_status_kind status;
1163   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1164      that are looked up in this context must be looked up both in the
1165      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1166      the context of the containing expression.  */
1167   tree object_type;
1168   /* A TREE_LIST representing name-lookups for which we have deferred
1169      checking access controls.  We cannot check the accessibility of
1170      names used in a decl-specifier-seq until we know what is being
1171      declared because code like:
1172
1173        class A { 
1174          class B {};
1175          B* f();
1176        }
1177
1178        A::B* A::f() { return 0; }
1179
1180      is valid, even though `A::B' is not generally accessible.  
1181
1182      The TREE_PURPOSE of each node is the scope used to qualify the
1183      name being looked up; the TREE_VALUE is the DECL to which the
1184      name was resolved.  */
1185   tree deferred_access_checks;
1186   /* TRUE iff we are deferring access checks.  */
1187   bool deferring_access_checks_p;
1188   /* The next parsing context in the stack.  */
1189   struct cp_parser_context *next;
1190 } cp_parser_context;
1191
1192 /* Prototypes.  */
1193
1194 /* Constructors and destructors.  */
1195
1196 static cp_parser_context *cp_parser_context_new
1197   PARAMS ((cp_parser_context *));
1198
1199 /* Constructors and destructors.  */
1200
1201 /* Construct a new context.  The context below this one on the stack
1202    is given by NEXT.  */
1203
1204 static cp_parser_context *
1205 cp_parser_context_new (next)
1206      cp_parser_context *next;
1207 {
1208   cp_parser_context *context;
1209
1210   /* Allocate the storage.  */
1211   context = ((cp_parser_context *) 
1212              ggc_alloc_cleared (sizeof (cp_parser_context)));
1213   /* No errors have occurred yet in this context.  */
1214   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1215   /* If this is not the bottomost context, copy information that we
1216      need from the previous context.  */
1217   if (next)
1218     {
1219       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1220          expression, then we are parsing one in this context, too.  */
1221       context->object_type = next->object_type;
1222       /* We are deferring access checks here if we were in the NEXT
1223          context.  */
1224       context->deferring_access_checks_p 
1225         = next->deferring_access_checks_p;
1226       /* Thread the stack.  */
1227       context->next = next;
1228     }
1229
1230   return context;
1231 }
1232
1233 /* The cp_parser structure represents the C++ parser.  */
1234
1235 typedef struct cp_parser GTY(())
1236 {
1237   /* The lexer from which we are obtaining tokens.  */
1238   cp_lexer *lexer;
1239
1240   /* The scope in which names should be looked up.  If NULL_TREE, then
1241      we look up names in the scope that is currently open in the
1242      source program.  If non-NULL, this is either a TYPE or
1243      NAMESPACE_DECL for the scope in which we should look.  
1244
1245      This value is not cleared automatically after a name is looked
1246      up, so we must be careful to clear it before starting a new look
1247      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1248      will look up `Z' in the scope of `X', rather than the current
1249      scope.)  Unfortunately, it is difficult to tell when name lookup
1250      is complete, because we sometimes peek at a token, look it up,
1251      and then decide not to consume it.  */
1252   tree scope;
1253
1254   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1255      last lookup took place.  OBJECT_SCOPE is used if an expression
1256      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1257      respectively.  QUALIFYING_SCOPE is used for an expression of the 
1258      form "X::Y"; it refers to X.  */
1259   tree object_scope;
1260   tree qualifying_scope;
1261
1262   /* A stack of parsing contexts.  All but the bottom entry on the
1263      stack will be tentative contexts.
1264
1265      We parse tentatively in order to determine which construct is in
1266      use in some situations.  For example, in order to determine
1267      whether a statement is an expression-statement or a
1268      declaration-statement we parse it tentatively as a
1269      declaration-statement.  If that fails, we then reparse the same
1270      token stream as an expression-statement.  */
1271   cp_parser_context *context;
1272
1273   /* True if we are parsing GNU C++.  If this flag is not set, then
1274      GNU extensions are not recognized.  */
1275   bool allow_gnu_extensions_p;
1276
1277   /* TRUE if the `>' token should be interpreted as the greater-than
1278      operator.  FALSE if it is the end of a template-id or
1279      template-parameter-list.  */
1280   bool greater_than_is_operator_p;
1281
1282   /* TRUE if default arguments are allowed within a parameter list
1283      that starts at this point. FALSE if only a gnu extension makes
1284      them permissable.  */
1285   bool default_arg_ok_p;
1286   
1287   /* TRUE if we are parsing an integral constant-expression.  See
1288      [expr.const] for a precise definition.  */
1289   /* FIXME: Need to implement code that checks this flag.  */
1290   bool constant_expression_p;
1291
1292   /* TRUE if local variable names and `this' are forbidden in the
1293      current context.  */
1294   bool local_variables_forbidden_p;
1295
1296   /* TRUE if the declaration we are parsing is part of a
1297      linkage-specification of the form `extern string-literal
1298      declaration'.  */
1299   bool in_unbraced_linkage_specification_p;
1300
1301   /* TRUE if we are presently parsing a declarator, after the
1302      direct-declarator.  */
1303   bool in_declarator_p;
1304
1305   /* If non-NULL, then we are parsing a construct where new type
1306      definitions are not permitted.  The string stored here will be
1307      issued as an error message if a type is defined.  */
1308   const char *type_definition_forbidden_message;
1309
1310   /* List of FUNCTION_TYPEs which contain unprocessed DEFAULT_ARGs
1311      during class parsing, and are not FUNCTION_DECLs.  G++ has an
1312      awkward extension allowing default args on pointers to functions
1313      etc.  */
1314   tree default_arg_types;
1315
1316   /* A TREE_LIST of queues of functions whose bodies have been lexed,
1317      but may not have been parsed.  These functions are friends of
1318      members defined within a class-specification; they are not
1319      procssed until the class is complete.  The active queue is at the
1320      front of the list.
1321
1322      Within each queue, functions appear in the reverse order that
1323      they appeared in the source.  The TREE_PURPOSE of each node is
1324      the class in which the function was defined or declared; the
1325      TREE_VALUE is the FUNCTION_DECL itself.  */
1326   tree unparsed_functions_queues;
1327
1328   /* The number of classes whose definitions are currently in
1329      progress.  */
1330   unsigned num_classes_being_defined;
1331
1332   /* The number of template parameter lists that apply directly to the
1333      current declaration.  */
1334   unsigned num_template_parameter_lists;
1335 } cp_parser;
1336
1337 /* The type of a function that parses some kind of expression  */
1338 typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1339
1340 /* Prototypes.  */
1341
1342 /* Constructors and destructors.  */
1343
1344 static cp_parser *cp_parser_new
1345   PARAMS ((void));
1346
1347 /* Routines to parse various constructs.  
1348
1349    Those that return `tree' will return the error_mark_node (rather
1350    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1351    Sometimes, they will return an ordinary node if error-recovery was
1352    attempted, even though a parse error occurrred.  So, to check
1353    whether or not a parse error occurred, you should always use
1354    cp_parser_error_occurred.  If the construct is optional (indicated
1355    either by an `_opt' in the name of the function that does the
1356    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1357    the construct is not present.  */
1358
1359 /* Lexical conventions [gram.lex]  */
1360
1361 static tree cp_parser_identifier
1362   PARAMS ((cp_parser *));
1363
1364 /* Basic concepts [gram.basic]  */
1365
1366 static bool cp_parser_translation_unit
1367   PARAMS ((cp_parser *));
1368
1369 /* Expressions [gram.expr]  */
1370
1371 static tree cp_parser_primary_expression
1372   (cp_parser *, cp_parser_id_kind *, tree *);
1373 static tree cp_parser_id_expression
1374   PARAMS ((cp_parser *, bool, bool, bool *));
1375 static tree cp_parser_unqualified_id
1376   PARAMS ((cp_parser *, bool, bool));
1377 static tree cp_parser_nested_name_specifier_opt
1378   (cp_parser *, bool, bool, bool);
1379 static tree cp_parser_nested_name_specifier
1380   (cp_parser *, bool, bool, bool);
1381 static tree cp_parser_class_or_namespace_name
1382   (cp_parser *, bool, bool, bool, bool);
1383 static tree cp_parser_postfix_expression
1384   (cp_parser *, bool);
1385 static tree cp_parser_expression_list
1386   PARAMS ((cp_parser *));
1387 static void cp_parser_pseudo_destructor_name
1388   PARAMS ((cp_parser *, tree *, tree *));
1389 static tree cp_parser_unary_expression
1390   (cp_parser *, bool);
1391 static enum tree_code cp_parser_unary_operator
1392   PARAMS ((cp_token *));
1393 static tree cp_parser_new_expression
1394   PARAMS ((cp_parser *));
1395 static tree cp_parser_new_placement
1396   PARAMS ((cp_parser *));
1397 static tree cp_parser_new_type_id
1398   PARAMS ((cp_parser *));
1399 static tree cp_parser_new_declarator_opt
1400   PARAMS ((cp_parser *));
1401 static tree cp_parser_direct_new_declarator
1402   PARAMS ((cp_parser *));
1403 static tree cp_parser_new_initializer
1404   PARAMS ((cp_parser *));
1405 static tree cp_parser_delete_expression
1406   PARAMS ((cp_parser *));
1407 static tree cp_parser_cast_expression 
1408   (cp_parser *, bool);
1409 static tree cp_parser_pm_expression
1410   PARAMS ((cp_parser *));
1411 static tree cp_parser_multiplicative_expression
1412   PARAMS ((cp_parser *));
1413 static tree cp_parser_additive_expression
1414   PARAMS ((cp_parser *));
1415 static tree cp_parser_shift_expression
1416   PARAMS ((cp_parser *));
1417 static tree cp_parser_relational_expression
1418   PARAMS ((cp_parser *));
1419 static tree cp_parser_equality_expression
1420   PARAMS ((cp_parser *));
1421 static tree cp_parser_and_expression
1422   PARAMS ((cp_parser *));
1423 static tree cp_parser_exclusive_or_expression
1424   PARAMS ((cp_parser *));
1425 static tree cp_parser_inclusive_or_expression
1426   PARAMS ((cp_parser *));
1427 static tree cp_parser_logical_and_expression
1428   PARAMS ((cp_parser *));
1429 static tree cp_parser_logical_or_expression 
1430   PARAMS ((cp_parser *));
1431 static tree cp_parser_conditional_expression
1432   PARAMS ((cp_parser *));
1433 static tree cp_parser_question_colon_clause
1434   PARAMS ((cp_parser *, tree));
1435 static tree cp_parser_assignment_expression
1436   PARAMS ((cp_parser *));
1437 static enum tree_code cp_parser_assignment_operator_opt
1438   PARAMS ((cp_parser *));
1439 static tree cp_parser_expression
1440   PARAMS ((cp_parser *));
1441 static tree cp_parser_constant_expression
1442   PARAMS ((cp_parser *));
1443
1444 /* Statements [gram.stmt.stmt]  */
1445
1446 static void cp_parser_statement
1447   PARAMS ((cp_parser *));
1448 static tree cp_parser_labeled_statement
1449   PARAMS ((cp_parser *));
1450 static tree cp_parser_expression_statement
1451   PARAMS ((cp_parser *));
1452 static tree cp_parser_compound_statement
1453   (cp_parser *);
1454 static void cp_parser_statement_seq_opt
1455   PARAMS ((cp_parser *));
1456 static tree cp_parser_selection_statement
1457   PARAMS ((cp_parser *));
1458 static tree cp_parser_condition
1459   PARAMS ((cp_parser *));
1460 static tree cp_parser_iteration_statement
1461   PARAMS ((cp_parser *));
1462 static void cp_parser_for_init_statement
1463   PARAMS ((cp_parser *));
1464 static tree cp_parser_jump_statement
1465   PARAMS ((cp_parser *));
1466 static void cp_parser_declaration_statement
1467   PARAMS ((cp_parser *));
1468
1469 static tree cp_parser_implicitly_scoped_statement
1470   PARAMS ((cp_parser *));
1471 static void cp_parser_already_scoped_statement
1472   PARAMS ((cp_parser *));
1473
1474 /* Declarations [gram.dcl.dcl] */
1475
1476 static void cp_parser_declaration_seq_opt
1477   PARAMS ((cp_parser *));
1478 static void cp_parser_declaration
1479   PARAMS ((cp_parser *));
1480 static void cp_parser_block_declaration
1481   PARAMS ((cp_parser *, bool));
1482 static void cp_parser_simple_declaration
1483   PARAMS ((cp_parser *, bool));
1484 static tree cp_parser_decl_specifier_seq 
1485   PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1486 static tree cp_parser_storage_class_specifier_opt
1487   PARAMS ((cp_parser *));
1488 static tree cp_parser_function_specifier_opt
1489   PARAMS ((cp_parser *));
1490 static tree cp_parser_type_specifier
1491  (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1492 static tree cp_parser_simple_type_specifier
1493   PARAMS ((cp_parser *, cp_parser_flags));
1494 static tree cp_parser_type_name
1495   PARAMS ((cp_parser *));
1496 static tree cp_parser_elaborated_type_specifier
1497   PARAMS ((cp_parser *, bool, bool));
1498 static tree cp_parser_enum_specifier
1499   PARAMS ((cp_parser *));
1500 static void cp_parser_enumerator_list
1501   PARAMS ((cp_parser *, tree));
1502 static void cp_parser_enumerator_definition 
1503   PARAMS ((cp_parser *, tree));
1504 static tree cp_parser_namespace_name
1505   PARAMS ((cp_parser *));
1506 static void cp_parser_namespace_definition
1507   PARAMS ((cp_parser *));
1508 static void cp_parser_namespace_body
1509   PARAMS ((cp_parser *));
1510 static tree cp_parser_qualified_namespace_specifier
1511   PARAMS ((cp_parser *));
1512 static void cp_parser_namespace_alias_definition
1513   PARAMS ((cp_parser *));
1514 static void cp_parser_using_declaration
1515   PARAMS ((cp_parser *));
1516 static void cp_parser_using_directive
1517   PARAMS ((cp_parser *));
1518 static void cp_parser_asm_definition
1519   PARAMS ((cp_parser *));
1520 static void cp_parser_linkage_specification
1521   PARAMS ((cp_parser *));
1522
1523 /* Declarators [gram.dcl.decl] */
1524
1525 static tree cp_parser_init_declarator
1526   PARAMS ((cp_parser *, tree, tree, tree, bool, bool, bool *));
1527 static tree cp_parser_declarator
1528   PARAMS ((cp_parser *, bool, bool *));
1529 static tree cp_parser_direct_declarator
1530   PARAMS ((cp_parser *, bool, bool *));
1531 static enum tree_code cp_parser_ptr_operator
1532   PARAMS ((cp_parser *, tree *, tree *));
1533 static tree cp_parser_cv_qualifier_seq_opt
1534   PARAMS ((cp_parser *));
1535 static tree cp_parser_cv_qualifier_opt
1536   PARAMS ((cp_parser *));
1537 static tree cp_parser_declarator_id
1538   PARAMS ((cp_parser *));
1539 static tree cp_parser_type_id
1540   PARAMS ((cp_parser *));
1541 static tree cp_parser_type_specifier_seq
1542   PARAMS ((cp_parser *));
1543 static tree cp_parser_parameter_declaration_clause
1544   PARAMS ((cp_parser *));
1545 static tree cp_parser_parameter_declaration_list
1546   PARAMS ((cp_parser *));
1547 static tree cp_parser_parameter_declaration
1548   PARAMS ((cp_parser *, bool));
1549 static tree cp_parser_function_definition
1550   PARAMS ((cp_parser *, bool *));
1551 static void cp_parser_function_body
1552   (cp_parser *);
1553 static tree cp_parser_initializer
1554   PARAMS ((cp_parser *, bool *));
1555 static tree cp_parser_initializer_clause
1556   PARAMS ((cp_parser *));
1557 static tree cp_parser_initializer_list
1558   PARAMS ((cp_parser *));
1559
1560 static bool cp_parser_ctor_initializer_opt_and_function_body
1561   (cp_parser *);
1562
1563 /* Classes [gram.class] */
1564
1565 static tree cp_parser_class_name
1566   (cp_parser *, bool, bool, bool, bool, bool, bool);
1567 static tree cp_parser_class_specifier
1568   PARAMS ((cp_parser *));
1569 static tree cp_parser_class_head
1570   PARAMS ((cp_parser *, bool *, bool *, tree *));
1571 static enum tag_types cp_parser_class_key
1572   PARAMS ((cp_parser *));
1573 static void cp_parser_member_specification_opt
1574   PARAMS ((cp_parser *));
1575 static void cp_parser_member_declaration
1576   PARAMS ((cp_parser *));
1577 static tree cp_parser_pure_specifier
1578   PARAMS ((cp_parser *));
1579 static tree cp_parser_constant_initializer
1580   PARAMS ((cp_parser *));
1581
1582 /* Derived classes [gram.class.derived] */
1583
1584 static tree cp_parser_base_clause
1585   PARAMS ((cp_parser *));
1586 static tree cp_parser_base_specifier
1587   PARAMS ((cp_parser *));
1588
1589 /* Special member functions [gram.special] */
1590
1591 static tree cp_parser_conversion_function_id
1592   PARAMS ((cp_parser *));
1593 static tree cp_parser_conversion_type_id
1594   PARAMS ((cp_parser *));
1595 static tree cp_parser_conversion_declarator_opt
1596   PARAMS ((cp_parser *));
1597 static bool cp_parser_ctor_initializer_opt
1598   PARAMS ((cp_parser *));
1599 static void cp_parser_mem_initializer_list
1600   PARAMS ((cp_parser *));
1601 static tree cp_parser_mem_initializer
1602   PARAMS ((cp_parser *));
1603 static tree cp_parser_mem_initializer_id
1604   PARAMS ((cp_parser *));
1605
1606 /* Overloading [gram.over] */
1607
1608 static tree cp_parser_operator_function_id
1609   PARAMS ((cp_parser *));
1610 static tree cp_parser_operator
1611   PARAMS ((cp_parser *));
1612
1613 /* Templates [gram.temp] */
1614
1615 static void cp_parser_template_declaration
1616   PARAMS ((cp_parser *, bool));
1617 static tree cp_parser_template_parameter_list
1618   PARAMS ((cp_parser *));
1619 static tree cp_parser_template_parameter
1620   PARAMS ((cp_parser *));
1621 static tree cp_parser_type_parameter
1622   PARAMS ((cp_parser *));
1623 static tree cp_parser_template_id
1624   PARAMS ((cp_parser *, bool, bool));
1625 static tree cp_parser_template_name
1626   PARAMS ((cp_parser *, bool, bool));
1627 static tree cp_parser_template_argument_list
1628   PARAMS ((cp_parser *));
1629 static tree cp_parser_template_argument
1630   PARAMS ((cp_parser *));
1631 static void cp_parser_explicit_instantiation
1632   PARAMS ((cp_parser *));
1633 static void cp_parser_explicit_specialization
1634   PARAMS ((cp_parser *));
1635
1636 /* Exception handling [gram.exception] */
1637
1638 static tree cp_parser_try_block 
1639   PARAMS ((cp_parser *));
1640 static bool cp_parser_function_try_block
1641   PARAMS ((cp_parser *));
1642 static void cp_parser_handler_seq
1643   PARAMS ((cp_parser *));
1644 static void cp_parser_handler
1645   PARAMS ((cp_parser *));
1646 static tree cp_parser_exception_declaration
1647   PARAMS ((cp_parser *));
1648 static tree cp_parser_throw_expression
1649   PARAMS ((cp_parser *));
1650 static tree cp_parser_exception_specification_opt
1651   PARAMS ((cp_parser *));
1652 static tree cp_parser_type_id_list
1653   PARAMS ((cp_parser *));
1654
1655 /* GNU Extensions */
1656
1657 static tree cp_parser_asm_specification_opt
1658   PARAMS ((cp_parser *));
1659 static tree cp_parser_asm_operand_list
1660   PARAMS ((cp_parser *));
1661 static tree cp_parser_asm_clobber_list
1662   PARAMS ((cp_parser *));
1663 static tree cp_parser_attributes_opt
1664   PARAMS ((cp_parser *));
1665 static tree cp_parser_attribute_list
1666   PARAMS ((cp_parser *));
1667 static bool cp_parser_extension_opt
1668   PARAMS ((cp_parser *, int *));
1669 static void cp_parser_label_declaration
1670   PARAMS ((cp_parser *));
1671
1672 /* Utility Routines */
1673
1674 static tree cp_parser_lookup_name
1675   PARAMS ((cp_parser *, tree, bool, bool, bool));
1676 static tree cp_parser_lookup_name_simple
1677   PARAMS ((cp_parser *, tree));
1678 static tree cp_parser_resolve_typename_type
1679   PARAMS ((cp_parser *, tree));
1680 static tree cp_parser_maybe_treat_template_as_class
1681   (tree, bool);
1682 static bool cp_parser_check_declarator_template_parameters
1683   PARAMS ((cp_parser *, tree));
1684 static bool cp_parser_check_template_parameters
1685   PARAMS ((cp_parser *, unsigned));
1686 static tree cp_parser_binary_expression
1687   PARAMS ((cp_parser *, 
1688            cp_parser_token_tree_map,
1689            cp_parser_expression_fn));
1690 static tree cp_parser_global_scope_opt
1691   PARAMS ((cp_parser *, bool));
1692 static bool cp_parser_constructor_declarator_p
1693   (cp_parser *, bool);
1694 static tree cp_parser_function_definition_from_specifiers_and_declarator
1695   PARAMS ((cp_parser *, tree, tree, tree, tree));
1696 static tree cp_parser_function_definition_after_declarator
1697   PARAMS ((cp_parser *, bool));
1698 static void cp_parser_template_declaration_after_export
1699   PARAMS ((cp_parser *, bool));
1700 static tree cp_parser_single_declaration
1701   PARAMS ((cp_parser *, bool, bool *));
1702 static tree cp_parser_functional_cast
1703   PARAMS ((cp_parser *, tree));
1704 static void cp_parser_late_parsing_for_member
1705   PARAMS ((cp_parser *, tree));
1706 static void cp_parser_late_parsing_default_args
1707   PARAMS ((cp_parser *, tree));
1708 static tree cp_parser_sizeof_operand
1709   PARAMS ((cp_parser *, enum rid));
1710 static bool cp_parser_declares_only_class_p
1711   PARAMS ((cp_parser *));
1712 static bool cp_parser_friend_p
1713   PARAMS ((tree));
1714 static cp_token *cp_parser_require
1715   PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1716 static cp_token *cp_parser_require_keyword
1717   PARAMS ((cp_parser *, enum rid, const char *));
1718 static bool cp_parser_token_starts_function_definition_p 
1719   PARAMS ((cp_token *));
1720 static bool cp_parser_next_token_starts_class_definition_p
1721   (cp_parser *);
1722 static enum tag_types cp_parser_token_is_class_key
1723   PARAMS ((cp_token *));
1724 static void cp_parser_check_class_key
1725   (enum tag_types, tree type);
1726 static bool cp_parser_optional_template_keyword
1727   (cp_parser *);
1728 static void cp_parser_cache_group
1729   (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1730 static void cp_parser_parse_tentatively 
1731   PARAMS ((cp_parser *));
1732 static void cp_parser_commit_to_tentative_parse
1733   PARAMS ((cp_parser *));
1734 static void cp_parser_abort_tentative_parse
1735   PARAMS ((cp_parser *));
1736 static bool cp_parser_parse_definitely
1737   PARAMS ((cp_parser *));
1738 static bool cp_parser_parsing_tentatively
1739   PARAMS ((cp_parser *));
1740 static bool cp_parser_committed_to_tentative_parse
1741   PARAMS ((cp_parser *));
1742 static void cp_parser_error
1743   PARAMS ((cp_parser *, const char *));
1744 static void cp_parser_simulate_error
1745   PARAMS ((cp_parser *));
1746 static void cp_parser_check_type_definition
1747   PARAMS ((cp_parser *));
1748 static bool cp_parser_skip_to_closing_parenthesis
1749   PARAMS ((cp_parser *));
1750 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1751   (cp_parser *);
1752 static void cp_parser_skip_to_end_of_statement
1753   PARAMS ((cp_parser *));
1754 static void cp_parser_skip_to_end_of_block_or_statement
1755   PARAMS ((cp_parser *));
1756 static void cp_parser_skip_to_closing_brace
1757   (cp_parser *);
1758 static void cp_parser_skip_until_found
1759   PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1760 static bool cp_parser_error_occurred
1761   PARAMS ((cp_parser *));
1762 static bool cp_parser_allow_gnu_extensions_p
1763   PARAMS ((cp_parser *));
1764 static bool cp_parser_is_string_literal
1765   PARAMS ((cp_token *));
1766 static bool cp_parser_is_keyword 
1767   PARAMS ((cp_token *, enum rid));
1768 static bool cp_parser_dependent_type_p
1769   (tree);
1770 static bool cp_parser_value_dependent_expression_p
1771   (tree);
1772 static bool cp_parser_type_dependent_expression_p
1773   (tree);
1774 static bool cp_parser_dependent_template_arg_p
1775   (tree);
1776 static bool cp_parser_dependent_template_id_p
1777   (tree, tree);
1778 static bool cp_parser_dependent_template_p
1779   (tree);
1780 static void cp_parser_defer_access_check
1781   (cp_parser *, tree, tree);
1782 static void cp_parser_start_deferring_access_checks
1783   (cp_parser *);
1784 static tree cp_parser_stop_deferring_access_checks
1785   PARAMS ((cp_parser *));
1786 static void cp_parser_perform_deferred_access_checks
1787   PARAMS ((tree));
1788 static tree cp_parser_scope_through_which_access_occurs
1789   (tree, tree, tree);
1790
1791 /* Returns non-zero if TOKEN is a string literal.  */
1792
1793 static bool
1794 cp_parser_is_string_literal (token)
1795      cp_token *token;
1796 {
1797   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1798 }
1799
1800 /* Returns non-zero if TOKEN is the indicated KEYWORD.  */
1801
1802 static bool
1803 cp_parser_is_keyword (token, keyword)
1804      cp_token *token;
1805      enum rid keyword;
1806 {
1807   return token->keyword == keyword;
1808 }
1809
1810 /* Returns TRUE if TYPE is dependent, in the sense of
1811    [temp.dep.type].  */
1812
1813 static bool
1814 cp_parser_dependent_type_p (type)
1815      tree type;
1816 {
1817   tree scope;
1818
1819   if (!processing_template_decl)
1820     return false;
1821
1822   /* If the type is NULL, we have not computed a type for the entity
1823      in question; in that case, the type is dependent.  */
1824   if (!type)
1825     return true;
1826
1827   /* Erroneous types can be considered non-dependent.  */
1828   if (type == error_mark_node)
1829     return false;
1830
1831   /* [temp.dep.type]
1832
1833      A type is dependent if it is:
1834
1835      -- a template parameter.  */
1836   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1837     return true;
1838   /* -- a qualified-id with a nested-name-specifier which contains a
1839         class-name that names a dependent type or whose unqualified-id
1840         names a dependent type.  */
1841   if (TREE_CODE (type) == TYPENAME_TYPE)
1842     return true;
1843   /* -- a cv-qualified type where the cv-unqualified type is
1844         dependent.  */
1845   type = TYPE_MAIN_VARIANT (type);
1846   /* -- a compound type constructed from any dependent type.  */
1847   if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1848     return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1849             || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE 
1850                                            (type)));
1851   else if (TREE_CODE (type) == POINTER_TYPE
1852            || TREE_CODE (type) == REFERENCE_TYPE)
1853     return cp_parser_dependent_type_p (TREE_TYPE (type));
1854   else if (TREE_CODE (type) == FUNCTION_TYPE
1855            || TREE_CODE (type) == METHOD_TYPE)
1856     {
1857       tree arg_type;
1858
1859       if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1860         return true;
1861       for (arg_type = TYPE_ARG_TYPES (type); 
1862            arg_type; 
1863            arg_type = TREE_CHAIN (arg_type))
1864         if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1865           return true;
1866       return false;
1867     }
1868   /* -- an array type constructed from any dependent type or whose
1869         size is specified by a constant expression that is
1870         value-dependent.  */
1871   if (TREE_CODE (type) == ARRAY_TYPE)
1872     {
1873       if (TYPE_DOMAIN (TREE_TYPE (type))
1874           && ((cp_parser_value_dependent_expression_p 
1875                (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1876               || (cp_parser_type_dependent_expression_p
1877                   (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1878         return true;
1879       return cp_parser_dependent_type_p (TREE_TYPE (type));
1880     }
1881   /* -- a template-id in which either the template name is a template
1882         parameter or any of the template arguments is a dependent type or
1883         an expression that is type-dependent or value-dependent.  
1884
1885      This language seems somewhat confused; for example, it does not
1886      discuss template template arguments.  Therefore, we use the
1887      definition for dependent template arguments in [temp.dep.temp].  */
1888   if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1889       && (cp_parser_dependent_template_id_p
1890           (CLASSTYPE_TI_TEMPLATE (type),
1891            CLASSTYPE_TI_ARGS (type))))
1892     return true;
1893   else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1894     return true;
1895   /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1896      expression is not type-dependent, then it should already been
1897      have resolved.  */
1898   if (TREE_CODE (type) == TYPEOF_TYPE)
1899     return true;
1900   /* The standard does not specifically mention types that are local
1901      to template functions or local classes, but they should be
1902      considered dependent too.  For example:
1903
1904        template <int I> void f() { 
1905          enum E { a = I }; 
1906          S<sizeof (E)> s;
1907        }
1908
1909      The size of `E' cannot be known until the value of `I' has been
1910      determined.  Therefore, `E' must be considered dependent.  */
1911   scope = TYPE_CONTEXT (type);
1912   if (scope && TYPE_P (scope))
1913     return cp_parser_dependent_type_p (scope);
1914   else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1915     return cp_parser_type_dependent_expression_p (scope);
1916
1917   /* Other types are non-dependent.  */
1918   return false;
1919 }
1920
1921 /* Returns TRUE if the EXPRESSION is value-dependent.  */
1922
1923 static bool
1924 cp_parser_value_dependent_expression_p (tree expression)
1925 {
1926   if (!processing_template_decl)
1927     return false;
1928
1929   /* A name declared with a dependent type.  */
1930   if (DECL_P (expression)
1931       && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1932     return true;
1933   /* A non-type template parameter.  */
1934   if ((TREE_CODE (expression) == CONST_DECL
1935        && DECL_TEMPLATE_PARM_P (expression))
1936       || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1937     return true;
1938   /* A constant with integral or enumeration type and is initialized 
1939      with an expression that is value-dependent.  */
1940   if (TREE_CODE (expression) == VAR_DECL
1941       && DECL_INITIAL (expression)
1942       && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1943           || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1944       && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1945     return true;
1946   /* These expressions are value-dependent if the type to which the
1947      cast occurs is dependent.  */
1948   if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1949        || TREE_CODE (expression) == STATIC_CAST_EXPR
1950        || TREE_CODE (expression) == CONST_CAST_EXPR
1951        || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
1952        || TREE_CODE (expression) == CAST_EXPR)
1953       && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1954     return true;
1955   /* A `sizeof' expression where the sizeof operand is a type is
1956      value-dependent if the type is dependent.  If the type was not
1957      dependent, we would no longer have a SIZEOF_EXPR, so any
1958      SIZEOF_EXPR is dependent.  */
1959   if (TREE_CODE (expression) == SIZEOF_EXPR)
1960     return true;
1961   /* A constant expression is value-dependent if any subexpression is
1962      value-dependent.  */
1963   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
1964     {
1965       switch (TREE_CODE_CLASS (TREE_CODE (expression)))
1966         {
1967         case '1':
1968           return (cp_parser_value_dependent_expression_p 
1969                   (TREE_OPERAND (expression, 0)));
1970         case '<':
1971         case '2':
1972           return ((cp_parser_value_dependent_expression_p 
1973                    (TREE_OPERAND (expression, 0)))
1974                   || (cp_parser_value_dependent_expression_p 
1975                       (TREE_OPERAND (expression, 1))));
1976         case 'e':
1977           {
1978             int i;
1979             for (i = 0; 
1980                  i < TREE_CODE_LENGTH (TREE_CODE (expression));
1981                  ++i)
1982               if (cp_parser_value_dependent_expression_p
1983                   (TREE_OPERAND (expression, i)))
1984                 return true;
1985             return false;
1986           }
1987         }
1988     }
1989
1990   /* The expression is not value-dependent.  */
1991   return false;
1992 }
1993
1994 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
1995    [temp.dep.expr].  */
1996
1997 static bool
1998 cp_parser_type_dependent_expression_p (expression)
1999      tree expression;
2000 {
2001   if (!processing_template_decl)
2002     return false;
2003
2004   /* Some expression forms are never type-dependent.  */
2005   if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2006       || TREE_CODE (expression) == SIZEOF_EXPR
2007       || TREE_CODE (expression) == ALIGNOF_EXPR
2008       || TREE_CODE (expression) == TYPEID_EXPR
2009       || TREE_CODE (expression) == DELETE_EXPR
2010       || TREE_CODE (expression) == VEC_DELETE_EXPR
2011       || TREE_CODE (expression) == THROW_EXPR)
2012     return false;
2013
2014   /* The types of these expressions depends only on the type to which
2015      the cast occurs.  */
2016   if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2017       || TREE_CODE (expression) == STATIC_CAST_EXPR
2018       || TREE_CODE (expression) == CONST_CAST_EXPR
2019       || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2020       || TREE_CODE (expression) == CAST_EXPR)
2021     return cp_parser_dependent_type_p (TREE_TYPE (expression));
2022   /* The types of these expressions depends only on the type created
2023      by the expression.  */
2024   else if (TREE_CODE (expression) == NEW_EXPR
2025            || TREE_CODE (expression) == VEC_NEW_EXPR)
2026     return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2027
2028   if (TREE_CODE (expression) == FUNCTION_DECL
2029       && DECL_LANG_SPECIFIC (expression)
2030       && DECL_TEMPLATE_INFO (expression)
2031       && (cp_parser_dependent_template_id_p
2032           (DECL_TI_TEMPLATE (expression),
2033            INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2034     return true;
2035
2036   return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2037 }
2038
2039 /* Returns TRUE if the ARG (a template argument) is dependent.  */
2040
2041 static bool
2042 cp_parser_dependent_template_arg_p (tree arg)
2043 {
2044   if (!processing_template_decl)
2045     return false;
2046
2047   if (TREE_CODE (arg) == TEMPLATE_DECL
2048       || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2049     return cp_parser_dependent_template_p (arg);
2050   else if (TYPE_P (arg))
2051     return cp_parser_dependent_type_p (arg);
2052   else
2053     return (cp_parser_type_dependent_expression_p (arg)
2054             || cp_parser_value_dependent_expression_p (arg));
2055 }
2056
2057 /* Returns TRUE if the specialization TMPL<ARGS> is dependent.  */
2058
2059 static bool
2060 cp_parser_dependent_template_id_p (tree tmpl, tree args)
2061 {
2062   int i;
2063
2064   if (cp_parser_dependent_template_p (tmpl))
2065     return true;
2066   for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2067     if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2068       return true;
2069   return false;
2070 }
2071
2072 /* Returns TRUE if the template TMPL is dependent.  */
2073
2074 static bool
2075 cp_parser_dependent_template_p (tree tmpl)
2076 {
2077   /* Template template parameters are dependent.  */
2078   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2079       || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2080     return true;
2081   /* So are member templates of dependent classes.  */
2082   if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2083     return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2084   return false;
2085 }
2086
2087 /* Defer checking the accessibility of DECL, when looked up in
2088    CLASS_TYPE.  */
2089
2090 static void
2091 cp_parser_defer_access_check (cp_parser *parser, 
2092                               tree class_type,
2093                               tree decl)
2094 {
2095   tree check;
2096
2097   /* If we are not supposed to defer access checks, just check now.  */
2098   if (!parser->context->deferring_access_checks_p)
2099     {
2100       enforce_access (class_type, decl);
2101       return;
2102     }
2103
2104   /* See if we are already going to perform this check.  */
2105   for (check = parser->context->deferred_access_checks;
2106        check;
2107        check = TREE_CHAIN (check))
2108     if (TREE_VALUE (check) == decl
2109         && same_type_p (TREE_PURPOSE (check), class_type))
2110       return;
2111   /* If not, record the check.  */
2112   parser->context->deferred_access_checks
2113     = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2114 }
2115
2116 /* Start deferring access control checks.  */
2117
2118 static void
2119 cp_parser_start_deferring_access_checks (cp_parser *parser)
2120 {
2121   parser->context->deferring_access_checks_p = true;
2122 }
2123
2124 /* Stop deferring access control checks.  Returns a TREE_LIST
2125    representing the deferred checks.  The TREE_PURPOSE of each node is
2126    the type through which the access occurred; the TREE_VALUE is the
2127    declaration named.  */
2128
2129 static tree
2130 cp_parser_stop_deferring_access_checks (parser)
2131      cp_parser *parser;
2132 {
2133   tree access_checks;
2134
2135   parser->context->deferring_access_checks_p = false;
2136   access_checks = parser->context->deferred_access_checks;
2137   parser->context->deferred_access_checks = NULL_TREE;
2138
2139   return access_checks;
2140 }
2141
2142 /* Perform the deferred ACCESS_CHECKS, whose representation is as
2143    documented with cp_parser_stop_deferrring_access_checks.  */
2144
2145 static void
2146 cp_parser_perform_deferred_access_checks (access_checks)
2147      tree access_checks;
2148 {
2149   tree deferred_check;
2150
2151   /* Look through all the deferred checks.  */
2152   for (deferred_check = access_checks;
2153        deferred_check;
2154        deferred_check = TREE_CHAIN (deferred_check))
2155     /* Check access.  */
2156     enforce_access (TREE_PURPOSE (deferred_check), 
2157                     TREE_VALUE (deferred_check));
2158 }
2159
2160 /* Returns the scope through which DECL is being accessed, or
2161    NULL_TREE if DECL is not a member.  If OBJECT_TYPE is non-NULL, we
2162    have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2163    or `x', respectively.  If the DECL was named as `A::B' then
2164    NESTED_NAME_SPECIFIER is `A'.  */
2165
2166 tree
2167 cp_parser_scope_through_which_access_occurs (decl, 
2168                                              object_type,
2169                                              nested_name_specifier)
2170      tree decl;
2171      tree object_type;
2172      tree nested_name_specifier;
2173 {
2174   tree scope;
2175   tree qualifying_type = NULL_TREE;
2176   
2177   /* Determine the SCOPE of DECL.  */
2178   scope = context_for_name_lookup (decl);
2179   /* If the SCOPE is not a type, then DECL is not a member.  */
2180   if (!TYPE_P (scope))
2181     return NULL_TREE;
2182   /* Figure out the type through which DECL is being accessed.  */
2183   if (object_type && DERIVED_FROM_P (scope, object_type))
2184     /* If we are processing a `->' or `.' expression, use the type of the
2185        left-hand side.  */
2186     qualifying_type = object_type;
2187   else if (nested_name_specifier)
2188     {
2189       /* If the reference is to a non-static member of the
2190          current class, treat it as if it were referenced through
2191          `this'.  */
2192       if (DECL_NONSTATIC_MEMBER_P (decl)
2193           && current_class_ptr
2194           && DERIVED_FROM_P (scope, current_class_type))
2195         qualifying_type = current_class_type;
2196       /* Otherwise, use the type indicated by the
2197          nested-name-specifier.  */
2198       else
2199         qualifying_type = nested_name_specifier;
2200     }
2201   else
2202     /* Otherwise, the name must be from the current class or one of
2203        its bases.  */
2204     qualifying_type = currently_open_derived_class (scope);
2205
2206   return qualifying_type;
2207 }
2208
2209 /* Issue the indicated error MESSAGE.  */
2210
2211 static void
2212 cp_parser_error (parser, message)
2213      cp_parser *parser;
2214      const char *message;
2215 {
2216   /* Remember that we have issued an error.  */
2217   cp_parser_simulate_error (parser);
2218   /* Output the MESSAGE -- unless we're parsing tentatively.  */
2219   if (!cp_parser_parsing_tentatively (parser) 
2220       || cp_parser_committed_to_tentative_parse (parser))
2221     error (message);
2222 }
2223
2224 /* If we are parsing tentatively, remember that an error has occurred
2225    during this tentative parse.  */
2226
2227 static void
2228 cp_parser_simulate_error (parser)
2229      cp_parser *parser;
2230 {
2231   if (cp_parser_parsing_tentatively (parser)
2232       && !cp_parser_committed_to_tentative_parse (parser))
2233     parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2234 }
2235
2236 /* This function is called when a type is defined.  If type
2237    definitions are forbidden at this point, an error message is
2238    issued.  */
2239
2240 static void
2241 cp_parser_check_type_definition (parser)
2242      cp_parser *parser;
2243 {
2244   /* If types are forbidden here, issue a message.  */
2245   if (parser->type_definition_forbidden_message)
2246     /* Use `%s' to print the string in case there are any escape
2247        characters in the message.  */
2248     error ("%s", parser->type_definition_forbidden_message);
2249 }
2250
2251 /* Consume tokens up to, and including, the next non-nested closing `)'. 
2252    Returns TRUE iff we found a closing `)'.  */
2253
2254 static bool
2255 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2256 {
2257   unsigned nesting_depth = 0;
2258
2259   while (true)
2260     {
2261       cp_token *token;
2262
2263       /* If we've run out of tokens, then there is no closing `)'.  */
2264       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2265         return false;
2266       /* Consume the token.  */
2267       token = cp_lexer_consume_token (parser->lexer);
2268       /* If it is an `(', we have entered another level of nesting.  */
2269       if (token->type == CPP_OPEN_PAREN)
2270         ++nesting_depth;
2271       /* If it is a `)', then we might be done.  */
2272       else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2273         return true;
2274     }
2275 }
2276
2277 /* Consume tokens until the next token is a `)', or a `,'.  Returns
2278    TRUE if the next token is a `,'.  */
2279
2280 static bool
2281 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2282 {
2283   unsigned nesting_depth = 0;
2284
2285   while (true)
2286     {
2287       cp_token *token = cp_lexer_peek_token (parser->lexer);
2288
2289       /* If we've run out of tokens, then there is no closing `)'.  */
2290       if (token->type == CPP_EOF)
2291         return false;
2292       /* If it is a `,' stop.  */
2293       else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2294         return true;
2295       /* If it is a `)', stop.  */
2296       else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2297         return false;
2298       /* If it is an `(', we have entered another level of nesting.  */
2299       else if (token->type == CPP_OPEN_PAREN)
2300         ++nesting_depth;
2301       /* Consume the token.  */
2302       token = cp_lexer_consume_token (parser->lexer);
2303     }
2304 }
2305
2306 /* Consume tokens until we reach the end of the current statement.
2307    Normally, that will be just before consuming a `;'.  However, if a
2308    non-nested `}' comes first, then we stop before consuming that.  */
2309
2310 static void
2311 cp_parser_skip_to_end_of_statement (parser)
2312      cp_parser *parser;
2313 {
2314   unsigned nesting_depth = 0;
2315
2316   while (true)
2317     {
2318       cp_token *token;
2319
2320       /* Peek at the next token.  */
2321       token = cp_lexer_peek_token (parser->lexer);
2322       /* If we've run out of tokens, stop.  */
2323       if (token->type == CPP_EOF)
2324         break;
2325       /* If the next token is a `;', we have reached the end of the
2326          statement.  */
2327       if (token->type == CPP_SEMICOLON && !nesting_depth)
2328         break;
2329       /* If the next token is a non-nested `}', then we have reached
2330          the end of the current block.  */
2331       if (token->type == CPP_CLOSE_BRACE)
2332         {
2333           /* If this is a non-nested `}', stop before consuming it.
2334              That way, when confronted with something like:
2335
2336                { 3 + } 
2337
2338              we stop before consuming the closing `}', even though we
2339              have not yet reached a `;'.  */
2340           if (nesting_depth == 0)
2341             break;
2342           /* If it is the closing `}' for a block that we have
2343              scanned, stop -- but only after consuming the token.
2344              That way given:
2345
2346                 void f g () { ... }
2347                 typedef int I;
2348
2349              we will stop after the body of the erroneously declared
2350              function, but before consuming the following `typedef'
2351              declaration.  */
2352           if (--nesting_depth == 0)
2353             {
2354               cp_lexer_consume_token (parser->lexer);
2355               break;
2356             }
2357         }
2358       /* If it the next token is a `{', then we are entering a new
2359          block.  Consume the entire block.  */
2360       else if (token->type == CPP_OPEN_BRACE)
2361         ++nesting_depth;
2362       /* Consume the token.  */
2363       cp_lexer_consume_token (parser->lexer);
2364     }
2365 }
2366
2367 /* Skip tokens until we have consumed an entire block, or until we
2368    have consumed a non-nested `;'.  */
2369
2370 static void
2371 cp_parser_skip_to_end_of_block_or_statement (parser)
2372      cp_parser *parser;
2373 {
2374   unsigned nesting_depth = 0;
2375
2376   while (true)
2377     {
2378       cp_token *token;
2379
2380       /* Peek at the next token.  */
2381       token = cp_lexer_peek_token (parser->lexer);
2382       /* If we've run out of tokens, stop.  */
2383       if (token->type == CPP_EOF)
2384         break;
2385       /* If the next token is a `;', we have reached the end of the
2386          statement.  */
2387       if (token->type == CPP_SEMICOLON && !nesting_depth)
2388         {
2389           /* Consume the `;'.  */
2390           cp_lexer_consume_token (parser->lexer);
2391           break;
2392         }
2393       /* Consume the token.  */
2394       token = cp_lexer_consume_token (parser->lexer);
2395       /* If the next token is a non-nested `}', then we have reached
2396          the end of the current block.  */
2397       if (token->type == CPP_CLOSE_BRACE 
2398           && (nesting_depth == 0 || --nesting_depth == 0))
2399         break;
2400       /* If it the next token is a `{', then we are entering a new
2401          block.  Consume the entire block.  */
2402       if (token->type == CPP_OPEN_BRACE)
2403         ++nesting_depth;
2404     }
2405 }
2406
2407 /* Skip tokens until a non-nested closing curly brace is the next
2408    token.  */
2409
2410 static void
2411 cp_parser_skip_to_closing_brace (cp_parser *parser)
2412 {
2413   unsigned nesting_depth = 0;
2414
2415   while (true)
2416     {
2417       cp_token *token;
2418
2419       /* Peek at the next token.  */
2420       token = cp_lexer_peek_token (parser->lexer);
2421       /* If we've run out of tokens, stop.  */
2422       if (token->type == CPP_EOF)
2423         break;
2424       /* If the next token is a non-nested `}', then we have reached
2425          the end of the current block.  */
2426       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2427         break;
2428       /* If it the next token is a `{', then we are entering a new
2429          block.  Consume the entire block.  */
2430       else if (token->type == CPP_OPEN_BRACE)
2431         ++nesting_depth;
2432       /* Consume the token.  */
2433       cp_lexer_consume_token (parser->lexer);
2434     }
2435 }
2436
2437 /* Create a new C++ parser.  */
2438
2439 static cp_parser *
2440 cp_parser_new ()
2441 {
2442   cp_parser *parser;
2443
2444   parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2445   parser->lexer = cp_lexer_new (/*main_lexer_p=*/true);
2446   parser->context = cp_parser_context_new (NULL);
2447
2448   /* For now, we always accept GNU extensions.  */
2449   parser->allow_gnu_extensions_p = 1;
2450
2451   /* The `>' token is a greater-than operator, not the end of a
2452      template-id.  */
2453   parser->greater_than_is_operator_p = true;
2454
2455   parser->default_arg_ok_p = true;
2456   
2457   /* We are not parsing a constant-expression.  */
2458   parser->constant_expression_p = false;
2459
2460   /* Local variable names are not forbidden.  */
2461   parser->local_variables_forbidden_p = false;
2462
2463   /* We are not procesing an `extern "C"' declaration.  */
2464   parser->in_unbraced_linkage_specification_p = false;
2465
2466   /* We are not processing a declarator.  */
2467   parser->in_declarator_p = false;
2468
2469   /* There are no default args to process.  */
2470   parser->default_arg_types = NULL;
2471
2472   /* The unparsed function queue is empty.  */
2473   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2474
2475   /* There are no classes being defined.  */
2476   parser->num_classes_being_defined = 0;
2477
2478   /* No template parameters apply.  */
2479   parser->num_template_parameter_lists = 0;
2480
2481   return parser;
2482 }
2483
2484 /* Lexical conventions [gram.lex]  */
2485
2486 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2487    identifier.  */
2488
2489 static tree 
2490 cp_parser_identifier (parser)
2491      cp_parser *parser;
2492 {
2493   cp_token *token;
2494
2495   /* Look for the identifier.  */
2496   token = cp_parser_require (parser, CPP_NAME, "identifier");
2497   /* Return the value.  */
2498   return token ? token->value : error_mark_node;
2499 }
2500
2501 /* Basic concepts [gram.basic]  */
2502
2503 /* Parse a translation-unit.
2504
2505    translation-unit:
2506      declaration-seq [opt]  
2507
2508    Returns TRUE if all went well.  */
2509
2510 static bool
2511 cp_parser_translation_unit (parser)
2512      cp_parser *parser;
2513 {
2514   while (true)
2515     {
2516       cp_parser_declaration_seq_opt (parser);
2517
2518       /* If there are no tokens left then all went well.  */
2519       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2520         break;
2521       
2522       /* Otherwise, issue an error message.  */
2523       cp_parser_error (parser, "expected declaration");
2524       return false;
2525     }
2526
2527   /* Consume the EOF token.  */
2528   cp_parser_require (parser, CPP_EOF, "end-of-file");
2529   
2530   /* Finish up.  */
2531   finish_translation_unit ();
2532
2533   /* All went well.  */
2534   return true;
2535 }
2536
2537 /* Expressions [gram.expr] */
2538
2539 /* Parse a primary-expression.
2540
2541    primary-expression:
2542      literal
2543      this
2544      ( expression )
2545      id-expression
2546
2547    GNU Extensions:
2548
2549    primary-expression:
2550      ( compound-statement )
2551      __builtin_va_arg ( assignment-expression , type-id )
2552
2553    literal:
2554      __null
2555
2556    Returns a representation of the expression.  
2557
2558    *IDK indicates what kind of id-expression (if any) was present.  
2559
2560    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2561    used as the operand of a pointer-to-member.  In that case,
2562    *QUALIFYING_CLASS gives the class that is used as the qualifying
2563    class in the pointer-to-member.  */
2564
2565 static tree
2566 cp_parser_primary_expression (cp_parser *parser, 
2567                               cp_parser_id_kind *idk,
2568                               tree *qualifying_class)
2569 {
2570   cp_token *token;
2571
2572   /* Assume the primary expression is not an id-expression.  */
2573   *idk = CP_PARSER_ID_KIND_NONE;
2574   /* And that it cannot be used as pointer-to-member.  */
2575   *qualifying_class = NULL_TREE;
2576
2577   /* Peek at the next token.  */
2578   token = cp_lexer_peek_token (parser->lexer);
2579   switch (token->type)
2580     {
2581       /* literal:
2582            integer-literal
2583            character-literal
2584            floating-literal
2585            string-literal
2586            boolean-literal  */
2587     case CPP_CHAR:
2588     case CPP_WCHAR:
2589     case CPP_STRING:
2590     case CPP_WSTRING:
2591     case CPP_NUMBER:
2592       token = cp_lexer_consume_token (parser->lexer);
2593       return token->value;
2594
2595     case CPP_OPEN_PAREN:
2596       {
2597         tree expr;
2598         bool saved_greater_than_is_operator_p;
2599
2600         /* Consume the `('.  */
2601         cp_lexer_consume_token (parser->lexer);
2602         /* Within a parenthesized expression, a `>' token is always
2603            the greater-than operator.  */
2604         saved_greater_than_is_operator_p 
2605           = parser->greater_than_is_operator_p;
2606         parser->greater_than_is_operator_p = true;
2607         /* If we see `( { ' then we are looking at the beginning of
2608            a GNU statement-expression.  */
2609         if (cp_parser_allow_gnu_extensions_p (parser)
2610             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2611           {
2612             /* Statement-expressions are not allowed by the standard.  */
2613             if (pedantic)
2614               pedwarn ("ISO C++ forbids braced-groups within expressions");  
2615             
2616             /* And they're not allowed outside of a function-body; you
2617                cannot, for example, write:
2618                
2619                  int i = ({ int j = 3; j + 1; });
2620                
2621                at class or namespace scope.  */
2622             if (!at_function_scope_p ())
2623               error ("statement-expressions are allowed only inside functions");
2624             /* Start the statement-expression.  */
2625             expr = begin_stmt_expr ();
2626             /* Parse the compound-statement.  */
2627             cp_parser_compound_statement (parser);
2628             /* Finish up.  */
2629             expr = finish_stmt_expr (expr);
2630           }
2631         else
2632           {
2633             /* Parse the parenthesized expression.  */
2634             expr = cp_parser_expression (parser);
2635             /* Let the front end know that this expression was
2636                enclosed in parentheses. This matters in case, for
2637                example, the expression is of the form `A::B', since
2638                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2639                not.  */
2640             finish_parenthesized_expr (expr);
2641           }
2642         /* The `>' token might be the end of a template-id or
2643            template-parameter-list now.  */
2644         parser->greater_than_is_operator_p 
2645           = saved_greater_than_is_operator_p;
2646         /* Consume the `)'.  */
2647         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2648           cp_parser_skip_to_end_of_statement (parser);
2649
2650         return expr;
2651       }
2652
2653     case CPP_KEYWORD:
2654       switch (token->keyword)
2655         {
2656           /* These two are the boolean literals.  */
2657         case RID_TRUE:
2658           cp_lexer_consume_token (parser->lexer);
2659           return boolean_true_node;
2660         case RID_FALSE:
2661           cp_lexer_consume_token (parser->lexer);
2662           return boolean_false_node;
2663           
2664           /* The `__null' literal.  */
2665         case RID_NULL:
2666           cp_lexer_consume_token (parser->lexer);
2667           return null_node;
2668
2669           /* Recognize the `this' keyword.  */
2670         case RID_THIS:
2671           cp_lexer_consume_token (parser->lexer);
2672           if (parser->local_variables_forbidden_p)
2673             {
2674               error ("`this' may not be used in this context");
2675               return error_mark_node;
2676             }
2677           return finish_this_expr ();
2678
2679           /* The `operator' keyword can be the beginning of an
2680              id-expression.  */
2681         case RID_OPERATOR:
2682           goto id_expression;
2683
2684         case RID_FUNCTION_NAME:
2685         case RID_PRETTY_FUNCTION_NAME:
2686         case RID_C99_FUNCTION_NAME:
2687           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2688              __func__ are the names of variables -- but they are
2689              treated specially.  Therefore, they are handled here,
2690              rather than relying on the generic id-expression logic
2691              below.  Gramatically, these names are id-expressions.  
2692
2693              Consume the token.  */
2694           token = cp_lexer_consume_token (parser->lexer);
2695           /* Look up the name.  */
2696           return finish_fname (token->value);
2697
2698         case RID_VA_ARG:
2699           {
2700             tree expression;
2701             tree type;
2702
2703             /* The `__builtin_va_arg' construct is used to handle
2704                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2705             cp_lexer_consume_token (parser->lexer);
2706             /* Look for the opening `('.  */
2707             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2708             /* Now, parse the assignment-expression.  */
2709             expression = cp_parser_assignment_expression (parser);
2710             /* Look for the `,'.  */
2711             cp_parser_require (parser, CPP_COMMA, "`,'");
2712             /* Parse the type-id.  */
2713             type = cp_parser_type_id (parser);
2714             /* Look for the closing `)'.  */
2715             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2716
2717             return build_x_va_arg (expression, type);
2718           }
2719
2720         default:
2721           cp_parser_error (parser, "expected primary-expression");
2722           return error_mark_node;
2723         }
2724       /* Fall through. */
2725
2726       /* An id-expression can start with either an identifier, a
2727          `::' as the beginning of a qualified-id, or the "operator"
2728          keyword.  */
2729     case CPP_NAME:
2730     case CPP_SCOPE:
2731     case CPP_TEMPLATE_ID:
2732     case CPP_NESTED_NAME_SPECIFIER:
2733       {
2734         tree id_expression;
2735         tree decl;
2736
2737       id_expression:
2738         /* Parse the id-expression.  */
2739         id_expression 
2740           = cp_parser_id_expression (parser, 
2741                                      /*template_keyword_p=*/false,
2742                                      /*check_dependency_p=*/true,
2743                                      /*template_p=*/NULL);
2744         if (id_expression == error_mark_node)
2745           return error_mark_node;
2746         /* If we have a template-id, then no further lookup is
2747            required.  If the template-id was for a template-class, we
2748            will sometimes have a TYPE_DECL at this point.  */
2749         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2750             || TREE_CODE (id_expression) == TYPE_DECL)
2751           decl = id_expression;
2752         /* Look up the name.  */
2753         else 
2754           {
2755             decl = cp_parser_lookup_name_simple (parser, id_expression);
2756             /* If name lookup gives us a SCOPE_REF, then the
2757                qualifying scope was dependent.  Just propagate the
2758                name.  */
2759             if (TREE_CODE (decl) == SCOPE_REF)
2760               {
2761                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2762                   *qualifying_class = TREE_OPERAND (decl, 0);
2763                 return decl;
2764               }
2765             /* Check to see if DECL is a local variable in a context
2766                where that is forbidden.  */
2767             if (parser->local_variables_forbidden_p
2768                 && local_variable_p (decl))
2769               {
2770                 /* It might be that we only found DECL because we are
2771                    trying to be generous with pre-ISO scoping rules.
2772                    For example, consider:
2773
2774                      int i;
2775                      void g() {
2776                        for (int i = 0; i < 10; ++i) {}
2777                        extern void f(int j = i);
2778                      }
2779
2780                    Here, name look up will originally find the out 
2781                    of scope `i'.  We need to issue a warning message,
2782                    but then use the global `i'.  */
2783                 decl = check_for_out_of_scope_variable (decl);
2784                 if (local_variable_p (decl))
2785                   {
2786                     error ("local variable `%D' may not appear in this context",
2787                            decl);
2788                     return error_mark_node;
2789                   }
2790               }
2791
2792             /* If unqualified name lookup fails while processing a
2793                template, that just means that we need to do name
2794                lookup again when the template is instantiated.  */
2795             if (!parser->scope 
2796                 && decl == error_mark_node
2797                 && processing_template_decl)
2798               {
2799                 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2800                 return build_min_nt (LOOKUP_EXPR, id_expression);
2801               }
2802             else if (decl == error_mark_node
2803                      && !processing_template_decl)
2804               {
2805                 if (!parser->scope)
2806                   {
2807                     /* It may be resolvable as a koenig lookup function
2808                        call.  */
2809                     *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2810                     return id_expression;
2811                   }
2812                 else if (TYPE_P (parser->scope)
2813                          && !COMPLETE_TYPE_P (parser->scope))
2814                   error ("incomplete type `%T' used in nested name specifier",
2815                          parser->scope);
2816                 else if (parser->scope != global_namespace)
2817                   error ("`%D' is not a member of `%D'",
2818                          id_expression, parser->scope);
2819                 else
2820                   error ("`::%D' has not been declared", id_expression);
2821               }
2822             /* If DECL is a variable would be out of scope under
2823                ANSI/ISO rules, but in scope in the ARM, name lookup
2824                will succeed.  Issue a diagnostic here.  */
2825             else
2826               decl = check_for_out_of_scope_variable (decl);
2827
2828             /* Remember that the name was used in the definition of
2829                the current class so that we can check later to see if
2830                the meaning would have been different after the class
2831                was entirely defined.  */
2832             if (!parser->scope && decl != error_mark_node)
2833               maybe_note_name_used_in_class (id_expression, decl);
2834           }
2835
2836         /* If we didn't find anything, or what we found was a type,
2837            then this wasn't really an id-expression.  */
2838         if (TREE_CODE (decl) == TYPE_DECL
2839             || TREE_CODE (decl) == NAMESPACE_DECL
2840             || (TREE_CODE (decl) == TEMPLATE_DECL
2841                 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2842           {
2843             cp_parser_error (parser, 
2844                              "expected primary-expression");
2845             return error_mark_node;
2846           }
2847
2848         /* If the name resolved to a template parameter, there is no
2849            need to look it up again later.  Similarly, we resolve
2850            enumeration constants to their underlying values.  */
2851         if (TREE_CODE (decl) == CONST_DECL)
2852           {
2853             *idk = CP_PARSER_ID_KIND_NONE;
2854             if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2855               return DECL_INITIAL (decl);
2856             return decl;
2857           }
2858         else
2859           {
2860             bool dependent_p;
2861             
2862             /* If the declaration was explicitly qualified indicate
2863                that.  The semantics of `A::f(3)' are different than
2864                `f(3)' if `f' is virtual.  */
2865             *idk = (parser->scope 
2866                     ? CP_PARSER_ID_KIND_QUALIFIED
2867                     : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2868                        ? CP_PARSER_ID_KIND_TEMPLATE_ID
2869                        : CP_PARSER_ID_KIND_UNQUALIFIED));
2870
2871
2872             /* [temp.dep.expr]
2873                
2874                An id-expression is type-dependent if it contains an
2875                identifier that was declared with a dependent type.
2876                
2877                As an optimization, we could choose not to create a
2878                LOOKUP_EXPR for a name that resolved to a local
2879                variable in the template function that we are currently
2880                declaring; such a name cannot ever resolve to anything
2881                else.  If we did that we would not have to look up
2882                these names at instantiation time.
2883                
2884                The standard is not very specific about an
2885                id-expression that names a set of overloaded functions.
2886                What if some of them have dependent types and some of
2887                them do not?  Presumably, such a name should be treated
2888                as a dependent name.  */
2889             /* Assume the name is not dependent.  */
2890             dependent_p = false;
2891             if (!processing_template_decl)
2892               /* No names are dependent outside a template.  */
2893               ;
2894             /* A template-id where the name of the template was not
2895                resolved is definitely dependent.  */
2896             else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2897                      && (TREE_CODE (TREE_OPERAND (decl, 0)) 
2898                          == IDENTIFIER_NODE))
2899               dependent_p = true;
2900             /* For anything except an overloaded function, just check
2901                its type.  */
2902             else if (!is_overloaded_fn (decl))
2903               dependent_p 
2904                 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2905             /* For a set of overloaded functions, check each of the
2906                functions.  */
2907             else
2908               {
2909                 tree fns = decl;
2910
2911                 if (BASELINK_P (fns))
2912                   fns = BASELINK_FUNCTIONS (fns);
2913                   
2914                 /* For a template-id, check to see if the template
2915                    arguments are dependent.  */
2916                 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2917                   {
2918                     tree args = TREE_OPERAND (fns, 1);
2919
2920                     if (args && TREE_CODE (args) == TREE_LIST)
2921                       {
2922                         while (args)
2923                           {
2924                             if (cp_parser_dependent_template_arg_p
2925                                 (TREE_VALUE (args)))
2926                               {
2927                                 dependent_p = true;
2928                                 break;
2929                               }
2930                             args = TREE_CHAIN (args);
2931                           }
2932                       }
2933                     else if (args && TREE_CODE (args) == TREE_VEC)
2934                       {
2935                         int i; 
2936                         for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2937                           if (cp_parser_dependent_template_arg_p
2938                               (TREE_VEC_ELT (args, i)))
2939                             {
2940                               dependent_p = true;
2941                               break;
2942                             }
2943                       }
2944
2945                     /* The functions are those referred to by the
2946                        template-id.  */
2947                     fns = TREE_OPERAND (fns, 0);
2948                   }
2949
2950                 /* If there are no dependent template arguments, go
2951                    through the overlaoded functions.  */
2952                 while (fns && !dependent_p)
2953                   {
2954                     tree fn = OVL_CURRENT (fns);
2955                     
2956                     /* Member functions of dependent classes are
2957                        dependent.  */
2958                     if (TREE_CODE (fn) == FUNCTION_DECL
2959                         && cp_parser_type_dependent_expression_p (fn))
2960                       dependent_p = true;
2961                     else if (TREE_CODE (fn) == TEMPLATE_DECL
2962                              && cp_parser_dependent_template_p (fn))
2963                       dependent_p = true;
2964                     
2965                     fns = OVL_NEXT (fns);
2966                   }
2967               }
2968
2969             /* If the name was dependent on a template parameter,
2970                we will resolve the name at instantiation time.  */
2971             if (dependent_p)
2972               {
2973                 /* Create a SCOPE_REF for qualified names.  */
2974                 if (parser->scope)
2975                   {
2976                     if (TYPE_P (parser->scope))
2977                       *qualifying_class = parser->scope;
2978                     return build_nt (SCOPE_REF, 
2979                                      parser->scope, 
2980                                      id_expression);
2981                   }
2982                 /* A TEMPLATE_ID already contains all the information
2983                    we need.  */
2984                 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2985                   return id_expression;
2986                 /* Create a LOOKUP_EXPR for other unqualified names.  */
2987                 return build_min_nt (LOOKUP_EXPR, id_expression);
2988               }
2989
2990             if (parser->scope)
2991               {
2992                 decl = (adjust_result_of_qualified_name_lookup 
2993                         (decl, parser->scope, current_class_type));
2994                 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2995                   *qualifying_class = parser->scope;
2996               }
2997             /* Resolve references to variables of anonymous unions
2998                into COMPONENT_REFs.  */
2999             else if (TREE_CODE (decl) == ALIAS_DECL)
3000               decl = DECL_INITIAL (decl);
3001             else
3002               /* Transform references to non-static data members into
3003                  COMPONENT_REFs.  */
3004               decl = hack_identifier (decl, id_expression);
3005           }
3006
3007         if (TREE_DEPRECATED (decl))
3008           warn_deprecated_use (decl);
3009
3010         return decl;
3011       }
3012
3013       /* Anything else is an error.  */
3014     default:
3015       cp_parser_error (parser, "expected primary-expression");
3016       return error_mark_node;
3017     }
3018 }
3019
3020 /* Parse an id-expression.
3021
3022    id-expression:
3023      unqualified-id
3024      qualified-id
3025
3026    qualified-id:
3027      :: [opt] nested-name-specifier template [opt] unqualified-id
3028      :: identifier
3029      :: operator-function-id
3030      :: template-id
3031
3032    Return a representation of the unqualified portion of the
3033    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3034    a `::' or nested-name-specifier.
3035
3036    Often, if the id-expression was a qualified-id, the caller will
3037    want to make a SCOPE_REF to represent the qualified-id.  This
3038    function does not do this in order to avoid wastefully creating
3039    SCOPE_REFs when they are not required.
3040
3041    If ASSUME_TYPENAME_P is true then we assume that qualified names
3042    are typenames.  This flag is set when parsing a declarator-id;
3043    for something like:
3044
3045      template <class T>
3046      int S<T>::R::i = 3;
3047
3048    we are supposed to assume that `S<T>::R' is a class.
3049
3050    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3051    `template' keyword.
3052
3053    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3054    uninstantiated templates.  
3055
3056    If *TEMPLATE_KEYWORD_P is non-NULL, it is set to true iff the
3057    `template' keyword is used to explicitly indicate that the entity
3058    named is a template.  */
3059
3060 static tree
3061 cp_parser_id_expression (cp_parser *parser,
3062                          bool template_keyword_p,
3063                          bool check_dependency_p,
3064                          bool *template_p)
3065 {
3066   bool global_scope_p;
3067   bool nested_name_specifier_p;
3068
3069   /* Assume the `template' keyword was not used.  */
3070   if (template_p)
3071     *template_p = false;
3072
3073   /* Look for the optional `::' operator.  */
3074   global_scope_p 
3075     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) 
3076        != NULL_TREE);
3077   /* Look for the optional nested-name-specifier.  */
3078   nested_name_specifier_p 
3079     = (cp_parser_nested_name_specifier_opt (parser,
3080                                             /*typename_keyword_p=*/false,
3081                                             check_dependency_p,
3082                                             /*type_p=*/false)
3083        != NULL_TREE);
3084   /* If there is a nested-name-specifier, then we are looking at
3085      the first qualified-id production.  */
3086   if (nested_name_specifier_p)
3087     {
3088       tree saved_scope;
3089       tree saved_object_scope;
3090       tree saved_qualifying_scope;
3091       tree unqualified_id;
3092       bool is_template;
3093
3094       /* See if the next token is the `template' keyword.  */
3095       if (!template_p)
3096         template_p = &is_template;
3097       *template_p = cp_parser_optional_template_keyword (parser);
3098       /* Name lookup we do during the processing of the
3099          unqualified-id might obliterate SCOPE.  */
3100       saved_scope = parser->scope;
3101       saved_object_scope = parser->object_scope;
3102       saved_qualifying_scope = parser->qualifying_scope;
3103       /* Process the final unqualified-id.  */
3104       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3105                                                  check_dependency_p);
3106       /* Restore the SAVED_SCOPE for our caller.  */
3107       parser->scope = saved_scope;
3108       parser->object_scope = saved_object_scope;
3109       parser->qualifying_scope = saved_qualifying_scope;
3110
3111       return unqualified_id;
3112     }
3113   /* Otherwise, if we are in global scope, then we are looking at one
3114      of the other qualified-id productions.  */
3115   else if (global_scope_p)
3116     {
3117       cp_token *token;
3118       tree id;
3119
3120       /* We don't know yet whether or not this will be a 
3121          template-id.  */
3122       cp_parser_parse_tentatively (parser);
3123       /* Try a template-id.  */
3124       id = cp_parser_template_id (parser, 
3125                                   /*template_keyword_p=*/false,
3126                                   /*check_dependency_p=*/true);
3127       /* If that worked, we're done.  */
3128       if (cp_parser_parse_definitely (parser))
3129         return id;
3130
3131       /* Peek at the next token.  */
3132       token = cp_lexer_peek_token (parser->lexer);
3133
3134       switch (token->type)
3135         {
3136         case CPP_NAME:
3137           return cp_parser_identifier (parser);
3138
3139         case CPP_KEYWORD:
3140           if (token->keyword == RID_OPERATOR)
3141             return cp_parser_operator_function_id (parser);
3142           /* Fall through.  */
3143           
3144         default:
3145           cp_parser_error (parser, "expected id-expression");
3146           return error_mark_node;
3147         }
3148     }
3149   else
3150     return cp_parser_unqualified_id (parser, template_keyword_p,
3151                                      /*check_dependency_p=*/true);
3152 }
3153
3154 /* Parse an unqualified-id.
3155
3156    unqualified-id:
3157      identifier
3158      operator-function-id
3159      conversion-function-id
3160      ~ class-name
3161      template-id
3162
3163    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3164    keyword, in a construct like `A::template ...'.
3165
3166    Returns a representation of unqualified-id.  For the `identifier'
3167    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3168    production a BIT_NOT_EXPR is returned; the operand of the
3169    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3170    other productions, see the documentation accompanying the
3171    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3172    names are looked up in uninstantiated templates.  */
3173
3174 static tree
3175 cp_parser_unqualified_id (parser, template_keyword_p,
3176                           check_dependency_p)
3177      cp_parser *parser;
3178      bool template_keyword_p;
3179      bool check_dependency_p;
3180 {
3181   cp_token *token;
3182
3183   /* Peek at the next token.  */
3184   token = cp_lexer_peek_token (parser->lexer);
3185   
3186   switch (token->type)
3187     {
3188     case CPP_NAME:
3189       {
3190         tree id;
3191
3192         /* We don't know yet whether or not this will be a
3193            template-id.  */
3194         cp_parser_parse_tentatively (parser);
3195         /* Try a template-id.  */
3196         id = cp_parser_template_id (parser, template_keyword_p,
3197                                     check_dependency_p);
3198         /* If it worked, we're done.  */
3199         if (cp_parser_parse_definitely (parser))
3200           return id;
3201         /* Otherwise, it's an ordinary identifier.  */
3202         return cp_parser_identifier (parser);
3203       }
3204
3205     case CPP_TEMPLATE_ID:
3206       return cp_parser_template_id (parser, template_keyword_p,
3207                                     check_dependency_p);
3208
3209     case CPP_COMPL:
3210       {
3211         tree type_decl;
3212         tree qualifying_scope;
3213         tree object_scope;
3214         tree scope;
3215
3216         /* Consume the `~' token.  */
3217         cp_lexer_consume_token (parser->lexer);
3218         /* Parse the class-name.  The standard, as written, seems to
3219            say that:
3220
3221              template <typename T> struct S { ~S (); };
3222              template <typename T> S<T>::~S() {}
3223
3224            is invalid, since `~' must be followed by a class-name, but
3225            `S<T>' is dependent, and so not known to be a class.
3226            That's not right; we need to look in uninstantiated
3227            templates.  A further complication arises from:
3228
3229              template <typename T> void f(T t) {
3230                t.T::~T();
3231              } 
3232
3233            Here, it is not possible to look up `T' in the scope of `T'
3234            itself.  We must look in both the current scope, and the
3235            scope of the containing complete expression.  
3236
3237            Yet another issue is:
3238
3239              struct S {
3240                int S;
3241                ~S();
3242              };
3243
3244              S::~S() {}
3245
3246            The standard does not seem to say that the `S' in `~S'
3247            should refer to the type `S' and not the data member
3248            `S::S'.  */
3249
3250         /* DR 244 says that we look up the name after the "~" in the
3251            same scope as we looked up the qualifying name.  That idea
3252            isn't fully worked out; it's more complicated than that.  */
3253         scope = parser->scope;
3254         object_scope = parser->object_scope;
3255         qualifying_scope = parser->qualifying_scope;
3256
3257         /* If the name is of the form "X::~X" it's OK.  */
3258         if (scope && TYPE_P (scope)
3259             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3260             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
3261                 == CPP_OPEN_PAREN)
3262             && (cp_lexer_peek_token (parser->lexer)->value 
3263                 == TYPE_IDENTIFIER (scope)))
3264           {
3265             cp_lexer_consume_token (parser->lexer);
3266             return build_nt (BIT_NOT_EXPR, scope);
3267           }
3268
3269         /* If there was an explicit qualification (S::~T), first look
3270            in the scope given by the qualification (i.e., S).  */
3271         if (scope)
3272           {
3273             cp_parser_parse_tentatively (parser);
3274             type_decl = cp_parser_class_name (parser, 
3275                                               /*typename_keyword_p=*/false,
3276                                               /*template_keyword_p=*/false,
3277                                               /*type_p=*/false,
3278                                               /*check_access_p=*/true,
3279                                               /*check_dependency=*/false,
3280                                               /*class_head_p=*/false);
3281             if (cp_parser_parse_definitely (parser))
3282               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3283           }
3284         /* In "N::S::~S", look in "N" as well.  */
3285         if (scope && qualifying_scope)
3286           {
3287             cp_parser_parse_tentatively (parser);
3288             parser->scope = qualifying_scope;
3289             parser->object_scope = NULL_TREE;
3290             parser->qualifying_scope = NULL_TREE;
3291             type_decl 
3292               = cp_parser_class_name (parser, 
3293                                       /*typename_keyword_p=*/false,
3294                                       /*template_keyword_p=*/false,
3295                                       /*type_p=*/false,
3296                                       /*check_access_p=*/true,
3297                                       /*check_dependency=*/false,
3298                                       /*class_head_p=*/false);
3299             if (cp_parser_parse_definitely (parser))
3300               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3301           }
3302         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3303         else if (object_scope)
3304           {
3305             cp_parser_parse_tentatively (parser);
3306             parser->scope = object_scope;
3307             parser->object_scope = NULL_TREE;
3308             parser->qualifying_scope = NULL_TREE;
3309             type_decl 
3310               = cp_parser_class_name (parser, 
3311                                       /*typename_keyword_p=*/false,
3312                                       /*template_keyword_p=*/false,
3313                                       /*type_p=*/false,
3314                                       /*check_access_p=*/true,
3315                                       /*check_dependency=*/false,
3316                                       /*class_head_p=*/false);
3317             if (cp_parser_parse_definitely (parser))
3318               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3319           }
3320         /* Look in the surrounding context.  */
3321         parser->scope = NULL_TREE;
3322         parser->object_scope = NULL_TREE;
3323         parser->qualifying_scope = NULL_TREE;
3324         type_decl 
3325           = cp_parser_class_name (parser, 
3326                                   /*typename_keyword_p=*/false,
3327                                   /*template_keyword_p=*/false,
3328                                   /*type_p=*/false,
3329                                   /*check_access_p=*/true,
3330                                   /*check_dependency=*/false,
3331                                   /*class_head_p=*/false);
3332         /* If an error occurred, assume that the name of the
3333            destructor is the same as the name of the qualifying
3334            class.  That allows us to keep parsing after running
3335            into ill-formed destructor names.  */
3336         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3337           return build_nt (BIT_NOT_EXPR, scope);
3338         else if (type_decl == error_mark_node)
3339           return error_mark_node;
3340
3341         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3342       }
3343
3344     case CPP_KEYWORD:
3345       if (token->keyword == RID_OPERATOR)
3346         {
3347           tree id;
3348
3349           /* This could be a template-id, so we try that first.  */
3350           cp_parser_parse_tentatively (parser);
3351           /* Try a template-id.  */
3352           id = cp_parser_template_id (parser, template_keyword_p,
3353                                       /*check_dependency_p=*/true);
3354           /* If that worked, we're done.  */
3355           if (cp_parser_parse_definitely (parser))
3356             return id;
3357           /* We still don't know whether we're looking at an
3358              operator-function-id or a conversion-function-id.  */
3359           cp_parser_parse_tentatively (parser);
3360           /* Try an operator-function-id.  */
3361           id = cp_parser_operator_function_id (parser);
3362           /* If that didn't work, try a conversion-function-id.  */
3363           if (!cp_parser_parse_definitely (parser))
3364             id = cp_parser_conversion_function_id (parser);
3365
3366           return id;
3367         }
3368       /* Fall through.  */
3369
3370     default:
3371       cp_parser_error (parser, "expected unqualified-id");
3372       return error_mark_node;
3373     }
3374 }
3375
3376 /* Parse an (optional) nested-name-specifier.
3377
3378    nested-name-specifier:
3379      class-or-namespace-name :: nested-name-specifier [opt]
3380      class-or-namespace-name :: template nested-name-specifier [opt]
3381
3382    PARSER->SCOPE should be set appropriately before this function is
3383    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3384    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3385    in name lookups.
3386
3387    Sets PARSER->SCOPE to the class (TYPE) or namespace
3388    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3389    it unchanged if there is no nested-name-specifier.  Returns the new
3390    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.  */
3391
3392 static tree
3393 cp_parser_nested_name_specifier_opt (cp_parser *parser, 
3394                                      bool typename_keyword_p, 
3395                                      bool check_dependency_p,
3396                                      bool type_p)
3397 {
3398   bool success = false;
3399   tree access_check = NULL_TREE;
3400   ptrdiff_t start;
3401
3402   /* If the next token corresponds to a nested name specifier, there
3403      is no need to reparse it.  */
3404   if (cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3405     {
3406       tree value;
3407       tree check;
3408
3409       /* Get the stored value.  */
3410       value = cp_lexer_consume_token (parser->lexer)->value;
3411       /* Perform any access checks that were deferred.  */
3412       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
3413         cp_parser_defer_access_check (parser, 
3414                                       TREE_PURPOSE (check),
3415                                       TREE_VALUE (check));
3416       /* Set the scope from the stored value.  */
3417       parser->scope = TREE_VALUE (value);
3418       parser->qualifying_scope = TREE_TYPE (value);
3419       parser->object_scope = NULL_TREE;
3420       return parser->scope;
3421     }
3422
3423   /* Remember where the nested-name-specifier starts.  */
3424   if (cp_parser_parsing_tentatively (parser)
3425       && !cp_parser_committed_to_tentative_parse (parser))
3426     {
3427       cp_token *next_token = cp_lexer_peek_token (parser->lexer);
3428       start = cp_lexer_token_difference (parser->lexer,
3429                                          parser->lexer->first_token,
3430                                          next_token);
3431       access_check = parser->context->deferred_access_checks;
3432     }
3433   else
3434     start = -1;
3435
3436   while (true)
3437     {
3438       tree new_scope;
3439       tree old_scope;
3440       tree saved_qualifying_scope;
3441       cp_token *token;
3442       bool template_keyword_p;
3443
3444       /* Spot cases that cannot be the beginning of a
3445          nested-name-specifier.  On the second and subsequent times
3446          through the loop, we look for the `template' keyword.  */
3447       if (success 
3448           && cp_lexer_next_token_is_keyword (parser->lexer,
3449                                              RID_TEMPLATE))
3450         ;
3451       /* A template-id can start a nested-name-specifier.  */
3452       else if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
3453         ;
3454       else
3455         {
3456           /* If the next token is not an identifier, then it is
3457              definitely not a class-or-namespace-name.  */
3458           if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
3459             break;
3460           /* If the following token is neither a `<' (to begin a
3461              template-id), nor a `::', then we are not looking at a
3462              nested-name-specifier.  */
3463           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3464           if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3465             break;
3466         }
3467
3468       /* The nested-name-specifier is optional, so we parse
3469          tentatively.  */
3470       cp_parser_parse_tentatively (parser);
3471
3472       /* Look for the optional `template' keyword, if this isn't the
3473          first time through the loop.  */
3474       if (success)
3475         template_keyword_p = cp_parser_optional_template_keyword (parser);
3476       else
3477         template_keyword_p = false;
3478
3479       /* Save the old scope since the name lookup we are about to do
3480          might destroy it.  */
3481       old_scope = parser->scope;
3482       saved_qualifying_scope = parser->qualifying_scope;
3483       /* Parse the qualifying entity.  */
3484       new_scope 
3485         = cp_parser_class_or_namespace_name (parser,
3486                                              typename_keyword_p,
3487                                              template_keyword_p,
3488                                              check_dependency_p,
3489                                              type_p);
3490       /* Look for the `::' token.  */
3491       cp_parser_require (parser, CPP_SCOPE, "`::'");
3492
3493       /* If we found what we wanted, we keep going; otherwise, we're
3494          done.  */
3495       if (!cp_parser_parse_definitely (parser))
3496         {
3497           bool error_p = false;
3498
3499           /* Restore the OLD_SCOPE since it was valid before the
3500              failed attempt at finding the last
3501              class-or-namespace-name.  */
3502           parser->scope = old_scope;
3503           parser->qualifying_scope = saved_qualifying_scope;
3504           /* If the next token is an identifier, and the one after
3505              that is a `::', then any valid interpretation would have
3506              found a class-or-namespace-name.  */
3507           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3508                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
3509                      == CPP_SCOPE)
3510                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
3511                      != CPP_COMPL))
3512             {
3513               token = cp_lexer_consume_token (parser->lexer);
3514               if (!error_p) 
3515                 {
3516                   tree decl;
3517
3518                   decl = cp_parser_lookup_name_simple (parser, token->value);
3519                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3520                     error ("`%D' used without template parameters",
3521                            decl);
3522                   else if (parser->scope)
3523                     {
3524                       if (TYPE_P (parser->scope))
3525                         error ("`%T::%D' is not a class-name or "
3526                                "namespace-name",
3527                                parser->scope, token->value);
3528                       else
3529                         error ("`%D::%D' is not a class-name or "
3530                                "namespace-name",
3531                                parser->scope, token->value);
3532                     }
3533                   else
3534                     error ("`%D' is not a class-name or namespace-name",
3535                            token->value);
3536                   parser->scope = NULL_TREE;
3537                   error_p = true;
3538                 }
3539               cp_lexer_consume_token (parser->lexer);
3540             }
3541           break;
3542         }
3543
3544       /* We've found one valid nested-name-specifier.  */
3545       success = true;
3546       /* Make sure we look in the right scope the next time through
3547          the loop.  */
3548       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL 
3549                        ? TREE_TYPE (new_scope)
3550                        : new_scope);
3551       /* If it is a class scope, try to complete it; we are about to
3552          be looking up names inside the class.  */
3553       if (TYPE_P (parser->scope))
3554         complete_type (parser->scope);
3555     }
3556
3557   /* If parsing tentatively, replace the sequence of tokens that makes
3558      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3559      token.  That way, should we re-parse the token stream, we will
3560      not have to repeat the effort required to do the parse, nor will
3561      we issue duplicate error messages.  */
3562   if (success && start >= 0)
3563     {
3564       cp_token *token;
3565       tree c;
3566
3567       /* Find the token that corresponds to the start of the
3568          template-id.  */
3569       token = cp_lexer_advance_token (parser->lexer, 
3570                                       parser->lexer->first_token,
3571                                       start);
3572
3573       /* Remember the access checks associated with this
3574          nested-name-specifier.  */
3575       c = parser->context->deferred_access_checks;
3576       if (c == access_check)
3577         access_check = NULL_TREE;
3578       else
3579         {
3580           while (TREE_CHAIN (c) != access_check)
3581             c = TREE_CHAIN (c);
3582           access_check = parser->context->deferred_access_checks;
3583           parser->context->deferred_access_checks = TREE_CHAIN (c);
3584           TREE_CHAIN (c) = NULL_TREE;
3585         }
3586
3587       /* Reset the contents of the START token.  */
3588       token->type = CPP_NESTED_NAME_SPECIFIER;
3589       token->value = build_tree_list (access_check, parser->scope);
3590       TREE_TYPE (token->value) = parser->qualifying_scope;
3591       token->keyword = RID_MAX;
3592       /* Purge all subsequent tokens.  */
3593       cp_lexer_purge_tokens_after (parser->lexer, token);
3594     }
3595
3596   return success ? parser->scope : NULL_TREE;
3597 }
3598
3599 /* Parse a nested-name-specifier.  See
3600    cp_parser_nested_name_specifier_opt for details.  This function
3601    behaves identically, except that it will an issue an error if no
3602    nested-name-specifier is present, and it will return
3603    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3604    is present.  */
3605
3606 static tree
3607 cp_parser_nested_name_specifier (cp_parser *parser, 
3608                                  bool typename_keyword_p, 
3609                                  bool check_dependency_p,
3610                                  bool type_p)
3611 {
3612   tree scope;
3613
3614   /* Look for the nested-name-specifier.  */
3615   scope = cp_parser_nested_name_specifier_opt (parser,
3616                                                typename_keyword_p,
3617                                                check_dependency_p,
3618                                                type_p);
3619   /* If it was not present, issue an error message.  */
3620   if (!scope)
3621     {
3622       cp_parser_error (parser, "expected nested-name-specifier");
3623       return error_mark_node;
3624     }
3625
3626   return scope;
3627 }
3628
3629 /* Parse a class-or-namespace-name.
3630
3631    class-or-namespace-name:
3632      class-name
3633      namespace-name
3634
3635    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3636    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3637    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3638    TYPE_P is TRUE iff the next name should be taken as a class-name,
3639    even the same name is declared to be another entity in the same
3640    scope.
3641
3642    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3643    specified by the class-or-namespace-name.  */
3644
3645 static tree
3646 cp_parser_class_or_namespace_name (cp_parser *parser, 
3647                                    bool typename_keyword_p,
3648                                    bool template_keyword_p,
3649                                    bool check_dependency_p,
3650                                    bool type_p)
3651 {
3652   tree saved_scope;
3653   tree saved_qualifying_scope;
3654   tree saved_object_scope;
3655   tree scope;
3656
3657   /* If the next token is the `template' keyword, we know that we are
3658      looking at a class-name.  */
3659   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3660     return cp_parser_class_name (parser, 
3661                                  typename_keyword_p,
3662                                  template_keyword_p,
3663                                  type_p,
3664                                  /*check_access_p=*/true,
3665                                  check_dependency_p,
3666                                  /*class_head_p=*/false);
3667   /* Before we try to parse the class-name, we must save away the
3668      current PARSER->SCOPE since cp_parser_class_name will destroy
3669      it.  */
3670   saved_scope = parser->scope;
3671   saved_qualifying_scope = parser->qualifying_scope;
3672   saved_object_scope = parser->object_scope;
3673   /* Try for a class-name first.  */
3674   cp_parser_parse_tentatively (parser);
3675   scope = cp_parser_class_name (parser, 
3676                                 typename_keyword_p,
3677                                 template_keyword_p,
3678                                 type_p,
3679                                 /*check_access_p=*/true,
3680                                 check_dependency_p,
3681                                 /*class_head_p=*/false);
3682   /* If that didn't work, try for a namespace-name.  */
3683   if (!cp_parser_parse_definitely (parser))
3684     {
3685       /* Restore the saved scope.  */
3686       parser->scope = saved_scope;
3687       parser->qualifying_scope = saved_qualifying_scope;
3688       parser->object_scope = saved_object_scope;
3689       /* Now look for a namespace-name.  */
3690       scope = cp_parser_namespace_name (parser);
3691     }
3692
3693   return scope;
3694 }
3695
3696 /* Parse a postfix-expression.
3697
3698    postfix-expression:
3699      primary-expression
3700      postfix-expression [ expression ]
3701      postfix-expression ( expression-list [opt] )
3702      simple-type-specifier ( expression-list [opt] )
3703      typename :: [opt] nested-name-specifier identifier 
3704        ( expression-list [opt] )
3705      typename :: [opt] nested-name-specifier template [opt] template-id
3706        ( expression-list [opt] )
3707      postfix-expression . template [opt] id-expression
3708      postfix-expression -> template [opt] id-expression
3709      postfix-expression . pseudo-destructor-name
3710      postfix-expression -> pseudo-destructor-name
3711      postfix-expression ++
3712      postfix-expression --
3713      dynamic_cast < type-id > ( expression )
3714      static_cast < type-id > ( expression )
3715      reinterpret_cast < type-id > ( expression )
3716      const_cast < type-id > ( expression )
3717      typeid ( expression )
3718      typeid ( type-id )
3719
3720    GNU Extension:
3721      
3722    postfix-expression:
3723      ( type-id ) { initializer-list , [opt] }
3724
3725    This extension is a GNU version of the C99 compound-literal
3726    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3727    but they are essentially the same concept.)
3728
3729    If ADDRESS_P is true, the postfix expression is the operand of the
3730    `&' operator.
3731
3732    Returns a representation of the expression.  */
3733
3734 static tree
3735 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3736 {
3737   cp_token *token;
3738   enum rid keyword;
3739   cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3740   tree postfix_expression = NULL_TREE;
3741   /* Non-NULL only if the current postfix-expression can be used to
3742      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3743      class used to qualify the member.  */
3744   tree qualifying_class = NULL_TREE;
3745   bool done;
3746
3747   /* Peek at the next token.  */
3748   token = cp_lexer_peek_token (parser->lexer);
3749   /* Some of the productions are determined by keywords.  */
3750   keyword = token->keyword;
3751   switch (keyword)
3752     {
3753     case RID_DYNCAST:
3754     case RID_STATCAST:
3755     case RID_REINTCAST:
3756     case RID_CONSTCAST:
3757       {
3758         tree type;
3759         tree expression;
3760         const char *saved_message;
3761
3762         /* All of these can be handled in the same way from the point
3763            of view of parsing.  Begin by consuming the token
3764            identifying the cast.  */
3765         cp_lexer_consume_token (parser->lexer);
3766         
3767         /* New types cannot be defined in the cast.  */
3768         saved_message = parser->type_definition_forbidden_message;
3769         parser->type_definition_forbidden_message
3770           = "types may not be defined in casts";
3771
3772         /* Look for the opening `<'.  */
3773         cp_parser_require (parser, CPP_LESS, "`<'");
3774         /* Parse the type to which we are casting.  */
3775         type = cp_parser_type_id (parser);
3776         /* Look for the closing `>'.  */
3777         cp_parser_require (parser, CPP_GREATER, "`>'");
3778         /* Restore the old message.  */
3779         parser->type_definition_forbidden_message = saved_message;
3780
3781         /* And the expression which is being cast.  */
3782         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3783         expression = cp_parser_expression (parser);
3784         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3785
3786         switch (keyword)
3787           {
3788           case RID_DYNCAST:
3789             postfix_expression
3790               = build_dynamic_cast (type, expression);
3791             break;
3792           case RID_STATCAST:
3793             postfix_expression
3794               = build_static_cast (type, expression);
3795             break;
3796           case RID_REINTCAST:
3797             postfix_expression
3798               = build_reinterpret_cast (type, expression);
3799             break;
3800           case RID_CONSTCAST:
3801             postfix_expression
3802               = build_const_cast (type, expression);
3803             break;
3804           default:
3805             abort ();
3806           }
3807       }
3808       break;
3809
3810     case RID_TYPEID:
3811       {
3812         tree type;
3813         const char *saved_message;
3814
3815         /* Consume the `typeid' token.  */
3816         cp_lexer_consume_token (parser->lexer);
3817         /* Look for the `(' token.  */
3818         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3819         /* Types cannot be defined in a `typeid' expression.  */
3820         saved_message = parser->type_definition_forbidden_message;
3821         parser->type_definition_forbidden_message
3822           = "types may not be defined in a `typeid\' expression";
3823         /* We can't be sure yet whether we're looking at a type-id or an
3824            expression.  */
3825         cp_parser_parse_tentatively (parser);
3826         /* Try a type-id first.  */
3827         type = cp_parser_type_id (parser);
3828         /* Look for the `)' token.  Otherwise, we can't be sure that
3829            we're not looking at an expression: consider `typeid (int
3830            (3))', for example.  */
3831         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3832         /* If all went well, simply lookup the type-id.  */
3833         if (cp_parser_parse_definitely (parser))
3834           postfix_expression = get_typeid (type);
3835         /* Otherwise, fall back to the expression variant.  */
3836         else
3837           {
3838             tree expression;
3839
3840             /* Look for an expression.  */
3841             expression = cp_parser_expression (parser);
3842             /* Compute its typeid.  */
3843             postfix_expression = build_typeid (expression);
3844             /* Look for the `)' token.  */
3845             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3846           }
3847
3848         /* Restore the saved message.  */
3849         parser->type_definition_forbidden_message = saved_message;
3850       }
3851       break;
3852       
3853     case RID_TYPENAME:
3854       {
3855         bool template_p = false;
3856         tree id;
3857         tree type;
3858
3859         /* Consume the `typename' token.  */
3860         cp_lexer_consume_token (parser->lexer);
3861         /* Look for the optional `::' operator.  */
3862         cp_parser_global_scope_opt (parser, 
3863                                     /*current_scope_valid_p=*/false);
3864         /* Look for the nested-name-specifier.  */
3865         cp_parser_nested_name_specifier (parser,
3866                                          /*typename_keyword_p=*/true,
3867                                          /*check_dependency_p=*/true,
3868                                          /*type_p=*/true);
3869         /* Look for the optional `template' keyword.  */
3870         template_p = cp_parser_optional_template_keyword (parser);
3871         /* We don't know whether we're looking at a template-id or an
3872            identifier.  */
3873         cp_parser_parse_tentatively (parser);
3874         /* Try a template-id.  */
3875         id = cp_parser_template_id (parser, template_p,
3876                                     /*check_dependency_p=*/true);
3877         /* If that didn't work, try an identifier.  */
3878         if (!cp_parser_parse_definitely (parser))
3879           id = cp_parser_identifier (parser);
3880         /* Create a TYPENAME_TYPE to represent the type to which the
3881            functional cast is being performed.  */
3882         type = make_typename_type (parser->scope, id, 
3883                                    /*complain=*/1);
3884
3885         postfix_expression = cp_parser_functional_cast (parser, type);
3886       }
3887       break;
3888
3889     default:
3890       {
3891         tree type;
3892
3893         /* If the next thing is a simple-type-specifier, we may be
3894            looking at a functional cast.  We could also be looking at
3895            an id-expression.  So, we try the functional cast, and if
3896            that doesn't work we fall back to the primary-expression.  */
3897         cp_parser_parse_tentatively (parser);
3898         /* Look for the simple-type-specifier.  */
3899         type = cp_parser_simple_type_specifier (parser, 
3900                                                 CP_PARSER_FLAGS_NONE);
3901         /* Parse the cast itself.  */
3902         if (!cp_parser_error_occurred (parser))
3903           postfix_expression 
3904             = cp_parser_functional_cast (parser, type);
3905         /* If that worked, we're done.  */
3906         if (cp_parser_parse_definitely (parser))
3907           break;
3908
3909         /* If the functional-cast didn't work out, try a
3910            compound-literal.  */
3911         if (cp_parser_allow_gnu_extensions_p (parser))
3912           {
3913             tree initializer_list = NULL_TREE;
3914
3915             cp_parser_parse_tentatively (parser);
3916             /* Look for the `('.  */
3917             if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3918               {
3919                 type = cp_parser_type_id (parser);
3920                 /* Look for the `)'.  */
3921                 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3922                 /* Look for the `{'.  */
3923                 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3924                 /* If things aren't going well, there's no need to
3925                    keep going.  */
3926                 if (!cp_parser_error_occurred (parser))
3927                   {
3928                     /* Parse the initializer-list.  */
3929                     initializer_list 
3930                       = cp_parser_initializer_list (parser);
3931                     /* Allow a trailing `,'.  */
3932                     if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3933                       cp_lexer_consume_token (parser->lexer);
3934                     /* Look for the final `}'.  */
3935                     cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3936                   }
3937               }
3938             /* If that worked, we're definitely looking at a
3939                compound-literal expression.  */
3940             if (cp_parser_parse_definitely (parser))
3941               {
3942                 /* Warn the user that a compound literal is not
3943                    allowed in standard C++.  */
3944                 if (pedantic)
3945                   pedwarn ("ISO C++ forbids compound-literals");
3946                 /* Form the representation of the compound-literal.  */
3947                 postfix_expression 
3948                   = finish_compound_literal (type, initializer_list);
3949                 break;
3950               }
3951           }
3952
3953         /* It must be a primary-expression.  */
3954         postfix_expression = cp_parser_primary_expression (parser, 
3955                                                            &idk,
3956                                                            &qualifying_class);
3957       }
3958       break;
3959     }
3960
3961   /* Peek at the next token.  */
3962   token = cp_lexer_peek_token (parser->lexer);
3963   done = (token->type != CPP_OPEN_SQUARE
3964           && token->type != CPP_OPEN_PAREN
3965           && token->type != CPP_DOT
3966           && token->type != CPP_DEREF
3967           && token->type != CPP_PLUS_PLUS
3968           && token->type != CPP_MINUS_MINUS);
3969
3970   /* If the postfix expression is complete, finish up.  */
3971   if (address_p && qualifying_class && done)
3972     {
3973       if (TREE_CODE (postfix_expression) == SCOPE_REF)
3974         postfix_expression = TREE_OPERAND (postfix_expression, 1);
3975       postfix_expression 
3976         = build_offset_ref (qualifying_class, postfix_expression);
3977       return postfix_expression;
3978     }
3979
3980   /* Otherwise, if we were avoiding committing until we knew
3981      whether or not we had a pointer-to-member, we now know that
3982      the expression is an ordinary reference to a qualified name.  */
3983   if (qualifying_class && !processing_template_decl)
3984     {
3985       if (TREE_CODE (postfix_expression) == FIELD_DECL)
3986         postfix_expression 
3987           = finish_non_static_data_member (postfix_expression,
3988                                            qualifying_class);
3989       else if (BASELINK_P (postfix_expression))
3990         {
3991           tree fn;
3992           tree fns;
3993
3994           /* See if any of the functions are non-static members.  */
3995           fns = BASELINK_FUNCTIONS (postfix_expression);
3996           if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3997             fns = TREE_OPERAND (fns, 0);
3998           for (fn = fns; fn; fn = OVL_NEXT (fn))
3999             if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4000               break;
4001           /* If so, the expression may be relative to the current
4002              class.  */
4003           if (fn && current_class_type 
4004               && DERIVED_FROM_P (qualifying_class, current_class_type))
4005             postfix_expression 
4006               = (build_class_member_access_expr 
4007                  (maybe_dummy_object (qualifying_class, NULL),
4008                   postfix_expression,
4009                   BASELINK_ACCESS_BINFO (postfix_expression),
4010                   /*preserve_reference=*/false));
4011           else if (done)
4012             return build_offset_ref (qualifying_class,
4013                                      postfix_expression);
4014         }
4015     }
4016
4017   /* Remember that there was a reference to this entity.  */
4018   if (DECL_P (postfix_expression))
4019     mark_used (postfix_expression);
4020
4021   /* Keep looping until the postfix-expression is complete.  */
4022   while (true)
4023     {
4024       if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4025           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4026         {
4027           /* It is not a Koenig lookup function call.  */
4028           unqualified_name_lookup_error (postfix_expression);
4029           postfix_expression = error_mark_node;
4030         }
4031       
4032       /* Peek at the next token.  */
4033       token = cp_lexer_peek_token (parser->lexer);
4034
4035       switch (token->type)
4036         {
4037         case CPP_OPEN_SQUARE:
4038           /* postfix-expression [ expression ] */
4039           {
4040             tree index;
4041
4042             /* Consume the `[' token.  */
4043             cp_lexer_consume_token (parser->lexer);
4044             /* Parse the index expression.  */
4045             index = cp_parser_expression (parser);
4046             /* Look for the closing `]'.  */
4047             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4048
4049             /* Build the ARRAY_REF.  */
4050             postfix_expression 
4051               = grok_array_decl (postfix_expression, index);
4052             idk = CP_PARSER_ID_KIND_NONE;
4053           }
4054           break;
4055
4056         case CPP_OPEN_PAREN:
4057           /* postfix-expression ( expression-list [opt] ) */
4058           {
4059             tree args;
4060
4061             /* Consume the `(' token.  */
4062             cp_lexer_consume_token (parser->lexer);
4063             /* If the next token is not a `)', then there are some
4064                arguments.  */
4065             if (cp_lexer_next_token_is_not (parser->lexer, 
4066                                             CPP_CLOSE_PAREN))
4067               args = cp_parser_expression_list (parser);
4068             else
4069               args = NULL_TREE;
4070             /* Look for the closing `)'.  */
4071             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4072
4073             if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4074                 && (is_overloaded_fn (postfix_expression)
4075                     || DECL_P (postfix_expression)
4076                     || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4077                 && args)
4078               {
4079                 tree arg;
4080                 tree identifier = NULL_TREE;
4081                 tree functions = NULL_TREE;
4082
4083                 /* Find the name of the overloaded function.  */
4084                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4085                   identifier = postfix_expression;
4086                 else if (is_overloaded_fn (postfix_expression))
4087                   {
4088                     functions = postfix_expression;
4089                     identifier = DECL_NAME (get_first_fn (functions));
4090                   }
4091                 else if (DECL_P (postfix_expression))
4092                   {
4093                     functions = postfix_expression;
4094                     identifier = DECL_NAME (postfix_expression);
4095                   }
4096
4097                 /* A call to a namespace-scope function using an
4098                    unqualified name.
4099
4100                    Do Koenig lookup -- unless any of the arguments are
4101                    type-dependent.  */
4102                 for (arg = args; arg; arg = TREE_CHAIN (arg))
4103                   if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4104                       break;
4105                 if (!arg)
4106                   {
4107                     postfix_expression 
4108                       = lookup_arg_dependent(identifier, functions, args);
4109                     if (!postfix_expression)
4110                       {
4111                         /* The unqualified name could not be resolved.  */
4112                         unqualified_name_lookup_error (identifier);
4113                         postfix_expression = error_mark_node;
4114                       }
4115                     postfix_expression
4116                       = build_call_from_tree (postfix_expression, args, 
4117                                               /*diallow_virtual=*/false);
4118                     break;
4119                   }
4120                 postfix_expression = build_min_nt (LOOKUP_EXPR,
4121                                                    identifier);
4122               }
4123             else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED 
4124                      && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4125               {
4126                 /* The unqualified name could not be resolved.  */
4127                 unqualified_name_lookup_error (postfix_expression);
4128                 postfix_expression = error_mark_node;
4129                 break;
4130               }
4131
4132             /* In the body of a template, no further processing is
4133                required.  */
4134             if (processing_template_decl)
4135               {
4136                 postfix_expression = build_nt (CALL_EXPR,
4137                                                postfix_expression, 
4138                                                args);
4139                 break;
4140               }
4141
4142             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4143               postfix_expression
4144                 = (build_new_method_call 
4145                    (TREE_OPERAND (postfix_expression, 0),
4146                     TREE_OPERAND (postfix_expression, 1),
4147                     args, NULL_TREE, 
4148                     (idk == CP_PARSER_ID_KIND_QUALIFIED 
4149                      ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4150             else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4151               postfix_expression = (build_offset_ref_call_from_tree
4152                                     (postfix_expression, args));
4153             else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4154               {
4155                 /* A call to a static class member, or a
4156                    namespace-scope function.  */
4157                 postfix_expression
4158                   = finish_call_expr (postfix_expression, args,
4159                                       /*disallow_virtual=*/true);
4160               }
4161             else
4162               {
4163                 /* All other function calls.  */
4164                 postfix_expression 
4165                   = finish_call_expr (postfix_expression, args, 
4166                                       /*disallow_virtual=*/false);
4167               }
4168
4169             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4170             idk = CP_PARSER_ID_KIND_NONE;
4171           }
4172           break;
4173           
4174         case CPP_DOT:
4175         case CPP_DEREF:
4176           /* postfix-expression . template [opt] id-expression  
4177              postfix-expression . pseudo-destructor-name 
4178              postfix-expression -> template [opt] id-expression
4179              postfix-expression -> pseudo-destructor-name */
4180           {
4181             tree name;
4182             bool dependent_p;
4183             bool template_p;
4184             tree scope = NULL_TREE;
4185
4186             /* If this is a `->' operator, dereference the pointer.  */
4187             if (token->type == CPP_DEREF)
4188               postfix_expression = build_x_arrow (postfix_expression);
4189             /* Check to see whether or not the expression is
4190                type-dependent.  */
4191             dependent_p = (cp_parser_type_dependent_expression_p 
4192                            (postfix_expression));
4193             /* The identifier following the `->' or `.' is not
4194                qualified.  */
4195             parser->scope = NULL_TREE;
4196             parser->qualifying_scope = NULL_TREE;
4197             parser->object_scope = NULL_TREE;
4198             /* Enter the scope corresponding to the type of the object
4199                given by the POSTFIX_EXPRESSION.  */
4200             if (!dependent_p 
4201                 && TREE_TYPE (postfix_expression) != NULL_TREE)
4202               {
4203                 scope = TREE_TYPE (postfix_expression);
4204                 /* According to the standard, no expression should
4205                    ever have reference type.  Unfortunately, we do not
4206                    currently match the standard in this respect in
4207                    that our internal representation of an expression
4208                    may have reference type even when the standard says
4209                    it does not.  Therefore, we have to manually obtain
4210                    the underlying type here.  */
4211                 if (TREE_CODE (scope) == REFERENCE_TYPE)
4212                   scope = TREE_TYPE (scope);
4213                 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4214                    type of the field.  We get an OFFSET_TYPE for
4215                    something like:
4216
4217                      S::T.a ...
4218
4219                    Probably, we should not get an OFFSET_TYPE here;
4220                    that transformation should be made only if `&S::T'
4221                    is written.  */
4222                 if (TREE_CODE (scope) == OFFSET_TYPE)
4223                   scope = TREE_TYPE (scope);
4224                 /* The type of the POSTFIX_EXPRESSION must be
4225                    complete.  */
4226                 scope = complete_type_or_else (scope, NULL_TREE);
4227                 /* Let the name lookup machinery know that we are
4228                    processing a class member access expression.  */
4229                 parser->context->object_type = scope;
4230                 /* If something went wrong, we want to be able to
4231                    discern that case, as opposed to the case where
4232                    there was no SCOPE due to the type of expression
4233                    being dependent.  */
4234                 if (!scope)
4235                   scope = error_mark_node;
4236               }
4237
4238             /* Consume the `.' or `->' operator.  */
4239             cp_lexer_consume_token (parser->lexer);
4240             /* If the SCOPE is not a scalar type, we are looking at an
4241                ordinary class member access expression, rather than a
4242                pseudo-destructor-name.  */
4243             if (!scope || !SCALAR_TYPE_P (scope))
4244               {
4245                 template_p = cp_parser_optional_template_keyword (parser);
4246                 /* Parse the id-expression.  */
4247                 name = cp_parser_id_expression (parser,
4248                                                 template_p,
4249                                                 /*check_dependency_p=*/true,
4250                                                 /*template_p=*/NULL);
4251                 /* In general, build a SCOPE_REF if the member name is
4252                    qualified.  However, if the name was not dependent
4253                    and has already been resolved; there is no need to
4254                    build the SCOPE_REF.  For example;
4255
4256                      struct X { void f(); };
4257                      template <typename T> void f(T* t) { t->X::f(); }
4258  
4259                    Even though "t" is dependent, "X::f" is not and has 
4260                    except that for a BASELINK there is no need to
4261                    include scope information.  */
4262                 if (name != error_mark_node 
4263                     && !BASELINK_P (name)
4264                     && parser->scope)
4265                   {
4266                     name = build_nt (SCOPE_REF, parser->scope, name);
4267                     parser->scope = NULL_TREE;
4268                     parser->qualifying_scope = NULL_TREE;
4269                     parser->object_scope = NULL_TREE;
4270                   }
4271                 postfix_expression 
4272                   = finish_class_member_access_expr (postfix_expression, name);
4273               }
4274             /* Otherwise, try the pseudo-destructor-name production.  */
4275             else
4276               {
4277                 tree s;
4278                 tree type;
4279
4280                 /* Parse the pseudo-destructor-name.  */
4281                 cp_parser_pseudo_destructor_name (parser, &s, &type);
4282                 /* Form the call.  */
4283                 postfix_expression 
4284                   = finish_pseudo_destructor_expr (postfix_expression,
4285                                                    s, TREE_TYPE (type));
4286               }
4287
4288             /* We no longer need to look up names in the scope of the
4289                object on the left-hand side of the `.' or `->'
4290                operator.  */
4291             parser->context->object_type = NULL_TREE;
4292             idk = CP_PARSER_ID_KIND_NONE;
4293           }
4294           break;
4295
4296         case CPP_PLUS_PLUS:
4297           /* postfix-expression ++  */
4298           /* Consume the `++' token.  */
4299           cp_lexer_consume_token (parser->lexer);
4300           /* Generate a reprsentation for the complete expression.  */
4301           postfix_expression 
4302             = finish_increment_expr (postfix_expression, 
4303                                      POSTINCREMENT_EXPR);
4304           idk = CP_PARSER_ID_KIND_NONE;
4305           break;
4306
4307         case CPP_MINUS_MINUS:
4308           /* postfix-expression -- */
4309           /* Consume the `--' token.  */
4310           cp_lexer_consume_token (parser->lexer);
4311           /* Generate a reprsentation for the complete expression.  */
4312           postfix_expression 
4313             = finish_increment_expr (postfix_expression, 
4314                                      POSTDECREMENT_EXPR);
4315           idk = CP_PARSER_ID_KIND_NONE;
4316           break;
4317
4318         default:
4319           return postfix_expression;
4320         }
4321     }
4322
4323   /* We should never get here.  */
4324   abort ();
4325   return error_mark_node;
4326 }
4327
4328 /* Parse an expression-list.
4329
4330    expression-list:
4331      assignment-expression
4332      expression-list, assignment-expression
4333
4334    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4335    representation of an assignment-expression.  Note that a TREE_LIST
4336    is returned even if there is only a single expression in the list.  */
4337
4338 static tree
4339 cp_parser_expression_list (parser)
4340      cp_parser *parser;
4341 {
4342   tree expression_list = NULL_TREE;
4343
4344   /* Consume expressions until there are no more.  */
4345   while (true)
4346     {
4347       tree expr;
4348
4349       /* Parse the next assignment-expression.  */
4350       expr = cp_parser_assignment_expression (parser);
4351       /* Add it to the list.  */
4352       expression_list = tree_cons (NULL_TREE, expr, expression_list);
4353
4354       /* If the next token isn't a `,', then we are done.  */
4355       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4356         {
4357           /* All uses of expression-list in the grammar are followed
4358              by a `)'.  Therefore, if the next token is not a `)' an
4359              error will be issued, unless we are parsing tentatively.
4360              Skip ahead to see if there is another `,' before the `)';
4361              if so, we can go there and recover.  */
4362           if (cp_parser_parsing_tentatively (parser)
4363               || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4364               || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4365             break;
4366         }
4367
4368       /* Otherwise, consume the `,' and keep going.  */
4369       cp_lexer_consume_token (parser->lexer);
4370     }
4371
4372   /* We built up the list in reverse order so we must reverse it now.  */
4373   return nreverse (expression_list);
4374 }
4375
4376 /* Parse a pseudo-destructor-name.
4377
4378    pseudo-destructor-name:
4379      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4380      :: [opt] nested-name-specifier template template-id :: ~ type-name
4381      :: [opt] nested-name-specifier [opt] ~ type-name
4382
4383    If either of the first two productions is used, sets *SCOPE to the
4384    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4385    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4386    or ERROR_MARK_NODE if no type-name is present.  */
4387
4388 static void
4389 cp_parser_pseudo_destructor_name (parser, scope, type)
4390      cp_parser *parser;
4391      tree *scope;
4392      tree *type;
4393 {
4394   bool nested_name_specifier_p;
4395
4396   /* Look for the optional `::' operator.  */
4397   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4398   /* Look for the optional nested-name-specifier.  */
4399   nested_name_specifier_p 
4400     = (cp_parser_nested_name_specifier_opt (parser,
4401                                             /*typename_keyword_p=*/false,
4402                                             /*check_dependency_p=*/true,
4403                                             /*type_p=*/false) 
4404        != NULL_TREE);
4405   /* Now, if we saw a nested-name-specifier, we might be doing the
4406      second production.  */
4407   if (nested_name_specifier_p 
4408       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4409     {
4410       /* Consume the `template' keyword.  */
4411       cp_lexer_consume_token (parser->lexer);
4412       /* Parse the template-id.  */
4413       cp_parser_template_id (parser, 
4414                              /*template_keyword_p=*/true,
4415                              /*check_dependency_p=*/false);
4416       /* Look for the `::' token.  */
4417       cp_parser_require (parser, CPP_SCOPE, "`::'");
4418     }
4419   /* If the next token is not a `~', then there might be some
4420      additional qualification. */
4421   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4422     {
4423       /* Look for the type-name.  */
4424       *scope = TREE_TYPE (cp_parser_type_name (parser));
4425       /* Look for the `::' token.  */
4426       cp_parser_require (parser, CPP_SCOPE, "`::'");
4427     }
4428   else
4429     *scope = NULL_TREE;
4430
4431   /* Look for the `~'.  */
4432   cp_parser_require (parser, CPP_COMPL, "`~'");
4433   /* Look for the type-name again.  We are not responsible for
4434      checking that it matches the first type-name.  */
4435   *type = cp_parser_type_name (parser);
4436 }
4437
4438 /* Parse a unary-expression.
4439
4440    unary-expression:
4441      postfix-expression
4442      ++ cast-expression
4443      -- cast-expression
4444      unary-operator cast-expression
4445      sizeof unary-expression
4446      sizeof ( type-id )
4447      new-expression
4448      delete-expression
4449
4450    GNU Extensions:
4451
4452    unary-expression:
4453      __extension__ cast-expression
4454      __alignof__ unary-expression
4455      __alignof__ ( type-id )
4456      __real__ cast-expression
4457      __imag__ cast-expression
4458      && identifier
4459
4460    ADDRESS_P is true iff the unary-expression is appearing as the
4461    operand of the `&' operator.
4462
4463    Returns a representation of the expresion.  */
4464
4465 static tree
4466 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4467 {
4468   cp_token *token;
4469   enum tree_code unary_operator;
4470
4471   /* Peek at the next token.  */
4472   token = cp_lexer_peek_token (parser->lexer);
4473   /* Some keywords give away the kind of expression.  */
4474   if (token->type == CPP_KEYWORD)
4475     {
4476       enum rid keyword = token->keyword;
4477
4478       switch (keyword)
4479         {
4480         case RID_ALIGNOF:
4481           {
4482             /* Consume the `alignof' token.  */
4483             cp_lexer_consume_token (parser->lexer);
4484             /* Parse the operand.  */
4485             return finish_alignof (cp_parser_sizeof_operand 
4486                                    (parser, keyword));
4487           }
4488
4489         case RID_SIZEOF:
4490           {
4491             tree operand;
4492             
4493             /* Consume the `sizeof' token.   */
4494             cp_lexer_consume_token (parser->lexer);
4495             /* Parse the operand.  */
4496             operand = cp_parser_sizeof_operand (parser, keyword);
4497
4498             /* If the type of the operand cannot be determined build a
4499                SIZEOF_EXPR.  */
4500             if (TYPE_P (operand)
4501                 ? cp_parser_dependent_type_p (operand)
4502                 : cp_parser_type_dependent_expression_p (operand))
4503               return build_min (SIZEOF_EXPR, size_type_node, operand);
4504             /* Otherwise, compute the constant value.  */
4505             else
4506               return finish_sizeof (operand);
4507           }
4508
4509         case RID_NEW:
4510           return cp_parser_new_expression (parser);
4511
4512         case RID_DELETE:
4513           return cp_parser_delete_expression (parser);
4514           
4515         case RID_EXTENSION:
4516           {
4517             /* The saved value of the PEDANTIC flag.  */
4518             int saved_pedantic;
4519             tree expr;
4520
4521             /* Save away the PEDANTIC flag.  */
4522             cp_parser_extension_opt (parser, &saved_pedantic);
4523             /* Parse the cast-expression.  */
4524             expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4525             /* Restore the PEDANTIC flag.  */
4526             pedantic = saved_pedantic;
4527
4528             return expr;
4529           }
4530
4531         case RID_REALPART:
4532         case RID_IMAGPART:
4533           {
4534             tree expression;
4535
4536             /* Consume the `__real__' or `__imag__' token.  */
4537             cp_lexer_consume_token (parser->lexer);
4538             /* Parse the cast-expression.  */
4539             expression = cp_parser_cast_expression (parser,
4540                                                     /*address_p=*/false);
4541             /* Create the complete representation.  */
4542             return build_x_unary_op ((keyword == RID_REALPART
4543                                       ? REALPART_EXPR : IMAGPART_EXPR),
4544                                      expression);
4545           }
4546           break;
4547
4548         default:
4549           break;
4550         }
4551     }
4552
4553   /* Look for the `:: new' and `:: delete', which also signal the
4554      beginning of a new-expression, or delete-expression,
4555      respectively.  If the next token is `::', then it might be one of
4556      these.  */
4557   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4558     {
4559       enum rid keyword;
4560
4561       /* See if the token after the `::' is one of the keywords in
4562          which we're interested.  */
4563       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4564       /* If it's `new', we have a new-expression.  */
4565       if (keyword == RID_NEW)
4566         return cp_parser_new_expression (parser);
4567       /* Similarly, for `delete'.  */
4568       else if (keyword == RID_DELETE)
4569         return cp_parser_delete_expression (parser);
4570     }
4571
4572   /* Look for a unary operator.  */
4573   unary_operator = cp_parser_unary_operator (token);
4574   /* The `++' and `--' operators can be handled similarly, even though
4575      they are not technically unary-operators in the grammar.  */
4576   if (unary_operator == ERROR_MARK)
4577     {
4578       if (token->type == CPP_PLUS_PLUS)
4579         unary_operator = PREINCREMENT_EXPR;
4580       else if (token->type == CPP_MINUS_MINUS)
4581         unary_operator = PREDECREMENT_EXPR;
4582       /* Handle the GNU address-of-label extension.  */
4583       else if (cp_parser_allow_gnu_extensions_p (parser)
4584                && token->type == CPP_AND_AND)
4585         {
4586           tree identifier;
4587
4588           /* Consume the '&&' token.  */
4589           cp_lexer_consume_token (parser->lexer);
4590           /* Look for the identifier.  */
4591           identifier = cp_parser_identifier (parser);
4592           /* Create an expression representing the address.  */
4593           return finish_label_address_expr (identifier);
4594         }
4595     }
4596   if (unary_operator != ERROR_MARK)
4597     {
4598       tree cast_expression;
4599
4600       /* Consume the operator token.  */
4601       token = cp_lexer_consume_token (parser->lexer);
4602       /* Parse the cast-expression.  */
4603       cast_expression 
4604         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4605       /* Now, build an appropriate representation.  */
4606       switch (unary_operator)
4607         {
4608         case INDIRECT_REF:
4609           return build_x_indirect_ref (cast_expression, "unary *");
4610           
4611         case ADDR_EXPR:
4612           return build_x_unary_op (ADDR_EXPR, cast_expression);
4613           
4614         case CONVERT_EXPR:
4615         case NEGATE_EXPR:
4616         case TRUTH_NOT_EXPR:
4617         case PREINCREMENT_EXPR:
4618         case PREDECREMENT_EXPR:
4619           return finish_unary_op_expr (unary_operator, cast_expression);
4620
4621         case BIT_NOT_EXPR:
4622           return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4623
4624         default:
4625           abort ();
4626           return error_mark_node;
4627         }
4628     }
4629
4630   return cp_parser_postfix_expression (parser, address_p);
4631 }
4632
4633 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4634    unary-operator, the corresponding tree code is returned.  */
4635
4636 static enum tree_code
4637 cp_parser_unary_operator (token)
4638      cp_token *token;
4639 {
4640   switch (token->type)
4641     {
4642     case CPP_MULT:
4643       return INDIRECT_REF;
4644
4645     case CPP_AND:
4646       return ADDR_EXPR;
4647
4648     case CPP_PLUS:
4649       return CONVERT_EXPR;
4650
4651     case CPP_MINUS:
4652       return NEGATE_EXPR;
4653
4654     case CPP_NOT:
4655       return TRUTH_NOT_EXPR;
4656       
4657     case CPP_COMPL:
4658       return BIT_NOT_EXPR;
4659
4660     default:
4661       return ERROR_MARK;
4662     }
4663 }
4664
4665 /* Parse a new-expression.
4666
4667      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4668      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4669
4670    Returns a representation of the expression.  */
4671
4672 static tree
4673 cp_parser_new_expression (parser)
4674      cp_parser *parser;
4675 {
4676   bool global_scope_p;
4677   tree placement;
4678   tree type;
4679   tree initializer;
4680
4681   /* Look for the optional `::' operator.  */
4682   global_scope_p 
4683     = (cp_parser_global_scope_opt (parser,
4684                                    /*current_scope_valid_p=*/false)
4685        != NULL_TREE);
4686   /* Look for the `new' operator.  */
4687   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4688   /* There's no easy way to tell a new-placement from the
4689      `( type-id )' construct.  */
4690   cp_parser_parse_tentatively (parser);
4691   /* Look for a new-placement.  */
4692   placement = cp_parser_new_placement (parser);
4693   /* If that didn't work out, there's no new-placement.  */
4694   if (!cp_parser_parse_definitely (parser))
4695     placement = NULL_TREE;
4696
4697   /* If the next token is a `(', then we have a parenthesized
4698      type-id.  */
4699   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4700     {
4701       /* Consume the `('.  */
4702       cp_lexer_consume_token (parser->lexer);
4703       /* Parse the type-id.  */
4704       type = cp_parser_type_id (parser);
4705       /* Look for the closing `)'.  */
4706       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4707     }
4708   /* Otherwise, there must be a new-type-id.  */
4709   else
4710     type = cp_parser_new_type_id (parser);
4711
4712   /* If the next token is a `(', then we have a new-initializer.  */
4713   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4714     initializer = cp_parser_new_initializer (parser);
4715   else
4716     initializer = NULL_TREE;
4717
4718   /* Create a representation of the new-expression.  */
4719   return build_new (placement, type, initializer, global_scope_p);
4720 }
4721
4722 /* Parse a new-placement.
4723
4724    new-placement:
4725      ( expression-list )
4726
4727    Returns the same representation as for an expression-list.  */
4728
4729 static tree
4730 cp_parser_new_placement (parser)
4731      cp_parser *parser;
4732 {
4733   tree expression_list;
4734
4735   /* Look for the opening `('.  */
4736   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4737     return error_mark_node;
4738   /* Parse the expression-list.  */
4739   expression_list = cp_parser_expression_list (parser);
4740   /* Look for the closing `)'.  */
4741   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4742
4743   return expression_list;
4744 }
4745
4746 /* Parse a new-type-id.
4747
4748    new-type-id:
4749      type-specifier-seq new-declarator [opt]
4750
4751    Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4752    and whose TREE_VALUE is the new-declarator.  */
4753
4754 static tree
4755 cp_parser_new_type_id (parser)
4756      cp_parser *parser;
4757 {
4758   tree type_specifier_seq;
4759   tree declarator;
4760   const char *saved_message;
4761
4762   /* The type-specifier sequence must not contain type definitions.
4763      (It cannot contain declarations of new types either, but if they
4764      are not definitions we will catch that because they are not
4765      complete.)  */
4766   saved_message = parser->type_definition_forbidden_message;
4767   parser->type_definition_forbidden_message
4768     = "types may not be defined in a new-type-id";
4769   /* Parse the type-specifier-seq.  */
4770   type_specifier_seq = cp_parser_type_specifier_seq (parser);
4771   /* Restore the old message.  */
4772   parser->type_definition_forbidden_message = saved_message;
4773   /* Parse the new-declarator.  */
4774   declarator = cp_parser_new_declarator_opt (parser);
4775
4776   return build_tree_list (type_specifier_seq, declarator);
4777 }
4778
4779 /* Parse an (optional) new-declarator.
4780
4781    new-declarator:
4782      ptr-operator new-declarator [opt]
4783      direct-new-declarator
4784
4785    Returns a representation of the declarator.  See
4786    cp_parser_declarator for the representations used.  */
4787
4788 static tree
4789 cp_parser_new_declarator_opt (parser)
4790      cp_parser *parser;
4791 {
4792   enum tree_code code;
4793   tree type;
4794   tree cv_qualifier_seq;
4795
4796   /* We don't know if there's a ptr-operator next, or not.  */
4797   cp_parser_parse_tentatively (parser);
4798   /* Look for a ptr-operator.  */
4799   code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4800   /* If that worked, look for more new-declarators.  */
4801   if (cp_parser_parse_definitely (parser))
4802     {
4803       tree declarator;
4804
4805       /* Parse another optional declarator.  */
4806       declarator = cp_parser_new_declarator_opt (parser);
4807
4808       /* Create the representation of the declarator.  */
4809       if (code == INDIRECT_REF)
4810         declarator = make_pointer_declarator (cv_qualifier_seq,
4811                                               declarator);
4812       else
4813         declarator = make_reference_declarator (cv_qualifier_seq,
4814                                                 declarator);
4815
4816      /* Handle the pointer-to-member case.  */
4817      if (type)
4818        declarator = build_nt (SCOPE_REF, type, declarator);
4819
4820       return declarator;
4821     }
4822
4823   /* If the next token is a `[', there is a direct-new-declarator.  */
4824   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4825     return cp_parser_direct_new_declarator (parser);
4826
4827   return NULL_TREE;
4828 }
4829
4830 /* Parse a direct-new-declarator.
4831
4832    direct-new-declarator:
4833      [ expression ]
4834      direct-new-declarator [constant-expression]  
4835
4836    Returns an ARRAY_REF, following the same conventions as are
4837    documented for cp_parser_direct_declarator.  */
4838
4839 static tree
4840 cp_parser_direct_new_declarator (parser)
4841      cp_parser *parser;
4842 {
4843   tree declarator = NULL_TREE;
4844
4845   while (true)
4846     {
4847       tree expression;
4848
4849       /* Look for the opening `['.  */
4850       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4851       /* The first expression is not required to be constant.  */
4852       if (!declarator)
4853         {
4854           expression = cp_parser_expression (parser);
4855           /* The standard requires that the expression have integral
4856              type.  DR 74 adds enumeration types.  We believe that the
4857              real intent is that these expressions be handled like the
4858              expression in a `switch' condition, which also allows
4859              classes with a single conversion to integral or
4860              enumeration type.  */
4861           if (!processing_template_decl)
4862             {
4863               expression 
4864                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4865                                               expression,
4866                                               /*complain=*/1);
4867               if (!expression)
4868                 {
4869                   error ("expression in new-declarator must have integral or enumeration type");
4870                   expression = error_mark_node;
4871                 }
4872             }
4873         }
4874       /* But all the other expressions must be.  */
4875       else
4876         expression = cp_parser_constant_expression (parser);
4877       /* Look for the closing `]'.  */
4878       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4879
4880       /* Add this bound to the declarator.  */
4881       declarator = build_nt (ARRAY_REF, declarator, expression);
4882
4883       /* If the next token is not a `[', then there are no more
4884          bounds.  */
4885       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4886         break;
4887     }
4888
4889   return declarator;
4890 }
4891
4892 /* Parse a new-initializer.
4893
4894    new-initializer:
4895      ( expression-list [opt] )
4896
4897    Returns a reprsentation of the expression-list.  If there is no
4898    expression-list, VOID_ZERO_NODE is returned.  */
4899
4900 static tree
4901 cp_parser_new_initializer (parser)
4902      cp_parser *parser;
4903 {
4904   tree expression_list;
4905
4906   /* Look for the opening parenthesis.  */
4907   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4908   /* If the next token is not a `)', then there is an
4909      expression-list.  */
4910   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4911     expression_list = cp_parser_expression_list (parser);
4912   else
4913     expression_list = void_zero_node;
4914   /* Look for the closing parenthesis.  */
4915   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4916
4917   return expression_list;
4918 }
4919
4920 /* Parse a delete-expression.
4921
4922    delete-expression:
4923      :: [opt] delete cast-expression
4924      :: [opt] delete [ ] cast-expression
4925
4926    Returns a representation of the expression.  */
4927
4928 static tree
4929 cp_parser_delete_expression (parser)
4930      cp_parser *parser;
4931 {
4932   bool global_scope_p;
4933   bool array_p;
4934   tree expression;
4935
4936   /* Look for the optional `::' operator.  */
4937   global_scope_p
4938     = (cp_parser_global_scope_opt (parser,
4939                                    /*current_scope_valid_p=*/false)
4940        != NULL_TREE);
4941   /* Look for the `delete' keyword.  */
4942   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4943   /* See if the array syntax is in use.  */
4944   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4945     {
4946       /* Consume the `[' token.  */
4947       cp_lexer_consume_token (parser->lexer);
4948       /* Look for the `]' token.  */
4949       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4950       /* Remember that this is the `[]' construct.  */
4951       array_p = true;
4952     }
4953   else
4954     array_p = false;
4955
4956   /* Parse the cast-expression.  */
4957   expression = cp_parser_cast_expression (parser, /*address_p=*/false);
4958
4959   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4960 }
4961
4962 /* Parse a cast-expression.
4963
4964    cast-expression:
4965      unary-expression
4966      ( type-id ) cast-expression
4967
4968    Returns a representation of the expression.  */
4969
4970 static tree
4971 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4972 {
4973   /* If it's a `(', then we might be looking at a cast.  */
4974   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4975     {
4976       tree type = NULL_TREE;
4977       tree expr = NULL_TREE;
4978       bool compound_literal_p;
4979       const char *saved_message;
4980
4981       /* There's no way to know yet whether or not this is a cast.
4982          For example, `(int (3))' is a unary-expression, while `(int)
4983          3' is a cast.  So, we resort to parsing tentatively.  */
4984       cp_parser_parse_tentatively (parser);
4985       /* Types may not be defined in a cast.  */
4986       saved_message = parser->type_definition_forbidden_message;
4987       parser->type_definition_forbidden_message
4988         = "types may not be defined in casts";
4989       /* Consume the `('.  */
4990       cp_lexer_consume_token (parser->lexer);
4991       /* A very tricky bit is that `(struct S) { 3 }' is a
4992          compound-literal (which we permit in C++ as an extension).
4993          But, that construct is not a cast-expression -- it is a
4994          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
4995          is legal; if the compound-literal were a cast-expression,
4996          you'd need an extra set of parentheses.)  But, if we parse
4997          the type-id, and it happens to be a class-specifier, then we
4998          will commit to the parse at that point, because we cannot
4999          undo the action that is done when creating a new class.  So,
5000          then we cannot back up and do a postfix-expression.  
5001
5002          Therefore, we scan ahead to the closing `)', and check to see
5003          if the token after the `)' is a `{'.  If so, we are not
5004          looking at a cast-expression.  
5005
5006          Save tokens so that we can put them back.  */
5007       cp_lexer_save_tokens (parser->lexer);
5008       /* Skip tokens until the next token is a closing parenthesis.
5009          If we find the closing `)', and the next token is a `{', then
5010          we are looking at a compound-literal.  */
5011       compound_literal_p 
5012         = (cp_parser_skip_to_closing_parenthesis (parser)
5013            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5014       /* Roll back the tokens we skipped.  */
5015       cp_lexer_rollback_tokens (parser->lexer);
5016       /* If we were looking at a compound-literal, simulate an error
5017          so that the call to cp_parser_parse_definitely below will
5018          fail.  */
5019       if (compound_literal_p)
5020         cp_parser_simulate_error (parser);
5021       else
5022         {
5023           /* Look for the type-id.  */
5024           type = cp_parser_type_id (parser);
5025           /* Look for the closing `)'.  */
5026           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5027         }
5028
5029       /* Restore the saved message.  */
5030       parser->type_definition_forbidden_message = saved_message;
5031
5032       /* If all went well, this is a cast.  */
5033       if (cp_parser_parse_definitely (parser))
5034         {
5035           /* Parse the dependent expression.  */
5036           expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5037           /* Warn about old-style casts, if so requested.  */
5038           if (warn_old_style_cast 
5039               && !in_system_header 
5040               && !VOID_TYPE_P (type) 
5041               && current_lang_name != lang_name_c)
5042             warning ("use of old-style cast");
5043           /* Perform the cast.  */
5044           expr = build_c_cast (type, expr);
5045         }
5046
5047       if (expr)
5048         return expr;
5049     }
5050
5051   /* If we get here, then it's not a cast, so it must be a
5052      unary-expression.  */
5053   return cp_parser_unary_expression (parser, address_p);
5054 }
5055
5056 /* Parse a pm-expression.
5057
5058    pm-expression:
5059      cast-expression
5060      pm-expression .* cast-expression
5061      pm-expression ->* cast-expression
5062
5063      Returns a representation of the expression.  */
5064
5065 static tree
5066 cp_parser_pm_expression (parser)
5067      cp_parser *parser;
5068 {
5069   tree cast_expr;
5070   tree pm_expr;
5071
5072   /* Parse the cast-expresion.  */
5073   cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5074   pm_expr = cast_expr;
5075   /* Now look for pointer-to-member operators.  */
5076   while (true)
5077     {
5078       cp_token *token;
5079       enum cpp_ttype token_type;
5080
5081       /* Peek at the next token.  */
5082       token = cp_lexer_peek_token (parser->lexer);
5083       token_type = token->type;
5084       /* If it's not `.*' or `->*' there's no pointer-to-member
5085          operation.  */
5086       if (token_type != CPP_DOT_STAR 
5087           && token_type != CPP_DEREF_STAR)
5088         break;
5089
5090       /* Consume the token.  */
5091       cp_lexer_consume_token (parser->lexer);
5092
5093       /* Parse another cast-expression.  */
5094       cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5095
5096       /* Build the representation of the pointer-to-member 
5097          operation.  */
5098       if (token_type == CPP_DEREF_STAR)
5099         pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5100       else
5101         pm_expr = build_m_component_ref (pm_expr, cast_expr);
5102     }
5103
5104   return pm_expr;
5105 }
5106
5107 /* Parse a multiplicative-expression.
5108
5109    mulitplicative-expression:
5110      pm-expression
5111      multiplicative-expression * pm-expression
5112      multiplicative-expression / pm-expression
5113      multiplicative-expression % pm-expression
5114
5115    Returns a representation of the expression.  */
5116
5117 static tree
5118 cp_parser_multiplicative_expression (parser)
5119      cp_parser *parser;
5120 {
5121   static cp_parser_token_tree_map map = {
5122     { CPP_MULT, MULT_EXPR },
5123     { CPP_DIV, TRUNC_DIV_EXPR },
5124     { CPP_MOD, TRUNC_MOD_EXPR },
5125     { CPP_EOF, ERROR_MARK }
5126   };
5127
5128   return cp_parser_binary_expression (parser,
5129                                       map,
5130                                       cp_parser_pm_expression);
5131 }
5132
5133 /* Parse an additive-expression.
5134
5135    additive-expression:
5136      multiplicative-expression
5137      additive-expression + multiplicative-expression
5138      additive-expression - multiplicative-expression
5139
5140    Returns a representation of the expression.  */
5141
5142 static tree
5143 cp_parser_additive_expression (parser)
5144      cp_parser *parser;
5145 {
5146   static cp_parser_token_tree_map map = {
5147     { CPP_PLUS, PLUS_EXPR },
5148     { CPP_MINUS, MINUS_EXPR },
5149     { CPP_EOF, ERROR_MARK }
5150   };
5151
5152   return cp_parser_binary_expression (parser,
5153                                       map,
5154                                       cp_parser_multiplicative_expression);
5155 }
5156
5157 /* Parse a shift-expression.
5158
5159    shift-expression:
5160      additive-expression
5161      shift-expression << additive-expression
5162      shift-expression >> additive-expression
5163
5164    Returns a representation of the expression.  */
5165
5166 static tree
5167 cp_parser_shift_expression (parser)
5168      cp_parser *parser;
5169 {
5170   static cp_parser_token_tree_map map = {
5171     { CPP_LSHIFT, LSHIFT_EXPR },
5172     { CPP_RSHIFT, RSHIFT_EXPR },
5173     { CPP_EOF, ERROR_MARK }
5174   };
5175
5176   return cp_parser_binary_expression (parser,
5177                                       map,
5178                                       cp_parser_additive_expression);
5179 }
5180
5181 /* Parse a relational-expression.
5182
5183    relational-expression:
5184      shift-expression
5185      relational-expression < shift-expression
5186      relational-expression > shift-expression
5187      relational-expression <= shift-expression
5188      relational-expression >= shift-expression
5189
5190    GNU Extension:
5191
5192    relational-expression:
5193      relational-expression <? shift-expression
5194      relational-expression >? shift-expression
5195
5196    Returns a representation of the expression.  */
5197
5198 static tree
5199 cp_parser_relational_expression (parser)
5200      cp_parser *parser;
5201 {
5202   static cp_parser_token_tree_map map = {
5203     { CPP_LESS, LT_EXPR },
5204     { CPP_GREATER, GT_EXPR },
5205     { CPP_LESS_EQ, LE_EXPR },
5206     { CPP_GREATER_EQ, GE_EXPR },
5207     { CPP_MIN, MIN_EXPR },
5208     { CPP_MAX, MAX_EXPR },
5209     { CPP_EOF, ERROR_MARK }
5210   };
5211
5212   return cp_parser_binary_expression (parser,
5213                                       map,
5214                                       cp_parser_shift_expression);
5215 }
5216
5217 /* Parse an equality-expression.
5218
5219    equality-expression:
5220      relational-expression
5221      equality-expression == relational-expression
5222      equality-expression != relational-expression
5223
5224    Returns a representation of the expression.  */
5225
5226 static tree
5227 cp_parser_equality_expression (parser)
5228      cp_parser *parser;
5229 {
5230   static cp_parser_token_tree_map map = {
5231     { CPP_EQ_EQ, EQ_EXPR },
5232     { CPP_NOT_EQ, NE_EXPR },
5233     { CPP_EOF, ERROR_MARK }
5234   };
5235
5236   return cp_parser_binary_expression (parser,
5237                                       map,
5238                                       cp_parser_relational_expression);
5239 }
5240
5241 /* Parse an and-expression.
5242
5243    and-expression:
5244      equality-expression
5245      and-expression & equality-expression
5246
5247    Returns a representation of the expression.  */
5248
5249 static tree
5250 cp_parser_and_expression (parser)
5251      cp_parser *parser;
5252 {
5253   static cp_parser_token_tree_map map = {
5254     { CPP_AND, BIT_AND_EXPR },
5255     { CPP_EOF, ERROR_MARK }
5256   };
5257
5258   return cp_parser_binary_expression (parser,
5259                                       map,
5260                                       cp_parser_equality_expression);
5261 }
5262
5263 /* Parse an exclusive-or-expression.
5264
5265    exclusive-or-expression:
5266      and-expression
5267      exclusive-or-expression ^ and-expression
5268
5269    Returns a representation of the expression.  */
5270
5271 static tree
5272 cp_parser_exclusive_or_expression (parser)
5273      cp_parser *parser;
5274 {
5275   static cp_parser_token_tree_map map = {
5276     { CPP_XOR, BIT_XOR_EXPR },
5277     { CPP_EOF, ERROR_MARK }
5278   };
5279
5280   return cp_parser_binary_expression (parser,
5281                                       map,
5282                                       cp_parser_and_expression);
5283 }
5284
5285
5286 /* Parse an inclusive-or-expression.
5287
5288    inclusive-or-expression:
5289      exclusive-or-expression
5290      inclusive-or-expression | exclusive-or-expression
5291
5292    Returns a representation of the expression.  */
5293
5294 static tree
5295 cp_parser_inclusive_or_expression (parser)
5296      cp_parser *parser;
5297 {
5298   static cp_parser_token_tree_map map = {
5299     { CPP_OR, BIT_IOR_EXPR },
5300     { CPP_EOF, ERROR_MARK }
5301   };
5302
5303   return cp_parser_binary_expression (parser,
5304                                       map,
5305                                       cp_parser_exclusive_or_expression);
5306 }
5307
5308 /* Parse a logical-and-expression.
5309
5310    logical-and-expression:
5311      inclusive-or-expression
5312      logical-and-expression && inclusive-or-expression
5313
5314    Returns a representation of the expression.  */
5315
5316 static tree
5317 cp_parser_logical_and_expression (parser)
5318      cp_parser *parser;
5319 {
5320   static cp_parser_token_tree_map map = {
5321     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5322     { CPP_EOF, ERROR_MARK }
5323   };
5324
5325   return cp_parser_binary_expression (parser,
5326                                       map,
5327                                       cp_parser_inclusive_or_expression);
5328 }
5329
5330 /* Parse a logical-or-expression.
5331
5332    logical-or-expression:
5333      logical-and-expresion
5334      logical-or-expression || logical-and-expression
5335
5336    Returns a representation of the expression.  */
5337
5338 static tree
5339 cp_parser_logical_or_expression (parser)
5340      cp_parser *parser;
5341 {
5342   static cp_parser_token_tree_map map = {
5343     { CPP_OR_OR, TRUTH_ORIF_EXPR },
5344     { CPP_EOF, ERROR_MARK }
5345   };
5346
5347   return cp_parser_binary_expression (parser,
5348                                       map,
5349                                       cp_parser_logical_and_expression);
5350 }
5351
5352 /* Parse a conditional-expression.
5353
5354    conditional-expression:
5355      logical-or-expression
5356      logical-or-expression ? expression : assignment-expression
5357      
5358    GNU Extensions:
5359    
5360    conditional-expression:
5361      logical-or-expression ?  : assignment-expression
5362
5363    Returns a representation of the expression.  */
5364
5365 static tree
5366 cp_parser_conditional_expression (parser)
5367      cp_parser *parser;
5368 {
5369   tree logical_or_expr;
5370
5371   /* Parse the logical-or-expression.  */
5372   logical_or_expr = cp_parser_logical_or_expression (parser);
5373   /* If the next token is a `?', then we have a real conditional
5374      expression.  */
5375   if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5376     return cp_parser_question_colon_clause (parser, logical_or_expr);
5377   /* Otherwise, the value is simply the logical-or-expression.  */
5378   else
5379     return logical_or_expr;
5380 }
5381
5382 /* Parse the `? expression : assignment-expression' part of a
5383    conditional-expression.  The LOGICAL_OR_EXPR is the
5384    logical-or-expression that started the conditional-expression.
5385    Returns a representation of the entire conditional-expression.
5386
5387    This routine exists only so that it can be shared between
5388    cp_parser_conditional_expression and
5389    cp_parser_assignment_expression.
5390
5391      ? expression : assignment-expression
5392    
5393    GNU Extensions:
5394    
5395      ? : assignment-expression */
5396
5397 static tree
5398 cp_parser_question_colon_clause (parser, logical_or_expr)
5399      cp_parser *parser;
5400      tree logical_or_expr;
5401 {
5402   tree expr;
5403   tree assignment_expr;
5404
5405   /* Consume the `?' token.  */
5406   cp_lexer_consume_token (parser->lexer);
5407   if (cp_parser_allow_gnu_extensions_p (parser)
5408       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5409     /* Implicit true clause.  */
5410     expr = NULL_TREE;
5411   else
5412     /* Parse the expression.  */
5413     expr = cp_parser_expression (parser);
5414   
5415   /* The next token should be a `:'.  */
5416   cp_parser_require (parser, CPP_COLON, "`:'");
5417   /* Parse the assignment-expression.  */
5418   assignment_expr = cp_parser_assignment_expression (parser);
5419
5420   /* Build the conditional-expression.  */
5421   return build_x_conditional_expr (logical_or_expr,
5422                                    expr,
5423                                    assignment_expr);
5424 }
5425
5426 /* Parse an assignment-expression.
5427
5428    assignment-expression:
5429      conditional-expression
5430      logical-or-expression assignment-operator assignment_expression
5431      throw-expression
5432
5433    Returns a representation for the expression.  */
5434
5435 static tree
5436 cp_parser_assignment_expression (parser)
5437      cp_parser *parser;
5438 {
5439   tree expr;
5440
5441   /* If the next token is the `throw' keyword, then we're looking at
5442      a throw-expression.  */
5443   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5444     expr = cp_parser_throw_expression (parser);
5445   /* Otherwise, it must be that we are looking at a
5446      logical-or-expression.  */
5447   else
5448     {
5449       /* Parse the logical-or-expression.  */
5450       expr = cp_parser_logical_or_expression (parser);
5451       /* If the next token is a `?' then we're actually looking at a
5452          conditional-expression.  */
5453       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5454         return cp_parser_question_colon_clause (parser, expr);
5455       else 
5456         {
5457           enum tree_code assignment_operator;
5458
5459           /* If it's an assignment-operator, we're using the second
5460              production.  */
5461           assignment_operator 
5462             = cp_parser_assignment_operator_opt (parser);
5463           if (assignment_operator != ERROR_MARK)
5464             {
5465               tree rhs;
5466
5467               /* Parse the right-hand side of the assignment.  */
5468               rhs = cp_parser_assignment_expression (parser);
5469               /* Build the asignment expression.  */
5470               expr = build_x_modify_expr (expr, 
5471                                           assignment_operator, 
5472                                           rhs);
5473             }
5474         }
5475     }
5476
5477   return expr;
5478 }
5479
5480 /* Parse an (optional) assignment-operator.
5481
5482    assignment-operator: one of 
5483      = *= /= %= += -= >>= <<= &= ^= |=  
5484
5485    GNU Extension:
5486    
5487    assignment-operator: one of
5488      <?= >?=
5489
5490    If the next token is an assignment operator, the corresponding tree
5491    code is returned, and the token is consumed.  For example, for
5492    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5493    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5494    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5495    operator, ERROR_MARK is returned.  */
5496
5497 static enum tree_code
5498 cp_parser_assignment_operator_opt (parser)
5499      cp_parser *parser;
5500 {
5501   enum tree_code op;
5502   cp_token *token;
5503
5504   /* Peek at the next toen.  */
5505   token = cp_lexer_peek_token (parser->lexer);
5506
5507   switch (token->type)
5508     {
5509     case CPP_EQ:
5510       op = NOP_EXPR;
5511       break;
5512
5513     case CPP_MULT_EQ:
5514       op = MULT_EXPR;
5515       break;
5516
5517     case CPP_DIV_EQ:
5518       op = TRUNC_DIV_EXPR;
5519       break;
5520
5521     case CPP_MOD_EQ:
5522       op = TRUNC_MOD_EXPR;
5523       break;
5524
5525     case CPP_PLUS_EQ:
5526       op = PLUS_EXPR;
5527       break;
5528
5529     case CPP_MINUS_EQ:
5530       op = MINUS_EXPR;
5531       break;
5532
5533     case CPP_RSHIFT_EQ:
5534       op = RSHIFT_EXPR;
5535       break;
5536
5537     case CPP_LSHIFT_EQ:
5538       op = LSHIFT_EXPR;
5539       break;
5540
5541     case CPP_AND_EQ:
5542       op = BIT_AND_EXPR;
5543       break;
5544
5545     case CPP_XOR_EQ:
5546       op = BIT_XOR_EXPR;
5547       break;
5548
5549     case CPP_OR_EQ:
5550       op = BIT_IOR_EXPR;
5551       break;
5552
5553     case CPP_MIN_EQ:
5554       op = MIN_EXPR;
5555       break;
5556
5557     case CPP_MAX_EQ:
5558       op = MAX_EXPR;
5559       break;
5560
5561     default: 
5562       /* Nothing else is an assignment operator.  */
5563       op = ERROR_MARK;
5564     }
5565
5566   /* If it was an assignment operator, consume it.  */
5567   if (op != ERROR_MARK)
5568     cp_lexer_consume_token (parser->lexer);
5569
5570   return op;
5571 }
5572
5573 /* Parse an expression.
5574
5575    expression:
5576      assignment-expression
5577      expression , assignment-expression
5578
5579    Returns a representation of the expression.  */
5580
5581 static tree
5582 cp_parser_expression (parser)
5583      cp_parser *parser;
5584 {
5585   tree expression = NULL_TREE;
5586   bool saw_comma_p = false;
5587
5588   while (true)
5589     {
5590       tree assignment_expression;
5591
5592       /* Parse the next assignment-expression.  */
5593       assignment_expression 
5594         = cp_parser_assignment_expression (parser);
5595       /* If this is the first assignment-expression, we can just
5596          save it away.  */
5597       if (!expression)
5598         expression = assignment_expression;
5599       /* Otherwise, chain the expressions together.  It is unclear why
5600          we do not simply build COMPOUND_EXPRs as we go.  */
5601       else
5602         expression = tree_cons (NULL_TREE, 
5603                                 assignment_expression,
5604                                 expression);
5605       /* If the next token is not a comma, then we are done with the
5606          expression.  */
5607       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5608         break;
5609       /* Consume the `,'.  */
5610       cp_lexer_consume_token (parser->lexer);
5611       /* The first time we see a `,', we must take special action
5612          because the representation used for a single expression is
5613          different from that used for a list containing the single
5614          expression.  */
5615       if (!saw_comma_p)
5616         {
5617           /* Remember that this expression has a `,' in it.  */
5618           saw_comma_p = true;
5619           /* Turn the EXPRESSION into a TREE_LIST so that we can link
5620              additional expressions to it.  */
5621           expression = build_tree_list (NULL_TREE, expression);
5622         }
5623     }
5624
5625   /* Build a COMPOUND_EXPR to represent the entire expression, if
5626      necessary.  We built up the list in reverse order, so we must
5627      straighten it out here.  */
5628   if (saw_comma_p)
5629     expression = build_x_compound_expr (nreverse (expression));
5630
5631   return expression;
5632 }
5633
5634 /* Parse a constant-expression. 
5635
5636    constant-expression:
5637      conditional-expression  */
5638
5639 static tree
5640 cp_parser_constant_expression (parser)
5641      cp_parser *parser;
5642 {
5643   bool saved_constant_expression_p;
5644   tree expression;
5645
5646   /* It might seem that we could simply parse the
5647      conditional-expression, and then check to see if it were
5648      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5649      one that the compiler can figure out is constant, possibly after
5650      doing some simplifications or optimizations.  The standard has a
5651      precise definition of constant-expression, and we must honor
5652      that, even though it is somewhat more restrictive.
5653
5654      For example:
5655
5656        int i[(2, 3)];
5657
5658      is not a legal declaration, because `(2, 3)' is not a
5659      constant-expression.  The `,' operator is forbidden in a
5660      constant-expression.  However, GCC's constant-folding machinery
5661      will fold this operation to an INTEGER_CST for `3'.  */
5662
5663   /* Save the old setting of CONSTANT_EXPRESSION_P.  */
5664   saved_constant_expression_p = parser->constant_expression_p;
5665   /* We are now parsing a constant-expression.  */
5666   parser->constant_expression_p = true;
5667   /* Parse the conditional-expression.  */
5668   expression = cp_parser_conditional_expression (parser);
5669   /* Restore the old setting of CONSTANT_EXPRESSION_P.  */
5670   parser->constant_expression_p = saved_constant_expression_p;
5671
5672   return expression;
5673 }
5674
5675 /* Statements [gram.stmt.stmt]  */
5676
5677 /* Parse a statement.  
5678
5679    statement:
5680      labeled-statement
5681      expression-statement
5682      compound-statement
5683      selection-statement
5684      iteration-statement
5685      jump-statement
5686      declaration-statement
5687      try-block  */
5688
5689 static void
5690 cp_parser_statement (parser)
5691      cp_parser *parser;
5692 {
5693   tree statement;
5694   cp_token *token;
5695   int statement_line_number;
5696
5697   /* There is no statement yet.  */
5698   statement = NULL_TREE;
5699   /* Peek at the next token.  */
5700   token = cp_lexer_peek_token (parser->lexer);
5701   /* Remember the line number of the first token in the statement.  */
5702   statement_line_number = token->line_number;
5703   /* If this is a keyword, then that will often determine what kind of
5704      statement we have.  */
5705   if (token->type == CPP_KEYWORD)
5706     {
5707       enum rid keyword = token->keyword;
5708
5709       switch (keyword)
5710         {
5711         case RID_CASE:
5712         case RID_DEFAULT:
5713           statement = cp_parser_labeled_statement (parser);
5714           break;
5715
5716         case RID_IF:
5717         case RID_SWITCH:
5718           statement = cp_parser_selection_statement (parser);
5719           break;
5720
5721         case RID_WHILE:
5722         case RID_DO:
5723         case RID_FOR:
5724           statement = cp_parser_iteration_statement (parser);
5725           break;
5726
5727         case RID_BREAK:
5728         case RID_CONTINUE:
5729         case RID_RETURN:
5730         case RID_GOTO:
5731           statement = cp_parser_jump_statement (parser);
5732           break;
5733
5734         case RID_TRY:
5735           statement = cp_parser_try_block (parser);
5736           break;
5737
5738         default:
5739           /* It might be a keyword like `int' that can start a
5740              declaration-statement.  */
5741           break;
5742         }
5743     }
5744   else if (token->type == CPP_NAME)
5745     {
5746       /* If the next token is a `:', then we are looking at a
5747          labeled-statement.  */
5748       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5749       if (token->type == CPP_COLON)
5750         statement = cp_parser_labeled_statement (parser);
5751     }
5752   /* Anything that starts with a `{' must be a compound-statement.  */
5753   else if (token->type == CPP_OPEN_BRACE)
5754     statement = cp_parser_compound_statement (parser);
5755
5756   /* Everything else must be a declaration-statement or an
5757      expression-statement.  Try for the declaration-statement 
5758      first, unless we are looking at a `;', in which case we know that
5759      we have an expression-statement.  */
5760   if (!statement)
5761     {
5762       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5763         {
5764           cp_parser_parse_tentatively (parser);
5765           /* Try to parse the declaration-statement.  */
5766           cp_parser_declaration_statement (parser);
5767           /* If that worked, we're done.  */
5768           if (cp_parser_parse_definitely (parser))
5769             return;
5770         }
5771       /* Look for an expression-statement instead.  */
5772       statement = cp_parser_expression_statement (parser);
5773     }
5774
5775   /* Set the line number for the statement.  */
5776   if (statement && statement_code_p (TREE_CODE (statement)))
5777     STMT_LINENO (statement) = statement_line_number;
5778 }
5779
5780 /* Parse a labeled-statement.
5781
5782    labeled-statement:
5783      identifier : statement
5784      case constant-expression : statement
5785      default : statement  
5786
5787    Returns the new CASE_LABEL, for a `case' or `default' label.  For
5788    an ordinary label, returns a LABEL_STMT.  */
5789
5790 static tree
5791 cp_parser_labeled_statement (parser)
5792      cp_parser *parser;
5793 {
5794   cp_token *token;
5795   tree statement = NULL_TREE;
5796
5797   /* The next token should be an identifier.  */
5798   token = cp_lexer_peek_token (parser->lexer);
5799   if (token->type != CPP_NAME
5800       && token->type != CPP_KEYWORD)
5801     {
5802       cp_parser_error (parser, "expected labeled-statement");
5803       return error_mark_node;
5804     }
5805
5806   switch (token->keyword)
5807     {
5808     case RID_CASE:
5809       {
5810         tree expr;
5811
5812         /* Consume the `case' token.  */
5813         cp_lexer_consume_token (parser->lexer);
5814         /* Parse the constant-expression.  */
5815         expr = cp_parser_constant_expression (parser);
5816         /* Create the label.  */
5817         statement = finish_case_label (expr, NULL_TREE);
5818       }
5819       break;
5820
5821     case RID_DEFAULT:
5822       /* Consume the `default' token.  */
5823       cp_lexer_consume_token (parser->lexer);
5824       /* Create the label.  */
5825       statement = finish_case_label (NULL_TREE, NULL_TREE);
5826       break;
5827
5828     default:
5829       /* Anything else must be an ordinary label.  */
5830       statement = finish_label_stmt (cp_parser_identifier (parser));
5831       break;
5832     }
5833
5834   /* Require the `:' token.  */
5835   cp_parser_require (parser, CPP_COLON, "`:'");
5836   /* Parse the labeled statement.  */
5837   cp_parser_statement (parser);
5838
5839   /* Return the label, in the case of a `case' or `default' label.  */
5840   return statement;
5841 }
5842
5843 /* Parse an expression-statement.
5844
5845    expression-statement:
5846      expression [opt] ;
5847
5848    Returns the new EXPR_STMT -- or NULL_TREE if the expression
5849    statement consists of nothing more than an `;'.  */
5850
5851 static tree
5852 cp_parser_expression_statement (parser)
5853      cp_parser *parser;
5854 {
5855   tree statement;
5856
5857   /* If the next token is not a `;', then there is an expression to parse.  */
5858   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5859     statement = finish_expr_stmt (cp_parser_expression (parser));
5860   /* Otherwise, we do not even bother to build an EXPR_STMT.  */
5861   else
5862     {
5863       finish_stmt ();
5864       statement = NULL_TREE;
5865     }
5866   /* Consume the final `;'.  */
5867   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5868     {
5869       /* If there is additional (erroneous) input, skip to the end of
5870          the statement.  */
5871       cp_parser_skip_to_end_of_statement (parser);
5872       /* If the next token is now a `;', consume it.  */
5873       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5874         cp_lexer_consume_token (parser->lexer);
5875     }
5876
5877   return statement;
5878 }
5879
5880 /* Parse a compound-statement.
5881
5882    compound-statement:
5883      { statement-seq [opt] }
5884      
5885    Returns a COMPOUND_STMT representing the statement.  */
5886
5887 static tree
5888 cp_parser_compound_statement (cp_parser *parser)
5889 {
5890   tree compound_stmt;
5891
5892   /* Consume the `{'.  */
5893   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5894     return error_mark_node;
5895   /* Begin the compound-statement.  */
5896   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5897   /* Parse an (optional) statement-seq.  */
5898   cp_parser_statement_seq_opt (parser);
5899   /* Finish the compound-statement.  */
5900   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5901   /* Consume the `}'.  */
5902   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5903
5904   return compound_stmt;
5905 }
5906
5907 /* Parse an (optional) statement-seq.
5908
5909    statement-seq:
5910      statement
5911      statement-seq [opt] statement  */
5912
5913 static void
5914 cp_parser_statement_seq_opt (parser)
5915      cp_parser *parser;
5916 {
5917   /* Scan statements until there aren't any more.  */
5918   while (true)
5919     {
5920       /* If we're looking at a `}', then we've run out of statements.  */
5921       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5922           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5923         break;
5924
5925       /* Parse the statement.  */
5926       cp_parser_statement (parser);
5927     }
5928 }
5929
5930 /* Parse a selection-statement.
5931
5932    selection-statement:
5933      if ( condition ) statement
5934      if ( condition ) statement else statement
5935      switch ( condition ) statement  
5936
5937    Returns the new IF_STMT or SWITCH_STMT.  */
5938
5939 static tree
5940 cp_parser_selection_statement (parser)
5941      cp_parser *parser;
5942 {
5943   cp_token *token;
5944   enum rid keyword;
5945
5946   /* Peek at the next token.  */
5947   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5948
5949   /* See what kind of keyword it is.  */
5950   keyword = token->keyword;
5951   switch (keyword)
5952     {
5953     case RID_IF:
5954     case RID_SWITCH:
5955       {
5956         tree statement;
5957         tree condition;
5958
5959         /* Look for the `('.  */
5960         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5961           {
5962             cp_parser_skip_to_end_of_statement (parser);
5963             return error_mark_node;
5964           }
5965
5966         /* Begin the selection-statement.  */
5967         if (keyword == RID_IF)
5968           statement = begin_if_stmt ();
5969         else
5970           statement = begin_switch_stmt ();
5971
5972         /* Parse the condition.  */
5973         condition = cp_parser_condition (parser);
5974         /* Look for the `)'.  */
5975         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5976           cp_parser_skip_to_closing_parenthesis (parser);
5977
5978         if (keyword == RID_IF)
5979           {
5980             tree then_stmt;
5981
5982             /* Add the condition.  */
5983             finish_if_stmt_cond (condition, statement);
5984
5985             /* Parse the then-clause.  */
5986             then_stmt = cp_parser_implicitly_scoped_statement (parser);
5987             finish_then_clause (statement);
5988
5989             /* If the next token is `else', parse the else-clause.  */
5990             if (cp_lexer_next_token_is_keyword (parser->lexer,
5991                                                 RID_ELSE))
5992               {
5993                 tree else_stmt;
5994
5995                 /* Consume the `else' keyword.  */
5996                 cp_lexer_consume_token (parser->lexer);
5997                 /* Parse the else-clause.  */
5998                 else_stmt 
5999                   = cp_parser_implicitly_scoped_statement (parser);
6000                 finish_else_clause (statement);
6001               }
6002
6003             /* Now we're all done with the if-statement.  */
6004             finish_if_stmt ();
6005           }
6006         else
6007           {
6008             tree body;
6009
6010             /* Add the condition.  */
6011             finish_switch_cond (condition, statement);
6012
6013             /* Parse the body of the switch-statement.  */
6014             body = cp_parser_implicitly_scoped_statement (parser);
6015
6016             /* Now we're all done with the switch-statement.  */
6017             finish_switch_stmt (statement);
6018           }
6019
6020         return statement;
6021       }
6022       break;
6023
6024     default:
6025       cp_parser_error (parser, "expected selection-statement");
6026       return error_mark_node;
6027     }
6028 }
6029
6030 /* Parse a condition. 
6031
6032    condition:
6033      expression
6034      type-specifier-seq declarator = assignment-expression  
6035
6036    GNU Extension:
6037    
6038    condition:
6039      type-specifier-seq declarator asm-specification [opt] 
6040        attributes [opt] = assignment-expression
6041  
6042    Returns the expression that should be tested.  */
6043
6044 static tree
6045 cp_parser_condition (parser)
6046      cp_parser *parser;
6047 {
6048   tree type_specifiers;
6049   const char *saved_message;
6050
6051   /* Try the declaration first.  */
6052   cp_parser_parse_tentatively (parser);
6053   /* New types are not allowed in the type-specifier-seq for a
6054      condition.  */
6055   saved_message = parser->type_definition_forbidden_message;
6056   parser->type_definition_forbidden_message
6057     = "types may not be defined in conditions";
6058   /* Parse the type-specifier-seq.  */
6059   type_specifiers = cp_parser_type_specifier_seq (parser);
6060   /* Restore the saved message.  */
6061   parser->type_definition_forbidden_message = saved_message;
6062   /* If all is well, we might be looking at a declaration.  */
6063   if (!cp_parser_error_occurred (parser))
6064     {
6065       tree decl;
6066       tree asm_specification;
6067       tree attributes;
6068       tree declarator;
6069       tree initializer = NULL_TREE;
6070       
6071       /* Parse the declarator.  */
6072       declarator = cp_parser_declarator (parser, 
6073                                          /*abstract_p=*/false,
6074                                          /*ctor_dtor_or_conv_p=*/NULL);
6075       /* Parse the attributes.  */
6076       attributes = cp_parser_attributes_opt (parser);
6077       /* Parse the asm-specification.  */
6078       asm_specification = cp_parser_asm_specification_opt (parser);
6079       /* If the next token is not an `=', then we might still be
6080          looking at an expression.  For example:
6081          
6082            if (A(a).x)
6083           
6084          looks like a decl-specifier-seq and a declarator -- but then
6085          there is no `=', so this is an expression.  */
6086       cp_parser_require (parser, CPP_EQ, "`='");
6087       /* If we did see an `=', then we are looking at a declaration
6088          for sure.  */
6089       if (cp_parser_parse_definitely (parser))
6090         {
6091           /* Create the declaration.  */
6092           decl = start_decl (declarator, type_specifiers, 
6093                              /*initialized_p=*/true,
6094                              attributes, /*prefix_attributes=*/NULL_TREE);
6095           /* Parse the assignment-expression.  */
6096           initializer = cp_parser_assignment_expression (parser);
6097           
6098           /* Process the initializer.  */
6099           cp_finish_decl (decl, 
6100                           initializer, 
6101                           asm_specification, 
6102                           LOOKUP_ONLYCONVERTING);
6103           
6104           return convert_from_reference (decl);
6105         }
6106     }
6107   /* If we didn't even get past the declarator successfully, we are
6108      definitely not looking at a declaration.  */
6109   else
6110     cp_parser_abort_tentative_parse (parser);
6111
6112   /* Otherwise, we are looking at an expression.  */
6113   return cp_parser_expression (parser);
6114 }
6115
6116 /* Parse an iteration-statement.
6117
6118    iteration-statement:
6119      while ( condition ) statement
6120      do statement while ( expression ) ;
6121      for ( for-init-statement condition [opt] ; expression [opt] )
6122        statement
6123
6124    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6125
6126 static tree
6127 cp_parser_iteration_statement (parser)
6128      cp_parser *parser;
6129 {
6130   cp_token *token;
6131   enum rid keyword;
6132   tree statement;
6133
6134   /* Peek at the next token.  */
6135   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6136   if (!token)
6137     return error_mark_node;
6138
6139   /* See what kind of keyword it is.  */
6140   keyword = token->keyword;
6141   switch (keyword)
6142     {
6143     case RID_WHILE:
6144       {
6145         tree condition;
6146
6147         /* Begin the while-statement.  */
6148         statement = begin_while_stmt ();
6149         /* Look for the `('.  */
6150         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6151         /* Parse the condition.  */
6152         condition = cp_parser_condition (parser);
6153         finish_while_stmt_cond (condition, statement);
6154         /* Look for the `)'.  */
6155         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6156         /* Parse the dependent statement.  */
6157         cp_parser_already_scoped_statement (parser);
6158         /* We're done with the while-statement.  */
6159         finish_while_stmt (statement);
6160       }
6161       break;
6162
6163     case RID_DO:
6164       {
6165         tree expression;
6166
6167         /* Begin the do-statement.  */
6168         statement = begin_do_stmt ();
6169         /* Parse the body of the do-statement.  */
6170         cp_parser_implicitly_scoped_statement (parser);
6171         finish_do_body (statement);
6172         /* Look for the `while' keyword.  */
6173         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6174         /* Look for the `('.  */
6175         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6176         /* Parse the expression.  */
6177         expression = cp_parser_expression (parser);
6178         /* We're done with the do-statement.  */
6179         finish_do_stmt (expression, statement);
6180         /* Look for the `)'.  */
6181         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6182         /* Look for the `;'.  */
6183         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6184       }
6185       break;
6186
6187     case RID_FOR:
6188       {
6189         tree condition = NULL_TREE;
6190         tree expression = NULL_TREE;
6191
6192         /* Begin the for-statement.  */
6193         statement = begin_for_stmt ();
6194         /* Look for the `('.  */
6195         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6196         /* Parse the initialization.  */
6197         cp_parser_for_init_statement (parser);
6198         finish_for_init_stmt (statement);
6199
6200         /* If there's a condition, process it.  */
6201         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6202           condition = cp_parser_condition (parser);
6203         finish_for_cond (condition, statement);
6204         /* Look for the `;'.  */
6205         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6206
6207         /* If there's an expression, process it.  */
6208         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6209           expression = cp_parser_expression (parser);
6210         finish_for_expr (expression, statement);
6211         /* Look for the `)'.  */
6212         cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6213
6214         /* Parse the body of the for-statement.  */
6215         cp_parser_already_scoped_statement (parser);
6216
6217         /* We're done with the for-statement.  */
6218         finish_for_stmt (statement);
6219       }
6220       break;
6221
6222     default:
6223       cp_parser_error (parser, "expected iteration-statement");
6224       statement = error_mark_node;
6225       break;
6226     }
6227
6228   return statement;
6229 }
6230
6231 /* Parse a for-init-statement.
6232
6233    for-init-statement:
6234      expression-statement
6235      simple-declaration  */
6236
6237 static void
6238 cp_parser_for_init_statement (parser)
6239      cp_parser *parser;
6240 {
6241   /* If the next token is a `;', then we have an empty
6242      expression-statement.  Gramatically, this is also a
6243      simple-declaration, but an invalid one, because it does not
6244      declare anything.  Therefore, if we did not handle this case
6245      specially, we would issue an error message about an invalid
6246      declaration.  */
6247   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6248     {
6249       /* We're going to speculatively look for a declaration, falling back
6250          to an expression, if necessary.  */
6251       cp_parser_parse_tentatively (parser);
6252       /* Parse the declaration.  */
6253       cp_parser_simple_declaration (parser,
6254                                     /*function_definition_allowed_p=*/false);
6255       /* If the tentative parse failed, then we shall need to look for an
6256          expression-statement.  */
6257       if (cp_parser_parse_definitely (parser))
6258         return;
6259     }
6260
6261   cp_parser_expression_statement (parser);
6262 }
6263
6264 /* Parse a jump-statement.
6265
6266    jump-statement:
6267      break ;
6268      continue ;
6269      return expression [opt] ;
6270      goto identifier ;  
6271
6272    GNU extension:
6273
6274    jump-statement:
6275      goto * expression ;
6276
6277    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6278    GOTO_STMT.  */
6279
6280 static tree
6281 cp_parser_jump_statement (parser)
6282      cp_parser *parser;
6283 {
6284   tree statement = error_mark_node;
6285   cp_token *token;
6286   enum rid keyword;
6287
6288   /* Peek at the next token.  */
6289   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6290   if (!token)
6291     return error_mark_node;
6292
6293   /* See what kind of keyword it is.  */
6294   keyword = token->keyword;
6295   switch (keyword)
6296     {
6297     case RID_BREAK:
6298       statement = finish_break_stmt ();
6299       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6300       break;
6301
6302     case RID_CONTINUE:
6303       statement = finish_continue_stmt ();
6304       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6305       break;
6306
6307     case RID_RETURN:
6308       {
6309         tree expr;
6310
6311         /* If the next token is a `;', then there is no 
6312            expression.  */
6313         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6314           expr = cp_parser_expression (parser);
6315         else
6316           expr = NULL_TREE;
6317         /* Build the return-statement.  */
6318         statement = finish_return_stmt (expr);
6319         /* Look for the final `;'.  */
6320         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6321       }
6322       break;
6323
6324     case RID_GOTO:
6325       /* Create the goto-statement.  */
6326       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6327         {
6328           /* Issue a warning about this use of a GNU extension.  */
6329           if (pedantic)
6330             pedwarn ("ISO C++ forbids computed gotos");
6331           /* Consume the '*' token.  */
6332           cp_lexer_consume_token (parser->lexer);
6333           /* Parse the dependent expression.  */
6334           finish_goto_stmt (cp_parser_expression (parser));
6335         }
6336       else
6337         finish_goto_stmt (cp_parser_identifier (parser));
6338       /* Look for the final `;'.  */
6339       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6340       break;
6341
6342     default:
6343       cp_parser_error (parser, "expected jump-statement");
6344       break;
6345     }
6346
6347   return statement;
6348 }
6349
6350 /* Parse a declaration-statement.
6351
6352    declaration-statement:
6353      block-declaration  */
6354
6355 static void
6356 cp_parser_declaration_statement (parser)
6357      cp_parser *parser;
6358 {
6359   /* Parse the block-declaration.  */
6360   cp_parser_block_declaration (parser, /*statement_p=*/true);
6361
6362   /* Finish off the statement.  */
6363   finish_stmt ();
6364 }
6365
6366 /* Some dependent statements (like `if (cond) statement'), are
6367    implicitly in their own scope.  In other words, if the statement is
6368    a single statement (as opposed to a compound-statement), it is
6369    none-the-less treated as if it were enclosed in braces.  Any
6370    declarations appearing in the dependent statement are out of scope
6371    after control passes that point.  This function parses a statement,
6372    but ensures that is in its own scope, even if it is not a
6373    compound-statement.  
6374
6375    Returns the new statement.  */
6376
6377 static tree
6378 cp_parser_implicitly_scoped_statement (parser)
6379      cp_parser *parser;
6380 {
6381   tree statement;
6382
6383   /* If the token is not a `{', then we must take special action.  */
6384   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6385     {
6386       /* Create a compound-statement.  */
6387       statement = begin_compound_stmt (/*has_no_scope=*/0);
6388       /* Parse the dependent-statement.  */
6389       cp_parser_statement (parser);
6390       /* Finish the dummy compound-statement.  */
6391       finish_compound_stmt (/*has_no_scope=*/0, statement);
6392     }
6393   /* Otherwise, we simply parse the statement directly.  */
6394   else
6395     statement = cp_parser_compound_statement (parser);
6396
6397   /* Return the statement.  */
6398   return statement;
6399 }
6400
6401 /* For some dependent statements (like `while (cond) statement'), we
6402    have already created a scope.  Therefore, even if the dependent
6403    statement is a compound-statement, we do not want to create another
6404    scope.  */
6405
6406 static void
6407 cp_parser_already_scoped_statement (parser)
6408      cp_parser *parser;
6409 {
6410   /* If the token is not a `{', then we must take special action.  */
6411   if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6412     {
6413       tree statement;
6414
6415       /* Create a compound-statement.  */
6416       statement = begin_compound_stmt (/*has_no_scope=*/1);
6417       /* Parse the dependent-statement.  */
6418       cp_parser_statement (parser);
6419       /* Finish the dummy compound-statement.  */
6420       finish_compound_stmt (/*has_no_scope=*/1, statement);
6421     }
6422   /* Otherwise, we simply parse the statement directly.  */
6423   else
6424     cp_parser_statement (parser);
6425 }
6426
6427 /* Declarations [gram.dcl.dcl] */
6428
6429 /* Parse an optional declaration-sequence.
6430
6431    declaration-seq:
6432      declaration
6433      declaration-seq declaration  */
6434
6435 static void
6436 cp_parser_declaration_seq_opt (parser)
6437      cp_parser *parser;
6438 {
6439   while (true)
6440     {
6441       cp_token *token;
6442
6443       token = cp_lexer_peek_token (parser->lexer);
6444
6445       if (token->type == CPP_CLOSE_BRACE
6446           || token->type == CPP_EOF)
6447         break;
6448
6449       if (token->type == CPP_SEMICOLON) 
6450         {
6451           /* A declaration consisting of a single semicolon is
6452              invalid.  Allow it unless we're being pedantic.  */
6453           if (pedantic)
6454             pedwarn ("extra `;'");
6455           cp_lexer_consume_token (parser->lexer);
6456           continue;
6457         }
6458
6459       cp_parser_declaration (parser);
6460     }
6461 }
6462
6463 /* Parse a declaration.
6464
6465    declaration:
6466      block-declaration
6467      function-definition
6468      template-declaration
6469      explicit-instantiation
6470      explicit-specialization
6471      linkage-specification
6472      namespace-definition    */
6473
6474 static void
6475 cp_parser_declaration (parser)
6476      cp_parser *parser;
6477 {
6478   cp_token token1;
6479   cp_token token2;
6480
6481   /* Try to figure out what kind of declaration is present.  */
6482   token1 = *cp_lexer_peek_token (parser->lexer);
6483   if (token1.type != CPP_EOF)
6484     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6485
6486   /* If the next token is `extern' and the following token is a string
6487      literal, then we have a linkage specification.  */
6488   if (token1.keyword == RID_EXTERN
6489       && cp_parser_is_string_literal (&token2))
6490     cp_parser_linkage_specification (parser);
6491   /* If the next token is `template', then we have either a template
6492      declaration, an explicit instantiation, or an explicit
6493      specialization.  */
6494   else if (token1.keyword == RID_TEMPLATE)
6495     {
6496       /* `template <>' indicates a template specialization.  */
6497       if (token2.type == CPP_LESS
6498           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6499         cp_parser_explicit_specialization (parser);
6500       /* `template <' indicates a template declaration.  */
6501       else if (token2.type == CPP_LESS)
6502         cp_parser_template_declaration (parser, /*member_p=*/false);
6503       /* Anything else must be an explicit instantiation.  */
6504       else
6505         cp_parser_explicit_instantiation (parser);
6506     }
6507   /* If the next token is `export', then we have a template
6508      declaration.  */
6509   else if (token1.keyword == RID_EXPORT)
6510     cp_parser_template_declaration (parser, /*member_p=*/false);
6511   /* If the next token is `extern', 'static' or 'inline' and the one
6512      after that is `template', we have a GNU extended explicit
6513      instantiation directive.  */
6514   else if (cp_parser_allow_gnu_extensions_p (parser)
6515            && (token1.keyword == RID_EXTERN
6516                || token1.keyword == RID_STATIC
6517                || token1.keyword == RID_INLINE)
6518            && token2.keyword == RID_TEMPLATE)
6519     cp_parser_explicit_instantiation (parser);
6520   /* If the next token is `namespace', check for a named or unnamed
6521      namespace definition.  */
6522   else if (token1.keyword == RID_NAMESPACE
6523            && (/* A named namespace definition.  */
6524                (token2.type == CPP_NAME
6525                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 
6526                     == CPP_OPEN_BRACE))
6527                /* An unnamed namespace definition.  */
6528                || token2.type == CPP_OPEN_BRACE))
6529     cp_parser_namespace_definition (parser);
6530   /* We must have either a block declaration or a function
6531      definition.  */
6532   else
6533     /* Try to parse a block-declaration, or a function-definition.  */
6534     cp_parser_block_declaration (parser, /*statement_p=*/false);
6535 }
6536
6537 /* Parse a block-declaration.  
6538
6539    block-declaration:
6540      simple-declaration
6541      asm-definition
6542      namespace-alias-definition
6543      using-declaration
6544      using-directive  
6545
6546    GNU Extension:
6547
6548    block-declaration:
6549      __extension__ block-declaration 
6550      label-declaration
6551
6552    If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6553    part of a declaration-statement.  */
6554
6555 static void
6556 cp_parser_block_declaration (cp_parser *parser, 
6557                              bool      statement_p)
6558 {
6559   cp_token *token1;
6560   int saved_pedantic;
6561
6562   /* Check for the `__extension__' keyword.  */
6563   if (cp_parser_extension_opt (parser, &saved_pedantic))
6564     {
6565       /* Parse the qualified declaration.  */
6566       cp_parser_block_declaration (parser, statement_p);
6567       /* Restore the PEDANTIC flag.  */
6568       pedantic = saved_pedantic;
6569
6570       return;
6571     }
6572
6573   /* Peek at the next token to figure out which kind of declaration is
6574      present.  */
6575   token1 = cp_lexer_peek_token (parser->lexer);
6576
6577   /* If the next keyword is `asm', we have an asm-definition.  */
6578   if (token1->keyword == RID_ASM)
6579     {
6580       if (statement_p)
6581         cp_parser_commit_to_tentative_parse (parser);
6582       cp_parser_asm_definition (parser);
6583     }
6584   /* If the next keyword is `namespace', we have a
6585      namespace-alias-definition.  */
6586   else if (token1->keyword == RID_NAMESPACE)
6587     cp_parser_namespace_alias_definition (parser);
6588   /* If the next keyword is `using', we have either a
6589      using-declaration or a using-directive.  */
6590   else if (token1->keyword == RID_USING)
6591     {
6592       cp_token *token2;
6593
6594       if (statement_p)
6595         cp_parser_commit_to_tentative_parse (parser);
6596       /* If the token after `using' is `namespace', then we have a
6597          using-directive.  */
6598       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6599       if (token2->keyword == RID_NAMESPACE)
6600         cp_parser_using_directive (parser);
6601       /* Otherwise, it's a using-declaration.  */
6602       else
6603         cp_parser_using_declaration (parser);
6604     }
6605   /* If the next keyword is `__label__' we have a label declaration.  */
6606   else if (token1->keyword == RID_LABEL)
6607     {
6608       if (statement_p)
6609         cp_parser_commit_to_tentative_parse (parser);
6610       cp_parser_label_declaration (parser);
6611     }
6612   /* Anything else must be a simple-declaration.  */
6613   else
6614     cp_parser_simple_declaration (parser, !statement_p);
6615 }
6616
6617 /* Parse a simple-declaration.
6618
6619    simple-declaration:
6620      decl-specifier-seq [opt] init-declarator-list [opt] ;  
6621
6622    init-declarator-list:
6623      init-declarator
6624      init-declarator-list , init-declarator 
6625
6626    If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6627    function-definition as a simple-declaration.   */
6628
6629 static void
6630 cp_parser_simple_declaration (parser, function_definition_allowed_p)
6631      cp_parser *parser;
6632      bool function_definition_allowed_p;
6633 {
6634   tree decl_specifiers;
6635   tree attributes;
6636   tree access_checks;
6637   bool declares_class_or_enum;
6638   bool saw_declarator;
6639
6640   /* Defer access checks until we know what is being declared; the
6641      checks for names appearing in the decl-specifier-seq should be
6642      done as if we were in the scope of the thing being declared.  */
6643   cp_parser_start_deferring_access_checks (parser);
6644   /* Parse the decl-specifier-seq.  We have to keep track of whether
6645      or not the decl-specifier-seq declares a named class or
6646      enumeration type, since that is the only case in which the
6647      init-declarator-list is allowed to be empty.  
6648
6649      [dcl.dcl]
6650
6651      In a simple-declaration, the optional init-declarator-list can be
6652      omitted only when declaring a class or enumeration, that is when
6653      the decl-specifier-seq contains either a class-specifier, an
6654      elaborated-type-specifier, or an enum-specifier.  */
6655   decl_specifiers
6656     = cp_parser_decl_specifier_seq (parser, 
6657                                     CP_PARSER_FLAGS_OPTIONAL,
6658                                     &attributes,
6659                                     &declares_class_or_enum);
6660   /* We no longer need to defer access checks.  */
6661   access_checks = cp_parser_stop_deferring_access_checks (parser);
6662
6663   /* Keep going until we hit the `;' at the end of the simple
6664      declaration.  */
6665   saw_declarator = false;
6666   while (cp_lexer_next_token_is_not (parser->lexer, 
6667                                      CPP_SEMICOLON))
6668     {
6669       cp_token *token;
6670       bool function_definition_p;
6671
6672       saw_declarator = true;
6673       /* Parse the init-declarator.  */
6674       cp_parser_init_declarator (parser, decl_specifiers, attributes,
6675                                  access_checks,
6676                                  function_definition_allowed_p,
6677                                  /*member_p=*/false,
6678                                  &function_definition_p);
6679       /* Handle function definitions specially.  */
6680       if (function_definition_p)
6681         {
6682           /* If the next token is a `,', then we are probably
6683              processing something like:
6684
6685                void f() {}, *p;
6686
6687              which is erroneous.  */
6688           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6689             error ("mixing declarations and function-definitions is forbidden");
6690           /* Otherwise, we're done with the list of declarators.  */
6691           else
6692             return;
6693         }
6694       /* The next token should be either a `,' or a `;'.  */
6695       token = cp_lexer_peek_token (parser->lexer);
6696       /* If it's a `,', there are more declarators to come.  */
6697       if (token->type == CPP_COMMA)
6698         cp_lexer_consume_token (parser->lexer);
6699       /* If it's a `;', we are done.  */
6700       else if (token->type == CPP_SEMICOLON)
6701         break;
6702       /* Anything else is an error.  */
6703       else
6704         {
6705           cp_parser_error (parser, "expected `,' or `;'");
6706           /* Skip tokens until we reach the end of the statement.  */
6707           cp_parser_skip_to_end_of_statement (parser);
6708           return;
6709         }
6710       /* After the first time around, a function-definition is not
6711          allowed -- even if it was OK at first.  For example:
6712
6713            int i, f() {}
6714
6715          is not valid.  */
6716       function_definition_allowed_p = false;
6717     }
6718
6719   /* Issue an error message if no declarators are present, and the
6720      decl-specifier-seq does not itself declare a class or
6721      enumeration.  */
6722   if (!saw_declarator)
6723     {
6724       if (cp_parser_declares_only_class_p (parser))
6725         shadow_tag (decl_specifiers);
6726       /* Perform any deferred access checks.  */
6727       cp_parser_perform_deferred_access_checks (access_checks);
6728     }
6729
6730   /* Consume the `;'.  */
6731   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6732
6733   /* Mark all the classes that appeared in the decl-specifier-seq as
6734      having received a `;'.  */
6735   note_list_got_semicolon (decl_specifiers);
6736 }
6737
6738 /* Parse a decl-specifier-seq.
6739
6740    decl-specifier-seq:
6741      decl-specifier-seq [opt] decl-specifier
6742
6743    decl-specifier:
6744      storage-class-specifier
6745      type-specifier
6746      function-specifier
6747      friend
6748      typedef  
6749
6750    GNU Extension:
6751
6752    decl-specifier-seq:
6753      decl-specifier-seq [opt] attributes
6754
6755    Returns a TREE_LIST, giving the decl-specifiers in the order they
6756    appear in the source code.  The TREE_VALUE of each node is the
6757    decl-specifier.  For a keyword (such as `auto' or `friend'), the
6758    TREE_VALUE is simply the correspoding TREE_IDENTIFIER.  For the
6759    representation of a type-specifier, see cp_parser_type_specifier.  
6760
6761    If there are attributes, they will be stored in *ATTRIBUTES,
6762    represented as described above cp_parser_attributes.  
6763
6764    If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6765    appears, and the entity that will be a friend is not going to be a
6766    class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
6767    even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6768    friendship is granted might not be a class.  */
6769
6770 static tree
6771 cp_parser_decl_specifier_seq (parser, flags, attributes,
6772                               declares_class_or_enum)
6773      cp_parser *parser;
6774      cp_parser_flags flags;
6775      tree *attributes;
6776      bool *declares_class_or_enum;
6777 {
6778   tree decl_specs = NULL_TREE;
6779   bool friend_p = false;
6780
6781   /* Assume no class or enumeration type is declared.  */
6782   *declares_class_or_enum = false;
6783
6784   /* Assume there are no attributes.  */
6785   *attributes = NULL_TREE;
6786
6787   /* Keep reading specifiers until there are no more to read.  */
6788   while (true)
6789     {
6790       tree decl_spec = NULL_TREE;
6791       bool constructor_p;
6792       cp_token *token;
6793
6794       /* Peek at the next token.  */
6795       token = cp_lexer_peek_token (parser->lexer);
6796       /* Handle attributes.  */
6797       if (token->keyword == RID_ATTRIBUTE)
6798         {
6799           /* Parse the attributes.  */
6800           decl_spec = cp_parser_attributes_opt (parser);
6801           /* Add them to the list.  */
6802           *attributes = chainon (*attributes, decl_spec);
6803           continue;
6804         }
6805       /* If the next token is an appropriate keyword, we can simply
6806          add it to the list.  */
6807       switch (token->keyword)
6808         {
6809         case RID_FRIEND:
6810           /* decl-specifier:
6811                friend  */
6812           friend_p = true;
6813           /* The representation of the specifier is simply the
6814              appropriate TREE_IDENTIFIER node.  */
6815           decl_spec = token->value;
6816           /* Consume the token.  */
6817           cp_lexer_consume_token (parser->lexer);
6818           break;
6819
6820           /* function-specifier:
6821                inline
6822                virtual
6823                explicit  */
6824         case RID_INLINE:
6825         case RID_VIRTUAL:
6826         case RID_EXPLICIT:
6827           decl_spec = cp_parser_function_specifier_opt (parser);
6828           break;
6829           
6830           /* decl-specifier:
6831                typedef  */
6832         case RID_TYPEDEF:
6833           /* The representation of the specifier is simply the
6834              appropriate TREE_IDENTIFIER node.  */
6835           decl_spec = token->value;
6836           /* Consume the token.  */
6837           cp_lexer_consume_token (parser->lexer);
6838           break;
6839
6840           /* storage-class-specifier:
6841                auto
6842                register
6843                static
6844                extern
6845                mutable  
6846
6847              GNU Extension:
6848                thread  */
6849         case RID_AUTO:
6850         case RID_REGISTER:
6851         case RID_STATIC:
6852         case RID_EXTERN:
6853         case RID_MUTABLE:
6854         case RID_THREAD:
6855           decl_spec = cp_parser_storage_class_specifier_opt (parser);
6856           break;
6857           
6858         default:
6859           break;
6860         }
6861
6862       /* Constructors are a special case.  The `S' in `S()' is not a
6863          decl-specifier; it is the beginning of the declarator.  */
6864       constructor_p = (!decl_spec 
6865                        && cp_parser_constructor_declarator_p (parser,
6866                                                               friend_p));
6867
6868       /* If we don't have a DECL_SPEC yet, then we must be looking at
6869          a type-specifier.  */
6870       if (!decl_spec && !constructor_p)
6871         {
6872           bool decl_spec_declares_class_or_enum;
6873           bool is_cv_qualifier;
6874
6875           decl_spec
6876             = cp_parser_type_specifier (parser, flags,
6877                                         friend_p,
6878                                         /*is_declaration=*/true,
6879                                         &decl_spec_declares_class_or_enum,
6880                                         &is_cv_qualifier);
6881
6882           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6883
6884           /* If this type-specifier referenced a user-defined type
6885              (a typedef, class-name, etc.), then we can't allow any
6886              more such type-specifiers henceforth.
6887
6888              [dcl.spec]
6889
6890              The longest sequence of decl-specifiers that could
6891              possibly be a type name is taken as the
6892              decl-specifier-seq of a declaration.  The sequence shall
6893              be self-consistent as described below.
6894
6895              [dcl.type]
6896
6897              As a general rule, at most one type-specifier is allowed
6898              in the complete decl-specifier-seq of a declaration.  The
6899              only exceptions are the following:
6900
6901              -- const or volatile can be combined with any other
6902                 type-specifier. 
6903
6904              -- signed or unsigned can be combined with char, long,
6905                 short, or int.
6906
6907              -- ..
6908
6909              Example:
6910
6911                typedef char* Pc;
6912                void g (const int Pc);
6913
6914              Here, Pc is *not* part of the decl-specifier seq; it's
6915              the declarator.  Therefore, once we see a type-specifier
6916              (other than a cv-qualifier), we forbid any additional
6917              user-defined types.  We *do* still allow things like `int
6918              int' to be considered a decl-specifier-seq, and issue the
6919              error message later.  */
6920           if (decl_spec && !is_cv_qualifier)
6921             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6922         }
6923
6924       /* If we still do not have a DECL_SPEC, then there are no more
6925          decl-specifiers.  */
6926       if (!decl_spec)
6927         {
6928           /* Issue an error message, unless the entire construct was
6929              optional.  */
6930           if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6931             {
6932               cp_parser_error (parser, "expected decl specifier");
6933               return error_mark_node;
6934             }
6935
6936           break;
6937         }
6938
6939       /* Add the DECL_SPEC to the list of specifiers.  */
6940       decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6941
6942       /* After we see one decl-specifier, further decl-specifiers are
6943          always optional.  */
6944       flags |= CP_PARSER_FLAGS_OPTIONAL;
6945     }
6946
6947   /* We have built up the DECL_SPECS in reverse order.  Return them in
6948      the correct order.  */
6949   return nreverse (decl_specs);
6950 }
6951
6952 /* Parse an (optional) storage-class-specifier. 
6953
6954    storage-class-specifier:
6955      auto
6956      register
6957      static
6958      extern
6959      mutable  
6960
6961    GNU Extension:
6962
6963    storage-class-specifier:
6964      thread
6965
6966    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6967    
6968 static tree
6969 cp_parser_storage_class_specifier_opt (parser)
6970      cp_parser *parser;
6971 {
6972   switch (cp_lexer_peek_token (parser->lexer)->keyword)
6973     {
6974     case RID_AUTO:
6975     case RID_REGISTER:
6976     case RID_STATIC:
6977     case RID_EXTERN:
6978     case RID_MUTABLE:
6979     case RID_THREAD:
6980       /* Consume the token.  */
6981       return cp_lexer_consume_token (parser->lexer)->value;
6982
6983     default:
6984       return NULL_TREE;
6985     }
6986 }
6987
6988 /* Parse an (optional) function-specifier. 
6989
6990    function-specifier:
6991      inline
6992      virtual
6993      explicit
6994
6995    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6996    
6997 static tree
6998 cp_parser_function_specifier_opt (parser)
6999      cp_parser *parser;
7000 {
7001   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7002     {
7003     case RID_INLINE:
7004     case RID_VIRTUAL:
7005     case RID_EXPLICIT:
7006       /* Consume the token.  */
7007       return cp_lexer_consume_token (parser->lexer)->value;
7008
7009     default:
7010       return NULL_TREE;
7011     }
7012 }
7013
7014 /* Parse a linkage-specification.
7015
7016    linkage-specification:
7017      extern string-literal { declaration-seq [opt] }
7018      extern string-literal declaration  */
7019
7020 static void
7021 cp_parser_linkage_specification (parser)
7022      cp_parser *parser;
7023 {
7024   cp_token *token;
7025   tree linkage;
7026
7027   /* Look for the `extern' keyword.  */
7028   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7029
7030   /* Peek at the next token.  */
7031   token = cp_lexer_peek_token (parser->lexer);
7032   /* If it's not a string-literal, then there's a problem.  */
7033   if (!cp_parser_is_string_literal (token))
7034     {
7035       cp_parser_error (parser, "expected language-name");
7036       return;
7037     }
7038   /* Consume the token.  */
7039   cp_lexer_consume_token (parser->lexer);
7040
7041   /* Transform the literal into an identifier.  If the literal is a
7042      wide-character string, or contains embedded NULs, then we can't
7043      handle it as the user wants.  */
7044   if (token->type == CPP_WSTRING
7045       || (strlen (TREE_STRING_POINTER (token->value))
7046           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7047     {
7048       cp_parser_error (parser, "invalid linkage-specification");
7049       /* Assume C++ linkage.  */
7050       linkage = get_identifier ("c++");
7051     }
7052   /* If it's a simple string constant, things are easier.  */
7053   else
7054     linkage = get_identifier (TREE_STRING_POINTER (token->value));
7055
7056   /* We're now using the new linkage.  */
7057   push_lang_context (linkage);
7058
7059   /* If the next token is a `{', then we're using the first
7060      production.  */
7061   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7062     {
7063       /* Consume the `{' token.  */
7064       cp_lexer_consume_token (parser->lexer);
7065       /* Parse the declarations.  */
7066       cp_parser_declaration_seq_opt (parser);
7067       /* Look for the closing `}'.  */
7068       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7069     }
7070   /* Otherwise, there's just one declaration.  */
7071   else
7072     {
7073       bool saved_in_unbraced_linkage_specification_p;
7074
7075       saved_in_unbraced_linkage_specification_p 
7076         = parser->in_unbraced_linkage_specification_p;
7077       parser->in_unbraced_linkage_specification_p = true;
7078       have_extern_spec = true;
7079       cp_parser_declaration (parser);
7080       have_extern_spec = false;
7081       parser->in_unbraced_linkage_specification_p 
7082         = saved_in_unbraced_linkage_specification_p;
7083     }
7084
7085   /* We're done with the linkage-specification.  */
7086   pop_lang_context ();
7087 }
7088
7089 /* Special member functions [gram.special] */
7090
7091 /* Parse a conversion-function-id.
7092
7093    conversion-function-id:
7094      operator conversion-type-id  
7095
7096    Returns an IDENTIFIER_NODE representing the operator.  */
7097
7098 static tree 
7099 cp_parser_conversion_function_id (parser)
7100      cp_parser *parser;
7101 {
7102   tree type;
7103   tree saved_scope;
7104   tree saved_qualifying_scope;
7105   tree saved_object_scope;
7106
7107   /* Look for the `operator' token.  */
7108   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7109     return error_mark_node;
7110   /* When we parse the conversion-type-id, the current scope will be
7111      reset.  However, we need that information in able to look up the
7112      conversion function later, so we save it here.  */
7113   saved_scope = parser->scope;
7114   saved_qualifying_scope = parser->qualifying_scope;
7115   saved_object_scope = parser->object_scope;
7116   /* We must enter the scope of the class so that the names of
7117      entities declared within the class are available in the
7118      conversion-type-id.  For example, consider:
7119
7120        struct S { 
7121          typedef int I;
7122          operator I();
7123        };
7124
7125        S::operator I() { ... }
7126
7127      In order to see that `I' is a type-name in the definition, we
7128      must be in the scope of `S'.  */
7129   if (saved_scope)
7130     push_scope (saved_scope);
7131   /* Parse the conversion-type-id.  */
7132   type = cp_parser_conversion_type_id (parser);
7133   /* Leave the scope of the class, if any.  */
7134   if (saved_scope)
7135     pop_scope (saved_scope);
7136   /* Restore the saved scope.  */
7137   parser->scope = saved_scope;
7138   parser->qualifying_scope = saved_qualifying_scope;
7139   parser->object_scope = saved_object_scope;
7140   /* If the TYPE is invalid, indicate failure.  */
7141   if (type == error_mark_node)
7142     return error_mark_node;
7143   return mangle_conv_op_name_for_type (type);
7144 }
7145
7146 /* Parse a conversion-type-id:
7147
7148    conversion-type-id:
7149      type-specifier-seq conversion-declarator [opt]
7150
7151    Returns the TYPE specified.  */
7152
7153 static tree
7154 cp_parser_conversion_type_id (parser)
7155      cp_parser *parser;
7156 {
7157   tree attributes;
7158   tree type_specifiers;
7159   tree declarator;
7160
7161   /* Parse the attributes.  */
7162   attributes = cp_parser_attributes_opt (parser);
7163   /* Parse the type-specifiers.  */
7164   type_specifiers = cp_parser_type_specifier_seq (parser);
7165   /* If that didn't work, stop.  */
7166   if (type_specifiers == error_mark_node)
7167     return error_mark_node;
7168   /* Parse the conversion-declarator.  */
7169   declarator = cp_parser_conversion_declarator_opt (parser);
7170
7171   return grokdeclarator (declarator, type_specifiers, TYPENAME,
7172                          /*initialized=*/0, &attributes);
7173 }
7174
7175 /* Parse an (optional) conversion-declarator.
7176
7177    conversion-declarator:
7178      ptr-operator conversion-declarator [opt]  
7179
7180    Returns a representation of the declarator.  See
7181    cp_parser_declarator for details.  */
7182
7183 static tree
7184 cp_parser_conversion_declarator_opt (parser)
7185      cp_parser *parser;
7186 {
7187   enum tree_code code;
7188   tree class_type;
7189   tree cv_qualifier_seq;
7190
7191   /* We don't know if there's a ptr-operator next, or not.  */
7192   cp_parser_parse_tentatively (parser);
7193   /* Try the ptr-operator.  */
7194   code = cp_parser_ptr_operator (parser, &class_type, 
7195                                  &cv_qualifier_seq);
7196   /* If it worked, look for more conversion-declarators.  */
7197   if (cp_parser_parse_definitely (parser))
7198     {
7199      tree declarator;
7200
7201      /* Parse another optional declarator.  */
7202      declarator = cp_parser_conversion_declarator_opt (parser);
7203
7204      /* Create the representation of the declarator.  */
7205      if (code == INDIRECT_REF)
7206        declarator = make_pointer_declarator (cv_qualifier_seq,
7207                                              declarator);
7208      else
7209        declarator =  make_reference_declarator (cv_qualifier_seq,
7210                                                 declarator);
7211
7212      /* Handle the pointer-to-member case.  */
7213      if (class_type)
7214        declarator = build_nt (SCOPE_REF, class_type, declarator);
7215
7216      return declarator;
7217    }
7218
7219   return NULL_TREE;
7220 }
7221
7222 /* Parse an (optional) ctor-initializer.
7223
7224    ctor-initializer:
7225      : mem-initializer-list  
7226
7227    Returns TRUE iff the ctor-initializer was actually present.  */
7228
7229 static bool
7230 cp_parser_ctor_initializer_opt (parser)
7231      cp_parser *parser;
7232 {
7233   /* If the next token is not a `:', then there is no
7234      ctor-initializer.  */
7235   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7236     {
7237       /* Do default initialization of any bases and members.  */
7238       if (DECL_CONSTRUCTOR_P (current_function_decl))
7239         finish_mem_initializers (NULL_TREE);
7240
7241       return false;
7242     }
7243
7244   /* Consume the `:' token.  */
7245   cp_lexer_consume_token (parser->lexer);
7246   /* And the mem-initializer-list.  */
7247   cp_parser_mem_initializer_list (parser);
7248
7249   return true;
7250 }
7251
7252 /* Parse a mem-initializer-list.
7253
7254    mem-initializer-list:
7255      mem-initializer
7256      mem-initializer , mem-initializer-list  */
7257
7258 static void
7259 cp_parser_mem_initializer_list (parser)
7260      cp_parser *parser;
7261 {
7262   tree mem_initializer_list = NULL_TREE;
7263
7264   /* Let the semantic analysis code know that we are starting the
7265      mem-initializer-list.  */
7266   begin_mem_initializers ();
7267
7268   /* Loop through the list.  */
7269   while (true)
7270     {
7271       tree mem_initializer;
7272
7273       /* Parse the mem-initializer.  */
7274       mem_initializer = cp_parser_mem_initializer (parser);
7275       /* Add it to the list, unless it was erroneous.  */
7276       if (mem_initializer)
7277         {
7278           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7279           mem_initializer_list = mem_initializer;
7280         }
7281       /* If the next token is not a `,', we're done.  */
7282       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7283         break;
7284       /* Consume the `,' token.  */
7285       cp_lexer_consume_token (parser->lexer);
7286     }
7287
7288   /* Perform semantic analysis.  */
7289   finish_mem_initializers (mem_initializer_list);
7290 }
7291
7292 /* Parse a mem-initializer.
7293
7294    mem-initializer:
7295      mem-initializer-id ( expression-list [opt] )  
7296
7297    GNU extension:
7298   
7299    mem-initializer:
7300      ( expresion-list [opt] )
7301
7302    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7303    class) or FIELD_DECL (for a non-static data member) to initialize;
7304    the TREE_VALUE is the expression-list.  */
7305
7306 static tree
7307 cp_parser_mem_initializer (parser)
7308      cp_parser *parser;
7309 {
7310   tree mem_initializer_id;
7311   tree expression_list;
7312
7313   /* Find out what is being initialized.  */
7314   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7315     {
7316       pedwarn ("anachronistic old-style base class initializer");
7317       mem_initializer_id = NULL_TREE;
7318     }
7319   else
7320     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7321   /* Look for the opening `('.  */
7322   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7323   /* Parse the expression-list.  */
7324   if (cp_lexer_next_token_is_not (parser->lexer,
7325                                   CPP_CLOSE_PAREN))
7326     expression_list = cp_parser_expression_list (parser);
7327   else
7328     expression_list = void_type_node;
7329   /* Look for the closing `)'.  */
7330   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7331
7332   return expand_member_init (mem_initializer_id,
7333                              expression_list);
7334 }
7335
7336 /* Parse a mem-initializer-id.
7337
7338    mem-initializer-id:
7339      :: [opt] nested-name-specifier [opt] class-name
7340      identifier  
7341
7342    Returns a TYPE indicating the class to be initializer for the first
7343    production.  Returns an IDENTIFIER_NODE indicating the data member
7344    to be initialized for the second production.  */
7345
7346 static tree
7347 cp_parser_mem_initializer_id (parser)
7348      cp_parser *parser;
7349 {
7350   bool global_scope_p;
7351   bool nested_name_specifier_p;
7352   tree id;
7353
7354   /* Look for the optional `::' operator.  */
7355   global_scope_p 
7356     = (cp_parser_global_scope_opt (parser, 
7357                                    /*current_scope_valid_p=*/false) 
7358        != NULL_TREE);
7359   /* Look for the optional nested-name-specifier.  The simplest way to
7360      implement:
7361
7362        [temp.res]
7363
7364        The keyword `typename' is not permitted in a base-specifier or
7365        mem-initializer; in these contexts a qualified name that
7366        depends on a template-parameter is implicitly assumed to be a
7367        type name.
7368
7369      is to assume that we have seen the `typename' keyword at this
7370      point.  */
7371   nested_name_specifier_p 
7372     = (cp_parser_nested_name_specifier_opt (parser,
7373                                             /*typename_keyword_p=*/true,
7374                                             /*check_dependency_p=*/true,
7375                                             /*type_p=*/true)
7376        != NULL_TREE);
7377   /* If there is a `::' operator or a nested-name-specifier, then we
7378      are definitely looking for a class-name.  */
7379   if (global_scope_p || nested_name_specifier_p)
7380     return cp_parser_class_name (parser,
7381                                  /*typename_keyword_p=*/true,
7382                                  /*template_keyword_p=*/false,
7383                                  /*type_p=*/false,
7384                                  /*check_access_p=*/true,
7385                                  /*check_dependency_p=*/true,
7386                                  /*class_head_p=*/false);
7387   /* Otherwise, we could also be looking for an ordinary identifier.  */
7388   cp_parser_parse_tentatively (parser);
7389   /* Try a class-name.  */
7390   id = cp_parser_class_name (parser, 
7391                              /*typename_keyword_p=*/true,
7392                              /*template_keyword_p=*/false,
7393                              /*type_p=*/false,
7394                              /*check_access_p=*/true,
7395                              /*check_dependency_p=*/true,
7396                              /*class_head_p=*/false);
7397   /* If we found one, we're done.  */
7398   if (cp_parser_parse_definitely (parser))
7399     return id;
7400   /* Otherwise, look for an ordinary identifier.  */
7401   return cp_parser_identifier (parser);
7402 }
7403
7404 /* Overloading [gram.over] */
7405
7406 /* Parse an operator-function-id.
7407
7408    operator-function-id:
7409      operator operator  
7410
7411    Returns an IDENTIFIER_NODE for the operator which is a
7412    human-readable spelling of the identifier, e.g., `operator +'.  */
7413
7414 static tree 
7415 cp_parser_operator_function_id (parser)
7416      cp_parser *parser;
7417 {
7418   /* Look for the `operator' keyword.  */
7419   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7420     return error_mark_node;
7421   /* And then the name of the operator itself.  */
7422   return cp_parser_operator (parser);
7423 }
7424
7425 /* Parse an operator.
7426
7427    operator:
7428      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7429      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7430      || ++ -- , ->* -> () []
7431
7432    GNU Extensions:
7433    
7434    operator:
7435      <? >? <?= >?=
7436
7437    Returns an IDENTIFIER_NODE for the operator which is a
7438    human-readable spelling of the identifier, e.g., `operator +'.  */
7439    
7440 static tree
7441 cp_parser_operator (parser)
7442      cp_parser *parser;
7443 {
7444   tree id = NULL_TREE;
7445   cp_token *token;
7446
7447   /* Peek at the next token.  */
7448   token = cp_lexer_peek_token (parser->lexer);
7449   /* Figure out which operator we have.  */
7450   switch (token->type)
7451     {
7452     case CPP_KEYWORD:
7453       {
7454         enum tree_code op;
7455
7456         /* The keyword should be either `new' or `delete'.  */
7457         if (token->keyword == RID_NEW)
7458           op = NEW_EXPR;
7459         else if (token->keyword == RID_DELETE)
7460           op = DELETE_EXPR;
7461         else
7462           break;
7463
7464         /* Consume the `new' or `delete' token.  */
7465         cp_lexer_consume_token (parser->lexer);
7466
7467         /* Peek at the next token.  */
7468         token = cp_lexer_peek_token (parser->lexer);
7469         /* If it's a `[' token then this is the array variant of the
7470            operator.  */
7471         if (token->type == CPP_OPEN_SQUARE)
7472           {
7473             /* Consume the `[' token.  */
7474             cp_lexer_consume_token (parser->lexer);
7475             /* Look for the `]' token.  */
7476             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7477             id = ansi_opname (op == NEW_EXPR 
7478                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7479           }
7480         /* Otherwise, we have the non-array variant.  */
7481         else
7482           id = ansi_opname (op);
7483
7484         return id;
7485       }
7486
7487     case CPP_PLUS:
7488       id = ansi_opname (PLUS_EXPR);
7489       break;
7490
7491     case CPP_MINUS:
7492       id = ansi_opname (MINUS_EXPR);
7493       break;
7494
7495     case CPP_MULT:
7496       id = ansi_opname (MULT_EXPR);
7497       break;
7498
7499     case CPP_DIV:
7500       id = ansi_opname (TRUNC_DIV_EXPR);
7501       break;
7502
7503     case CPP_MOD:
7504       id = ansi_opname (TRUNC_MOD_EXPR);
7505       break;
7506
7507     case CPP_XOR:
7508       id = ansi_opname (BIT_XOR_EXPR);
7509       break;
7510
7511     case CPP_AND:
7512       id = ansi_opname (BIT_AND_EXPR);
7513       break;
7514
7515     case CPP_OR:
7516       id = ansi_opname (BIT_IOR_EXPR);
7517       break;
7518
7519     case CPP_COMPL:
7520       id = ansi_opname (BIT_NOT_EXPR);
7521       break;
7522       
7523     case CPP_NOT:
7524       id = ansi_opname (TRUTH_NOT_EXPR);
7525       break;
7526
7527     case CPP_EQ:
7528       id = ansi_assopname (NOP_EXPR);
7529       break;
7530
7531     case CPP_LESS:
7532       id = ansi_opname (LT_EXPR);
7533       break;
7534
7535     case CPP_GREATER:
7536       id = ansi_opname (GT_EXPR);
7537       break;
7538
7539     case CPP_PLUS_EQ:
7540       id = ansi_assopname (PLUS_EXPR);
7541       break;
7542
7543     case CPP_MINUS_EQ:
7544       id = ansi_assopname (MINUS_EXPR);
7545       break;
7546
7547     case CPP_MULT_EQ:
7548       id = ansi_assopname (MULT_EXPR);
7549       break;
7550
7551     case CPP_DIV_EQ:
7552       id = ansi_assopname (TRUNC_DIV_EXPR);
7553       break;
7554
7555     case CPP_MOD_EQ:
7556       id = ansi_assopname (TRUNC_MOD_EXPR);
7557       break;
7558
7559     case CPP_XOR_EQ:
7560       id = ansi_assopname (BIT_XOR_EXPR);
7561       break;
7562
7563     case CPP_AND_EQ:
7564       id = ansi_assopname (BIT_AND_EXPR);
7565       break;
7566
7567     case CPP_OR_EQ:
7568       id = ansi_assopname (BIT_IOR_EXPR);
7569       break;
7570
7571     case CPP_LSHIFT:
7572       id = ansi_opname (LSHIFT_EXPR);
7573       break;
7574
7575     case CPP_RSHIFT:
7576       id = ansi_opname (RSHIFT_EXPR);
7577       break;
7578
7579     case CPP_LSHIFT_EQ:
7580       id = ansi_assopname (LSHIFT_EXPR);
7581       break;
7582
7583     case CPP_RSHIFT_EQ:
7584       id = ansi_assopname (RSHIFT_EXPR);
7585       break;
7586
7587     case CPP_EQ_EQ:
7588       id = ansi_opname (EQ_EXPR);
7589       break;
7590
7591     case CPP_NOT_EQ:
7592       id = ansi_opname (NE_EXPR);
7593       break;
7594
7595     case CPP_LESS_EQ:
7596       id = ansi_opname (LE_EXPR);
7597       break;
7598
7599     case CPP_GREATER_EQ:
7600       id = ansi_opname (GE_EXPR);
7601       break;
7602
7603     case CPP_AND_AND:
7604       id = ansi_opname (TRUTH_ANDIF_EXPR);
7605       break;
7606
7607     case CPP_OR_OR:
7608       id = ansi_opname (TRUTH_ORIF_EXPR);
7609       break;
7610       
7611     case CPP_PLUS_PLUS:
7612       id = ansi_opname (POSTINCREMENT_EXPR);
7613       break;
7614
7615     case CPP_MINUS_MINUS:
7616       id = ansi_opname (PREDECREMENT_EXPR);
7617       break;
7618
7619     case CPP_COMMA:
7620       id = ansi_opname (COMPOUND_EXPR);
7621       break;
7622
7623     case CPP_DEREF_STAR:
7624       id = ansi_opname (MEMBER_REF);
7625       break;
7626
7627     case CPP_DEREF:
7628       id = ansi_opname (COMPONENT_REF);
7629       break;
7630
7631     case CPP_OPEN_PAREN:
7632       /* Consume the `('.  */
7633       cp_lexer_consume_token (parser->lexer);
7634       /* Look for the matching `)'.  */
7635       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7636       return ansi_opname (CALL_EXPR);
7637
7638     case CPP_OPEN_SQUARE:
7639       /* Consume the `['.  */
7640       cp_lexer_consume_token (parser->lexer);
7641       /* Look for the matching `]'.  */
7642       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7643       return ansi_opname (ARRAY_REF);
7644
7645       /* Extensions.  */
7646     case CPP_MIN:
7647       id = ansi_opname (MIN_EXPR);
7648       break;
7649
7650     case CPP_MAX:
7651       id = ansi_opname (MAX_EXPR);
7652       break;
7653
7654     case CPP_MIN_EQ:
7655       id = ansi_assopname (MIN_EXPR);
7656       break;
7657
7658     case CPP_MAX_EQ:
7659       id = ansi_assopname (MAX_EXPR);
7660       break;
7661
7662     default:
7663       /* Anything else is an error.  */
7664       break;
7665     }
7666
7667   /* If we have selected an identifier, we need to consume the
7668      operator token.  */
7669   if (id)
7670     cp_lexer_consume_token (parser->lexer);
7671   /* Otherwise, no valid operator name was present.  */
7672   else
7673     {
7674       cp_parser_error (parser, "expected operator");
7675       id = error_mark_node;
7676     }
7677
7678   return id;
7679 }
7680
7681 /* Parse a template-declaration.
7682
7683    template-declaration:
7684      export [opt] template < template-parameter-list > declaration  
7685
7686    If MEMBER_P is TRUE, this template-declaration occurs within a
7687    class-specifier.  
7688
7689    The grammar rule given by the standard isn't correct.  What
7690    is really meant is:
7691
7692    template-declaration:
7693      export [opt] template-parameter-list-seq 
7694        decl-specifier-seq [opt] init-declarator [opt] ;
7695      export [opt] template-parameter-list-seq 
7696        function-definition
7697
7698    template-parameter-list-seq:
7699      template-parameter-list-seq [opt]
7700      template < template-parameter-list >  */
7701
7702 static void
7703 cp_parser_template_declaration (parser, member_p)
7704      cp_parser *parser;
7705      bool member_p;
7706 {
7707   /* Check for `export'.  */
7708   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7709     {
7710       /* Consume the `export' token.  */
7711       cp_lexer_consume_token (parser->lexer);
7712       /* Warn that we do not support `export'.  */
7713       warning ("keyword `export' not implemented, and will be ignored");
7714     }
7715
7716   cp_parser_template_declaration_after_export (parser, member_p);
7717 }
7718
7719 /* Parse a template-parameter-list.
7720
7721    template-parameter-list:
7722      template-parameter
7723      template-parameter-list , template-parameter
7724
7725    Returns a TREE_LIST.  Each node represents a template parameter.
7726    The nodes are connected via their TREE_CHAINs.  */
7727
7728 static tree
7729 cp_parser_template_parameter_list (parser)
7730      cp_parser *parser;
7731 {
7732   tree parameter_list = NULL_TREE;
7733
7734   while (true)
7735     {
7736       tree parameter;
7737       cp_token *token;
7738
7739       /* Parse the template-parameter.  */
7740       parameter = cp_parser_template_parameter (parser);
7741       /* Add it to the list.  */
7742       parameter_list = process_template_parm (parameter_list,
7743                                               parameter);
7744
7745       /* Peek at the next token.  */
7746       token = cp_lexer_peek_token (parser->lexer);
7747       /* If it's not a `,', we're done.  */
7748       if (token->type != CPP_COMMA)
7749         break;
7750       /* Otherwise, consume the `,' token.  */
7751       cp_lexer_consume_token (parser->lexer);
7752     }
7753
7754   return parameter_list;
7755 }
7756
7757 /* Parse a template-parameter.
7758
7759    template-parameter:
7760      type-parameter
7761      parameter-declaration
7762
7763    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
7764    TREE_PURPOSE is the default value, if any.  */
7765
7766 static tree
7767 cp_parser_template_parameter (parser)
7768      cp_parser *parser;
7769 {
7770   cp_token *token;
7771
7772   /* Peek at the next token.  */
7773   token = cp_lexer_peek_token (parser->lexer);
7774   /* If it is `class' or `template', we have a type-parameter.  */
7775   if (token->keyword == RID_TEMPLATE)
7776     return cp_parser_type_parameter (parser);
7777   /* If it is `class' or `typename' we do not know yet whether it is a
7778      type parameter or a non-type parameter.  Consider:
7779
7780        template <typename T, typename T::X X> ...
7781
7782      or:
7783      
7784        template <class C, class D*> ...
7785
7786      Here, the first parameter is a type parameter, and the second is
7787      a non-type parameter.  We can tell by looking at the token after
7788      the identifier -- if it is a `,', `=', or `>' then we have a type
7789      parameter.  */
7790   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7791     {
7792       /* Peek at the token after `class' or `typename'.  */
7793       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7794       /* If it's an identifier, skip it.  */
7795       if (token->type == CPP_NAME)
7796         token = cp_lexer_peek_nth_token (parser->lexer, 3);
7797       /* Now, see if the token looks like the end of a template
7798          parameter.  */
7799       if (token->type == CPP_COMMA 
7800           || token->type == CPP_EQ
7801           || token->type == CPP_GREATER)
7802         return cp_parser_type_parameter (parser);
7803     }
7804
7805   /* Otherwise, it is a non-type parameter.  
7806
7807      [temp.param]
7808
7809      When parsing a default template-argument for a non-type
7810      template-parameter, the first non-nested `>' is taken as the end
7811      of the template parameter-list rather than a greater-than
7812      operator.  */
7813   return 
7814     cp_parser_parameter_declaration (parser,
7815                                      /*greater_than_is_operator_p=*/false);
7816 }
7817
7818 /* Parse a type-parameter.
7819
7820    type-parameter:
7821      class identifier [opt]
7822      class identifier [opt] = type-id
7823      typename identifier [opt]
7824      typename identifier [opt] = type-id
7825      template < template-parameter-list > class identifier [opt]
7826      template < template-parameter-list > class identifier [opt] 
7827        = id-expression  
7828
7829    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
7830    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
7831    the declaration of the parameter.  */
7832
7833 static tree
7834 cp_parser_type_parameter (parser)
7835      cp_parser *parser;
7836 {
7837   cp_token *token;
7838   tree parameter;
7839
7840   /* Look for a keyword to tell us what kind of parameter this is.  */
7841   token = cp_parser_require (parser, CPP_KEYWORD, 
7842                              "expected `class', `typename', or `template'");
7843   if (!token)
7844     return error_mark_node;
7845
7846   switch (token->keyword)
7847     {
7848     case RID_CLASS:
7849     case RID_TYPENAME:
7850       {
7851         tree identifier;
7852         tree default_argument;
7853
7854         /* If the next token is an identifier, then it names the
7855            parameter.  */
7856         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7857           identifier = cp_parser_identifier (parser);
7858         else
7859           identifier = NULL_TREE;
7860
7861         /* Create the parameter.  */
7862         parameter = finish_template_type_parm (class_type_node, identifier);
7863
7864         /* If the next token is an `=', we have a default argument.  */
7865         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7866           {
7867             /* Consume the `=' token.  */
7868             cp_lexer_consume_token (parser->lexer);
7869             /* Parse the default-argumen.  */
7870             default_argument = cp_parser_type_id (parser);
7871           }
7872         else
7873           default_argument = NULL_TREE;
7874
7875         /* Create the combined representation of the parameter and the
7876            default argument.  */
7877         parameter = build_tree_list (default_argument, 
7878                                      parameter);
7879       }
7880       break;
7881
7882     case RID_TEMPLATE:
7883       {
7884         tree parameter_list;
7885         tree identifier;
7886         tree default_argument;
7887
7888         /* Look for the `<'.  */
7889         cp_parser_require (parser, CPP_LESS, "`<'");
7890         /* Parse the template-parameter-list.  */
7891         begin_template_parm_list ();
7892         parameter_list 
7893           = cp_parser_template_parameter_list (parser);
7894         parameter_list = end_template_parm_list (parameter_list);
7895         /* Look for the `>'.  */
7896         cp_parser_require (parser, CPP_GREATER, "`>'");
7897         /* Look for the `class' keyword.  */
7898         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7899         /* If the next token is an `=', then there is a
7900            default-argument.  If the next token is a `>', we are at
7901            the end of the parameter-list.  If the next token is a `,',
7902            then we are at the end of this parameter.  */
7903         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7904             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7905             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7906           identifier = cp_parser_identifier (parser);
7907         else
7908           identifier = NULL_TREE;
7909         /* Create the template parameter.  */
7910         parameter = finish_template_template_parm (class_type_node,
7911                                                    identifier);
7912                                                    
7913         /* If the next token is an `=', then there is a
7914            default-argument.  */
7915         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7916           {
7917             /* Consume the `='.  */
7918             cp_lexer_consume_token (parser->lexer);
7919             /* Parse the id-expression.  */
7920             default_argument 
7921               = cp_parser_id_expression (parser,
7922                                          /*template_keyword_p=*/false,
7923                                          /*check_dependency_p=*/true,
7924                                          /*template_p=*/NULL);
7925             /* Look up the name.  */
7926             default_argument 
7927               = cp_parser_lookup_name_simple (parser, default_argument);
7928             /* See if the default argument is valid.  */
7929             default_argument
7930               = check_template_template_default_arg (default_argument);
7931           }
7932         else
7933           default_argument = NULL_TREE;
7934
7935         /* Create the combined representation of the parameter and the
7936            default argument.  */
7937         parameter =  build_tree_list (default_argument, 
7938                                       parameter);
7939       }
7940       break;
7941
7942     default:
7943       /* Anything else is an error.  */
7944       cp_parser_error (parser,
7945                        "expected `class', `typename', or `template'");
7946       parameter = error_mark_node;
7947     }
7948   
7949   return parameter;
7950 }
7951
7952 /* Parse a template-id.
7953
7954    template-id:
7955      template-name < template-argument-list [opt] >
7956
7957    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7958    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
7959    returned.  Otherwise, if the template-name names a function, or set
7960    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
7961    names a class, returns a TYPE_DECL for the specialization.  
7962
7963    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7964    uninstantiated templates.  */
7965
7966 static tree
7967 cp_parser_template_id (cp_parser *parser, 
7968                        bool template_keyword_p, 
7969                        bool check_dependency_p)
7970 {
7971   tree template;
7972   tree arguments;
7973   tree saved_scope;
7974   tree saved_qualifying_scope;
7975   tree saved_object_scope;
7976   tree template_id;
7977   bool saved_greater_than_is_operator_p;
7978   ptrdiff_t start_of_id;
7979   tree access_check = NULL_TREE;
7980
7981   /* If the next token corresponds to a template-id, there is no need
7982      to reparse it.  */
7983   if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
7984     {
7985       tree value;
7986       tree check;
7987
7988       /* Get the stored value.  */
7989       value = cp_lexer_consume_token (parser->lexer)->value;
7990       /* Perform any access checks that were deferred.  */
7991       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7992         cp_parser_defer_access_check (parser, 
7993                                       TREE_PURPOSE (check),
7994                                       TREE_VALUE (check));
7995       /* Return the stored value.  */
7996       return TREE_VALUE (value);
7997     }
7998
7999   /* Remember where the template-id starts.  */
8000   if (cp_parser_parsing_tentatively (parser)
8001       && !cp_parser_committed_to_tentative_parse (parser))
8002     {
8003       cp_token *next_token = cp_lexer_peek_token (parser->lexer);
8004       start_of_id = cp_lexer_token_difference (parser->lexer,
8005                                                parser->lexer->first_token,
8006                                                next_token);
8007       access_check = parser->context->deferred_access_checks;
8008     }
8009   else
8010     start_of_id = -1;
8011
8012   /* Parse the template-name.  */
8013   template = cp_parser_template_name (parser, template_keyword_p,
8014                                       check_dependency_p);
8015   if (template == error_mark_node)
8016     return error_mark_node;
8017
8018   /* Look for the `<' that starts the template-argument-list.  */
8019   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8020     return error_mark_node;
8021
8022   /* [temp.names]
8023
8024      When parsing a template-id, the first non-nested `>' is taken as
8025      the end of the template-argument-list rather than a greater-than
8026      operator.  */
8027   saved_greater_than_is_operator_p 
8028     = parser->greater_than_is_operator_p;
8029   parser->greater_than_is_operator_p = false;
8030   /* Parsing the argument list may modify SCOPE, so we save it
8031      here.  */
8032   saved_scope = parser->scope;
8033   saved_qualifying_scope = parser->qualifying_scope;
8034   saved_object_scope = parser->object_scope;
8035   /* Parse the template-argument-list itself.  */
8036   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8037     arguments = NULL_TREE;
8038   else
8039     arguments = cp_parser_template_argument_list (parser);
8040   /* Look for the `>' that ends the template-argument-list.  */
8041   cp_parser_require (parser, CPP_GREATER, "`>'");
8042   /* The `>' token might be a greater-than operator again now.  */
8043   parser->greater_than_is_operator_p 
8044     = saved_greater_than_is_operator_p;
8045   /* Restore the SAVED_SCOPE.  */
8046   parser->scope = saved_scope;
8047   parser->qualifying_scope = saved_qualifying_scope;
8048   parser->object_scope = saved_object_scope;
8049
8050   /* Build a representation of the specialization.  */
8051   if (TREE_CODE (template) == IDENTIFIER_NODE)
8052     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8053   else if (DECL_CLASS_TEMPLATE_P (template)
8054            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8055     template_id 
8056       = finish_template_type (template, arguments, 
8057                               cp_lexer_next_token_is (parser->lexer, 
8058                                                       CPP_SCOPE));
8059   else
8060     {
8061       /* If it's not a class-template or a template-template, it should be
8062          a function-template.  */
8063       my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8064                            || TREE_CODE (template) == OVERLOAD
8065                            || BASELINK_P (template)),
8066                           20010716);
8067       
8068       template_id = lookup_template_function (template, arguments);
8069     }
8070   
8071   /* If parsing tentatively, replace the sequence of tokens that makes
8072      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8073      should we re-parse the token stream, we will not have to repeat
8074      the effort required to do the parse, nor will we issue duplicate
8075      error messages about problems during instantiation of the
8076      template.  */
8077   if (start_of_id >= 0)
8078     {
8079       cp_token *token;
8080       tree c;
8081
8082       /* Find the token that corresponds to the start of the
8083          template-id.  */
8084       token = cp_lexer_advance_token (parser->lexer, 
8085                                       parser->lexer->first_token,
8086                                       start_of_id);
8087
8088       /* Remember the access checks associated with this
8089          nested-name-specifier.  */
8090       c = parser->context->deferred_access_checks;
8091       if (c == access_check)
8092         access_check = NULL_TREE;
8093       else
8094         {
8095           while (TREE_CHAIN (c) != access_check)
8096             c = TREE_CHAIN (c);
8097           access_check = parser->context->deferred_access_checks;
8098           parser->context->deferred_access_checks = TREE_CHAIN (c);
8099           TREE_CHAIN (c) = NULL_TREE;
8100         }
8101
8102       /* Reset the contents of the START_OF_ID token.  */
8103       token->type = CPP_TEMPLATE_ID;
8104       token->value = build_tree_list (access_check, template_id);
8105       token->keyword = RID_MAX;
8106       /* Purge all subsequent tokens.  */
8107       cp_lexer_purge_tokens_after (parser->lexer, token);
8108     }
8109
8110   return template_id;
8111 }
8112
8113 /* Parse a template-name.
8114
8115    template-name:
8116      identifier
8117  
8118    The standard should actually say:
8119
8120    template-name:
8121      identifier
8122      operator-function-id
8123      conversion-function-id
8124
8125    A defect report has been filed about this issue.
8126
8127    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8128    `template' keyword, in a construction like:
8129
8130      T::template f<3>()
8131
8132    In that case `f' is taken to be a template-name, even though there
8133    is no way of knowing for sure.
8134
8135    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8136    name refers to a set of overloaded functions, at least one of which
8137    is a template, or an IDENTIFIER_NODE with the name of the template,
8138    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8139    names are looked up inside uninstantiated templates.  */
8140
8141 static tree
8142 cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8143      cp_parser *parser;
8144      bool template_keyword_p;
8145      bool check_dependency_p;
8146 {
8147   tree identifier;
8148   tree decl;
8149   tree fns;
8150
8151   /* If the next token is `operator', then we have either an
8152      operator-function-id or a conversion-function-id.  */
8153   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8154     {
8155       /* We don't know whether we're looking at an
8156          operator-function-id or a conversion-function-id.  */
8157       cp_parser_parse_tentatively (parser);
8158       /* Try an operator-function-id.  */
8159       identifier = cp_parser_operator_function_id (parser);
8160       /* If that didn't work, try a conversion-function-id.  */
8161       if (!cp_parser_parse_definitely (parser))
8162         identifier = cp_parser_conversion_function_id (parser);
8163     }
8164   /* Look for the identifier.  */
8165   else
8166     identifier = cp_parser_identifier (parser);
8167   
8168   /* If we didn't find an identifier, we don't have a template-id.  */
8169   if (identifier == error_mark_node)
8170     return error_mark_node;
8171
8172   /* If the name immediately followed the `template' keyword, then it
8173      is a template-name.  However, if the next token is not `<', then
8174      we do not treat it as a template-name, since it is not being used
8175      as part of a template-id.  This enables us to handle constructs
8176      like:
8177
8178        template <typename T> struct S { S(); };
8179        template <typename T> S<T>::S();
8180
8181      correctly.  We would treat `S' as a template -- if it were `S<T>'
8182      -- but we do not if there is no `<'.  */
8183   if (template_keyword_p && processing_template_decl
8184       && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8185     return identifier;
8186
8187   /* Look up the name.  */
8188   decl = cp_parser_lookup_name (parser, identifier,
8189                                 /*check_access=*/true,
8190                                 /*is_type=*/false,
8191                                 check_dependency_p);
8192   decl = maybe_get_template_decl_from_type_decl (decl);
8193
8194   /* If DECL is a template, then the name was a template-name.  */
8195   if (TREE_CODE (decl) == TEMPLATE_DECL)
8196     ;
8197   else 
8198     {
8199       /* The standard does not explicitly indicate whether a name that
8200          names a set of overloaded declarations, some of which are
8201          templates, is a template-name.  However, such a name should
8202          be a template-name; otherwise, there is no way to form a
8203          template-id for the overloaded templates.  */
8204       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8205       if (TREE_CODE (fns) == OVERLOAD)
8206         {
8207           tree fn;
8208           
8209           for (fn = fns; fn; fn = OVL_NEXT (fn))
8210             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8211               break;
8212         }
8213       else
8214         {
8215           /* Otherwise, the name does not name a template.  */
8216           cp_parser_error (parser, "expected template-name");
8217           return error_mark_node;
8218         }
8219     }
8220
8221   /* If DECL is dependent, and refers to a function, then just return
8222      its name; we will look it up again during template instantiation.  */
8223   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8224     {
8225       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8226       if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8227         return identifier;
8228     }
8229
8230   return decl;
8231 }
8232
8233 /* Parse a template-argument-list.
8234
8235    template-argument-list:
8236      template-argument
8237      template-argument-list , template-argument
8238
8239    Returns a TREE_LIST representing the arguments, in the order they
8240    appeared.  The TREE_VALUE of each node is a representation of the
8241    argument.  */
8242
8243 static tree
8244 cp_parser_template_argument_list (parser)
8245      cp_parser *parser;
8246 {
8247   tree arguments = NULL_TREE;
8248
8249   while (true)
8250     {
8251       tree argument;
8252
8253       /* Parse the template-argument.  */
8254       argument = cp_parser_template_argument (parser);
8255       /* Add it to the list.  */
8256       arguments = tree_cons (NULL_TREE, argument, arguments);
8257       /* If it is not a `,', then there are no more arguments.  */
8258       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8259         break;
8260       /* Otherwise, consume the ','.  */
8261       cp_lexer_consume_token (parser->lexer);
8262     }
8263
8264   /* We built up the arguments in reverse order.  */
8265   return nreverse (arguments);
8266 }
8267
8268 /* Parse a template-argument.
8269
8270    template-argument:
8271      assignment-expression
8272      type-id
8273      id-expression
8274
8275    The representation is that of an assignment-expression, type-id, or
8276    id-expression -- except that the qualified id-expression is
8277    evaluated, so that the value returned is either a DECL or an
8278    OVERLOAD.  */
8279
8280 static tree
8281 cp_parser_template_argument (parser)
8282      cp_parser *parser;
8283 {
8284   tree argument;
8285   bool template_p;
8286
8287   /* There's really no way to know what we're looking at, so we just
8288      try each alternative in order.  
8289
8290        [temp.arg]
8291
8292        In a template-argument, an ambiguity between a type-id and an
8293        expression is resolved to a type-id, regardless of the form of
8294        the corresponding template-parameter.  
8295
8296      Therefore, we try a type-id first.  */
8297   cp_parser_parse_tentatively (parser);
8298   /* Otherwise, try a type-id.  */
8299   argument = cp_parser_type_id (parser);
8300   /* If the next token isn't a `,' or a `>', then this argument wasn't
8301      really finished.  */
8302   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8303       && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8304     cp_parser_error (parser, "expected template-argument");
8305   /* If that worked, we're done.  */
8306   if (cp_parser_parse_definitely (parser))
8307     return argument;
8308   /* We're still not sure what the argument will be.  */
8309   cp_parser_parse_tentatively (parser);
8310   /* Try a template.  */
8311   argument = cp_parser_id_expression (parser, 
8312                                       /*template_keyword_p=*/false,
8313                                       /*check_dependency_p=*/true,
8314                                       &template_p);
8315   /* If the next token isn't a `,' or a `>', then this argument wasn't
8316      really finished.  */
8317   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8318       && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8319     cp_parser_error (parser, "expected template-argument");
8320   if (!cp_parser_error_occurred (parser))
8321     {
8322       /* Figure out what is being referred to.  */
8323       argument = cp_parser_lookup_name_simple (parser, argument);
8324       if (template_p)
8325         argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8326                                                 TREE_OPERAND (argument, 1),
8327                                                 tf_error | tf_parsing);
8328       else if (TREE_CODE (argument) != TEMPLATE_DECL)
8329         cp_parser_error (parser, "expected template-name");
8330     }
8331   if (cp_parser_parse_definitely (parser))
8332     return argument;
8333   /* It must be an assignment-expression.  */
8334   return cp_parser_assignment_expression (parser);
8335 }
8336
8337 /* Parse an explicit-instantiation.
8338
8339    explicit-instantiation:
8340      template declaration  
8341
8342    Although the standard says `declaration', what it really means is:
8343
8344    explicit-instantiation:
8345      template decl-specifier-seq [opt] declarator [opt] ; 
8346
8347    Things like `template int S<int>::i = 5, int S<double>::j;' are not
8348    supposed to be allowed.  A defect report has been filed about this
8349    issue.  
8350
8351    GNU Extension:
8352   
8353    explicit-instantiation:
8354      storage-class-specifier template 
8355        decl-specifier-seq [opt] declarator [opt] ;
8356      function-specifier template 
8357        decl-specifier-seq [opt] declarator [opt] ;  */
8358
8359 static void
8360 cp_parser_explicit_instantiation (parser)
8361      cp_parser *parser;
8362 {
8363   bool declares_class_or_enum;
8364   tree decl_specifiers;
8365   tree attributes;
8366   tree extension_specifier = NULL_TREE;
8367
8368   /* Look for an (optional) storage-class-specifier or
8369      function-specifier.  */
8370   if (cp_parser_allow_gnu_extensions_p (parser))
8371     {
8372       extension_specifier 
8373         = cp_parser_storage_class_specifier_opt (parser);
8374       if (!extension_specifier)
8375         extension_specifier = cp_parser_function_specifier_opt (parser);
8376     }
8377
8378   /* Look for the `template' keyword.  */
8379   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8380   /* Let the front end know that we are processing an explicit
8381      instantiation.  */
8382   begin_explicit_instantiation ();
8383   /* [temp.explicit] says that we are supposed to ignore access
8384      control while processing explicit instantiation directives.  */
8385   scope_chain->check_access = 0;
8386   /* Parse a decl-specifier-seq.  */
8387   decl_specifiers 
8388     = cp_parser_decl_specifier_seq (parser,
8389                                     CP_PARSER_FLAGS_OPTIONAL,
8390                                     &attributes,
8391                                     &declares_class_or_enum);
8392   /* If there was exactly one decl-specifier, and it declared a class,
8393      and there's no declarator, then we have an explicit type
8394      instantiation.  */
8395   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8396     {
8397       tree type;
8398
8399       type = check_tag_decl (decl_specifiers);
8400       if (type)
8401         do_type_instantiation (type, extension_specifier, /*complain=*/1);
8402     }
8403   else
8404     {
8405       tree declarator;
8406       tree decl;
8407
8408       /* Parse the declarator.  */
8409       declarator 
8410         = cp_parser_declarator (parser, 
8411                                 /*abstract_p=*/false, 
8412                                 /*ctor_dtor_or_conv_p=*/NULL);
8413       decl = grokdeclarator (declarator, decl_specifiers, 
8414                              NORMAL, 0, NULL);
8415       /* Do the explicit instantiation.  */
8416       do_decl_instantiation (decl, extension_specifier);
8417     }
8418   /* We're done with the instantiation.  */
8419   end_explicit_instantiation ();
8420   /* Trun access control back on.  */
8421   scope_chain->check_access = flag_access_control;
8422
8423   /* Look for the trailing `;'.  */
8424   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8425 }
8426
8427 /* Parse an explicit-specialization.
8428
8429    explicit-specialization:
8430      template < > declaration  
8431
8432    Although the standard says `declaration', what it really means is:
8433
8434    explicit-specialization:
8435      template <> decl-specifier [opt] init-declarator [opt] ;
8436      template <> function-definition 
8437      template <> explicit-specialization
8438      template <> template-declaration  */
8439
8440 static void
8441 cp_parser_explicit_specialization (parser)
8442      cp_parser *parser;
8443 {
8444   /* Look for the `template' keyword.  */
8445   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8446   /* Look for the `<'.  */
8447   cp_parser_require (parser, CPP_LESS, "`<'");
8448   /* Look for the `>'.  */
8449   cp_parser_require (parser, CPP_GREATER, "`>'");
8450   /* We have processed another parameter list.  */
8451   ++parser->num_template_parameter_lists;
8452   /* Let the front end know that we are beginning a specialization.  */
8453   begin_specialization ();
8454
8455   /* If the next keyword is `template', we need to figure out whether
8456      or not we're looking a template-declaration.  */
8457   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8458     {
8459       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8460           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8461         cp_parser_template_declaration_after_export (parser,
8462                                                      /*member_p=*/false);
8463       else
8464         cp_parser_explicit_specialization (parser);
8465     }
8466   else
8467     /* Parse the dependent declaration.  */
8468     cp_parser_single_declaration (parser, 
8469                                   /*member_p=*/false,
8470                                   /*friend_p=*/NULL);
8471
8472   /* We're done with the specialization.  */
8473   end_specialization ();
8474   /* We're done with this parameter list.  */
8475   --parser->num_template_parameter_lists;
8476 }
8477
8478 /* Parse a type-specifier.
8479
8480    type-specifier:
8481      simple-type-specifier
8482      class-specifier
8483      enum-specifier
8484      elaborated-type-specifier
8485      cv-qualifier
8486
8487    GNU Extension:
8488
8489    type-specifier:
8490      __complex__
8491
8492    Returns a representation of the type-specifier.  If the
8493    type-specifier is a keyword (like `int' or `const', or
8494    `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8495    For a class-specifier, enum-specifier, or elaborated-type-specifier
8496    a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8497
8498    If IS_FRIEND is TRUE then this type-specifier is being declared a
8499    `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
8500    appearing in a decl-specifier-seq.
8501
8502    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8503    class-specifier, enum-specifier, or elaborated-type-specifier, then
8504    *DECLARES_CLASS_OR_ENUM is set to TRUE.  Otherwise, it is set to
8505    FALSE.
8506
8507    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8508    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
8509    is set to FALSE.  */
8510
8511 static tree
8512 cp_parser_type_specifier (parser, 
8513                           flags, 
8514                           is_friend,
8515                           is_declaration,
8516                           declares_class_or_enum,
8517                           is_cv_qualifier)
8518      cp_parser *parser;
8519      cp_parser_flags flags;
8520      bool is_friend;
8521      bool is_declaration;
8522      bool *declares_class_or_enum;
8523      bool *is_cv_qualifier;
8524 {
8525   tree type_spec = NULL_TREE;
8526   cp_token *token;
8527   enum rid keyword;
8528
8529   /* Assume this type-specifier does not declare a new type.  */
8530   if (declares_class_or_enum)
8531     *declares_class_or_enum = false;
8532   /* And that it does not specify a cv-qualifier.  */
8533   if (is_cv_qualifier)
8534     *is_cv_qualifier = false;
8535   /* Peek at the next token.  */
8536   token = cp_lexer_peek_token (parser->lexer);
8537
8538   /* If we're looking at a keyword, we can use that to guide the
8539      production we choose.  */
8540   keyword = token->keyword;
8541   switch (keyword)
8542     {
8543       /* Any of these indicate either a class-specifier, or an
8544          elaborated-type-specifier.  */
8545     case RID_CLASS:
8546     case RID_STRUCT:
8547     case RID_UNION:
8548     case RID_ENUM:
8549       /* Parse tentatively so that we can back up if we don't find a
8550          class-specifier or enum-specifier.  */
8551       cp_parser_parse_tentatively (parser);
8552       /* Look for the class-specifier or enum-specifier.  */
8553       if (keyword == RID_ENUM)
8554         type_spec = cp_parser_enum_specifier (parser);
8555       else
8556         type_spec = cp_parser_class_specifier (parser);
8557
8558       /* If that worked, we're done.  */
8559       if (cp_parser_parse_definitely (parser))
8560         {
8561           if (declares_class_or_enum)
8562             *declares_class_or_enum = true;
8563           return type_spec;
8564         }
8565
8566       /* Fall through.  */
8567
8568     case RID_TYPENAME:
8569       /* Look for an elaborated-type-specifier.  */
8570       type_spec = cp_parser_elaborated_type_specifier (parser,
8571                                                        is_friend,
8572                                                        is_declaration);
8573       /* We're declaring a class or enum -- unless we're using
8574          `typename'.  */
8575       if (declares_class_or_enum && keyword != RID_TYPENAME)
8576         *declares_class_or_enum = true;
8577       return type_spec;
8578
8579     case RID_CONST:
8580     case RID_VOLATILE:
8581     case RID_RESTRICT:
8582       type_spec = cp_parser_cv_qualifier_opt (parser);
8583       /* Even though we call a routine that looks for an optional
8584          qualifier, we know that there should be one.  */
8585       my_friendly_assert (type_spec != NULL, 20000328);
8586       /* This type-specifier was a cv-qualified.  */
8587       if (is_cv_qualifier)
8588         *is_cv_qualifier = true;
8589
8590       return type_spec;
8591
8592     case RID_COMPLEX:
8593       /* The `__complex__' keyword is a GNU extension.  */
8594       return cp_lexer_consume_token (parser->lexer)->value;
8595
8596     default:
8597       break;
8598     }
8599
8600   /* If we do not already have a type-specifier, assume we are looking
8601      at a simple-type-specifier.  */
8602   type_spec = cp_parser_simple_type_specifier (parser, flags);
8603
8604   /* If we didn't find a type-specifier, and a type-specifier was not
8605      optional in this context, issue an error message.  */
8606   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8607     {
8608       cp_parser_error (parser, "expected type specifier");
8609       return error_mark_node;
8610     }
8611
8612   return type_spec;
8613 }
8614
8615 /* Parse a simple-type-specifier.
8616
8617    simple-type-specifier:
8618      :: [opt] nested-name-specifier [opt] type-name
8619      :: [opt] nested-name-specifier template template-id
8620      char
8621      wchar_t
8622      bool
8623      short
8624      int
8625      long
8626      signed
8627      unsigned
8628      float
8629      double
8630      void  
8631
8632    GNU Extension:
8633
8634    simple-type-specifier:
8635      __typeof__ unary-expression
8636      __typeof__ ( type-id )
8637
8638    For the various keywords, the value returned is simply the
8639    TREE_IDENTIFIER representing the keyword.  For the first two
8640    productions, the value returned is the indicated TYPE_DECL.  */
8641
8642 static tree
8643 cp_parser_simple_type_specifier (parser, flags)
8644      cp_parser *parser;
8645      cp_parser_flags flags;
8646 {
8647   tree type = NULL_TREE;
8648   cp_token *token;
8649
8650   /* Peek at the next token.  */
8651   token = cp_lexer_peek_token (parser->lexer);
8652
8653   /* If we're looking at a keyword, things are easy.  */
8654   switch (token->keyword)
8655     {
8656     case RID_CHAR:
8657     case RID_WCHAR:
8658     case RID_BOOL:
8659     case RID_SHORT:
8660     case RID_INT:
8661     case RID_LONG:
8662     case RID_SIGNED:
8663     case RID_UNSIGNED:
8664     case RID_FLOAT:
8665     case RID_DOUBLE:
8666     case RID_VOID:
8667       /* Consume the token.  */
8668       return cp_lexer_consume_token (parser->lexer)->value;
8669
8670     case RID_TYPEOF:
8671       {
8672         tree operand;
8673
8674         /* Consume the `typeof' token.  */
8675         cp_lexer_consume_token (parser->lexer);
8676         /* Parse the operand to `typeof'  */
8677         operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8678         /* If it is not already a TYPE, take its type.  */
8679         if (!TYPE_P (operand))
8680           operand = finish_typeof (operand);
8681
8682         return operand;
8683       }
8684
8685     default:
8686       break;
8687     }
8688
8689   /* The type-specifier must be a user-defined type.  */
8690   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) 
8691     {
8692       /* Don't gobble tokens or issue error messages if this is an
8693          optional type-specifier.  */
8694       if (flags & CP_PARSER_FLAGS_OPTIONAL)
8695         cp_parser_parse_tentatively (parser);
8696
8697       /* Look for the optional `::' operator.  */
8698       cp_parser_global_scope_opt (parser,
8699                                   /*current_scope_valid_p=*/false);
8700       /* Look for the nested-name specifier.  */
8701       cp_parser_nested_name_specifier_opt (parser,
8702                                            /*typename_keyword_p=*/false,
8703                                            /*check_dependency_p=*/true,
8704                                            /*type_p=*/false);
8705       /* If we have seen a nested-name-specifier, and the next token
8706          is `template', then we are using the template-id production.  */
8707       if (parser->scope 
8708           && cp_parser_optional_template_keyword (parser))
8709         {
8710           /* Look for the template-id.  */
8711           type = cp_parser_template_id (parser, 
8712                                         /*template_keyword_p=*/true,
8713                                         /*check_dependency_p=*/true);
8714           /* If the template-id did not name a type, we are out of
8715              luck.  */
8716           if (TREE_CODE (type) != TYPE_DECL)
8717             {
8718               cp_parser_error (parser, "expected template-id for type");
8719               type = NULL_TREE;
8720             }
8721         }
8722       /* Otherwise, look for a type-name.  */
8723       else
8724         {
8725           type = cp_parser_type_name (parser);
8726           if (type == error_mark_node)
8727             type = NULL_TREE;
8728         }
8729
8730       /* If it didn't work out, we don't have a TYPE.  */
8731       if ((flags & CP_PARSER_FLAGS_OPTIONAL) 
8732           && !cp_parser_parse_definitely (parser))
8733         type = NULL_TREE;
8734     }
8735
8736   /* If we didn't get a type-name, issue an error message.  */
8737   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8738     {
8739       cp_parser_error (parser, "expected type-name");
8740       return error_mark_node;
8741     }
8742
8743   return type;
8744 }
8745
8746 /* Parse a type-name.
8747
8748    type-name:
8749      class-name
8750      enum-name
8751      typedef-name  
8752
8753    enum-name:
8754      identifier
8755
8756    typedef-name:
8757      identifier 
8758
8759    Returns a TYPE_DECL for the the type.  */
8760
8761 static tree
8762 cp_parser_type_name (parser)
8763      cp_parser *parser;
8764 {
8765   tree type_decl;
8766   tree identifier;
8767
8768   /* We can't know yet whether it is a class-name or not.  */
8769   cp_parser_parse_tentatively (parser);
8770   /* Try a class-name.  */
8771   type_decl = cp_parser_class_name (parser, 
8772                                     /*typename_keyword_p=*/false,
8773                                     /*template_keyword_p=*/false,
8774                                     /*type_p=*/false,
8775                                     /*check_access_p=*/true,
8776                                     /*check_dependency_p=*/true,
8777                                     /*class_head_p=*/false);
8778   /* If it's not a class-name, keep looking.  */
8779   if (!cp_parser_parse_definitely (parser))
8780     {
8781       /* It must be a typedef-name or an enum-name.  */
8782       identifier = cp_parser_identifier (parser);
8783       if (identifier == error_mark_node)
8784         return error_mark_node;
8785       
8786       /* Look up the type-name.  */
8787       type_decl = cp_parser_lookup_name_simple (parser, identifier);
8788       /* Issue an error if we did not find a type-name.  */
8789       if (TREE_CODE (type_decl) != TYPE_DECL)
8790         {
8791           cp_parser_error (parser, "expected type-name");
8792           type_decl = error_mark_node;
8793         }
8794       /* Remember that the name was used in the definition of the
8795          current class so that we can check later to see if the
8796          meaning would have been different after the class was
8797          entirely defined.  */
8798       else if (type_decl != error_mark_node
8799                && !parser->scope)
8800         maybe_note_name_used_in_class (identifier, type_decl);
8801     }
8802   
8803   return type_decl;
8804 }
8805
8806
8807 /* Parse an elaborated-type-specifier.  Note that the grammar given
8808    here incorporates the resolution to DR68.
8809
8810    elaborated-type-specifier:
8811      class-key :: [opt] nested-name-specifier [opt] identifier
8812      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8813      enum :: [opt] nested-name-specifier [opt] identifier
8814      typename :: [opt] nested-name-specifier identifier
8815      typename :: [opt] nested-name-specifier template [opt] 
8816        template-id 
8817
8818    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8819    declared `friend'.  If IS_DECLARATION is TRUE, then this
8820    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8821    something is being declared.
8822
8823    Returns the TYPE specified.  */
8824
8825 static tree
8826 cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8827      cp_parser *parser;
8828      bool is_friend;
8829      bool is_declaration;
8830 {
8831   enum tag_types tag_type;
8832   tree identifier;
8833   tree type = NULL_TREE;
8834
8835   /* See if we're looking at the `enum' keyword.  */
8836   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8837     {
8838       /* Consume the `enum' token.  */
8839       cp_lexer_consume_token (parser->lexer);
8840       /* Remember that it's an enumeration type.  */
8841       tag_type = enum_type;
8842     }
8843   /* Or, it might be `typename'.  */
8844   else if (cp_lexer_next_token_is_keyword (parser->lexer,
8845                                            RID_TYPENAME))
8846     {
8847       /* Consume the `typename' token.  */
8848       cp_lexer_consume_token (parser->lexer);
8849       /* Remember that it's a `typename' type.  */
8850       tag_type = typename_type;
8851       /* The `typename' keyword is only allowed in templates.  */
8852       if (!processing_template_decl)
8853         pedwarn ("using `typename' outside of template");
8854     }
8855   /* Otherwise it must be a class-key.  */
8856   else
8857     {
8858       tag_type = cp_parser_class_key (parser);
8859       if (tag_type == none_type)
8860         return error_mark_node;
8861     }
8862
8863   /* Look for the `::' operator.  */
8864   cp_parser_global_scope_opt (parser, 
8865                               /*current_scope_valid_p=*/false);
8866   /* Look for the nested-name-specifier.  */
8867   if (tag_type == typename_type)
8868     cp_parser_nested_name_specifier (parser,
8869                                      /*typename_keyword_p=*/true,
8870                                      /*check_dependency_p=*/true,
8871                                      /*type_p=*/true);
8872   else
8873     /* Even though `typename' is not present, the proposed resolution
8874        to Core Issue 180 says that in `class A<T>::B', `B' should be
8875        considered a type-name, even if `A<T>' is dependent.  */
8876     cp_parser_nested_name_specifier_opt (parser,
8877                                          /*typename_keyword_p=*/true,
8878                                          /*check_dependency_p=*/true,
8879                                          /*type_p=*/true);
8880   /* For everything but enumeration types, consider a template-id.  */
8881   if (tag_type != enum_type)
8882     {
8883       bool template_p = false;
8884       tree decl;
8885
8886       /* Allow the `template' keyword.  */
8887       template_p = cp_parser_optional_template_keyword (parser);
8888       /* If we didn't see `template', we don't know if there's a
8889          template-id or not.  */
8890       if (!template_p)
8891         cp_parser_parse_tentatively (parser);
8892       /* Parse the template-id.  */
8893       decl = cp_parser_template_id (parser, template_p,
8894                                     /*check_dependency_p=*/true);
8895       /* If we didn't find a template-id, look for an ordinary
8896          identifier.  */
8897       if (!template_p && !cp_parser_parse_definitely (parser))
8898         ;
8899       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8900          in effect, then we must assume that, upon instantiation, the
8901          template will correspond to a class.  */
8902       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8903                && tag_type == typename_type)
8904         type = make_typename_type (parser->scope, decl,
8905                                    /*complain=*/1);
8906       else 
8907         type = TREE_TYPE (decl);
8908     }
8909
8910   /* For an enumeration type, consider only a plain identifier.  */
8911   if (!type)
8912     {
8913       identifier = cp_parser_identifier (parser);
8914
8915       if (identifier == error_mark_node)
8916         return error_mark_node;
8917
8918       /* For a `typename', we needn't call xref_tag.  */
8919       if (tag_type == typename_type)
8920         return make_typename_type (parser->scope, identifier, 
8921                                    /*complain=*/1);
8922       /* Look up a qualified name in the usual way.  */
8923       if (parser->scope)
8924         {
8925           tree decl;
8926
8927           /* In an elaborated-type-specifier, names are assumed to name
8928              types, so we set IS_TYPE to TRUE when calling
8929              cp_parser_lookup_name.  */
8930           decl = cp_parser_lookup_name (parser, identifier, 
8931                                         /*check_access=*/true,
8932                                         /*is_type=*/true,
8933                                         /*check_dependency=*/true);
8934           decl = (cp_parser_maybe_treat_template_as_class 
8935                   (decl, /*tag_name_p=*/is_friend));
8936
8937           if (TREE_CODE (decl) != TYPE_DECL)
8938             {
8939               error ("expected type-name");
8940               return error_mark_node;
8941             }
8942           else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
8943                    && tag_type != enum_type)
8944             error ("`%T' referred to as `%s'", TREE_TYPE (decl),
8945                    tag_type == record_type ? "struct" : "class");
8946           else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
8947                    && tag_type == enum_type)
8948             error ("`%T' referred to as enum", TREE_TYPE (decl));
8949
8950           type = TREE_TYPE (decl);
8951         }
8952       else 
8953         {
8954           /* An elaborated-type-specifier sometimes introduces a new type and
8955              sometimes names an existing type.  Normally, the rule is that it
8956              introduces a new type only if there is not an existing type of
8957              the same name already in scope.  For example, given:
8958
8959                struct S {};
8960                void f() { struct S s; }
8961
8962              the `struct S' in the body of `f' is the same `struct S' as in
8963              the global scope; the existing definition is used.  However, if
8964              there were no global declaration, this would introduce a new 
8965              local class named `S'.
8966
8967              An exception to this rule applies to the following code:
8968
8969                namespace N { struct S; }
8970
8971              Here, the elaborated-type-specifier names a new type
8972              unconditionally; even if there is already an `S' in the
8973              containing scope this declaration names a new type.
8974              This exception only applies if the elaborated-type-specifier
8975              forms the complete declaration:
8976
8977                [class.name] 
8978
8979                A declaration consisting solely of `class-key identifier ;' is
8980                either a redeclaration of the name in the current scope or a
8981                forward declaration of the identifier as a class name.  It
8982                introduces the name into the current scope.
8983
8984              We are in this situation precisely when the next token is a `;'.
8985
8986              An exception to the exception is that a `friend' declaration does
8987              *not* name a new type; i.e., given:
8988
8989                struct S { friend struct T; };
8990
8991              `T' is not a new type in the scope of `S'.  
8992
8993              Also, `new struct S' or `sizeof (struct S)' never results in the
8994              definition of a new type; a new type can only be declared in a
8995              declaration context.   */
8996
8997           type = xref_tag (tag_type, identifier, 
8998                            /*attributes=*/NULL_TREE,
8999                            (is_friend 
9000                             || !is_declaration
9001                             || cp_lexer_next_token_is_not (parser->lexer, 
9002                                                            CPP_SEMICOLON)));
9003         }
9004     }
9005   if (tag_type != enum_type)
9006     cp_parser_check_class_key (tag_type, type);
9007   return type;
9008 }
9009
9010 /* Parse an enum-specifier.
9011
9012    enum-specifier:
9013      enum identifier [opt] { enumerator-list [opt] }
9014
9015    Returns an ENUM_TYPE representing the enumeration.  */
9016
9017 static tree
9018 cp_parser_enum_specifier (parser)
9019      cp_parser *parser;
9020 {
9021   cp_token *token;
9022   tree identifier = NULL_TREE;
9023   tree type;
9024
9025   /* Look for the `enum' keyword.  */
9026   if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9027     return error_mark_node;
9028   /* Peek at the next token.  */
9029   token = cp_lexer_peek_token (parser->lexer);
9030
9031   /* See if it is an identifier.  */
9032   if (token->type == CPP_NAME)
9033     identifier = cp_parser_identifier (parser);
9034
9035   /* Look for the `{'.  */
9036   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9037     return error_mark_node;
9038
9039   /* At this point, we're going ahead with the enum-specifier, even
9040      if some other problem occurs.  */
9041   cp_parser_commit_to_tentative_parse (parser);
9042
9043   /* Issue an error message if type-definitions are forbidden here.  */
9044   cp_parser_check_type_definition (parser);
9045
9046   /* Create the new type.  */
9047   type = start_enum (identifier ? identifier : make_anon_name ());
9048
9049   /* Peek at the next token.  */
9050   token = cp_lexer_peek_token (parser->lexer);
9051   /* If it's not a `}', then there are some enumerators.  */
9052   if (token->type != CPP_CLOSE_BRACE)
9053     cp_parser_enumerator_list (parser, type);
9054   /* Look for the `}'.  */
9055   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9056
9057   /* Finish up the enumeration.  */
9058   finish_enum (type);
9059
9060   return type;
9061 }
9062
9063 /* Parse an enumerator-list.  The enumerators all have the indicated
9064    TYPE.  
9065
9066    enumerator-list:
9067      enumerator-definition
9068      enumerator-list , enumerator-definition  */
9069
9070 static void
9071 cp_parser_enumerator_list (parser, type)
9072      cp_parser *parser;
9073      tree type;
9074 {
9075   while (true)
9076     {
9077       cp_token *token;
9078
9079       /* Parse an enumerator-definition.  */
9080       cp_parser_enumerator_definition (parser, type);
9081       /* Peek at the next token.  */
9082       token = cp_lexer_peek_token (parser->lexer);
9083       /* If it's not a `,', then we've reached the end of the 
9084          list.  */
9085       if (token->type != CPP_COMMA)
9086         break;
9087       /* Otherwise, consume the `,' and keep going.  */
9088       cp_lexer_consume_token (parser->lexer);
9089       /* If the next token is a `}', there is a trailing comma.  */
9090       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9091         {
9092           if (pedantic && !in_system_header)
9093             pedwarn ("comma at end of enumerator list");
9094           break;
9095         }
9096     }
9097 }
9098
9099 /* Parse an enumerator-definition.  The enumerator has the indicated
9100    TYPE.
9101
9102    enumerator-definition:
9103      enumerator
9104      enumerator = constant-expression
9105     
9106    enumerator:
9107      identifier  */
9108
9109 static void
9110 cp_parser_enumerator_definition (parser, type)
9111      cp_parser *parser;
9112      tree type;
9113 {
9114   cp_token *token;
9115   tree identifier;
9116   tree value;
9117
9118   /* Look for the identifier.  */
9119   identifier = cp_parser_identifier (parser);
9120   if (identifier == error_mark_node)
9121     return;
9122   
9123   /* Peek at the next token.  */
9124   token = cp_lexer_peek_token (parser->lexer);
9125   /* If it's an `=', then there's an explicit value.  */
9126   if (token->type == CPP_EQ)
9127     {
9128       /* Consume the `=' token.  */
9129       cp_lexer_consume_token (parser->lexer);
9130       /* Parse the value.  */
9131       value = cp_parser_constant_expression (parser);
9132     }
9133   else
9134     value = NULL_TREE;
9135
9136   /* Create the enumerator.  */
9137   build_enumerator (identifier, value, type);
9138 }
9139
9140 /* Parse a namespace-name.
9141
9142    namespace-name:
9143      original-namespace-name
9144      namespace-alias
9145
9146    Returns the NAMESPACE_DECL for the namespace.  */
9147
9148 static tree
9149 cp_parser_namespace_name (parser)
9150      cp_parser *parser;
9151 {
9152   tree identifier;
9153   tree namespace_decl;
9154
9155   /* Get the name of the namespace.  */
9156   identifier = cp_parser_identifier (parser);
9157   if (identifier == error_mark_node)
9158     return error_mark_node;
9159
9160   /* Look up the identifier in the currently active scope.  */
9161   namespace_decl = cp_parser_lookup_name_simple (parser, identifier);
9162   /* If it's not a namespace, issue an error.  */
9163   if (namespace_decl == error_mark_node
9164       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9165     {
9166       cp_parser_error (parser, "expected namespace-name");
9167       namespace_decl = error_mark_node;
9168     }
9169   
9170   return namespace_decl;
9171 }
9172
9173 /* Parse a namespace-definition.
9174
9175    namespace-definition:
9176      named-namespace-definition
9177      unnamed-namespace-definition  
9178
9179    named-namespace-definition:
9180      original-namespace-definition
9181      extension-namespace-definition
9182
9183    original-namespace-definition:
9184      namespace identifier { namespace-body }
9185    
9186    extension-namespace-definition:
9187      namespace original-namespace-name { namespace-body }
9188  
9189    unnamed-namespace-definition:
9190      namespace { namespace-body } */
9191
9192 static void
9193 cp_parser_namespace_definition (parser)
9194      cp_parser *parser;
9195 {
9196   tree identifier;
9197
9198   /* Look for the `namespace' keyword.  */
9199   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9200
9201   /* Get the name of the namespace.  We do not attempt to distinguish
9202      between an original-namespace-definition and an
9203      extension-namespace-definition at this point.  The semantic
9204      analysis routines are responsible for that.  */
9205   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9206     identifier = cp_parser_identifier (parser);
9207   else
9208     identifier = NULL_TREE;
9209
9210   /* Look for the `{' to start the namespace.  */
9211   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9212   /* Start the namespace.  */
9213   push_namespace (identifier);
9214   /* Parse the body of the namespace.  */
9215   cp_parser_namespace_body (parser);
9216   /* Finish the namespace.  */
9217   pop_namespace ();
9218   /* Look for the final `}'.  */
9219   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9220 }
9221
9222 /* Parse a namespace-body.
9223
9224    namespace-body:
9225      declaration-seq [opt]  */
9226
9227 static void
9228 cp_parser_namespace_body (parser)
9229      cp_parser *parser;
9230 {
9231   cp_parser_declaration_seq_opt (parser);
9232 }
9233
9234 /* Parse a namespace-alias-definition.
9235
9236    namespace-alias-definition:
9237      namespace identifier = qualified-namespace-specifier ;  */
9238
9239 static void
9240 cp_parser_namespace_alias_definition (parser)
9241      cp_parser *parser;
9242 {
9243   tree identifier;
9244   tree namespace_specifier;
9245
9246   /* Look for the `namespace' keyword.  */
9247   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9248   /* Look for the identifier.  */
9249   identifier = cp_parser_identifier (parser);
9250   if (identifier == error_mark_node)
9251     return;
9252   /* Look for the `=' token.  */
9253   cp_parser_require (parser, CPP_EQ, "`='");
9254   /* Look for the qualified-namespace-specifier.  */
9255   namespace_specifier 
9256     = cp_parser_qualified_namespace_specifier (parser);
9257   /* Look for the `;' token.  */
9258   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9259
9260   /* Register the alias in the symbol table.  */
9261   do_namespace_alias (identifier, namespace_specifier);
9262 }
9263
9264 /* Parse a qualified-namespace-specifier.
9265
9266    qualified-namespace-specifier:
9267      :: [opt] nested-name-specifier [opt] namespace-name
9268
9269    Returns a NAMESPACE_DECL corresponding to the specified
9270    namespace.  */
9271
9272 static tree
9273 cp_parser_qualified_namespace_specifier (parser)
9274      cp_parser *parser;
9275 {
9276   /* Look for the optional `::'.  */
9277   cp_parser_global_scope_opt (parser, 
9278                               /*current_scope_valid_p=*/false);
9279
9280   /* Look for the optional nested-name-specifier.  */
9281   cp_parser_nested_name_specifier_opt (parser,
9282                                        /*typename_keyword_p=*/false,
9283                                        /*check_dependency_p=*/true,
9284                                        /*type_p=*/false);
9285
9286   return cp_parser_namespace_name (parser);
9287 }
9288
9289 /* Parse a using-declaration.
9290
9291    using-declaration:
9292      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9293      using :: unqualified-id ;  */
9294
9295 static void
9296 cp_parser_using_declaration (parser)
9297      cp_parser *parser;
9298 {
9299   cp_token *token;
9300   bool typename_p = false;
9301   bool global_scope_p;
9302   tree decl;
9303   tree identifier;
9304   tree scope;
9305
9306   /* Look for the `using' keyword.  */
9307   cp_parser_require_keyword (parser, RID_USING, "`using'");
9308   
9309   /* Peek at the next token.  */
9310   token = cp_lexer_peek_token (parser->lexer);
9311   /* See if it's `typename'.  */
9312   if (token->keyword == RID_TYPENAME)
9313     {
9314       /* Remember that we've seen it.  */
9315       typename_p = true;
9316       /* Consume the `typename' token.  */
9317       cp_lexer_consume_token (parser->lexer);
9318     }
9319
9320   /* Look for the optional global scope qualification.  */
9321   global_scope_p 
9322     = (cp_parser_global_scope_opt (parser,
9323                                    /*current_scope_valid_p=*/false) 
9324        != NULL_TREE);
9325
9326   /* If we saw `typename', or didn't see `::', then there must be a
9327      nested-name-specifier present.  */
9328   if (typename_p || !global_scope_p)
9329     cp_parser_nested_name_specifier (parser, typename_p, 
9330                                      /*check_dependency_p=*/true,
9331                                      /*type_p=*/false);
9332   /* Otherwise, we could be in either of the two productions.  In that
9333      case, treat the nested-name-specifier as optional.  */
9334   else
9335     cp_parser_nested_name_specifier_opt (parser,
9336                                          /*typename_keyword_p=*/false,
9337                                          /*check_dependency_p=*/true,
9338                                          /*type_p=*/false);
9339
9340   /* Parse the unqualified-id.  */
9341   identifier = cp_parser_unqualified_id (parser, 
9342                                          /*template_keyword_p=*/false,
9343                                          /*check_dependency_p=*/true);
9344
9345   /* The function we call to handle a using-declaration is different
9346      depending on what scope we are in.  */
9347   scope = current_scope ();
9348   if (scope && TYPE_P (scope))
9349     {
9350       /* Create the USING_DECL.  */
9351       decl = do_class_using_decl (build_nt (SCOPE_REF,
9352                                             parser->scope,
9353                                             identifier));
9354       /* Add it to the list of members in this class.  */
9355       finish_member_declaration (decl);
9356     }
9357   else
9358     {
9359       decl = cp_parser_lookup_name_simple (parser, identifier);
9360       if (scope)
9361         do_local_using_decl (decl);
9362       else
9363         do_toplevel_using_decl (decl);
9364     }
9365
9366   /* Look for the final `;'.  */
9367   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9368 }
9369
9370 /* Parse a using-directive.  
9371  
9372    using-directive:
9373      using namespace :: [opt] nested-name-specifier [opt]
9374        namespace-name ;  */
9375
9376 static void
9377 cp_parser_using_directive (parser)
9378      cp_parser *parser;
9379 {
9380   tree namespace_decl;
9381
9382   /* Look for the `using' keyword.  */
9383   cp_parser_require_keyword (parser, RID_USING, "`using'");
9384   /* And the `namespace' keyword.  */
9385   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9386   /* Look for the optional `::' operator.  */
9387   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9388   /* And the optional nested-name-sepcifier.  */
9389   cp_parser_nested_name_specifier_opt (parser,
9390                                        /*typename_keyword_p=*/false,
9391                                        /*check_dependency_p=*/true,
9392                                        /*type_p=*/false);
9393   /* Get the namespace being used.  */
9394   namespace_decl = cp_parser_namespace_name (parser);
9395   /* Update the symbol table.  */
9396   do_using_directive (namespace_decl);
9397   /* Look for the final `;'.  */
9398   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9399 }
9400
9401 /* Parse an asm-definition.
9402
9403    asm-definition:
9404      asm ( string-literal ) ;  
9405
9406    GNU Extension:
9407
9408    asm-definition:
9409      asm volatile [opt] ( string-literal ) ;
9410      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9411      asm volatile [opt] ( string-literal : asm-operand-list [opt]
9412                           : asm-operand-list [opt] ) ;
9413      asm volatile [opt] ( string-literal : asm-operand-list [opt] 
9414                           : asm-operand-list [opt] 
9415                           : asm-operand-list [opt] ) ;  */
9416
9417 static void
9418 cp_parser_asm_definition (parser)
9419      cp_parser *parser;
9420 {
9421   cp_token *token;
9422   tree string;
9423   tree outputs = NULL_TREE;
9424   tree inputs = NULL_TREE;
9425   tree clobbers = NULL_TREE;
9426   tree asm_stmt;
9427   bool volatile_p = false;
9428   bool extended_p = false;
9429
9430   /* Look for the `asm' keyword.  */
9431   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9432   /* See if the next token is `volatile'.  */
9433   if (cp_parser_allow_gnu_extensions_p (parser)
9434       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9435     {
9436       /* Remember that we saw the `volatile' keyword.  */
9437       volatile_p = true;
9438       /* Consume the token.  */
9439       cp_lexer_consume_token (parser->lexer);
9440     }
9441   /* Look for the opening `('.  */
9442   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9443   /* Look for the string.  */
9444   token = cp_parser_require (parser, CPP_STRING, "asm body");
9445   if (!token)
9446     return;
9447   string = token->value;
9448   /* If we're allowing GNU extensions, check for the extended assembly
9449      syntax.  Unfortunately, the `:' tokens need not be separated by 
9450      a space in C, and so, for compatibility, we tolerate that here
9451      too.  Doing that means that we have to treat the `::' operator as
9452      two `:' tokens.  */
9453   if (cp_parser_allow_gnu_extensions_p (parser)
9454       && at_function_scope_p ()
9455       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9456           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9457     {
9458       bool inputs_p = false;
9459       bool clobbers_p = false;
9460
9461       /* The extended syntax was used.  */
9462       extended_p = true;
9463
9464       /* Look for outputs.  */
9465       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9466         {
9467           /* Consume the `:'.  */
9468           cp_lexer_consume_token (parser->lexer);
9469           /* Parse the output-operands.  */
9470           if (cp_lexer_next_token_is_not (parser->lexer, 
9471                                           CPP_COLON)
9472               && cp_lexer_next_token_is_not (parser->lexer,
9473                                              CPP_SCOPE))
9474             outputs = cp_parser_asm_operand_list (parser);
9475         }
9476       /* If the next token is `::', there are no outputs, and the
9477          next token is the beginning of the inputs.  */
9478       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9479         {
9480           /* Consume the `::' token.  */
9481           cp_lexer_consume_token (parser->lexer);
9482           /* The inputs are coming next.  */
9483           inputs_p = true;
9484         }
9485
9486       /* Look for inputs.  */
9487       if (inputs_p
9488           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9489         {
9490           if (!inputs_p)
9491             /* Consume the `:'.  */
9492             cp_lexer_consume_token (parser->lexer);
9493           /* Parse the output-operands.  */
9494           if (cp_lexer_next_token_is_not (parser->lexer, 
9495                                           CPP_COLON)
9496               && cp_lexer_next_token_is_not (parser->lexer,
9497                                              CPP_SCOPE))
9498             inputs = cp_parser_asm_operand_list (parser);
9499         }
9500       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9501         /* The clobbers are coming next.  */
9502         clobbers_p = true;
9503
9504       /* Look for clobbers.  */
9505       if (clobbers_p 
9506           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9507         {
9508           if (!clobbers_p)
9509             /* Consume the `:'.  */
9510             cp_lexer_consume_token (parser->lexer);
9511           /* Parse the clobbers.  */
9512           clobbers = cp_parser_asm_clobber_list (parser);
9513         }
9514     }
9515   /* Look for the closing `)'.  */
9516   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9517     cp_parser_skip_to_closing_parenthesis (parser);
9518   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9519
9520   /* Create the ASM_STMT.  */
9521   if (at_function_scope_p ())
9522     {
9523       asm_stmt = 
9524         finish_asm_stmt (volatile_p 
9525                          ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9526                          string, outputs, inputs, clobbers);
9527       /* If the extended syntax was not used, mark the ASM_STMT.  */
9528       if (!extended_p)
9529         ASM_INPUT_P (asm_stmt) = 1;
9530     }
9531   else
9532     assemble_asm (string);
9533 }
9534
9535 /* Declarators [gram.dcl.decl] */
9536
9537 /* Parse an init-declarator.
9538
9539    init-declarator:
9540      declarator initializer [opt]
9541
9542    GNU Extension:
9543
9544    init-declarator:
9545      declarator asm-specification [opt] attributes [opt] initializer [opt]
9546
9547    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9548    Returns a reprsentation of the entity declared.  The ACCESS_CHECKS
9549    represent deferred access checks from the decl-specifier-seq.  If
9550    MEMBER_P is TRUE, then this declarator appears in a class scope.
9551    The new DECL created by this declarator is returned.
9552
9553    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9554    for a function-definition here as well.  If the declarator is a
9555    declarator for a function-definition, *FUNCTION_DEFINITION_P will
9556    be TRUE upon return.  By that point, the function-definition will
9557    have been completely parsed.
9558
9559    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9560    is FALSE.  */
9561
9562 static tree
9563 cp_parser_init_declarator (parser, 
9564                            decl_specifiers, 
9565                            prefix_attributes,
9566                            access_checks,
9567                            function_definition_allowed_p,
9568                            member_p,
9569                            function_definition_p)
9570      cp_parser *parser;
9571      tree decl_specifiers;
9572      tree prefix_attributes;
9573      tree access_checks;
9574      bool function_definition_allowed_p;
9575      bool member_p;
9576      bool *function_definition_p;
9577 {
9578   cp_token *token;
9579   tree declarator;
9580   tree attributes;
9581   tree asm_specification;
9582   tree initializer;
9583   tree decl = NULL_TREE;
9584   tree scope;
9585   tree declarator_access_checks;
9586   bool is_initialized;
9587   bool is_parenthesized_init;
9588   bool ctor_dtor_or_conv_p;
9589   bool friend_p;
9590
9591   /* Assume that this is not the declarator for a function
9592      definition.  */
9593   if (function_definition_p)
9594     *function_definition_p = false;
9595
9596   /* Defer access checks while parsing the declarator; we cannot know
9597      what names are accessible until we know what is being 
9598      declared.  */
9599   cp_parser_start_deferring_access_checks (parser);
9600   /* Parse the declarator.  */
9601   declarator 
9602     = cp_parser_declarator (parser,
9603                             /*abstract_p=*/false,
9604                             &ctor_dtor_or_conv_p);
9605   /* Gather up the deferred checks.  */
9606   declarator_access_checks 
9607     = cp_parser_stop_deferring_access_checks (parser);
9608
9609   /* If the DECLARATOR was erroneous, there's no need to go
9610      further.  */
9611   if (declarator == error_mark_node)
9612     return error_mark_node;
9613
9614   /* Figure out what scope the entity declared by the DECLARATOR is
9615      located in.  `grokdeclarator' sometimes changes the scope, so
9616      we compute it now.  */
9617   scope = get_scope_of_declarator (declarator);
9618
9619   /* If we're allowing GNU extensions, look for an asm-specification
9620      and attributes.  */
9621   if (cp_parser_allow_gnu_extensions_p (parser))
9622     {
9623       /* Look for an asm-specification.  */
9624       asm_specification = cp_parser_asm_specification_opt (parser);
9625       /* And attributes.  */
9626       attributes = cp_parser_attributes_opt (parser);
9627     }
9628   else
9629     {
9630       asm_specification = NULL_TREE;
9631       attributes = NULL_TREE;
9632     }
9633
9634   /* Peek at the next token.  */
9635   token = cp_lexer_peek_token (parser->lexer);
9636   /* Check to see if the token indicates the start of a
9637      function-definition.  */
9638   if (cp_parser_token_starts_function_definition_p (token))
9639     {
9640       if (!function_definition_allowed_p)
9641         {
9642           /* If a function-definition should not appear here, issue an
9643              error message.  */
9644           cp_parser_error (parser,
9645                            "a function-definition is not allowed here");
9646           return error_mark_node;
9647         }
9648       else
9649         {
9650           tree *ac;
9651
9652           /* Neither attributes nor an asm-specification are allowed
9653              on a function-definition.  */
9654           if (asm_specification)
9655             error ("an asm-specification is not allowed on a function-definition");
9656           if (attributes)
9657             error ("attributes are not allowed on a function-definition");
9658           /* This is a function-definition.  */
9659           *function_definition_p = true;
9660
9661           /* Thread the access checks together.  */
9662           ac = &access_checks;
9663           while (*ac)
9664             ac = &TREE_CHAIN (*ac);
9665           *ac = declarator_access_checks;
9666
9667           /* Parse the function definition.  */
9668           decl = (cp_parser_function_definition_from_specifiers_and_declarator
9669                   (parser, decl_specifiers, prefix_attributes, declarator,
9670                    access_checks));
9671
9672           /* Pull the access-checks apart again.  */
9673           *ac = NULL_TREE;
9674
9675           return decl;
9676         }
9677     }
9678
9679   /* [dcl.dcl]
9680
9681      Only in function declarations for constructors, destructors, and
9682      type conversions can the decl-specifier-seq be omitted.  
9683
9684      We explicitly postpone this check past the point where we handle
9685      function-definitions because we tolerate function-definitions
9686      that are missing their return types in some modes.  */
9687   if (!decl_specifiers && !ctor_dtor_or_conv_p)
9688     {
9689       cp_parser_error (parser, 
9690                        "expected constructor, destructor, or type conversion");
9691       return error_mark_node;
9692     }
9693
9694   /* An `=' or an `(' indicates an initializer.  */
9695   is_initialized = (token->type == CPP_EQ 
9696                      || token->type == CPP_OPEN_PAREN);
9697   /* If the init-declarator isn't initialized and isn't followed by a
9698      `,' or `;', it's not a valid init-declarator.  */
9699   if (!is_initialized 
9700       && token->type != CPP_COMMA
9701       && token->type != CPP_SEMICOLON)
9702     {
9703       cp_parser_error (parser, "expected init-declarator");
9704       return error_mark_node;
9705     }
9706
9707   /* Because start_decl has side-effects, we should only call it if we
9708      know we're going ahead.  By this point, we know that we cannot
9709      possibly be looking at any other construct.  */
9710   cp_parser_commit_to_tentative_parse (parser);
9711
9712   /* Check to see whether or not this declaration is a friend.  */
9713   friend_p = cp_parser_friend_p (decl_specifiers);
9714
9715   /* Check that the number of template-parameter-lists is OK.  */
9716   if (!cp_parser_check_declarator_template_parameters (parser, 
9717                                                        declarator))
9718     return error_mark_node;
9719
9720   /* Enter the newly declared entry in the symbol table.  If we're
9721      processing a declaration in a class-specifier, we wait until
9722      after processing the initializer.  */
9723   if (!member_p)
9724     {
9725       if (parser->in_unbraced_linkage_specification_p)
9726         {
9727           decl_specifiers = tree_cons (error_mark_node,
9728                                        get_identifier ("extern"),
9729                                        decl_specifiers);
9730           have_extern_spec = false;
9731         }
9732       decl = start_decl (declarator,
9733                          decl_specifiers,
9734                          is_initialized,
9735                          attributes,
9736                          prefix_attributes);
9737     }
9738
9739   /* Enter the SCOPE.  That way unqualified names appearing in the
9740      initializer will be looked up in SCOPE.  */
9741   if (scope)
9742     push_scope (scope);
9743
9744   /* Perform deferred access control checks, now that we know in which
9745      SCOPE the declared entity resides.  */
9746   if (!member_p && decl) 
9747     {
9748       tree saved_current_function_decl = NULL_TREE;
9749
9750       /* If the entity being declared is a function, pretend that we
9751          are in its scope.  If it is a `friend', it may have access to
9752          things that would not otherwise be accessible. */
9753       if (TREE_CODE (decl) == FUNCTION_DECL)
9754         {
9755           saved_current_function_decl = current_function_decl;
9756           current_function_decl = decl;
9757         }
9758         
9759       /* Perform the access control checks for the decl-specifiers.  */
9760       cp_parser_perform_deferred_access_checks (access_checks);
9761       /* And for the declarator.  */
9762       cp_parser_perform_deferred_access_checks (declarator_access_checks);
9763
9764       /* Restore the saved value.  */
9765       if (TREE_CODE (decl) == FUNCTION_DECL)
9766         current_function_decl = saved_current_function_decl;
9767     }
9768
9769   /* Parse the initializer.  */
9770   if (is_initialized)
9771     initializer = cp_parser_initializer (parser, 
9772                                          &is_parenthesized_init);
9773   else
9774     {
9775       initializer = NULL_TREE;
9776       is_parenthesized_init = false;
9777     }
9778
9779   /* The old parser allows attributes to appear after a parenthesized
9780      initializer.  Mark Mitchell proposed removing this functionality
9781      on the GCC mailing lists on 2002-08-13.  This parser accepts the
9782      attributes -- but ignores them.  */
9783   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9784     if (cp_parser_attributes_opt (parser))
9785       warning ("attributes after parenthesized initializer ignored");
9786
9787   /* Leave the SCOPE, now that we have processed the initializer.  It
9788      is important to do this before calling cp_finish_decl because it
9789      makes decisions about whether to create DECL_STMTs or not based
9790      on the current scope.  */
9791   if (scope)
9792     pop_scope (scope);
9793
9794   /* For an in-class declaration, use `grokfield' to create the
9795      declaration.  */
9796   if (member_p)
9797     decl = grokfield (declarator, decl_specifiers,
9798                       initializer, /*asmspec=*/NULL_TREE,
9799                         /*attributes=*/NULL_TREE);
9800
9801   /* Finish processing the declaration.  But, skip friend
9802      declarations.  */
9803   if (!friend_p && decl)
9804     cp_finish_decl (decl, 
9805                     initializer, 
9806                     asm_specification,
9807                     /* If the initializer is in parentheses, then this is
9808                        a direct-initialization, which means that an
9809                        `explicit' constructor is OK.  Otherwise, an
9810                        `explicit' constructor cannot be used.  */
9811                     ((is_parenthesized_init || !is_initialized)
9812                      ? 0 : LOOKUP_ONLYCONVERTING));
9813
9814   return decl;
9815 }
9816
9817 /* Parse a declarator.
9818    
9819    declarator:
9820      direct-declarator
9821      ptr-operator declarator  
9822
9823    abstract-declarator:
9824      ptr-operator abstract-declarator [opt]
9825      direct-abstract-declarator
9826
9827    GNU Extensions:
9828
9829    declarator:
9830      attributes [opt] direct-declarator
9831      attributes [opt] ptr-operator declarator  
9832
9833    abstract-declarator:
9834      attributes [opt] ptr-operator abstract-declarator [opt]
9835      attributes [opt] direct-abstract-declarator
9836      
9837    Returns a representation of the declarator.  If the declarator has
9838    the form `* declarator', then an INDIRECT_REF is returned, whose
9839    only operand is the sub-declarator.  Analagously, `& declarator' is
9840    represented as an ADDR_EXPR.  For `X::* declarator', a SCOPE_REF is
9841    used.  The first operand is the TYPE for `X'.  The second operand
9842    is an INDIRECT_REF whose operand is the sub-declarator.
9843
9844    Otherwise, the reprsentation is as for a direct-declarator.
9845
9846    (It would be better to define a structure type to represent
9847    declarators, rather than abusing `tree' nodes to represent
9848    declarators.  That would be much clearer and save some memory.
9849    There is no reason for declarators to be garbage-collected, for
9850    example; they are created during parser and no longer needed after
9851    `grokdeclarator' has been called.)
9852
9853    For a ptr-operator that has the optional cv-qualifier-seq,
9854    cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9855    node.
9856
9857    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9858    true if this declarator represents a constructor, destructor, or
9859    type conversion operator.  Otherwise, it is set to false.  
9860
9861    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9862    a decl-specifier-seq unless it declares a constructor, destructor,
9863    or conversion.  It might seem that we could check this condition in
9864    semantic analysis, rather than parsing, but that makes it difficult
9865    to handle something like `f()'.  We want to notice that there are
9866    no decl-specifiers, and therefore realize that this is an
9867    expression, not a declaration.)  */
9868
9869 static tree
9870 cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
9871      cp_parser *parser;
9872      bool abstract_p;
9873      bool *ctor_dtor_or_conv_p;
9874 {
9875   cp_token *token;
9876   tree declarator;
9877   enum tree_code code;
9878   tree cv_qualifier_seq;
9879   tree class_type;
9880   tree attributes = NULL_TREE;
9881
9882   /* Assume this is not a constructor, destructor, or type-conversion
9883      operator.  */
9884   if (ctor_dtor_or_conv_p)
9885     *ctor_dtor_or_conv_p = false;
9886
9887   if (cp_parser_allow_gnu_extensions_p (parser))
9888     attributes = cp_parser_attributes_opt (parser);
9889   
9890   /* Peek at the next token.  */
9891   token = cp_lexer_peek_token (parser->lexer);
9892   
9893   /* Check for the ptr-operator production.  */
9894   cp_parser_parse_tentatively (parser);
9895   /* Parse the ptr-operator.  */
9896   code = cp_parser_ptr_operator (parser, 
9897                                  &class_type, 
9898                                  &cv_qualifier_seq);
9899   /* If that worked, then we have a ptr-operator.  */
9900   if (cp_parser_parse_definitely (parser))
9901     {
9902       /* The dependent declarator is optional if we are parsing an
9903          abstract-declarator.  */
9904       if (abstract_p)
9905         cp_parser_parse_tentatively (parser);
9906
9907       /* Parse the dependent declarator.  */
9908       declarator = cp_parser_declarator (parser, abstract_p,
9909                                          /*ctor_dtor_or_conv_p=*/NULL);
9910
9911       /* If we are parsing an abstract-declarator, we must handle the
9912          case where the dependent declarator is absent.  */
9913       if (abstract_p && !cp_parser_parse_definitely (parser))
9914         declarator = NULL_TREE;
9915         
9916       /* Build the representation of the ptr-operator.  */
9917       if (code == INDIRECT_REF)
9918         declarator = make_pointer_declarator (cv_qualifier_seq, 
9919                                               declarator);
9920       else
9921         declarator = make_reference_declarator (cv_qualifier_seq,
9922                                                 declarator);
9923       /* Handle the pointer-to-member case.  */
9924       if (class_type)
9925         declarator = build_nt (SCOPE_REF, class_type, declarator);
9926     }
9927   /* Everything else is a direct-declarator.  */
9928   else
9929     declarator = cp_parser_direct_declarator (parser, 
9930                                               abstract_p,
9931                                               ctor_dtor_or_conv_p);
9932
9933   if (attributes && declarator != error_mark_node)
9934     declarator = tree_cons (attributes, declarator, NULL_TREE);
9935   
9936   return declarator;
9937 }
9938
9939 /* Parse a direct-declarator or direct-abstract-declarator.
9940
9941    direct-declarator:
9942      declarator-id
9943      direct-declarator ( parameter-declaration-clause )
9944        cv-qualifier-seq [opt] 
9945        exception-specification [opt]
9946      direct-declarator [ constant-expression [opt] ]
9947      ( declarator )  
9948
9949    direct-abstract-declarator:
9950      direct-abstract-declarator [opt]
9951        ( parameter-declaration-clause ) 
9952        cv-qualifier-seq [opt]
9953        exception-specification [opt]
9954      direct-abstract-declarator [opt] [ constant-expression [opt] ]
9955      ( abstract-declarator )
9956
9957    Returns a representation of the declarator.  ABSTRACT_P is TRUE if
9958    we are parsing a direct-abstract-declarator; FALSE if we are
9959    parsing a direct-declarator.  CTOR_DTOR_OR_CONV_P is as for 
9960    cp_parser_declarator.
9961
9962    For the declarator-id production, the representation is as for an
9963    id-expression, except that a qualified name is represented as a
9964    SCOPE_REF.  A function-declarator is represented as a CALL_EXPR;
9965    see the documentation of the FUNCTION_DECLARATOR_* macros for
9966    information about how to find the various declarator components.
9967    An array-declarator is represented as an ARRAY_REF.  The
9968    direct-declarator is the first operand; the constant-expression
9969    indicating the size of the array is the second operand.  */
9970
9971 static tree
9972 cp_parser_direct_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
9973      cp_parser *parser;
9974      bool abstract_p;
9975      bool *ctor_dtor_or_conv_p;
9976 {
9977   cp_token *token;
9978   tree declarator;
9979   tree scope = NULL_TREE;
9980   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9981   bool saved_in_declarator_p = parser->in_declarator_p;
9982
9983   /* Peek at the next token.  */
9984   token = cp_lexer_peek_token (parser->lexer);
9985   /* Find the initial direct-declarator.  It might be a parenthesized
9986      declarator.  */
9987   if (token->type == CPP_OPEN_PAREN)
9988     {
9989       /* For an abstract declarator we do not know whether we are
9990          looking at the beginning of a parameter-declaration-clause,
9991          or at a parenthesized abstract declarator.  For example, if
9992          we see `(int)', we are looking at a
9993          parameter-declaration-clause, and the
9994          direct-abstract-declarator has been omitted.  If, on the
9995          other hand we are looking at `((*))' then we are looking at a
9996          parenthesized abstract-declarator.  There is no easy way to
9997          tell which situation we are in.  */
9998       if (abstract_p)
9999         cp_parser_parse_tentatively (parser);
10000
10001       /* Consume the `('.  */
10002       cp_lexer_consume_token (parser->lexer);
10003       /* Parse the nested declarator.  */
10004       declarator 
10005         = cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p);
10006       /* Expect a `)'.  */
10007       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10008
10009       /* If parsing a parenthesized abstract declarator didn't work,
10010          try a parameter-declaration-clause.  */
10011       if (abstract_p && !cp_parser_parse_definitely (parser))
10012         declarator = NULL_TREE;
10013       /* If we were not parsing an abstract declarator, but failed to
10014          find a satisfactory nested declarator, then an error has
10015          occurred.  */
10016       else if (!abstract_p && declarator == error_mark_node)
10017         return error_mark_node;
10018       /* Default args cannot appear in an abstract decl.  */
10019       parser->default_arg_ok_p = false;
10020     }
10021   /* Otherwise, for a non-abstract declarator, there should be a
10022      declarator-id.  */
10023   else if (!abstract_p)
10024     {
10025       declarator = cp_parser_declarator_id (parser);
10026       
10027       if (TREE_CODE (declarator) == SCOPE_REF)
10028         {
10029           scope = TREE_OPERAND (declarator, 0);
10030           
10031           /* In the declaration of a member of a template class
10032              outside of the class itself, the SCOPE will sometimes be
10033              a TYPENAME_TYPE.  For example, given:
10034              
10035                template <typename T>
10036                int S<T>::R::i = 3;
10037
10038              the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In this
10039              context, we must resolve S<T>::R to an ordinary type,
10040              rather than a typename type.
10041
10042              The reason we normally avoid resolving TYPENAME_TYPEs is
10043              that a specialization of `S' might render `S<T>::R' not a
10044              type.  However, if `S' is specialized, then this `i' will
10045              not be used, so there is no harm in resolving the types
10046              here.  */
10047           if (TREE_CODE (scope) == TYPENAME_TYPE)
10048             {
10049               /* Resolve the TYPENAME_TYPE.  */
10050               scope = cp_parser_resolve_typename_type (parser, scope);
10051               /* If that failed, the declarator is invalid.  */
10052               if (scope == error_mark_node)
10053                 return error_mark_node;
10054               /* Build a new DECLARATOR.  */
10055               declarator = build_nt (SCOPE_REF, 
10056                                      scope,
10057                                      TREE_OPERAND (declarator, 1));
10058             }
10059         }
10060       else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10061         /* Default args can only appear for a function decl.  */
10062         parser->default_arg_ok_p = false;
10063       
10064       /* Check to see whether the declarator-id names a constructor, 
10065          destructor, or conversion.  */
10066       if (ctor_dtor_or_conv_p 
10067           && ((TREE_CODE (declarator) == SCOPE_REF 
10068                && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10069               || (TREE_CODE (declarator) != SCOPE_REF
10070                   && at_class_scope_p ())))
10071         {
10072           tree unqualified_name;
10073           tree class_type;
10074
10075           /* Get the unqualified part of the name.  */
10076           if (TREE_CODE (declarator) == SCOPE_REF)
10077             {
10078               class_type = TREE_OPERAND (declarator, 0);
10079               unqualified_name = TREE_OPERAND (declarator, 1);
10080             }
10081           else
10082             {
10083               class_type = current_class_type;
10084               unqualified_name = declarator;
10085             }
10086
10087           /* See if it names ctor, dtor or conv.  */
10088           if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10089               || IDENTIFIER_TYPENAME_P (unqualified_name)
10090               || constructor_name_p (unqualified_name, class_type))
10091             {
10092               *ctor_dtor_or_conv_p = true;
10093               /* We would have cleared the default arg flag above, but
10094                  they are ok.  */
10095               parser->default_arg_ok_p = saved_default_arg_ok_p;
10096             }
10097         }
10098     }
10099   /* But for an abstract declarator, the initial direct-declarator can
10100      be omitted.  */
10101   else
10102     {
10103       declarator = NULL_TREE;
10104       parser->default_arg_ok_p = false;
10105     }
10106
10107   scope = get_scope_of_declarator (declarator);
10108   if (scope)
10109     /* Any names that appear after the declarator-id for a member
10110        are looked up in the containing scope.  */
10111     push_scope (scope);
10112   else
10113     scope = NULL_TREE;
10114   parser->in_declarator_p = true;
10115
10116   /* Now, parse function-declarators and array-declarators until there
10117      are no more.  */
10118   while (true)
10119     {
10120       /* Peek at the next token.  */
10121       token = cp_lexer_peek_token (parser->lexer);
10122       /* If it's a `[', we're looking at an array-declarator.  */
10123       if (token->type == CPP_OPEN_SQUARE)
10124         {
10125           tree bounds;
10126
10127           /* Consume the `['.  */
10128           cp_lexer_consume_token (parser->lexer);
10129           /* Peek at the next token.  */
10130           token = cp_lexer_peek_token (parser->lexer);
10131           /* If the next token is `]', then there is no
10132              constant-expression.  */
10133           if (token->type != CPP_CLOSE_SQUARE)
10134             bounds = cp_parser_constant_expression (parser);
10135           else
10136             bounds = NULL_TREE;
10137           /* Look for the closing `]'.  */
10138           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
10139
10140           declarator = build_nt (ARRAY_REF, declarator, bounds);
10141         }
10142       /* If it's a `(', we're looking at a function-declarator.  */
10143       else if (token->type == CPP_OPEN_PAREN)
10144         {
10145           /* A function-declarator.  Or maybe not.  Consider, for
10146              example:
10147
10148                int i (int);
10149                int i (3);
10150
10151              The first is the declaration of a function while the
10152              second is a the definition of a variable, including its
10153              initializer.
10154
10155              Having seen only the parenthesis, we cannot know which of
10156              these two alternatives should be selected.  Even more
10157              complex are examples like:
10158
10159                int i (int (a));
10160                int i (int (3));
10161
10162              The former is a function-declaration; the latter is a
10163              variable initialization.  
10164
10165              First, we attempt to parse a parameter-declaration
10166              clause.  If this works, then we continue; otherwise, we
10167              replace the tokens consumed in the process and continue.  */
10168           tree params;
10169
10170           /* We are now parsing tentatively.  */
10171           cp_parser_parse_tentatively (parser);
10172           
10173           /* Consume the `('.  */
10174           cp_lexer_consume_token (parser->lexer);
10175           /* Parse the parameter-declaration-clause.  */
10176           params = cp_parser_parameter_declaration_clause (parser);
10177           
10178           /* If all went well, parse the cv-qualifier-seq and the
10179              exception-specification.  */
10180           if (cp_parser_parse_definitely (parser))
10181             {
10182               tree cv_qualifiers;
10183               tree exception_specification;
10184
10185               /* Consume the `)'.  */
10186               cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10187
10188               /* Parse the cv-qualifier-seq.  */
10189               cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10190               /* And the exception-specification.  */
10191               exception_specification 
10192                 = cp_parser_exception_specification_opt (parser);
10193
10194               /* Create the function-declarator.  */
10195               declarator = make_call_declarator (declarator,
10196                                                  params,
10197                                                  cv_qualifiers,
10198                                                  exception_specification);
10199             }
10200           /* Otherwise, we must be done with the declarator.  */
10201           else
10202             break;
10203         }
10204       /* Otherwise, we're done with the declarator.  */
10205       else
10206         break;
10207       /* Any subsequent parameter lists are to do with return type, so
10208          are not those of the declared function.  */
10209       parser->default_arg_ok_p = false;
10210     }
10211
10212   /* For an abstract declarator, we might wind up with nothing at this
10213      point.  That's an error; the declarator is not optional.  */
10214   if (!declarator)
10215     cp_parser_error (parser, "expected declarator");
10216
10217   /* If we entered a scope, we must exit it now.  */
10218   if (scope)
10219     pop_scope (scope);
10220
10221   parser->default_arg_ok_p = saved_default_arg_ok_p;
10222   parser->in_declarator_p = saved_in_declarator_p;
10223   
10224   return declarator;
10225 }
10226
10227 /* Parse a ptr-operator.  
10228
10229    ptr-operator:
10230      * cv-qualifier-seq [opt]
10231      &
10232      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10233
10234    GNU Extension:
10235
10236    ptr-operator:
10237      & cv-qualifier-seq [opt]
10238
10239    Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10240    used.  Returns ADDR_EXPR if a reference was used.  In the
10241    case of a pointer-to-member, *TYPE is filled in with the 
10242    TYPE containing the member.  *CV_QUALIFIER_SEQ is filled in
10243    with the cv-qualifier-seq, or NULL_TREE, if there are no
10244    cv-qualifiers.  Returns ERROR_MARK if an error occurred.  */
10245    
10246 static enum tree_code
10247 cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10248      cp_parser *parser;
10249      tree *type;
10250      tree *cv_qualifier_seq;
10251 {
10252   enum tree_code code = ERROR_MARK;
10253   cp_token *token;
10254
10255   /* Assume that it's not a pointer-to-member.  */
10256   *type = NULL_TREE;
10257   /* And that there are no cv-qualifiers.  */
10258   *cv_qualifier_seq = NULL_TREE;
10259
10260   /* Peek at the next token.  */
10261   token = cp_lexer_peek_token (parser->lexer);
10262   /* If it's a `*' or `&' we have a pointer or reference.  */
10263   if (token->type == CPP_MULT || token->type == CPP_AND)
10264     {
10265       /* Remember which ptr-operator we were processing.  */
10266       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10267
10268       /* Consume the `*' or `&'.  */
10269       cp_lexer_consume_token (parser->lexer);
10270
10271       /* A `*' can be followed by a cv-qualifier-seq, and so can a
10272          `&', if we are allowing GNU extensions.  (The only qualifier
10273          that can legally appear after `&' is `restrict', but that is
10274          enforced during semantic analysis.  */
10275       if (code == INDIRECT_REF 
10276           || cp_parser_allow_gnu_extensions_p (parser))
10277         *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10278     }
10279   else
10280     {
10281       /* Try the pointer-to-member case.  */
10282       cp_parser_parse_tentatively (parser);
10283       /* Look for the optional `::' operator.  */
10284       cp_parser_global_scope_opt (parser,
10285                                   /*current_scope_valid_p=*/false);
10286       /* Look for the nested-name specifier.  */
10287       cp_parser_nested_name_specifier (parser,
10288                                        /*typename_keyword_p=*/false,
10289                                        /*check_dependency_p=*/true,
10290                                        /*type_p=*/false);
10291       /* If we found it, and the next token is a `*', then we are
10292          indeed looking at a pointer-to-member operator.  */
10293       if (!cp_parser_error_occurred (parser)
10294           && cp_parser_require (parser, CPP_MULT, "`*'"))
10295         {
10296           /* The type of which the member is a member is given by the
10297              current SCOPE.  */
10298           *type = parser->scope;
10299           /* The next name will not be qualified.  */
10300           parser->scope = NULL_TREE;
10301           parser->qualifying_scope = NULL_TREE;
10302           parser->object_scope = NULL_TREE;
10303           /* Indicate that the `*' operator was used.  */
10304           code = INDIRECT_REF;
10305           /* Look for the optional cv-qualifier-seq.  */
10306           *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10307         }
10308       /* If that didn't work we don't have a ptr-operator.  */
10309       if (!cp_parser_parse_definitely (parser))
10310         cp_parser_error (parser, "expected ptr-operator");
10311     }
10312
10313   return code;
10314 }
10315
10316 /* Parse an (optional) cv-qualifier-seq.
10317
10318    cv-qualifier-seq:
10319      cv-qualifier cv-qualifier-seq [opt]  
10320
10321    Returns a TREE_LIST.  The TREE_VALUE of each node is the
10322    representation of a cv-qualifier.  */
10323
10324 static tree
10325 cp_parser_cv_qualifier_seq_opt (parser)
10326      cp_parser *parser;
10327 {
10328   tree cv_qualifiers = NULL_TREE;
10329   
10330   while (true)
10331     {
10332       tree cv_qualifier;
10333
10334       /* Look for the next cv-qualifier.  */
10335       cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10336       /* If we didn't find one, we're done.  */
10337       if (!cv_qualifier)
10338         break;
10339
10340       /* Add this cv-qualifier to the list.  */
10341       cv_qualifiers 
10342         = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10343     }
10344
10345   /* We built up the list in reverse order.  */
10346   return nreverse (cv_qualifiers);
10347 }
10348
10349 /* Parse an (optional) cv-qualifier.
10350
10351    cv-qualifier:
10352      const
10353      volatile  
10354
10355    GNU Extension:
10356
10357    cv-qualifier:
10358      __restrict__ */
10359
10360 static tree
10361 cp_parser_cv_qualifier_opt (parser)
10362      cp_parser *parser;
10363 {
10364   cp_token *token;
10365   tree cv_qualifier = NULL_TREE;
10366
10367   /* Peek at the next token.  */
10368   token = cp_lexer_peek_token (parser->lexer);
10369   /* See if it's a cv-qualifier.  */
10370   switch (token->keyword)
10371     {
10372     case RID_CONST:
10373     case RID_VOLATILE:
10374     case RID_RESTRICT:
10375       /* Save the value of the token.  */
10376       cv_qualifier = token->value;
10377       /* Consume the token.  */
10378       cp_lexer_consume_token (parser->lexer);
10379       break;
10380
10381     default:
10382       break;
10383     }
10384
10385   return cv_qualifier;
10386 }
10387
10388 /* Parse a declarator-id.
10389
10390    declarator-id:
10391      id-expression
10392      :: [opt] nested-name-specifier [opt] type-name  
10393
10394    In the `id-expression' case, the value returned is as for
10395    cp_parser_id_expression if the id-expression was an unqualified-id.
10396    If the id-expression was a qualified-id, then a SCOPE_REF is
10397    returned.  The first operand is the scope (either a NAMESPACE_DECL
10398    or TREE_TYPE), but the second is still just a representation of an
10399    unqualified-id.  */
10400
10401 static tree
10402 cp_parser_declarator_id (parser)
10403      cp_parser *parser;
10404 {
10405   tree id_expression;
10406
10407   /* The expression must be an id-expression.  Assume that qualified
10408      names are the names of types so that:
10409
10410        template <class T>
10411        int S<T>::R::i = 3;
10412
10413      will work; we must treat `S<T>::R' as the name of a type.
10414      Similarly, assume that qualified names are templates, where
10415      required, so that:
10416
10417        template <class T>
10418        int S<T>::R<T>::i = 3;
10419
10420      will work, too.  */
10421   id_expression = cp_parser_id_expression (parser,
10422                                            /*template_keyword_p=*/false,
10423                                            /*check_dependency_p=*/false,
10424                                            /*template_p=*/NULL);
10425   /* If the name was qualified, create a SCOPE_REF to represent 
10426      that.  */
10427   if (parser->scope)
10428     id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10429
10430   return id_expression;
10431 }
10432
10433 /* Parse a type-id.
10434
10435    type-id:
10436      type-specifier-seq abstract-declarator [opt]
10437
10438    Returns the TYPE specified.  */
10439
10440 static tree
10441 cp_parser_type_id (parser)
10442      cp_parser *parser;
10443 {
10444   tree type_specifier_seq;
10445   tree abstract_declarator;
10446
10447   /* Parse the type-specifier-seq.  */
10448   type_specifier_seq 
10449     = cp_parser_type_specifier_seq (parser);
10450   if (type_specifier_seq == error_mark_node)
10451     return error_mark_node;
10452
10453   /* There might or might not be an abstract declarator.  */
10454   cp_parser_parse_tentatively (parser);
10455   /* Look for the declarator.  */
10456   abstract_declarator 
10457     = cp_parser_declarator (parser, /*abstract_p=*/true, NULL);
10458   /* Check to see if there really was a declarator.  */
10459   if (!cp_parser_parse_definitely (parser))
10460     abstract_declarator = NULL_TREE;
10461
10462   return groktypename (build_tree_list (type_specifier_seq,
10463                                         abstract_declarator));
10464 }
10465
10466 /* Parse a type-specifier-seq.
10467
10468    type-specifier-seq:
10469      type-specifier type-specifier-seq [opt]
10470
10471    GNU extension:
10472
10473    type-specifier-seq:
10474      attributes type-specifier-seq [opt]
10475
10476    Returns a TREE_LIST.  Either the TREE_VALUE of each node is a
10477    type-specifier, or the TREE_PURPOSE is a list of attributes.  */
10478
10479 static tree
10480 cp_parser_type_specifier_seq (parser)
10481      cp_parser *parser;
10482 {
10483   bool seen_type_specifier = false;
10484   tree type_specifier_seq = NULL_TREE;
10485
10486   /* Parse the type-specifiers and attributes.  */
10487   while (true)
10488     {
10489       tree type_specifier;
10490
10491       /* Check for attributes first.  */
10492       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10493         {
10494           type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10495                                           NULL_TREE,
10496                                           type_specifier_seq);
10497           continue;
10498         }
10499
10500       /* After the first type-specifier, others are optional.  */
10501       if (seen_type_specifier)
10502         cp_parser_parse_tentatively (parser);
10503       /* Look for the type-specifier.  */
10504       type_specifier = cp_parser_type_specifier (parser, 
10505                                                  CP_PARSER_FLAGS_NONE,
10506                                                  /*is_friend=*/false,
10507                                                  /*is_declaration=*/false,
10508                                                  NULL,
10509                                                  NULL);
10510       /* If the first type-specifier could not be found, this is not a
10511          type-specifier-seq at all.  */
10512       if (!seen_type_specifier && type_specifier == error_mark_node)
10513         return error_mark_node;
10514       /* If subsequent type-specifiers could not be found, the
10515          type-specifier-seq is complete.  */
10516       else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10517         break;
10518
10519       /* Add the new type-specifier to the list.  */
10520       type_specifier_seq 
10521         = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10522       seen_type_specifier = true;
10523     }
10524
10525   /* We built up the list in reverse order.  */
10526   return nreverse (type_specifier_seq);
10527 }
10528
10529 /* Parse a parameter-declaration-clause.
10530
10531    parameter-declaration-clause:
10532      parameter-declaration-list [opt] ... [opt]
10533      parameter-declaration-list , ...
10534
10535    Returns a representation for the parameter declarations.  Each node
10536    is a TREE_LIST.  (See cp_parser_parameter_declaration for the exact
10537    representation.)  If the parameter-declaration-clause ends with an
10538    ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10539    list.  A return value of NULL_TREE indicates a
10540    parameter-declaration-clause consisting only of an ellipsis.  */
10541
10542 static tree
10543 cp_parser_parameter_declaration_clause (parser)
10544      cp_parser *parser;
10545 {
10546   tree parameters;
10547   cp_token *token;
10548   bool ellipsis_p;
10549
10550   /* Peek at the next token.  */
10551   token = cp_lexer_peek_token (parser->lexer);
10552   /* Check for trivial parameter-declaration-clauses.  */
10553   if (token->type == CPP_ELLIPSIS)
10554     {
10555       /* Consume the `...' token.  */
10556       cp_lexer_consume_token (parser->lexer);
10557       return NULL_TREE;
10558     }
10559   else if (token->type == CPP_CLOSE_PAREN)
10560     /* There are no parameters.  */
10561     return void_list_node;
10562   /* Check for `(void)', too, which is a special case.  */
10563   else if (token->keyword == RID_VOID
10564            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 
10565                == CPP_CLOSE_PAREN))
10566     {
10567       /* Consume the `void' token.  */
10568       cp_lexer_consume_token (parser->lexer);
10569       /* There are no parameters.  */
10570       return void_list_node;
10571     }
10572   
10573   /* Parse the parameter-declaration-list.  */
10574   parameters = cp_parser_parameter_declaration_list (parser);
10575   /* If a parse error occurred while parsing the
10576      parameter-declaration-list, then the entire
10577      parameter-declaration-clause is erroneous.  */
10578   if (parameters == error_mark_node)
10579     return error_mark_node;
10580
10581   /* Peek at the next token.  */
10582   token = cp_lexer_peek_token (parser->lexer);
10583   /* If it's a `,', the clause should terminate with an ellipsis.  */
10584   if (token->type == CPP_COMMA)
10585     {
10586       /* Consume the `,'.  */
10587       cp_lexer_consume_token (parser->lexer);
10588       /* Expect an ellipsis.  */
10589       ellipsis_p 
10590         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10591     }
10592   /* It might also be `...' if the optional trailing `,' was 
10593      omitted.  */
10594   else if (token->type == CPP_ELLIPSIS)
10595     {
10596       /* Consume the `...' token.  */
10597       cp_lexer_consume_token (parser->lexer);
10598       /* And remember that we saw it.  */
10599       ellipsis_p = true;
10600     }
10601   else
10602     ellipsis_p = false;
10603
10604   /* Finish the parameter list.  */
10605   return finish_parmlist (parameters, ellipsis_p);
10606 }
10607
10608 /* Parse a parameter-declaration-list.
10609
10610    parameter-declaration-list:
10611      parameter-declaration
10612      parameter-declaration-list , parameter-declaration
10613
10614    Returns a representation of the parameter-declaration-list, as for
10615    cp_parser_parameter_declaration_clause.  However, the
10616    `void_list_node' is never appended to the list.  */
10617
10618 static tree
10619 cp_parser_parameter_declaration_list (parser)
10620      cp_parser *parser;
10621 {
10622   tree parameters = NULL_TREE;
10623
10624   /* Look for more parameters.  */
10625   while (true)
10626     {
10627       tree parameter;
10628       /* Parse the parameter.  */
10629       parameter 
10630         = cp_parser_parameter_declaration (parser,
10631                                            /*greater_than_is_operator_p=*/true);
10632       /* If a parse error ocurred parsing the parameter declaration,
10633          then the entire parameter-declaration-list is erroneous.  */
10634       if (parameter == error_mark_node)
10635         {
10636           parameters = error_mark_node;
10637           break;
10638         }
10639       /* Add the new parameter to the list.  */
10640       TREE_CHAIN (parameter) = parameters;
10641       parameters = parameter;
10642
10643       /* Peek at the next token.  */
10644       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10645           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10646         /* The parameter-declaration-list is complete.  */
10647         break;
10648       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10649         {
10650           cp_token *token;
10651
10652           /* Peek at the next token.  */
10653           token = cp_lexer_peek_nth_token (parser->lexer, 2);
10654           /* If it's an ellipsis, then the list is complete.  */
10655           if (token->type == CPP_ELLIPSIS)
10656             break;
10657           /* Otherwise, there must be more parameters.  Consume the
10658              `,'.  */
10659           cp_lexer_consume_token (parser->lexer);
10660         }
10661       else
10662         {
10663           cp_parser_error (parser, "expected `,' or `...'");
10664           break;
10665         }
10666     }
10667
10668   /* We built up the list in reverse order; straighten it out now.  */
10669   return nreverse (parameters);
10670 }
10671
10672 /* Parse a parameter declaration.
10673
10674    parameter-declaration:
10675      decl-specifier-seq declarator
10676      decl-specifier-seq declarator = assignment-expression
10677      decl-specifier-seq abstract-declarator [opt]
10678      decl-specifier-seq abstract-declarator [opt] = assignment-expression
10679
10680    If GREATER_THAN_IS_OPERATOR_P is FALSE, then a non-nested `>' token
10681    encountered during the parsing of the assignment-expression is not
10682    interpreted as a greater-than operator.
10683
10684    Returns a TREE_LIST representing the parameter-declaration.  The
10685    TREE_VALUE is a representation of the decl-specifier-seq and
10686    declarator.  In particular, the TREE_VALUE will be a TREE_LIST
10687    whose TREE_PURPOSE represents the decl-specifier-seq and whose
10688    TREE_VALUE represents the declarator.  */
10689
10690 static tree
10691 cp_parser_parameter_declaration (parser, greater_than_is_operator_p)
10692      cp_parser *parser;
10693      bool greater_than_is_operator_p;
10694 {
10695   bool declares_class_or_enum;
10696   tree decl_specifiers;
10697   tree attributes;
10698   tree declarator;
10699   tree default_argument;
10700   tree parameter;
10701   cp_token *token;
10702   const char *saved_message;
10703
10704   /* Type definitions may not appear in parameter types.  */
10705   saved_message = parser->type_definition_forbidden_message;
10706   parser->type_definition_forbidden_message 
10707     = "types may not be defined in parameter types";
10708
10709   /* Parse the declaration-specifiers.  */
10710   decl_specifiers 
10711     = cp_parser_decl_specifier_seq (parser,
10712                                     CP_PARSER_FLAGS_NONE,
10713                                     &attributes,
10714                                     &declares_class_or_enum);
10715   /* If an error occurred, there's no reason to attempt to parse the
10716      rest of the declaration.  */
10717   if (cp_parser_error_occurred (parser))
10718     {
10719       parser->type_definition_forbidden_message = saved_message;
10720       return error_mark_node;
10721     }
10722
10723   /* Peek at the next token.  */
10724   token = cp_lexer_peek_token (parser->lexer);
10725   /* If the next token is a `)', `,', `=', `>', or `...', then there
10726      is no declarator.  */
10727   if (token->type == CPP_CLOSE_PAREN 
10728       || token->type == CPP_COMMA
10729       || token->type == CPP_EQ
10730       || token->type == CPP_ELLIPSIS
10731       || token->type == CPP_GREATER)
10732     declarator = NULL_TREE;
10733   /* Otherwise, there should be a declarator.  */
10734   else
10735     {
10736       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10737       parser->default_arg_ok_p = false;
10738   
10739       /* We don't know whether the declarator will be abstract or
10740          not.  So, first we try an ordinary declarator.  */
10741       cp_parser_parse_tentatively (parser);
10742       declarator = cp_parser_declarator (parser,
10743                                          /*abstract_p=*/false,
10744                                          /*ctor_dtor_or_conv_p=*/NULL);
10745       /* If that didn't work, look for an abstract declarator.  */
10746       if (!cp_parser_parse_definitely (parser))
10747         declarator = cp_parser_declarator (parser,
10748                                            /*abstract_p=*/true,
10749                                            /*ctor_dtor_or_conv_p=*/NULL);
10750       parser->default_arg_ok_p = saved_default_arg_ok_p;
10751     }
10752
10753   /* The restriction on definining new types applies only to the type
10754      of the parameter, not to the default argument.  */
10755   parser->type_definition_forbidden_message = saved_message;
10756
10757   /* If the next token is `=', then process a default argument.  */
10758   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10759     {
10760       bool saved_greater_than_is_operator_p;
10761       /* Consume the `='.  */
10762       cp_lexer_consume_token (parser->lexer);
10763
10764       /* If we are defining a class, then the tokens that make up the
10765          default argument must be saved and processed later.  */
10766       if (at_class_scope_p () && TYPE_BEING_DEFINED (current_class_type))
10767         {
10768           unsigned depth = 0;
10769
10770           /* Create a DEFAULT_ARG to represented the unparsed default
10771              argument.  */
10772           default_argument = make_node (DEFAULT_ARG);
10773           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10774
10775           /* Add tokens until we have processed the entire default
10776              argument.  */
10777           while (true)
10778             {
10779               bool done = false;
10780               cp_token *token;
10781
10782               /* Peek at the next token.  */
10783               token = cp_lexer_peek_token (parser->lexer);
10784               /* What we do depends on what token we have.  */
10785               switch (token->type)
10786                 {
10787                   /* In valid code, a default argument must be
10788                      immediately followed by a `,' `)', or `...'.  */
10789                 case CPP_COMMA:
10790                 case CPP_CLOSE_PAREN:
10791                 case CPP_ELLIPSIS:
10792                   /* If we run into a non-nested `;', `}', or `]',
10793                      then the code is invalid -- but the default
10794                      argument is certainly over.  */
10795                 case CPP_SEMICOLON:
10796                 case CPP_CLOSE_BRACE:
10797                 case CPP_CLOSE_SQUARE:
10798                   if (depth == 0)
10799                     done = true;
10800                   /* Update DEPTH, if necessary.  */
10801                   else if (token->type == CPP_CLOSE_PAREN
10802                            || token->type == CPP_CLOSE_BRACE
10803                            || token->type == CPP_CLOSE_SQUARE)
10804                     --depth;
10805                   break;
10806
10807                 case CPP_OPEN_PAREN:
10808                 case CPP_OPEN_SQUARE:
10809                 case CPP_OPEN_BRACE:
10810                   ++depth;
10811                   break;
10812
10813                 case CPP_GREATER:
10814                   /* If we see a non-nested `>', and `>' is not an
10815                      operator, then it marks the end of the default
10816                      argument.  */
10817                   if (!depth && !greater_than_is_operator_p)
10818                     done = true;
10819                   break;
10820
10821                   /* If we run out of tokens, issue an error message.  */
10822                 case CPP_EOF:
10823                   error ("file ends in default argument");
10824                   done = true;
10825                   break;
10826
10827                 case CPP_NAME:
10828                 case CPP_SCOPE:
10829                   /* In these cases, we should look for template-ids.
10830                      For example, if the default argument is 
10831                      `X<int, double>()', we need to do name lookup to
10832                      figure out whether or not `X' is a template; if
10833                      so, the `,' does not end the deault argument.
10834
10835                      That is not yet done.  */
10836                   break;
10837
10838                 default:
10839                   break;
10840                 }
10841
10842               /* If we've reached the end, stop.  */
10843               if (done)
10844                 break;
10845               
10846               /* Add the token to the token block.  */
10847               token = cp_lexer_consume_token (parser->lexer);
10848               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10849                                          token);
10850             }
10851         }
10852       /* Outside of a class definition, we can just parse the
10853          assignment-expression.  */
10854       else
10855         {
10856           bool saved_local_variables_forbidden_p;
10857
10858           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10859              set correctly.  */
10860           saved_greater_than_is_operator_p 
10861             = parser->greater_than_is_operator_p;
10862           parser->greater_than_is_operator_p = greater_than_is_operator_p;
10863           /* Local variable names (and the `this' keyword) may not
10864              appear in a default argument.  */
10865           saved_local_variables_forbidden_p 
10866             = parser->local_variables_forbidden_p;
10867           parser->local_variables_forbidden_p = true;
10868           /* Parse the assignment-expression.  */
10869           default_argument = cp_parser_assignment_expression (parser);
10870           /* Restore saved state.  */
10871           parser->greater_than_is_operator_p 
10872             = saved_greater_than_is_operator_p;
10873           parser->local_variables_forbidden_p 
10874             = saved_local_variables_forbidden_p; 
10875         }
10876       if (!parser->default_arg_ok_p)
10877         {
10878           pedwarn ("default arguments are only permitted on functions");
10879           if (flag_pedantic_errors)
10880             default_argument = NULL_TREE;
10881         }
10882     }
10883   else
10884     default_argument = NULL_TREE;
10885   
10886   /* Create the representation of the parameter.  */
10887   if (attributes)
10888     decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10889   parameter = build_tree_list (default_argument, 
10890                                build_tree_list (decl_specifiers,
10891                                                 declarator));
10892
10893   return parameter;
10894 }
10895
10896 /* Parse a function-definition.  
10897
10898    function-definition:
10899      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10900        function-body 
10901      decl-specifier-seq [opt] declarator function-try-block  
10902
10903    GNU Extension:
10904
10905    function-definition:
10906      __extension__ function-definition 
10907
10908    Returns the FUNCTION_DECL for the function.  If FRIEND_P is
10909    non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10910    be a `friend'.  */
10911
10912 static tree
10913 cp_parser_function_definition (parser, friend_p)
10914      cp_parser *parser;
10915      bool *friend_p;
10916 {
10917   tree decl_specifiers;
10918   tree attributes;
10919   tree declarator;
10920   tree fn;
10921   tree access_checks;
10922   cp_token *token;
10923   bool declares_class_or_enum;
10924   bool member_p;
10925   /* The saved value of the PEDANTIC flag.  */
10926   int saved_pedantic;
10927
10928   /* Any pending qualification must be cleared by our caller.  It is
10929      more robust to force the callers to clear PARSER->SCOPE than to
10930      do it here since if the qualification is in effect here, it might
10931      also end up in effect elsewhere that it is not intended.  */
10932   my_friendly_assert (!parser->scope, 20010821);
10933
10934   /* Handle `__extension__'.  */
10935   if (cp_parser_extension_opt (parser, &saved_pedantic))
10936     {
10937       /* Parse the function-definition.  */
10938       fn = cp_parser_function_definition (parser, friend_p);
10939       /* Restore the PEDANTIC flag.  */
10940       pedantic = saved_pedantic;
10941
10942       return fn;
10943     }
10944
10945   /* Check to see if this definition appears in a class-specifier.  */
10946   member_p = (at_class_scope_p () 
10947               && TYPE_BEING_DEFINED (current_class_type));
10948   /* Defer access checks in the decl-specifier-seq until we know what
10949      function is being defined.  There is no need to do this for the
10950      definition of member functions; we cannot be defining a member
10951      from another class.  */
10952   if (!member_p)
10953     cp_parser_start_deferring_access_checks (parser);
10954   /* Parse the decl-specifier-seq.  */
10955   decl_specifiers 
10956     = cp_parser_decl_specifier_seq (parser,
10957                                     CP_PARSER_FLAGS_OPTIONAL,
10958                                     &attributes,
10959                                     &declares_class_or_enum);
10960   /* Figure out whether this declaration is a `friend'.  */
10961   if (friend_p)
10962     *friend_p = cp_parser_friend_p (decl_specifiers);
10963
10964   /* Parse the declarator.  */
10965   declarator = cp_parser_declarator (parser, 
10966                                      /*abstract_p=*/false,
10967                                      /*ctor_dtor_or_conv_p=*/NULL);
10968
10969   /* Gather up any access checks that occurred.  */
10970   if (!member_p)
10971     access_checks = cp_parser_stop_deferring_access_checks (parser);
10972   else
10973     access_checks = NULL_TREE;
10974
10975   /* If something has already gone wrong, we may as well stop now.  */
10976   if (declarator == error_mark_node)
10977     {
10978       /* Skip to the end of the function, or if this wasn't anything
10979          like a function-definition, to a `;' in the hopes of finding
10980          a sensible place from which to continue parsing.  */
10981       cp_parser_skip_to_end_of_block_or_statement (parser);
10982       return error_mark_node;
10983     }
10984
10985   /* The next character should be a `{' (for a simple function
10986      definition), a `:' (for a ctor-initializer), or `try' (for a
10987      function-try block).  */
10988   token = cp_lexer_peek_token (parser->lexer);
10989   if (!cp_parser_token_starts_function_definition_p (token))
10990     {
10991       /* Issue the error-message.  */
10992       cp_parser_error (parser, "expected function-definition");
10993       /* Skip to the next `;'.  */
10994       cp_parser_skip_to_end_of_block_or_statement (parser);
10995
10996       return error_mark_node;
10997     }
10998
10999   /* If we are in a class scope, then we must handle
11000      function-definitions specially.  In particular, we save away the
11001      tokens that make up the function body, and parse them again
11002      later, in order to handle code like:
11003
11004        struct S {
11005          int f () { return i; }
11006          int i;
11007        }; 
11008  
11009      Here, we cannot parse the body of `f' until after we have seen
11010      the declaration of `i'.  */
11011   if (member_p)
11012     {
11013       cp_token_cache *cache;
11014
11015       /* Create the function-declaration.  */
11016       fn = start_method (decl_specifiers, declarator, attributes);
11017       /* If something went badly wrong, bail out now.  */
11018       if (fn == error_mark_node)
11019         {
11020           /* If there's a function-body, skip it.  */
11021           if (cp_parser_token_starts_function_definition_p 
11022               (cp_lexer_peek_token (parser->lexer)))
11023             cp_parser_skip_to_end_of_block_or_statement (parser);
11024           return error_mark_node;
11025         }
11026
11027       /* Create a token cache.  */
11028       cache = cp_token_cache_new ();
11029       /* Save away the tokens that make up the body of the 
11030          function.  */
11031       cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11032       /* Handle function try blocks.  */
11033       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11034         cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11035
11036       /* Save away the inline definition; we will process it when the
11037          class is complete.  */
11038       DECL_PENDING_INLINE_INFO (fn) = cache;
11039       DECL_PENDING_INLINE_P (fn) = 1;
11040
11041       /* We're done with the inline definition.  */
11042       finish_method (fn);
11043
11044       /* Add FN to the queue of functions to be parsed later.  */
11045       TREE_VALUE (parser->unparsed_functions_queues)
11046         = tree_cons (current_class_type, fn, 
11047                      TREE_VALUE (parser->unparsed_functions_queues));
11048
11049       return fn;
11050     }
11051
11052   /* Check that the number of template-parameter-lists is OK.  */
11053   if (!cp_parser_check_declarator_template_parameters (parser, 
11054                                                        declarator))
11055     {
11056       cp_parser_skip_to_end_of_block_or_statement (parser);
11057       return error_mark_node;
11058     }
11059
11060   return (cp_parser_function_definition_from_specifiers_and_declarator
11061           (parser, decl_specifiers, attributes, declarator, access_checks));
11062 }
11063
11064 /* Parse a function-body.
11065
11066    function-body:
11067      compound_statement  */
11068
11069 static void
11070 cp_parser_function_body (cp_parser *parser)
11071 {
11072   cp_parser_compound_statement (parser);
11073 }
11074
11075 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11076    true if a ctor-initializer was present.  */
11077
11078 static bool
11079 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11080 {
11081   tree body;
11082   bool ctor_initializer_p;
11083
11084   /* Begin the function body.  */
11085   body = begin_function_body ();
11086   /* Parse the optional ctor-initializer.  */
11087   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11088   /* Parse the function-body.  */
11089   cp_parser_function_body (parser);
11090   /* Finish the function body.  */
11091   finish_function_body (body);
11092
11093   return ctor_initializer_p;
11094 }
11095
11096 /* Parse an initializer.
11097
11098    initializer:
11099      = initializer-clause
11100      ( expression-list )  
11101
11102    Returns a expression representing the initializer.  If no
11103    initializer is present, NULL_TREE is returned.  
11104
11105    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11106    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11107    set to FALSE if there is no initializer present.  */
11108
11109 static tree
11110 cp_parser_initializer (parser, is_parenthesized_init)
11111      cp_parser *parser;
11112      bool *is_parenthesized_init;
11113 {
11114   cp_token *token;
11115   tree init;
11116
11117   /* Peek at the next token.  */
11118   token = cp_lexer_peek_token (parser->lexer);
11119
11120   /* Let our caller know whether or not this initializer was
11121      parenthesized.  */
11122   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11123
11124   if (token->type == CPP_EQ)
11125     {
11126       /* Consume the `='.  */
11127       cp_lexer_consume_token (parser->lexer);
11128       /* Parse the initializer-clause.  */
11129       init = cp_parser_initializer_clause (parser);
11130     }
11131   else if (token->type == CPP_OPEN_PAREN)
11132     {
11133       /* Consume the `('.  */
11134       cp_lexer_consume_token (parser->lexer);
11135       /* Parse the expression-list.  */
11136       init = cp_parser_expression_list (parser);
11137       /* Consume the `)' token.  */
11138       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11139         cp_parser_skip_to_closing_parenthesis (parser);
11140     }
11141   else
11142     {
11143       /* Anything else is an error.  */
11144       cp_parser_error (parser, "expected initializer");
11145       init = error_mark_node;
11146     }
11147
11148   return init;
11149 }
11150
11151 /* Parse an initializer-clause.  
11152
11153    initializer-clause:
11154      assignment-expression
11155      { initializer-list , [opt] }
11156      { }
11157
11158    Returns an expression representing the initializer.  
11159
11160    If the `assignment-expression' production is used the value
11161    returned is simply a reprsentation for the expression.  
11162
11163    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11164    the elements of the initializer-list (or NULL_TREE, if the last
11165    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11166    NULL_TREE.  There is no way to detect whether or not the optional
11167    trailing `,' was provided.  */
11168
11169 static tree
11170 cp_parser_initializer_clause (parser)
11171      cp_parser *parser;
11172 {
11173   tree initializer;
11174
11175   /* If it is not a `{', then we are looking at an
11176      assignment-expression.  */
11177   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11178     initializer = cp_parser_assignment_expression (parser);
11179   else
11180     {
11181       /* Consume the `{' token.  */
11182       cp_lexer_consume_token (parser->lexer);
11183       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11184       initializer = make_node (CONSTRUCTOR);
11185       /* Mark it with TREE_HAS_CONSTRUCTOR.  This should not be
11186          necessary, but check_initializer depends upon it, for 
11187          now.  */
11188       TREE_HAS_CONSTRUCTOR (initializer) = 1;
11189       /* If it's not a `}', then there is a non-trivial initializer.  */
11190       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11191         {
11192           /* Parse the initializer list.  */
11193           CONSTRUCTOR_ELTS (initializer)
11194             = cp_parser_initializer_list (parser);
11195           /* A trailing `,' token is allowed.  */
11196           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11197             cp_lexer_consume_token (parser->lexer);
11198         }
11199
11200       /* Now, there should be a trailing `}'.  */
11201       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11202     }
11203
11204   return initializer;
11205 }
11206
11207 /* Parse an initializer-list.
11208
11209    initializer-list:
11210      initializer-clause
11211      initializer-list , initializer-clause
11212
11213    GNU Extension:
11214    
11215    initializer-list:
11216      identifier : initializer-clause
11217      initializer-list, identifier : initializer-clause
11218
11219    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
11220    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
11221    IDENTIFIER_NODE naming the field to initialize.   */
11222
11223 static tree
11224 cp_parser_initializer_list (parser)
11225      cp_parser *parser;
11226 {
11227   tree initializers = NULL_TREE;
11228
11229   /* Parse the rest of the list.  */
11230   while (true)
11231     {
11232       cp_token *token;
11233       tree identifier;
11234       tree initializer;
11235       
11236       /* If the next token is an identifier and the following one is a
11237          colon, we are looking at the GNU designated-initializer
11238          syntax.  */
11239       if (cp_parser_allow_gnu_extensions_p (parser)
11240           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11241           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11242         {
11243           /* Consume the identifier.  */
11244           identifier = cp_lexer_consume_token (parser->lexer)->value;
11245           /* Consume the `:'.  */
11246           cp_lexer_consume_token (parser->lexer);
11247         }
11248       else
11249         identifier = NULL_TREE;
11250
11251       /* Parse the initializer.  */
11252       initializer = cp_parser_initializer_clause (parser);
11253
11254       /* Add it to the list.  */
11255       initializers = tree_cons (identifier, initializer, initializers);
11256
11257       /* If the next token is not a comma, we have reached the end of
11258          the list.  */
11259       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11260         break;
11261
11262       /* Peek at the next token.  */
11263       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11264       /* If the next token is a `}', then we're still done.  An
11265          initializer-clause can have a trailing `,' after the
11266          initializer-list and before the closing `}'.  */
11267       if (token->type == CPP_CLOSE_BRACE)
11268         break;
11269
11270       /* Consume the `,' token.  */
11271       cp_lexer_consume_token (parser->lexer);
11272     }
11273
11274   /* The initializers were built up in reverse order, so we need to
11275      reverse them now.  */
11276   return nreverse (initializers);
11277 }
11278
11279 /* Classes [gram.class] */
11280
11281 /* Parse a class-name.
11282
11283    class-name:
11284      identifier
11285      template-id
11286
11287    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11288    to indicate that names looked up in dependent types should be
11289    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
11290    keyword has been used to indicate that the name that appears next
11291    is a template.  TYPE_P is true iff the next name should be treated
11292    as class-name, even if it is declared to be some other kind of name
11293    as well.  The accessibility of the class-name is checked iff
11294    CHECK_ACCESS_P is true.  If CHECK_DEPENDENCY_P is FALSE, names are
11295    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
11296    is the class being defined in a class-head.
11297
11298    Returns the TYPE_DECL representing the class.  */
11299
11300 static tree
11301 cp_parser_class_name (cp_parser *parser, 
11302                       bool typename_keyword_p, 
11303                       bool template_keyword_p, 
11304                       bool type_p,
11305                       bool check_access_p,
11306                       bool check_dependency_p,
11307                       bool class_head_p)
11308 {
11309   tree decl;
11310   tree scope;
11311   bool typename_p;
11312   
11313   /* PARSER->SCOPE can be cleared when parsing the template-arguments
11314      to a template-id, so we save it here.  */
11315   scope = parser->scope;
11316   /* Any name names a type if we're following the `typename' keyword
11317      in a qualified name where the enclosing scope is type-dependent.  */
11318   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11319                 && cp_parser_dependent_type_p (scope));
11320
11321   /* We don't know whether what comes next is a template-id or 
11322      not.  */
11323   cp_parser_parse_tentatively (parser);
11324   /* Try a template-id.  */
11325   decl = cp_parser_template_id (parser, template_keyword_p,
11326                                 check_dependency_p);
11327   if (cp_parser_parse_definitely (parser))
11328     {
11329       if (decl == error_mark_node)
11330         return error_mark_node;
11331     }
11332   else
11333     {
11334       /* If it wasn't a template-id, try a simple identifier.  */
11335       tree identifier;
11336
11337       /* Look for the identifier.  */
11338       identifier = cp_parser_identifier (parser);
11339       /* If the next token isn't an identifier, we are certainly not
11340          looking at a class-name.  */
11341       if (identifier == error_mark_node)
11342         decl = error_mark_node;
11343       /* If we know this is a type-name, there's no need to look it
11344          up.  */
11345       else if (typename_p)
11346         decl = identifier;
11347       else
11348         {
11349           /* If the next token is a `::', then the name must be a type
11350              name.
11351
11352              [basic.lookup.qual]
11353
11354              During the lookup for a name preceding the :: scope
11355              resolution operator, object, function, and enumerator
11356              names are ignored.  */
11357           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11358             type_p = true;
11359           /* Look up the name.  */
11360           decl = cp_parser_lookup_name (parser, identifier, 
11361                                         check_access_p,
11362                                         type_p,
11363                                         check_dependency_p);
11364         }
11365     }
11366
11367   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11368
11369   /* If this is a typename, create a TYPENAME_TYPE.  */
11370   if (typename_p && decl != error_mark_node)
11371     decl = TYPE_NAME (make_typename_type (scope, decl,
11372                                           /*complain=*/1));
11373
11374   /* Check to see that it is really the name of a class.  */
11375   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 
11376       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11377       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11378     /* Situations like this:
11379
11380          template <typename T> struct A {
11381            typename T::template X<int>::I i; 
11382          };
11383
11384        are problematic.  Is `T::template X<int>' a class-name?  The
11385        standard does not seem to be definitive, but there is no other
11386        valid interpretation of the following `::'.  Therefore, those
11387        names are considered class-names.  */
11388     decl = TYPE_NAME (make_typename_type (scope, decl, 
11389                                           tf_error | tf_parsing));
11390   else if (decl == error_mark_node
11391            || TREE_CODE (decl) != TYPE_DECL
11392            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11393     {
11394       cp_parser_error (parser, "expected class-name");
11395       return error_mark_node;
11396     }
11397
11398   return decl;
11399 }
11400
11401 /* Parse a class-specifier.
11402
11403    class-specifier:
11404      class-head { member-specification [opt] }
11405
11406    Returns the TREE_TYPE representing the class.  */
11407
11408 static tree
11409 cp_parser_class_specifier (parser)
11410      cp_parser *parser;
11411 {
11412   cp_token *token;
11413   tree type;
11414   tree attributes = NULL_TREE;
11415   int has_trailing_semicolon;
11416   bool nested_name_specifier_p;
11417   bool deferring_access_checks_p;
11418   tree saved_access_checks;
11419   unsigned saved_num_template_parameter_lists;
11420
11421   /* Parse the class-head.  */
11422   type = cp_parser_class_head (parser,
11423                                &nested_name_specifier_p,
11424                                &deferring_access_checks_p,
11425                                &saved_access_checks);
11426   /* If the class-head was a semantic disaster, skip the entire body
11427      of the class.  */
11428   if (!type)
11429     {
11430       cp_parser_skip_to_end_of_block_or_statement (parser);
11431       return error_mark_node;
11432     }
11433   /* Look for the `{'.  */
11434   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11435     return error_mark_node;
11436   /* Issue an error message if type-definitions are forbidden here.  */
11437   cp_parser_check_type_definition (parser);
11438   /* Remember that we are defining one more class.  */
11439   ++parser->num_classes_being_defined;
11440   /* Inside the class, surrounding template-parameter-lists do not
11441      apply.  */
11442   saved_num_template_parameter_lists 
11443     = parser->num_template_parameter_lists; 
11444   parser->num_template_parameter_lists = 0;
11445   /* Start the class.  */
11446   type = begin_class_definition (type);
11447   if (type == error_mark_node)
11448     /* If the type is erroneous, skip the entire body of the class. */
11449     cp_parser_skip_to_closing_brace (parser);
11450   else
11451     /* Parse the member-specification.  */
11452     cp_parser_member_specification_opt (parser);
11453   /* Look for the trailing `}'.  */
11454   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11455   /* We get better error messages by noticing a common problem: a
11456      missing trailing `;'.  */
11457   token = cp_lexer_peek_token (parser->lexer);
11458   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11459   /* Look for attributes to apply to this class.  */
11460   if (cp_parser_allow_gnu_extensions_p (parser))
11461     attributes = cp_parser_attributes_opt (parser);
11462   /* Finish the class definition.  */
11463   type = finish_class_definition (type, 
11464                                   attributes,
11465                                   has_trailing_semicolon,
11466                                   nested_name_specifier_p);
11467   /* If this class is not itself within the scope of another class,
11468      then we need to parse the bodies of all of the queued function
11469      definitions.  Note that the queued functions defined in a class
11470      are not always processed immediately following the
11471      class-specifier for that class.  Consider:
11472
11473        struct A {
11474          struct B { void f() { sizeof (A); } };
11475        };
11476
11477      If `f' were processed before the processing of `A' were
11478      completed, there would be no way to compute the size of `A'.
11479      Note that the nesting we are interested in here is lexical --
11480      not the semantic nesting given by TYPE_CONTEXT.  In particular,
11481      for:
11482
11483        struct A { struct B; };
11484        struct A::B { void f() { } };
11485
11486      there is no need to delay the parsing of `A::B::f'.  */
11487   if (--parser->num_classes_being_defined == 0) 
11488     {
11489       tree last_scope = NULL_TREE;
11490
11491       /* Process non FUNCTION_DECL related DEFAULT_ARGs.  */
11492       for (parser->default_arg_types = nreverse (parser->default_arg_types);
11493            parser->default_arg_types;
11494            parser->default_arg_types = TREE_CHAIN (parser->default_arg_types))
11495         cp_parser_late_parsing_default_args
11496           (parser, TREE_PURPOSE (parser->default_arg_types));
11497       
11498       /* Reverse the queue, so that we process it in the order the
11499          functions were declared.  */
11500       TREE_VALUE (parser->unparsed_functions_queues)
11501         = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11502       /* Loop through all of the functions.  */
11503       while (TREE_VALUE (parser->unparsed_functions_queues))
11504
11505         {
11506           tree fn;
11507           tree fn_scope;
11508           tree queue_entry;
11509
11510           /* Figure out which function we need to process.  */
11511           queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11512           fn_scope = TREE_PURPOSE (queue_entry);
11513           fn = TREE_VALUE (queue_entry);
11514
11515           /* Parse the function.  */
11516           cp_parser_late_parsing_for_member (parser, fn);
11517
11518           TREE_VALUE (parser->unparsed_functions_queues)
11519             = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11520         }
11521
11522       /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11523          more time than we have popped, so me must pop here.  */
11524       if (last_scope)
11525         pop_scope (last_scope);
11526     }
11527
11528   /* Put back any saved access checks.  */
11529   if (deferring_access_checks_p)
11530     {
11531       cp_parser_start_deferring_access_checks (parser);
11532       parser->context->deferred_access_checks = saved_access_checks;
11533     }
11534
11535   /* Restore the count of active template-parameter-lists.  */
11536   parser->num_template_parameter_lists
11537     = saved_num_template_parameter_lists;
11538
11539   return type;
11540 }
11541
11542 /* Parse a class-head.
11543
11544    class-head:
11545      class-key identifier [opt] base-clause [opt]
11546      class-key nested-name-specifier identifier base-clause [opt]
11547      class-key nested-name-specifier [opt] template-id 
11548        base-clause [opt]  
11549
11550    GNU Extensions:
11551      class-key attributes identifier [opt] base-clause [opt]
11552      class-key attributes nested-name-specifier identifier base-clause [opt]
11553      class-key attributes nested-name-specifier [opt] template-id 
11554        base-clause [opt]  
11555
11556    Returns the TYPE of the indicated class.  Sets
11557    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11558    involving a nested-name-specifier was used, and FALSE otherwise.
11559    Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11560    access checks before this class-head.  In that case,
11561    *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11562    checks.  
11563
11564    Returns NULL_TREE if the class-head is syntactically valid, but
11565    semantically invalid in a way that means we should skip the entire
11566    body of the class.  */
11567
11568 static tree
11569 cp_parser_class_head (parser, 
11570                       nested_name_specifier_p,
11571                       deferring_access_checks_p,
11572                       saved_access_checks)
11573      cp_parser *parser;
11574      bool *nested_name_specifier_p;
11575      bool *deferring_access_checks_p;
11576      tree *saved_access_checks;
11577 {
11578   cp_token *token;
11579   tree nested_name_specifier;
11580   enum tag_types class_key;
11581   tree id = NULL_TREE;
11582   tree type = NULL_TREE;
11583   tree attributes;
11584   bool template_id_p = false;
11585   bool qualified_p = false;
11586   bool invalid_nested_name_p = false;
11587   unsigned num_templates;
11588
11589   /* Assume no nested-name-specifier will be present.  */
11590   *nested_name_specifier_p = false;
11591   /* Assume no template parameter lists will be used in defining the
11592      type.  */
11593   num_templates = 0;
11594
11595   /* Look for the class-key.  */
11596   class_key = cp_parser_class_key (parser);
11597   if (class_key == none_type)
11598     return error_mark_node;
11599
11600   /* Parse the attributes.  */
11601   attributes = cp_parser_attributes_opt (parser);
11602
11603   /* If the next token is `::', that is invalid -- but sometimes
11604      people do try to write:
11605
11606        struct ::S {};  
11607
11608      Handle this gracefully by accepting the extra qualifier, and then
11609      issuing an error about it later if this really is a
11610      class-header.  If it turns out just to be an elaborated type
11611      specifier, remain silent.  */
11612   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11613     qualified_p = true;
11614
11615   /* Determine the name of the class.  Begin by looking for an
11616      optional nested-name-specifier.  */
11617   nested_name_specifier 
11618     = cp_parser_nested_name_specifier_opt (parser,
11619                                            /*typename_keyword_p=*/false,
11620                                            /*check_dependency_p=*/true,
11621                                            /*type_p=*/false);
11622   /* If there was a nested-name-specifier, then there *must* be an
11623      identifier.  */
11624   if (nested_name_specifier)
11625     {
11626       /* Although the grammar says `identifier', it really means
11627          `class-name' or `template-name'.  You are only allowed to
11628          define a class that has already been declared with this
11629          syntax.  
11630
11631          The proposed resolution for Core Issue 180 says that whever
11632          you see `class T::X' you should treat `X' as a type-name.
11633          
11634          It is OK to define an inaccessible class; for example:
11635          
11636            class A { class B; };
11637            class A::B {};
11638          
11639          So, we ask cp_parser_class_name not to check accessibility.  
11640
11641          We do not know if we will see a class-name, or a
11642          template-name.  We look for a class-name first, in case the
11643          class-name is a template-id; if we looked for the
11644          template-name first we would stop after the template-name.  */
11645       cp_parser_parse_tentatively (parser);
11646       type = cp_parser_class_name (parser,
11647                                    /*typename_keyword_p=*/false,
11648                                    /*template_keyword_p=*/false,
11649                                    /*type_p=*/true,
11650                                    /*check_access_p=*/false,
11651                                    /*check_dependency_p=*/false,
11652                                    /*class_head_p=*/true);
11653       /* If that didn't work, ignore the nested-name-specifier.  */
11654       if (!cp_parser_parse_definitely (parser))
11655         {
11656           invalid_nested_name_p = true;
11657           id = cp_parser_identifier (parser);
11658           if (id == error_mark_node)
11659             id = NULL_TREE;
11660         }
11661       /* If we could not find a corresponding TYPE, treat this
11662          declaration like an unqualified declaration.  */
11663       if (type == error_mark_node)
11664         nested_name_specifier = NULL_TREE;
11665       /* Otherwise, count the number of templates used in TYPE and its
11666          containing scopes.  */
11667       else 
11668         {
11669           tree scope;
11670
11671           for (scope = TREE_TYPE (type); 
11672                scope && TREE_CODE (scope) != NAMESPACE_DECL;
11673                scope = (TYPE_P (scope) 
11674                         ? TYPE_CONTEXT (scope)
11675                         : DECL_CONTEXT (scope))) 
11676             if (TYPE_P (scope) 
11677                 && CLASS_TYPE_P (scope)
11678                 && CLASSTYPE_TEMPLATE_INFO (scope)
11679                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
11680               ++num_templates;
11681         }
11682     }
11683   /* Otherwise, the identifier is optional.  */
11684   else
11685     {
11686       /* We don't know whether what comes next is a template-id,
11687          an identifier, or nothing at all.  */
11688       cp_parser_parse_tentatively (parser);
11689       /* Check for a template-id.  */
11690       id = cp_parser_template_id (parser, 
11691                                   /*template_keyword_p=*/false,
11692                                   /*check_dependency_p=*/true);
11693       /* If that didn't work, it could still be an identifier.  */
11694       if (!cp_parser_parse_definitely (parser))
11695         {
11696           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11697             id = cp_parser_identifier (parser);
11698           else
11699             id = NULL_TREE;
11700         }
11701       else
11702         {
11703           template_id_p = true;
11704           ++num_templates;
11705         }
11706     }
11707
11708   /* If it's not a `:' or a `{' then we can't really be looking at a
11709      class-head, since a class-head only appears as part of a
11710      class-specifier.  We have to detect this situation before calling
11711      xref_tag, since that has irreversible side-effects.  */
11712   if (!cp_parser_next_token_starts_class_definition_p (parser))
11713     {
11714       cp_parser_error (parser, "expected `{' or `:'");
11715       return error_mark_node;
11716     }
11717
11718   /* At this point, we're going ahead with the class-specifier, even
11719      if some other problem occurs.  */
11720   cp_parser_commit_to_tentative_parse (parser);
11721   /* Issue the error about the overly-qualified name now.  */
11722   if (qualified_p)
11723     cp_parser_error (parser,
11724                      "global qualification of class name is invalid");
11725   else if (invalid_nested_name_p)
11726     cp_parser_error (parser,
11727                      "qualified name does not name a class");
11728   /* Make sure that the right number of template parameters were
11729      present.  */
11730   if (!cp_parser_check_template_parameters (parser, num_templates))
11731     /* If something went wrong, there is no point in even trying to
11732        process the class-definition.  */
11733     return NULL_TREE;
11734
11735   /* We do not need to defer access checks for entities declared
11736      within the class.  But, we do need to save any access checks that
11737      are currently deferred and restore them later, in case we are in
11738      the middle of something else.  */
11739   *deferring_access_checks_p = parser->context->deferring_access_checks_p;
11740   if (*deferring_access_checks_p)
11741     *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
11742
11743   /* Look up the type.  */
11744   if (template_id_p)
11745     {
11746       type = TREE_TYPE (id);
11747       maybe_process_partial_specialization (type);
11748     }
11749   else if (!nested_name_specifier)
11750     {
11751       /* If the class was unnamed, create a dummy name.  */
11752       if (!id)
11753         id = make_anon_name ();
11754       type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11755     }
11756   else
11757     {
11758       int new_type_p;
11759       tree class_type;
11760
11761       /* Given:
11762
11763             template <typename T> struct S { struct T };
11764             template <typename T> struct S::T { };
11765
11766          we will get a TYPENAME_TYPE when processing the definition of
11767          `S::T'.  We need to resolve it to the actual type before we
11768          try to define it.  */
11769       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11770         {
11771           type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
11772           if (type != error_mark_node)
11773             type = TYPE_NAME (type);
11774         }
11775
11776       maybe_process_partial_specialization (TREE_TYPE (type));
11777       class_type = current_class_type;
11778       type = TREE_TYPE (handle_class_head (class_key, 
11779                                            nested_name_specifier,
11780                                            type,
11781                                            attributes,
11782                                            /*defn_p=*/1,
11783                                            &new_type_p));
11784       if (type != error_mark_node)
11785         {
11786           if (!class_type && TYPE_CONTEXT (type))
11787             *nested_name_specifier_p = true;
11788           else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11789                                                class_type))
11790             *nested_name_specifier_p = true;
11791         }
11792     }
11793   /* Indicate whether this class was declared as a `class' or as a
11794      `struct'.  */
11795   if (TREE_CODE (type) == RECORD_TYPE)
11796     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11797   cp_parser_check_class_key (class_key, type);
11798
11799   /* Enter the scope containing the class; the names of base classes
11800      should be looked up in that context.  For example, given:
11801
11802        struct A { struct B {}; struct C; };
11803        struct A::C : B {};
11804
11805      is valid.  */
11806   if (nested_name_specifier)
11807     push_scope (nested_name_specifier);
11808   /* Now, look for the base-clause.  */
11809   token = cp_lexer_peek_token (parser->lexer);
11810   if (token->type == CPP_COLON)
11811     {
11812       tree bases;
11813
11814       /* Get the list of base-classes.  */
11815       bases = cp_parser_base_clause (parser);
11816       /* Process them.  */
11817       xref_basetypes (type, bases);
11818     }
11819   /* Leave the scope given by the nested-name-specifier.  We will
11820      enter the class scope itself while processing the members.  */
11821   if (nested_name_specifier)
11822     pop_scope (nested_name_specifier);
11823
11824   return type;
11825 }
11826
11827 /* Parse a class-key.
11828
11829    class-key:
11830      class
11831      struct
11832      union
11833
11834    Returns the kind of class-key specified, or none_type to indicate
11835    error.  */
11836
11837 static enum tag_types
11838 cp_parser_class_key (parser)
11839      cp_parser *parser;
11840 {
11841   cp_token *token;
11842   enum tag_types tag_type;
11843
11844   /* Look for the class-key.  */
11845   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11846   if (!token)
11847     return none_type;
11848
11849   /* Check to see if the TOKEN is a class-key.  */
11850   tag_type = cp_parser_token_is_class_key (token);
11851   if (!tag_type)
11852     cp_parser_error (parser, "expected class-key");
11853   return tag_type;
11854 }
11855
11856 /* Parse an (optional) member-specification.
11857
11858    member-specification:
11859      member-declaration member-specification [opt]
11860      access-specifier : member-specification [opt]  */
11861
11862 static void
11863 cp_parser_member_specification_opt (parser)
11864      cp_parser *parser;
11865 {
11866   while (true)
11867     {
11868       cp_token *token;
11869       enum rid keyword;
11870
11871       /* Peek at the next token.  */
11872       token = cp_lexer_peek_token (parser->lexer);
11873       /* If it's a `}', or EOF then we've seen all the members.  */
11874       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11875         break;
11876
11877       /* See if this token is a keyword.  */
11878       keyword = token->keyword;
11879       switch (keyword)
11880         {
11881         case RID_PUBLIC:
11882         case RID_PROTECTED:
11883         case RID_PRIVATE:
11884           /* Consume the access-specifier.  */
11885           cp_lexer_consume_token (parser->lexer);
11886           /* Remember which access-specifier is active.  */
11887           current_access_specifier = token->value;
11888           /* Look for the `:'.  */
11889           cp_parser_require (parser, CPP_COLON, "`:'");
11890           break;
11891
11892         default:
11893           /* Otherwise, the next construction must be a
11894              member-declaration.  */
11895           cp_parser_member_declaration (parser);
11896           reset_type_access_control ();
11897         }
11898     }
11899 }
11900
11901 /* Parse a member-declaration.  
11902
11903    member-declaration:
11904      decl-specifier-seq [opt] member-declarator-list [opt] ;
11905      function-definition ; [opt]
11906      :: [opt] nested-name-specifier template [opt] unqualified-id ;
11907      using-declaration
11908      template-declaration 
11909
11910    member-declarator-list:
11911      member-declarator
11912      member-declarator-list , member-declarator
11913
11914    member-declarator:
11915      declarator pure-specifier [opt] 
11916      declarator constant-initializer [opt]
11917      identifier [opt] : constant-expression 
11918
11919    GNU Extensions:
11920
11921    member-declaration:
11922      __extension__ member-declaration
11923
11924    member-declarator:
11925      declarator attributes [opt] pure-specifier [opt]
11926      declarator attributes [opt] constant-initializer [opt]
11927      identifier [opt] attributes [opt] : constant-expression  */
11928
11929 static void
11930 cp_parser_member_declaration (parser)
11931      cp_parser *parser;
11932 {
11933   tree decl_specifiers;
11934   tree prefix_attributes;
11935   tree decl;
11936   bool declares_class_or_enum;
11937   bool friend_p;
11938   cp_token *token;
11939   int saved_pedantic;
11940
11941   /* Check for the `__extension__' keyword.  */
11942   if (cp_parser_extension_opt (parser, &saved_pedantic))
11943     {
11944       /* Recurse.  */
11945       cp_parser_member_declaration (parser);
11946       /* Restore the old value of the PEDANTIC flag.  */
11947       pedantic = saved_pedantic;
11948
11949       return;
11950     }
11951
11952   /* Check for a template-declaration.  */
11953   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11954     {
11955       /* Parse the template-declaration.  */
11956       cp_parser_template_declaration (parser, /*member_p=*/true);
11957
11958       return;
11959     }
11960
11961   /* Check for a using-declaration.  */
11962   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11963     {
11964       /* Parse the using-declaration.  */
11965       cp_parser_using_declaration (parser);
11966
11967       return;
11968     }
11969   
11970   /* We can't tell whether we're looking at a declaration or a
11971      function-definition.  */
11972   cp_parser_parse_tentatively (parser);
11973
11974   /* Parse the decl-specifier-seq.  */
11975   decl_specifiers 
11976     = cp_parser_decl_specifier_seq (parser,
11977                                     CP_PARSER_FLAGS_OPTIONAL,
11978                                     &prefix_attributes,
11979                                     &declares_class_or_enum);
11980   /* If there is no declarator, then the decl-specifier-seq should
11981      specify a type.  */
11982   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11983     {
11984       /* If there was no decl-specifier-seq, and the next token is a
11985          `;', then we have something like:
11986
11987            struct S { ; };
11988
11989          [class.mem]
11990
11991          Each member-declaration shall declare at least one member
11992          name of the class.  */
11993       if (!decl_specifiers)
11994         {
11995           if (pedantic)
11996             pedwarn ("extra semicolon");
11997         }
11998       else 
11999         {
12000           tree type;
12001           
12002           /* See if this declaration is a friend.  */
12003           friend_p = cp_parser_friend_p (decl_specifiers);
12004           /* If there were decl-specifiers, check to see if there was
12005              a class-declaration.  */
12006           type = check_tag_decl (decl_specifiers);
12007           /* Nested classes have already been added to the class, but
12008              a `friend' needs to be explicitly registered.  */
12009           if (friend_p)
12010             {
12011               /* If the `friend' keyword was present, the friend must
12012                  be introduced with a class-key.  */
12013                if (!declares_class_or_enum)
12014                  error ("a class-key must be used when declaring a friend");
12015                /* In this case:
12016
12017                     template <typename T> struct A { 
12018                       friend struct A<T>::B; 
12019                     };
12020  
12021                   A<T>::B will be represented by a TYPENAME_TYPE, and
12022                   therefore not recognized by check_tag_decl.  */
12023                if (!type)
12024                  {
12025                    tree specifier;
12026
12027                    for (specifier = decl_specifiers; 
12028                         specifier;
12029                         specifier = TREE_CHAIN (specifier))
12030                      {
12031                        tree s = TREE_VALUE (specifier);
12032
12033                        if (TREE_CODE (s) == IDENTIFIER_NODE
12034                            && IDENTIFIER_GLOBAL_VALUE (s))
12035                          type = IDENTIFIER_GLOBAL_VALUE (s);
12036                        if (TREE_CODE (s) == TYPE_DECL)
12037                          s = TREE_TYPE (s);
12038                        if (TYPE_P (s))
12039                          {
12040                            type = s;
12041                            break;
12042                          }
12043                      }
12044                  }
12045                if (!type)
12046                  error ("friend declaration does not name a class or "
12047                         "function");
12048                else
12049                  make_friend_class (current_class_type, type);
12050             }
12051           /* If there is no TYPE, an error message will already have
12052              been issued.  */
12053           else if (!type)
12054             ;
12055           /* An anonymous aggregate has to be handled specially; such
12056              a declaration really declares a data member (with a
12057              particular type), as opposed to a nested class.  */
12058           else if (ANON_AGGR_TYPE_P (type))
12059             {
12060               /* Remove constructors and such from TYPE, now that we
12061                  know it is an anoymous aggregate.  */
12062               fixup_anonymous_aggr (type);
12063               /* And make the corresponding data member.  */
12064               decl = build_decl (FIELD_DECL, NULL_TREE, type);
12065               /* Add it to the class.  */
12066               finish_member_declaration (decl);
12067             }
12068         }
12069     }
12070   else
12071     {
12072       /* See if these declarations will be friends.  */
12073       friend_p = cp_parser_friend_p (decl_specifiers);
12074
12075       /* Keep going until we hit the `;' at the end of the 
12076          declaration.  */
12077       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12078         {
12079           tree attributes = NULL_TREE;
12080           tree first_attribute;
12081
12082           /* Peek at the next token.  */
12083           token = cp_lexer_peek_token (parser->lexer);
12084
12085           /* Check for a bitfield declaration.  */
12086           if (token->type == CPP_COLON
12087               || (token->type == CPP_NAME
12088                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type 
12089                   == CPP_COLON))
12090             {
12091               tree identifier;
12092               tree width;
12093
12094               /* Get the name of the bitfield.  Note that we cannot just
12095                  check TOKEN here because it may have been invalidated by
12096                  the call to cp_lexer_peek_nth_token above.  */
12097               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12098                 identifier = cp_parser_identifier (parser);
12099               else
12100                 identifier = NULL_TREE;
12101
12102               /* Consume the `:' token.  */
12103               cp_lexer_consume_token (parser->lexer);
12104               /* Get the width of the bitfield.  */
12105               width = cp_parser_constant_expression (parser);
12106
12107               /* Look for attributes that apply to the bitfield.  */
12108               attributes = cp_parser_attributes_opt (parser);
12109               /* Remember which attributes are prefix attributes and
12110                  which are not.  */
12111               first_attribute = attributes;
12112               /* Combine the attributes.  */
12113               attributes = chainon (prefix_attributes, attributes);
12114
12115               /* Create the bitfield declaration.  */
12116               decl = grokbitfield (identifier, 
12117                                    decl_specifiers,
12118                                    width);
12119               /* Apply the attributes.  */
12120               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12121             }
12122           else
12123             {
12124               tree declarator;
12125               tree initializer;
12126               tree asm_specification;
12127               bool ctor_dtor_or_conv_p;
12128
12129               /* Parse the declarator.  */
12130               declarator 
12131                 = cp_parser_declarator (parser,
12132                                         /*abstract_p=*/false,
12133                                         &ctor_dtor_or_conv_p);
12134
12135               /* If something went wrong parsing the declarator, make sure
12136                  that we at least consume some tokens.  */
12137               if (declarator == error_mark_node)
12138                 {
12139                   /* Skip to the end of the statement.  */
12140                   cp_parser_skip_to_end_of_statement (parser);
12141                   break;
12142                 }
12143
12144               /* Look for an asm-specification.  */
12145               asm_specification = cp_parser_asm_specification_opt (parser);
12146               /* Look for attributes that apply to the declaration.  */
12147               attributes = cp_parser_attributes_opt (parser);
12148               /* Remember which attributes are prefix attributes and
12149                  which are not.  */
12150               first_attribute = attributes;
12151               /* Combine the attributes.  */
12152               attributes = chainon (prefix_attributes, attributes);
12153
12154               /* If it's an `=', then we have a constant-initializer or a
12155                  pure-specifier.  It is not correct to parse the
12156                  initializer before registering the member declaration
12157                  since the member declaration should be in scope while
12158                  its initializer is processed.  However, the rest of the
12159                  front end does not yet provide an interface that allows
12160                  us to handle this correctly.  */
12161               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12162                 {
12163                   /* In [class.mem]:
12164
12165                      A pure-specifier shall be used only in the declaration of
12166                      a virtual function.  
12167
12168                      A member-declarator can contain a constant-initializer
12169                      only if it declares a static member of integral or
12170                      enumeration type.  
12171
12172                      Therefore, if the DECLARATOR is for a function, we look
12173                      for a pure-specifier; otherwise, we look for a
12174                      constant-initializer.  When we call `grokfield', it will
12175                      perform more stringent semantics checks.  */
12176                   if (TREE_CODE (declarator) == CALL_EXPR)
12177                     initializer = cp_parser_pure_specifier (parser);
12178                   else
12179                     {
12180                       /* This declaration cannot be a function
12181                          definition.  */
12182                       cp_parser_commit_to_tentative_parse (parser);
12183                       /* Parse the initializer.  */
12184                       initializer = cp_parser_constant_initializer (parser);
12185                     }
12186                 }
12187               /* Otherwise, there is no initializer.  */
12188               else
12189                 initializer = NULL_TREE;
12190
12191               /* See if we are probably looking at a function
12192                  definition.  We are certainly not looking at at a
12193                  member-declarator.  Calling `grokfield' has
12194                  side-effects, so we must not do it unless we are sure
12195                  that we are looking at a member-declarator.  */
12196               if (cp_parser_token_starts_function_definition_p 
12197                   (cp_lexer_peek_token (parser->lexer)))
12198                 decl = error_mark_node;
12199               else
12200                 /* Create the declaration.  */
12201                 decl = grokfield (declarator, 
12202                                   decl_specifiers, 
12203                                   initializer,
12204                                   asm_specification,
12205                                   attributes);
12206             }
12207
12208           /* Reset PREFIX_ATTRIBUTES.  */
12209           while (attributes && TREE_CHAIN (attributes) != first_attribute)
12210             attributes = TREE_CHAIN (attributes);
12211           if (attributes)
12212             TREE_CHAIN (attributes) = NULL_TREE;
12213
12214           /* If there is any qualification still in effect, clear it
12215              now; we will be starting fresh with the next declarator.  */
12216           parser->scope = NULL_TREE;
12217           parser->qualifying_scope = NULL_TREE;
12218           parser->object_scope = NULL_TREE;
12219           /* If it's a `,', then there are more declarators.  */
12220           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12221             cp_lexer_consume_token (parser->lexer);
12222           /* If the next token isn't a `;', then we have a parse error.  */
12223           else if (cp_lexer_next_token_is_not (parser->lexer,
12224                                                CPP_SEMICOLON))
12225             {
12226               cp_parser_error (parser, "expected `;'");
12227               /* Skip tokens until we find a `;'  */
12228               cp_parser_skip_to_end_of_statement (parser);
12229
12230               break;
12231             }
12232
12233           if (decl)
12234             {
12235               /* Add DECL to the list of members.  */
12236               if (!friend_p)
12237                 finish_member_declaration (decl);
12238
12239               /* If DECL is a function, we must return
12240                  to parse it later.  (Even though there is no definition,
12241                  there might be default arguments that need handling.)  */
12242               if (TREE_CODE (decl) == FUNCTION_DECL)
12243                 TREE_VALUE (parser->unparsed_functions_queues)
12244                   = tree_cons (current_class_type, decl, 
12245                                TREE_VALUE (parser->unparsed_functions_queues));
12246             }
12247         }
12248     }
12249
12250   /* If everything went well, look for the `;'.  */
12251   if (cp_parser_parse_definitely (parser))
12252     {
12253       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12254       return;
12255     }
12256
12257   /* Parse the function-definition.  */
12258   decl = cp_parser_function_definition (parser, &friend_p);
12259   /* If the member was not a friend, declare it here.  */
12260   if (!friend_p)
12261     finish_member_declaration (decl);
12262   /* Peek at the next token.  */
12263   token = cp_lexer_peek_token (parser->lexer);
12264   /* If the next token is a semicolon, consume it.  */
12265   if (token->type == CPP_SEMICOLON)
12266     cp_lexer_consume_token (parser->lexer);
12267 }
12268
12269 /* Parse a pure-specifier.
12270
12271    pure-specifier:
12272      = 0
12273
12274    Returns INTEGER_ZERO_NODE if a pure specifier is found.
12275    Otherwiser, ERROR_MARK_NODE is returned.  */
12276
12277 static tree
12278 cp_parser_pure_specifier (parser)
12279      cp_parser *parser;
12280 {
12281   cp_token *token;
12282
12283   /* Look for the `=' token.  */
12284   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12285     return error_mark_node;
12286   /* Look for the `0' token.  */
12287   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12288   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
12289      to get information from the lexer about how the number was
12290      spelled in order to fix this problem.  */
12291   if (!token || !integer_zerop (token->value))
12292     return error_mark_node;
12293
12294   return integer_zero_node;
12295 }
12296
12297 /* Parse a constant-initializer.
12298
12299    constant-initializer:
12300      = constant-expression
12301
12302    Returns a representation of the constant-expression.  */
12303
12304 static tree
12305 cp_parser_constant_initializer (parser)
12306      cp_parser *parser;
12307 {
12308   /* Look for the `=' token.  */
12309   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12310     return error_mark_node;
12311
12312   /* It is invalid to write:
12313
12314        struct S { static const int i = { 7 }; };
12315
12316      */
12317   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12318     {
12319       cp_parser_error (parser,
12320                        "a brace-enclosed initializer is not allowed here");
12321       /* Consume the opening brace.  */
12322       cp_lexer_consume_token (parser->lexer);
12323       /* Skip the initializer.  */
12324       cp_parser_skip_to_closing_brace (parser);
12325       /* Look for the trailing `}'.  */
12326       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12327       
12328       return error_mark_node;
12329     }
12330
12331   return cp_parser_constant_expression (parser);
12332 }
12333
12334 /* Derived classes [gram.class.derived] */
12335
12336 /* Parse a base-clause.
12337
12338    base-clause:
12339      : base-specifier-list  
12340
12341    base-specifier-list:
12342      base-specifier
12343      base-specifier-list , base-specifier
12344
12345    Returns a TREE_LIST representing the base-classes, in the order in
12346    which they were declared.  The representation of each node is as
12347    described by cp_parser_base_specifier.  
12348
12349    In the case that no bases are specified, this function will return
12350    NULL_TREE, not ERROR_MARK_NODE.  */
12351
12352 static tree
12353 cp_parser_base_clause (parser)
12354      cp_parser *parser;
12355 {
12356   tree bases = NULL_TREE;
12357
12358   /* Look for the `:' that begins the list.  */
12359   cp_parser_require (parser, CPP_COLON, "`:'");
12360
12361   /* Scan the base-specifier-list.  */
12362   while (true)
12363     {
12364       cp_token *token;
12365       tree base;
12366
12367       /* Look for the base-specifier.  */
12368       base = cp_parser_base_specifier (parser);
12369       /* Add BASE to the front of the list.  */
12370       if (base != error_mark_node)
12371         {
12372           TREE_CHAIN (base) = bases;
12373           bases = base;
12374         }
12375       /* Peek at the next token.  */
12376       token = cp_lexer_peek_token (parser->lexer);
12377       /* If it's not a comma, then the list is complete.  */
12378       if (token->type != CPP_COMMA)
12379         break;
12380       /* Consume the `,'.  */
12381       cp_lexer_consume_token (parser->lexer);
12382     }
12383
12384   /* PARSER->SCOPE may still be non-NULL at this point, if the last
12385      base class had a qualified name.  However, the next name that
12386      appears is certainly not qualified.  */
12387   parser->scope = NULL_TREE;
12388   parser->qualifying_scope = NULL_TREE;
12389   parser->object_scope = NULL_TREE;
12390
12391   return nreverse (bases);
12392 }
12393
12394 /* Parse a base-specifier.
12395
12396    base-specifier:
12397      :: [opt] nested-name-specifier [opt] class-name
12398      virtual access-specifier [opt] :: [opt] nested-name-specifier
12399        [opt] class-name
12400      access-specifier virtual [opt] :: [opt] nested-name-specifier
12401        [opt] class-name
12402
12403    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
12404    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12405    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
12406    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
12407        
12408 static tree
12409 cp_parser_base_specifier (parser)
12410      cp_parser *parser;
12411 {
12412   cp_token *token;
12413   bool done = false;
12414   bool virtual_p = false;
12415   bool duplicate_virtual_error_issued_p = false;
12416   bool duplicate_access_error_issued_p = false;
12417   bool class_scope_p;
12418   access_kind access = ak_none;
12419   tree access_node;
12420   tree type;
12421
12422   /* Process the optional `virtual' and `access-specifier'.  */
12423   while (!done)
12424     {
12425       /* Peek at the next token.  */
12426       token = cp_lexer_peek_token (parser->lexer);
12427       /* Process `virtual'.  */
12428       switch (token->keyword)
12429         {
12430         case RID_VIRTUAL:
12431           /* If `virtual' appears more than once, issue an error.  */
12432           if (virtual_p && !duplicate_virtual_error_issued_p)
12433             {
12434               cp_parser_error (parser,
12435                                "`virtual' specified more than once in base-specified");
12436               duplicate_virtual_error_issued_p = true;
12437             }
12438
12439           virtual_p = true;
12440
12441           /* Consume the `virtual' token.  */
12442           cp_lexer_consume_token (parser->lexer);
12443
12444           break;
12445
12446         case RID_PUBLIC:
12447         case RID_PROTECTED:
12448         case RID_PRIVATE:
12449           /* If more than one access specifier appears, issue an
12450              error.  */
12451           if (access != ak_none && !duplicate_access_error_issued_p)
12452             {
12453               cp_parser_error (parser,
12454                                "more than one access specifier in base-specified");
12455               duplicate_access_error_issued_p = true;
12456             }
12457
12458           access = ((access_kind) 
12459                     tree_low_cst (ridpointers[(int) token->keyword],
12460                                   /*pos=*/1));
12461
12462           /* Consume the access-specifier.  */
12463           cp_lexer_consume_token (parser->lexer);
12464
12465           break;
12466
12467         default:
12468           done = true;
12469           break;
12470         }
12471     }
12472
12473   /* Map `virtual_p' and `access' onto one of the access 
12474      tree-nodes.  */
12475   if (!virtual_p)
12476     switch (access)
12477       {
12478       case ak_none:
12479         access_node = access_default_node;
12480         break;
12481       case ak_public:
12482         access_node = access_public_node;
12483         break;
12484       case ak_protected:
12485         access_node = access_protected_node;
12486         break;
12487       case ak_private:
12488         access_node = access_private_node;
12489         break;
12490       default:
12491         abort ();
12492       }
12493   else
12494     switch (access)
12495       {
12496       case ak_none:
12497         access_node = access_default_virtual_node;
12498         break;
12499       case ak_public:
12500         access_node = access_public_virtual_node;
12501         break;
12502       case ak_protected:
12503         access_node = access_protected_virtual_node;
12504         break;
12505       case ak_private:
12506         access_node = access_private_virtual_node;
12507         break;
12508       default:
12509         abort ();
12510       }
12511
12512   /* Look for the optional `::' operator.  */
12513   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12514   /* Look for the nested-name-specifier.  The simplest way to
12515      implement:
12516
12517        [temp.res]
12518
12519        The keyword `typename' is not permitted in a base-specifier or
12520        mem-initializer; in these contexts a qualified name that
12521        depends on a template-parameter is implicitly assumed to be a
12522        type name.
12523
12524      is to pretend that we have seen the `typename' keyword at this
12525      point.  */ 
12526   cp_parser_nested_name_specifier_opt (parser,
12527                                        /*typename_keyword_p=*/true,
12528                                        /*check_dependency_p=*/true,
12529                                        /*type_p=*/true);
12530   /* If the base class is given by a qualified name, assume that names
12531      we see are type names or templates, as appropriate.  */
12532   class_scope_p = (parser->scope && TYPE_P (parser->scope));
12533   /* Finally, look for the class-name.  */
12534   type = cp_parser_class_name (parser, 
12535                                class_scope_p,
12536                                class_scope_p,
12537                                /*type_p=*/true,
12538                                /*check_access=*/true,
12539                                /*check_dependency_p=*/true,
12540                                /*class_head_p=*/false);
12541
12542   if (type == error_mark_node)
12543     return error_mark_node;
12544
12545   return finish_base_specifier (access_node, TREE_TYPE (type));
12546 }
12547
12548 /* Exception handling [gram.exception] */
12549
12550 /* Parse an (optional) exception-specification.
12551
12552    exception-specification:
12553      throw ( type-id-list [opt] )
12554
12555    Returns a TREE_LIST representing the exception-specification.  The
12556    TREE_VALUE of each node is a type.  */
12557
12558 static tree
12559 cp_parser_exception_specification_opt (parser)
12560      cp_parser *parser;
12561 {
12562   cp_token *token;
12563   tree type_id_list;
12564
12565   /* Peek at the next token.  */
12566   token = cp_lexer_peek_token (parser->lexer);
12567   /* If it's not `throw', then there's no exception-specification.  */
12568   if (!cp_parser_is_keyword (token, RID_THROW))
12569     return NULL_TREE;
12570
12571   /* Consume the `throw'.  */
12572   cp_lexer_consume_token (parser->lexer);
12573
12574   /* Look for the `('.  */
12575   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12576
12577   /* Peek at the next token.  */
12578   token = cp_lexer_peek_token (parser->lexer);
12579   /* If it's not a `)', then there is a type-id-list.  */
12580   if (token->type != CPP_CLOSE_PAREN)
12581     {
12582       const char *saved_message;
12583
12584       /* Types may not be defined in an exception-specification.  */
12585       saved_message = parser->type_definition_forbidden_message;
12586       parser->type_definition_forbidden_message
12587         = "types may not be defined in an exception-specification";
12588       /* Parse the type-id-list.  */
12589       type_id_list = cp_parser_type_id_list (parser);
12590       /* Restore the saved message.  */
12591       parser->type_definition_forbidden_message = saved_message;
12592     }
12593   else
12594     type_id_list = empty_except_spec;
12595
12596   /* Look for the `)'.  */
12597   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12598
12599   return type_id_list;
12600 }
12601
12602 /* Parse an (optional) type-id-list.
12603
12604    type-id-list:
12605      type-id
12606      type-id-list , type-id
12607
12608    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
12609    in the order that the types were presented.  */
12610
12611 static tree
12612 cp_parser_type_id_list (parser)
12613      cp_parser *parser;
12614 {
12615   tree types = NULL_TREE;
12616
12617   while (true)
12618     {
12619       cp_token *token;
12620       tree type;
12621
12622       /* Get the next type-id.  */
12623       type = cp_parser_type_id (parser);
12624       /* Add it to the list.  */
12625       types = add_exception_specifier (types, type, /*complain=*/1);
12626       /* Peek at the next token.  */
12627       token = cp_lexer_peek_token (parser->lexer);
12628       /* If it is not a `,', we are done.  */
12629       if (token->type != CPP_COMMA)
12630         break;
12631       /* Consume the `,'.  */
12632       cp_lexer_consume_token (parser->lexer);
12633     }
12634
12635   return nreverse (types);
12636 }
12637
12638 /* Parse a try-block.
12639
12640    try-block:
12641      try compound-statement handler-seq  */
12642
12643 static tree
12644 cp_parser_try_block (parser)
12645      cp_parser *parser;
12646 {
12647   tree try_block;
12648
12649   cp_parser_require_keyword (parser, RID_TRY, "`try'");
12650   try_block = begin_try_block ();
12651   cp_parser_compound_statement (parser);
12652   finish_try_block (try_block);
12653   cp_parser_handler_seq (parser);
12654   finish_handler_sequence (try_block);
12655
12656   return try_block;
12657 }
12658
12659 /* Parse a function-try-block.
12660
12661    function-try-block:
12662      try ctor-initializer [opt] function-body handler-seq  */
12663
12664 static bool
12665 cp_parser_function_try_block (parser)
12666      cp_parser *parser;
12667 {
12668   tree try_block;
12669   bool ctor_initializer_p;
12670
12671   /* Look for the `try' keyword.  */
12672   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12673     return false;
12674   /* Let the rest of the front-end know where we are.  */
12675   try_block = begin_function_try_block ();
12676   /* Parse the function-body.  */
12677   ctor_initializer_p 
12678     = cp_parser_ctor_initializer_opt_and_function_body (parser);
12679   /* We're done with the `try' part.  */
12680   finish_function_try_block (try_block);
12681   /* Parse the handlers.  */
12682   cp_parser_handler_seq (parser);
12683   /* We're done with the handlers.  */
12684   finish_function_handler_sequence (try_block);
12685
12686   return ctor_initializer_p;
12687 }
12688
12689 /* Parse a handler-seq.
12690
12691    handler-seq:
12692      handler handler-seq [opt]  */
12693
12694 static void
12695 cp_parser_handler_seq (parser)
12696      cp_parser *parser;
12697 {
12698   while (true)
12699     {
12700       cp_token *token;
12701
12702       /* Parse the handler.  */
12703       cp_parser_handler (parser);
12704       /* Peek at the next token.  */
12705       token = cp_lexer_peek_token (parser->lexer);
12706       /* If it's not `catch' then there are no more handlers.  */
12707       if (!cp_parser_is_keyword (token, RID_CATCH))
12708         break;
12709     }
12710 }
12711
12712 /* Parse a handler.
12713
12714    handler:
12715      catch ( exception-declaration ) compound-statement  */
12716
12717 static void
12718 cp_parser_handler (parser)
12719      cp_parser *parser;
12720 {
12721   tree handler;
12722   tree declaration;
12723
12724   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12725   handler = begin_handler ();
12726   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12727   declaration = cp_parser_exception_declaration (parser);
12728   finish_handler_parms (declaration, handler);
12729   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12730   cp_parser_compound_statement (parser);
12731   finish_handler (handler);
12732 }
12733
12734 /* Parse an exception-declaration.
12735
12736    exception-declaration:
12737      type-specifier-seq declarator
12738      type-specifier-seq abstract-declarator
12739      type-specifier-seq
12740      ...  
12741
12742    Returns a VAR_DECL for the declaration, or NULL_TREE if the
12743    ellipsis variant is used.  */
12744
12745 static tree
12746 cp_parser_exception_declaration (parser)
12747      cp_parser *parser;
12748 {
12749   tree type_specifiers;
12750   tree declarator;
12751   const char *saved_message;
12752
12753   /* If it's an ellipsis, it's easy to handle.  */
12754   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12755     {
12756       /* Consume the `...' token.  */
12757       cp_lexer_consume_token (parser->lexer);
12758       return NULL_TREE;
12759     }
12760
12761   /* Types may not be defined in exception-declarations.  */
12762   saved_message = parser->type_definition_forbidden_message;
12763   parser->type_definition_forbidden_message
12764     = "types may not be defined in exception-declarations";
12765
12766   /* Parse the type-specifier-seq.  */
12767   type_specifiers = cp_parser_type_specifier_seq (parser);
12768   /* If it's a `)', then there is no declarator.  */
12769   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12770     declarator = NULL_TREE;
12771   else
12772     {
12773       /* Otherwise, we can't be sure whether we are looking at a
12774          direct, or an abstract, declarator.  */
12775       cp_parser_parse_tentatively (parser);
12776       /* Try an ordinary declarator.  */
12777       declarator = cp_parser_declarator (parser,
12778                                          /*abstract_p=*/false,
12779                                          /*ctor_dtor_or_conv_p=*/NULL);
12780       /* If that didn't work, try an abstract declarator.  */
12781       if (!cp_parser_parse_definitely (parser))
12782         declarator = cp_parser_declarator (parser,
12783                                            /*abstract_p=*/true,
12784                                            /*ctor_dtor_or_conv_p=*/NULL);
12785     }
12786
12787   /* Restore the saved message.  */
12788   parser->type_definition_forbidden_message = saved_message;
12789
12790   return start_handler_parms (type_specifiers, declarator);
12791 }
12792
12793 /* Parse a throw-expression. 
12794
12795    throw-expression:
12796      throw assignment-expresion [opt]
12797
12798    Returns a THROW_EXPR representing the throw-expression.  */
12799
12800 static tree
12801 cp_parser_throw_expression (parser)
12802      cp_parser *parser;
12803 {
12804   tree expression;
12805
12806   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12807   /* We can't be sure if there is an assignment-expression or not.  */
12808   cp_parser_parse_tentatively (parser);
12809   /* Try it.  */
12810   expression = cp_parser_assignment_expression (parser);
12811   /* If it didn't work, this is just a rethrow.  */
12812   if (!cp_parser_parse_definitely (parser))
12813     expression = NULL_TREE;
12814
12815   return build_throw (expression);
12816 }
12817
12818 /* GNU Extensions */
12819
12820 /* Parse an (optional) asm-specification.
12821
12822    asm-specification:
12823      asm ( string-literal )
12824
12825    If the asm-specification is present, returns a STRING_CST
12826    corresponding to the string-literal.  Otherwise, returns
12827    NULL_TREE.  */
12828
12829 static tree
12830 cp_parser_asm_specification_opt (parser)
12831      cp_parser *parser;
12832 {
12833   cp_token *token;
12834   tree asm_specification;
12835
12836   /* Peek at the next token.  */
12837   token = cp_lexer_peek_token (parser->lexer);
12838   /* If the next token isn't the `asm' keyword, then there's no 
12839      asm-specification.  */
12840   if (!cp_parser_is_keyword (token, RID_ASM))
12841     return NULL_TREE;
12842
12843   /* Consume the `asm' token.  */
12844   cp_lexer_consume_token (parser->lexer);
12845   /* Look for the `('.  */
12846   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12847
12848   /* Look for the string-literal.  */
12849   token = cp_parser_require (parser, CPP_STRING, "string-literal");
12850   if (token)
12851     asm_specification = token->value;
12852   else
12853     asm_specification = NULL_TREE;
12854
12855   /* Look for the `)'.  */
12856   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12857
12858   return asm_specification;
12859 }
12860
12861 /* Parse an asm-operand-list.  
12862
12863    asm-operand-list:
12864      asm-operand
12865      asm-operand-list , asm-operand
12866      
12867    asm-operand:
12868      string-literal ( expression )  
12869      [ string-literal ] string-literal ( expression )
12870
12871    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
12872    each node is the expression.  The TREE_PURPOSE is itself a
12873    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12874    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12875    is a STRING_CST for the string literal before the parenthesis.  */
12876
12877 static tree
12878 cp_parser_asm_operand_list (parser)
12879      cp_parser *parser;
12880 {
12881   tree asm_operands = NULL_TREE;
12882
12883   while (true)
12884     {
12885       tree string_literal;
12886       tree expression;
12887       tree name;
12888       cp_token *token;
12889       
12890       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 
12891         {
12892           /* Consume the `[' token.  */
12893           cp_lexer_consume_token (parser->lexer);
12894           /* Read the operand name.  */
12895           name = cp_parser_identifier (parser);
12896           if (name != error_mark_node) 
12897             name = build_string (IDENTIFIER_LENGTH (name),
12898                                  IDENTIFIER_POINTER (name));
12899           /* Look for the closing `]'.  */
12900           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12901         }
12902       else
12903         name = NULL_TREE;
12904       /* Look for the string-literal.  */
12905       token = cp_parser_require (parser, CPP_STRING, "string-literal");
12906       string_literal = token ? token->value : error_mark_node;
12907       /* Look for the `('.  */
12908       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12909       /* Parse the expression.  */
12910       expression = cp_parser_expression (parser);
12911       /* Look for the `)'.  */
12912       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12913       /* Add this operand to the list.  */
12914       asm_operands = tree_cons (build_tree_list (name, string_literal),
12915                                 expression, 
12916                                 asm_operands);
12917       /* If the next token is not a `,', there are no more 
12918          operands.  */
12919       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12920         break;
12921       /* Consume the `,'.  */
12922       cp_lexer_consume_token (parser->lexer);
12923     }
12924
12925   return nreverse (asm_operands);
12926 }
12927
12928 /* Parse an asm-clobber-list.  
12929
12930    asm-clobber-list:
12931      string-literal
12932      asm-clobber-list , string-literal  
12933
12934    Returns a TREE_LIST, indicating the clobbers in the order that they
12935    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
12936
12937 static tree
12938 cp_parser_asm_clobber_list (parser)
12939      cp_parser *parser;
12940 {
12941   tree clobbers = NULL_TREE;
12942
12943   while (true)
12944     {
12945       cp_token *token;
12946       tree string_literal;
12947
12948       /* Look for the string literal.  */
12949       token = cp_parser_require (parser, CPP_STRING, "string-literal");
12950       string_literal = token ? token->value : error_mark_node;
12951       /* Add it to the list.  */
12952       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12953       /* If the next token is not a `,', then the list is 
12954          complete.  */
12955       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12956         break;
12957       /* Consume the `,' token.  */
12958       cp_lexer_consume_token (parser->lexer);
12959     }
12960
12961   return clobbers;
12962 }
12963
12964 /* Parse an (optional) series of attributes.
12965
12966    attributes:
12967      attributes attribute
12968
12969    attribute:
12970      __attribute__ (( attribute-list [opt] ))  
12971
12972    The return value is as for cp_parser_attribute_list.  */
12973      
12974 static tree
12975 cp_parser_attributes_opt (parser)
12976      cp_parser *parser;
12977 {
12978   tree attributes = NULL_TREE;
12979
12980   while (true)
12981     {
12982       cp_token *token;
12983       tree attribute_list;
12984
12985       /* Peek at the next token.  */
12986       token = cp_lexer_peek_token (parser->lexer);
12987       /* If it's not `__attribute__', then we're done.  */
12988       if (token->keyword != RID_ATTRIBUTE)
12989         break;
12990
12991       /* Consume the `__attribute__' keyword.  */
12992       cp_lexer_consume_token (parser->lexer);
12993       /* Look for the two `(' tokens.  */
12994       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12995       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12996
12997       /* Peek at the next token.  */
12998       token = cp_lexer_peek_token (parser->lexer);
12999       if (token->type != CPP_CLOSE_PAREN)
13000         /* Parse the attribute-list.  */
13001         attribute_list = cp_parser_attribute_list (parser);
13002       else
13003         /* If the next token is a `)', then there is no attribute
13004            list.  */
13005         attribute_list = NULL;
13006
13007       /* Look for the two `)' tokens.  */
13008       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13009       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13010
13011       /* Add these new attributes to the list.  */
13012       attributes = chainon (attributes, attribute_list);
13013     }
13014
13015   return attributes;
13016 }
13017
13018 /* Parse an attribute-list.  
13019
13020    attribute-list:  
13021      attribute 
13022      attribute-list , attribute
13023
13024    attribute:
13025      identifier     
13026      identifier ( identifier )
13027      identifier ( identifier , expression-list )
13028      identifier ( expression-list ) 
13029
13030    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13031    TREE_PURPOSE of each node is the identifier indicating which
13032    attribute is in use.  The TREE_VALUE represents the arguments, if
13033    any.  */
13034
13035 static tree
13036 cp_parser_attribute_list (parser)
13037      cp_parser *parser;
13038 {
13039   tree attribute_list = NULL_TREE;
13040
13041   while (true)
13042     {
13043       cp_token *token;
13044       tree identifier;
13045       tree attribute;
13046
13047       /* Look for the identifier.  We also allow keywords here; for
13048          example `__attribute__ ((const))' is legal.  */
13049       token = cp_lexer_peek_token (parser->lexer);
13050       if (token->type != CPP_NAME 
13051           && token->type != CPP_KEYWORD)
13052         return error_mark_node;
13053       /* Consume the token.  */
13054       token = cp_lexer_consume_token (parser->lexer);
13055       
13056       /* Save away the identifier that indicates which attribute this is.  */
13057       identifier = token->value;
13058       attribute = build_tree_list (identifier, NULL_TREE);
13059
13060       /* Peek at the next token.  */
13061       token = cp_lexer_peek_token (parser->lexer);
13062       /* If it's an `(', then parse the attribute arguments.  */
13063       if (token->type == CPP_OPEN_PAREN)
13064         {
13065           tree arguments;
13066           int arguments_allowed_p = 1;
13067
13068           /* Consume the `('.  */
13069           cp_lexer_consume_token (parser->lexer);
13070           /* Peek at the next token.  */
13071           token = cp_lexer_peek_token (parser->lexer);
13072           /* Check to see if the next token is an identifier.  */
13073           if (token->type == CPP_NAME)
13074             {
13075               /* Save the identifier.  */
13076               identifier = token->value;
13077               /* Consume the identifier.  */
13078               cp_lexer_consume_token (parser->lexer);
13079               /* Peek at the next token.  */
13080               token = cp_lexer_peek_token (parser->lexer);
13081               /* If the next token is a `,', then there are some other
13082                  expressions as well.  */
13083               if (token->type == CPP_COMMA)
13084                 /* Consume the comma.  */
13085                 cp_lexer_consume_token (parser->lexer);
13086               else
13087                 arguments_allowed_p = 0;
13088             }
13089           else
13090             identifier = NULL_TREE;
13091
13092           /* If there are arguments, parse them too.  */
13093           if (arguments_allowed_p)
13094             arguments = cp_parser_expression_list (parser);
13095           else
13096             arguments = NULL_TREE;
13097
13098           /* Combine the identifier and the arguments.  */
13099           if (identifier)
13100             arguments = tree_cons (NULL_TREE, identifier, arguments);
13101
13102           /* Save the identifier and arguments away.  */
13103           TREE_VALUE (attribute) = arguments;
13104
13105           /* Look for the closing `)'.  */
13106           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13107         }
13108
13109       /* Add this attribute to the list.  */
13110       TREE_CHAIN (attribute) = attribute_list;
13111       attribute_list = attribute;
13112
13113       /* Now, look for more attributes.  */
13114       token = cp_lexer_peek_token (parser->lexer);
13115       /* If the next token isn't a `,', we're done.  */
13116       if (token->type != CPP_COMMA)
13117         break;
13118
13119       /* Consume the commma and keep going.  */
13120       cp_lexer_consume_token (parser->lexer);
13121     }
13122
13123   /* We built up the list in reverse order.  */
13124   return nreverse (attribute_list);
13125 }
13126
13127 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13128    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13129    current value of the PEDANTIC flag, regardless of whether or not
13130    the `__extension__' keyword is present.  The caller is responsible
13131    for restoring the value of the PEDANTIC flag.  */
13132
13133 static bool
13134 cp_parser_extension_opt (parser, saved_pedantic)
13135      cp_parser *parser;
13136      int *saved_pedantic;
13137 {
13138   /* Save the old value of the PEDANTIC flag.  */
13139   *saved_pedantic = pedantic;
13140
13141   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13142     {
13143       /* Consume the `__extension__' token.  */
13144       cp_lexer_consume_token (parser->lexer);
13145       /* We're not being pedantic while the `__extension__' keyword is
13146          in effect.  */
13147       pedantic = 0;
13148
13149       return true;
13150     }
13151
13152   return false;
13153 }
13154
13155 /* Parse a label declaration.
13156
13157    label-declaration:
13158      __label__ label-declarator-seq ;
13159
13160    label-declarator-seq:
13161      identifier , label-declarator-seq
13162      identifier  */
13163
13164 static void
13165 cp_parser_label_declaration (parser)
13166      cp_parser *parser;
13167 {
13168   /* Look for the `__label__' keyword.  */
13169   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13170
13171   while (true)
13172     {
13173       tree identifier;
13174
13175       /* Look for an identifier.  */
13176       identifier = cp_parser_identifier (parser);
13177       /* Declare it as a lobel.  */
13178       finish_label_decl (identifier);
13179       /* If the next token is a `;', stop.  */
13180       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13181         break;
13182       /* Look for the `,' separating the label declarations.  */
13183       cp_parser_require (parser, CPP_COMMA, "`,'");
13184     }
13185
13186   /* Look for the final `;'.  */
13187   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13188 }
13189
13190 /* Support Functions */
13191
13192 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13193    NAME should have one of the representations used for an
13194    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13195    is returned.  If PARSER->SCOPE is a dependent type, then a
13196    SCOPE_REF is returned.
13197
13198    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13199    returned; the name was already resolved when the TEMPLATE_ID_EXPR
13200    was formed.  Abstractly, such entities should not be passed to this
13201    function, because they do not need to be looked up, but it is
13202    simpler to check for this special case here, rather than at the
13203    call-sites.
13204
13205    In cases not explicitly covered above, this function returns a
13206    DECL, OVERLOAD, or baselink representing the result of the lookup.
13207    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13208    is returned.
13209
13210    If CHECK_ACCESS is TRUE, then access control is performed on the
13211    declaration to which the name resolves, and an error message is
13212    issued if the declaration is inaccessible.
13213
13214    If IS_TYPE is TRUE, bindings that do not refer to types are
13215    ignored.
13216
13217    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13218    types.  */
13219
13220 static tree
13221 cp_parser_lookup_name (parser, name, check_access, is_type, 
13222                        check_dependency)
13223      cp_parser *parser;
13224      tree name;
13225      bool check_access;
13226      bool is_type;
13227      bool check_dependency;
13228 {
13229   tree decl;
13230   tree object_type = parser->context->object_type;
13231
13232   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13233      no longer valid.  Note that if we are parsing tentatively, and
13234      the parse fails, OBJECT_TYPE will be automatically restored.  */
13235   parser->context->object_type = NULL_TREE;
13236
13237   if (name == error_mark_node)
13238     return error_mark_node;
13239
13240   /* A template-id has already been resolved; there is no lookup to
13241      do.  */
13242   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13243     return name;
13244   if (BASELINK_P (name))
13245     {
13246       my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13247                            == TEMPLATE_ID_EXPR),
13248                           20020909);
13249       return name;
13250     }
13251
13252   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
13253      it should already have been checked to make sure that the name
13254      used matches the type being destroyed.  */
13255   if (TREE_CODE (name) == BIT_NOT_EXPR)
13256     {
13257       tree type;
13258
13259       /* Figure out to which type this destructor applies.  */
13260       if (parser->scope)
13261         type = parser->scope;
13262       else if (object_type)
13263         type = object_type;
13264       else
13265         type = current_class_type;
13266       /* If that's not a class type, there is no destructor.  */
13267       if (!type || !CLASS_TYPE_P (type))
13268         return error_mark_node;
13269       /* If it was a class type, return the destructor.  */
13270       return CLASSTYPE_DESTRUCTORS (type);
13271     }
13272
13273   /* By this point, the NAME should be an ordinary identifier.  If
13274      the id-expression was a qualified name, the qualifying scope is
13275      stored in PARSER->SCOPE at this point.  */
13276   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13277                       20000619);
13278   
13279   /* Perform the lookup.  */
13280   if (parser->scope)
13281     { 
13282       bool dependent_type_p;
13283
13284       if (parser->scope == error_mark_node)
13285         return error_mark_node;
13286
13287       /* If the SCOPE is dependent, the lookup must be deferred until
13288          the template is instantiated -- unless we are explicitly
13289          looking up names in uninstantiated templates.  Even then, we
13290          cannot look up the name if the scope is not a class type; it
13291          might, for example, be a template type parameter.  */
13292       dependent_type_p = (TYPE_P (parser->scope)
13293                           && !(parser->in_declarator_p
13294                                && currently_open_class (parser->scope))
13295                           && cp_parser_dependent_type_p (parser->scope));
13296       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13297            && dependent_type_p)
13298         {
13299           if (!is_type)
13300             decl = build_nt (SCOPE_REF, parser->scope, name);
13301           else
13302             /* The resolution to Core Issue 180 says that `struct A::B'
13303                should be considered a type-name, even if `A' is
13304                dependent.  */
13305             decl = TYPE_NAME (make_typename_type (parser->scope,
13306                                                   name,
13307                                                   /*complain=*/1));
13308         }
13309       else
13310         {
13311           /* If PARSER->SCOPE is a dependent type, then it must be a
13312              class type, and we must not be checking dependencies;
13313              otherwise, we would have processed this lookup above.  So
13314              that PARSER->SCOPE is not considered a dependent base by
13315              lookup_member, we must enter the scope here.  */
13316           if (dependent_type_p)
13317             push_scope (parser->scope);
13318           /* If the PARSER->SCOPE is a a template specialization, it
13319              may be instantiated during name lookup.  In that case,
13320              errors may be issued.  Even if we rollback the current
13321              tentative parse, those errors are valid.  */
13322           decl = lookup_qualified_name (parser->scope, name, is_type,
13323                                         /*flags=*/0);
13324           if (dependent_type_p)
13325             pop_scope (parser->scope);
13326         }
13327       parser->qualifying_scope = parser->scope;
13328       parser->object_scope = NULL_TREE;
13329     }
13330   else if (object_type)
13331     {
13332       tree object_decl = NULL_TREE;
13333       /* Look up the name in the scope of the OBJECT_TYPE, unless the
13334          OBJECT_TYPE is not a class.  */
13335       if (CLASS_TYPE_P (object_type))
13336         /* If the OBJECT_TYPE is a template specialization, it may
13337            be instantiated during name lookup.  In that case, errors
13338            may be issued.  Even if we rollback the current tentative
13339            parse, those errors are valid.  */
13340         object_decl = lookup_member (object_type,
13341                                      name,
13342                                      /*protect=*/0, is_type);
13343       /* Look it up in the enclosing context, too.  */
13344       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13345                                /*namespaces_only=*/0, 
13346                                /*flags=*/0);
13347       parser->object_scope = object_type;
13348       parser->qualifying_scope = NULL_TREE;
13349       if (object_decl)
13350         decl = object_decl;
13351     }
13352   else
13353     {
13354       decl = lookup_name_real (name, is_type, /*nonclass=*/0, 
13355                                /*namespaces_only=*/0, 
13356                                /*flags=*/0);
13357       parser->qualifying_scope = NULL_TREE;
13358       parser->object_scope = NULL_TREE;
13359     }
13360
13361   /* If the lookup failed, let our caller know.  */
13362   if (!decl 
13363       || decl == error_mark_node
13364       || (TREE_CODE (decl) == FUNCTION_DECL 
13365           && DECL_ANTICIPATED (decl)))
13366     return error_mark_node;
13367
13368   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
13369   if (TREE_CODE (decl) == TREE_LIST)
13370     {
13371       /* The error message we have to print is too complicated for
13372          cp_parser_error, so we incorporate its actions directly.  */
13373       cp_parser_simulate_error (parser);
13374       if (!cp_parser_parsing_tentatively (parser)
13375           || cp_parser_committed_to_tentative_parse (parser))
13376         {
13377           error ("reference to `%D' is ambiguous", name);
13378           print_candidates (decl);
13379         }
13380       return error_mark_node;
13381     }
13382
13383   my_friendly_assert (DECL_P (decl) 
13384                       || TREE_CODE (decl) == OVERLOAD
13385                       || TREE_CODE (decl) == SCOPE_REF
13386                       || BASELINK_P (decl),
13387                       20000619);
13388
13389   /* If we have resolved the name of a member declaration, check to
13390      see if the declaration is accessible.  When the name resolves to
13391      set of overloaded functions, accesibility is checked when
13392      overload resolution is done.  
13393
13394      During an explicit instantiation, access is not checked at all,
13395      as per [temp.explicit].  */
13396   if (check_access && scope_chain->check_access && DECL_P (decl))
13397     {
13398       tree qualifying_type;
13399       
13400       /* Figure out the type through which DECL is being
13401          accessed.  */
13402       qualifying_type 
13403         = cp_parser_scope_through_which_access_occurs (decl,
13404                                                        object_type,
13405                                                        parser->scope);
13406       if (qualifying_type)
13407         {
13408           /* If we are supposed to defer access checks, just record
13409              the information for later.  */
13410           if (parser->context->deferring_access_checks_p)
13411             cp_parser_defer_access_check (parser, qualifying_type, decl);
13412           /* Otherwise, check accessibility now.  */
13413           else
13414             enforce_access (qualifying_type, decl);
13415         }
13416     }
13417
13418   return decl;
13419 }
13420
13421 /* Like cp_parser_lookup_name, but for use in the typical case where
13422    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13423    TRUE.  */
13424
13425 static tree
13426 cp_parser_lookup_name_simple (parser, name)
13427      cp_parser *parser;
13428      tree name;
13429 {
13430   return cp_parser_lookup_name (parser, name, 
13431                                 /*check_access=*/true,
13432                                 /*is_type=*/false, 
13433                                 /*check_dependency=*/true);
13434 }
13435
13436 /* TYPE is a TYPENAME_TYPE.  Returns the ordinary TYPE to which the
13437    TYPENAME_TYPE corresponds.  Note that this function peers inside
13438    uninstantiated templates and therefore should be used only in
13439    extremely limited situations.  */
13440
13441 static tree
13442 cp_parser_resolve_typename_type (parser, type)
13443      cp_parser *parser;
13444      tree type;
13445 {
13446   tree scope;
13447   tree name;
13448   tree decl;
13449
13450   my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13451                       20010702);
13452
13453   scope = TYPE_CONTEXT (type);
13454   name = DECL_NAME (TYPE_NAME (type));
13455
13456   /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13457      it first before we can figure out what NAME refers to.  */
13458   if (TREE_CODE (scope) == TYPENAME_TYPE)
13459     scope = cp_parser_resolve_typename_type (parser, scope);
13460   /* If we don't know what SCOPE refers to, then we cannot resolve the
13461      TYPENAME_TYPE.  */
13462   if (scope == error_mark_node)
13463     return error_mark_node;
13464   /* If the SCOPE is a template type parameter, we have no way of
13465      resolving the name.  */
13466   if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13467     return type;
13468   /* Enter the SCOPE so that name lookup will be resolved as if we
13469      were in the class definition.  In particular, SCOPE will no
13470      longer be considered a dependent type.  */
13471   push_scope (scope);
13472   /* Look up the declaration.  */
13473   decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13474   /* If all went well, we got a TYPE_DECL for a non-typename.  */
13475   if (!decl 
13476       || TREE_CODE (decl) != TYPE_DECL 
13477       || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13478     {
13479       cp_parser_error (parser, "could not resolve typename type");
13480       type = error_mark_node;
13481     }
13482   else
13483     type = TREE_TYPE (decl);
13484   /* Leave the SCOPE.  */
13485   pop_scope (scope);
13486
13487   return type;
13488 }
13489
13490 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13491    the current context, return the TYPE_DECL.  If TAG_NAME_P is
13492    true, the DECL indicates the class being defined in a class-head,
13493    or declared in an elaborated-type-specifier.
13494
13495    Otherwise, return DECL.  */
13496
13497 static tree
13498 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13499 {
13500   /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13501      the scope of the class, then treat the TEMPLATE_DECL as a
13502      class-name.  For example, in:
13503
13504        template <class T> struct S {
13505          S s;
13506        };
13507
13508      is OK.  
13509
13510      If the TEMPLATE_DECL is being declared as part of a class-head,
13511      the same translation occurs:
13512
13513        struct A { 
13514          template <typename T> struct B;
13515        };
13516
13517        template <typename T> struct A::B {}; 
13518    
13519      Similarly, in a elaborated-type-specifier:
13520
13521        namespace N { struct X{}; }
13522
13523        struct A {
13524          template <typename T> friend struct N::X;
13525        };
13526
13527      */
13528   if (DECL_CLASS_TEMPLATE_P (decl)
13529       && (tag_name_p
13530           || (current_class_type
13531               && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13532                               current_class_type))))
13533     return DECL_TEMPLATE_RESULT (decl);
13534
13535   return decl;
13536 }
13537
13538 /* If too many, or too few, template-parameter lists apply to the
13539    declarator, issue an error message.  Returns TRUE if all went well,
13540    and FALSE otherwise.  */
13541
13542 static bool
13543 cp_parser_check_declarator_template_parameters (parser, declarator)
13544      cp_parser *parser;
13545      tree declarator;
13546 {
13547   unsigned num_templates;
13548
13549   /* We haven't seen any classes that involve template parameters yet.  */
13550   num_templates = 0;
13551
13552   switch (TREE_CODE (declarator))
13553     {
13554     case CALL_EXPR:
13555     case ARRAY_REF:
13556     case INDIRECT_REF:
13557     case ADDR_EXPR:
13558       {
13559         tree main_declarator = TREE_OPERAND (declarator, 0);
13560         return
13561           cp_parser_check_declarator_template_parameters (parser, 
13562                                                           main_declarator);
13563       }
13564
13565     case SCOPE_REF:
13566       {
13567         tree scope;
13568         tree member;
13569
13570         scope = TREE_OPERAND (declarator, 0);
13571         member = TREE_OPERAND (declarator, 1);
13572
13573         /* If this is a pointer-to-member, then we are not interested
13574            in the SCOPE, because it does not qualify the thing that is
13575            being declared.  */
13576         if (TREE_CODE (member) == INDIRECT_REF)
13577           return (cp_parser_check_declarator_template_parameters
13578                   (parser, member));
13579
13580         while (scope && CLASS_TYPE_P (scope))
13581           {
13582             /* You're supposed to have one `template <...>'
13583                for every template class, but you don't need one
13584                for a full specialization.  For example:
13585                
13586                template <class T> struct S{};
13587                template <> struct S<int> { void f(); };
13588                void S<int>::f () {}
13589                
13590                is correct; there shouldn't be a `template <>' for
13591                the definition of `S<int>::f'.  */
13592             if (CLASSTYPE_TEMPLATE_INFO (scope)
13593                 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13594                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13595                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13596               ++num_templates;
13597
13598             scope = TYPE_CONTEXT (scope);
13599           }
13600       }
13601
13602       /* Fall through.  */
13603
13604     default:
13605       /* If the DECLARATOR has the form `X<y>' then it uses one
13606          additional level of template parameters.  */
13607       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13608         ++num_templates;
13609
13610       return cp_parser_check_template_parameters (parser, 
13611                                                   num_templates);
13612     }
13613 }
13614
13615 /* NUM_TEMPLATES were used in the current declaration.  If that is
13616    invalid, return FALSE and issue an error messages.  Otherwise,
13617    return TRUE.  */
13618
13619 static bool
13620 cp_parser_check_template_parameters (parser, num_templates)
13621      cp_parser *parser;
13622      unsigned num_templates;
13623 {
13624   /* If there are more template classes than parameter lists, we have
13625      something like:
13626      
13627        template <class T> void S<T>::R<T>::f ();  */
13628   if (parser->num_template_parameter_lists < num_templates)
13629     {
13630       error ("too few template-parameter-lists");
13631       return false;
13632     }
13633   /* If there are the same number of template classes and parameter
13634      lists, that's OK.  */
13635   if (parser->num_template_parameter_lists == num_templates)
13636     return true;
13637   /* If there are more, but only one more, then we are referring to a
13638      member template.  That's OK too.  */
13639   if (parser->num_template_parameter_lists == num_templates + 1)
13640       return true;
13641   /* Otherwise, there are too many template parameter lists.  We have
13642      something like:
13643
13644      template <class T> template <class U> void S::f();  */
13645   error ("too many template-parameter-lists");
13646   return false;
13647 }
13648
13649 /* Parse a binary-expression of the general form:
13650
13651    binary-expression:
13652      <expr>
13653      binary-expression <token> <expr>
13654
13655    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
13656    to parser the <expr>s.  If the first production is used, then the
13657    value returned by FN is returned directly.  Otherwise, a node with
13658    the indicated EXPR_TYPE is returned, with operands corresponding to
13659    the two sub-expressions.  */
13660
13661 static tree
13662 cp_parser_binary_expression (parser, token_tree_map, fn)
13663      cp_parser *parser;
13664      cp_parser_token_tree_map token_tree_map;
13665      cp_parser_expression_fn fn;
13666 {
13667   tree lhs;
13668
13669   /* Parse the first expression.  */
13670   lhs = (*fn) (parser);
13671   /* Now, look for more expressions.  */
13672   while (true)
13673     {
13674       cp_token *token;
13675       cp_parser_token_tree_map_node *map_node;
13676       tree rhs;
13677
13678       /* Peek at the next token.  */
13679       token = cp_lexer_peek_token (parser->lexer);
13680       /* If the token is `>', and that's not an operator at the
13681          moment, then we're done.  */
13682       if (token->type == CPP_GREATER
13683           && !parser->greater_than_is_operator_p)
13684         break;
13685       /* If we find one of the tokens we want, build the correspoding
13686          tree representation.  */
13687       for (map_node = token_tree_map; 
13688            map_node->token_type != CPP_EOF;
13689            ++map_node)
13690         if (map_node->token_type == token->type)
13691           {
13692             /* Consume the operator token.  */
13693             cp_lexer_consume_token (parser->lexer);
13694             /* Parse the right-hand side of the expression.  */
13695             rhs = (*fn) (parser);
13696             /* Build the binary tree node.  */
13697             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13698             break;
13699           }
13700
13701       /* If the token wasn't one of the ones we want, we're done.  */
13702       if (map_node->token_type == CPP_EOF)
13703         break;
13704     }
13705
13706   return lhs;
13707 }
13708
13709 /* Parse an optional `::' token indicating that the following name is
13710    from the global namespace.  If so, PARSER->SCOPE is set to the
13711    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13712    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13713    Returns the new value of PARSER->SCOPE, if the `::' token is
13714    present, and NULL_TREE otherwise.  */
13715
13716 static tree
13717 cp_parser_global_scope_opt (parser, current_scope_valid_p)
13718      cp_parser *parser;
13719      bool current_scope_valid_p;
13720 {
13721   cp_token *token;
13722
13723   /* Peek at the next token.  */
13724   token = cp_lexer_peek_token (parser->lexer);
13725   /* If we're looking at a `::' token then we're starting from the
13726      global namespace, not our current location.  */
13727   if (token->type == CPP_SCOPE)
13728     {
13729       /* Consume the `::' token.  */
13730       cp_lexer_consume_token (parser->lexer);
13731       /* Set the SCOPE so that we know where to start the lookup.  */
13732       parser->scope = global_namespace;
13733       parser->qualifying_scope = global_namespace;
13734       parser->object_scope = NULL_TREE;
13735
13736       return parser->scope;
13737     }
13738   else if (!current_scope_valid_p)
13739     {
13740       parser->scope = NULL_TREE;
13741       parser->qualifying_scope = NULL_TREE;
13742       parser->object_scope = NULL_TREE;
13743     }
13744
13745   return NULL_TREE;
13746 }
13747
13748 /* Returns TRUE if the upcoming token sequence is the start of a
13749    constructor declarator.  If FRIEND_P is true, the declarator is
13750    preceded by the `friend' specifier.  */
13751
13752 static bool
13753 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13754 {
13755   bool constructor_p;
13756   tree type_decl = NULL_TREE;
13757   bool nested_name_p;
13758
13759   /* Parse tentatively; we are going to roll back all of the tokens
13760      consumed here.  */
13761   cp_parser_parse_tentatively (parser);
13762   /* Assume that we are looking at a constructor declarator.  */
13763   constructor_p = true;
13764   /* Look for the optional `::' operator.  */
13765   cp_parser_global_scope_opt (parser,
13766                               /*current_scope_valid_p=*/false);
13767   /* Look for the nested-name-specifier.  */
13768   nested_name_p 
13769     = (cp_parser_nested_name_specifier_opt (parser,
13770                                             /*typename_keyword_p=*/false,
13771                                             /*check_dependency_p=*/false,
13772                                             /*type_p=*/false)
13773        != NULL_TREE);
13774   /* Outside of a class-specifier, there must be a
13775      nested-name-specifier.  */
13776   if (!nested_name_p && 
13777       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13778        || friend_p))
13779     constructor_p = false;
13780   /* If we still think that this might be a constructor-declarator,
13781      look for a class-name.  */
13782   if (constructor_p)
13783     {
13784       /* If we have:
13785
13786            template <typename T> struct S { S(); }
13787            template <typename T> S<T>::S ();
13788
13789          we must recognize that the nested `S' names a class.
13790          Similarly, for:
13791
13792            template <typename T> S<T>::S<T> ();
13793
13794          we must recognize that the nested `S' names a template.  */
13795       type_decl = cp_parser_class_name (parser,
13796                                         /*typename_keyword_p=*/false,
13797                                         /*template_keyword_p=*/false,
13798                                         /*type_p=*/false,
13799                                         /*check_access_p=*/false,
13800                                         /*check_dependency_p=*/false,
13801                                         /*class_head_p=*/false);
13802       /* If there was no class-name, then this is not a constructor.  */
13803       constructor_p = !cp_parser_error_occurred (parser);
13804     }
13805   /* If we're still considering a constructor, we have to see a `(',
13806      to begin the parameter-declaration-clause, followed by either a
13807      `)', an `...', or a decl-specifier.  We need to check for a
13808      type-specifier to avoid being fooled into thinking that:
13809
13810        S::S (f) (int);
13811
13812      is a constructor.  (It is actually a function named `f' that
13813      takes one parameter (of type `int') and returns a value of type
13814      `S::S'.  */
13815   if (constructor_p 
13816       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13817     {
13818       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13819           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13820           && !cp_parser_storage_class_specifier_opt (parser))
13821         {
13822           if (current_class_type 
13823               && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
13824             /* The constructor for one class cannot be declared inside
13825                another.  */
13826             constructor_p = false;
13827           else
13828             {
13829               tree type;
13830
13831               /* Names appearing in the type-specifier should be looked up
13832                  in the scope of the class.  */
13833               if (current_class_type)
13834                 type = NULL_TREE;
13835               else
13836                 {
13837                   type = TREE_TYPE (type_decl);
13838                   if (TREE_CODE (type) == TYPENAME_TYPE)
13839                     type = cp_parser_resolve_typename_type (parser, type);
13840                   push_scope (type);
13841                 }
13842               /* Look for the type-specifier.  */
13843               cp_parser_type_specifier (parser,
13844                                         CP_PARSER_FLAGS_NONE,
13845                                         /*is_friend=*/false,
13846                                         /*is_declarator=*/true,
13847                                         /*declares_class_or_enum=*/NULL,
13848                                         /*is_cv_qualifier=*/NULL);
13849               /* Leave the scope of the class.  */
13850               if (type)
13851                 pop_scope (type);
13852
13853               constructor_p = !cp_parser_error_occurred (parser);
13854             }
13855         }
13856     }
13857   else
13858     constructor_p = false;
13859   /* We did not really want to consume any tokens.  */
13860   cp_parser_abort_tentative_parse (parser);
13861
13862   return constructor_p;
13863 }
13864
13865 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13866    ATTRIBUTES, and DECLARATOR.  The ACCESS_CHECKS have been deferred;
13867    they must be performed once we are in the scope of the function.
13868
13869    Returns the function defined.  */
13870
13871 static tree
13872 cp_parser_function_definition_from_specifiers_and_declarator
13873   (parser, decl_specifiers, attributes, declarator, access_checks)
13874      cp_parser *parser;
13875      tree decl_specifiers;
13876      tree attributes;
13877      tree declarator;
13878      tree access_checks;
13879 {
13880   tree fn;
13881   bool success_p;
13882
13883   /* Begin the function-definition.  */
13884   success_p = begin_function_definition (decl_specifiers, 
13885                                          attributes, 
13886                                          declarator);
13887
13888   /* If there were names looked up in the decl-specifier-seq that we
13889      did not check, check them now.  We must wait until we are in the
13890      scope of the function to perform the checks, since the function
13891      might be a friend.  */
13892   cp_parser_perform_deferred_access_checks (access_checks);
13893
13894   if (!success_p)
13895     {
13896       /* If begin_function_definition didn't like the definition, skip
13897          the entire function.  */
13898       error ("invalid function declaration");
13899       cp_parser_skip_to_end_of_block_or_statement (parser);
13900       fn = error_mark_node;
13901     }
13902   else
13903     fn = cp_parser_function_definition_after_declarator (parser,
13904                                                          /*inline_p=*/false);
13905
13906   return fn;
13907 }
13908
13909 /* Parse the part of a function-definition that follows the
13910    declarator.  INLINE_P is TRUE iff this function is an inline
13911    function defined with a class-specifier.
13912
13913    Returns the function defined.  */
13914
13915 static tree 
13916 cp_parser_function_definition_after_declarator (parser, 
13917                                                 inline_p)
13918      cp_parser *parser;
13919      bool inline_p;
13920 {
13921   tree fn;
13922   bool ctor_initializer_p = false;
13923   bool saved_in_unbraced_linkage_specification_p;
13924   unsigned saved_num_template_parameter_lists;
13925
13926   /* If the next token is `return', then the code may be trying to
13927      make use of the "named return value" extension that G++ used to
13928      support.  */
13929   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13930     {
13931       /* Consume the `return' keyword.  */
13932       cp_lexer_consume_token (parser->lexer);
13933       /* Look for the identifier that indicates what value is to be
13934          returned.  */
13935       cp_parser_identifier (parser);
13936       /* Issue an error message.  */
13937       error ("named return values are no longer supported");
13938       /* Skip tokens until we reach the start of the function body.  */
13939       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13940         cp_lexer_consume_token (parser->lexer);
13941     }
13942   /* The `extern' in `extern "C" void f () { ... }' does not apply to
13943      anything declared inside `f'.  */
13944   saved_in_unbraced_linkage_specification_p 
13945     = parser->in_unbraced_linkage_specification_p;
13946   parser->in_unbraced_linkage_specification_p = false;
13947   /* Inside the function, surrounding template-parameter-lists do not
13948      apply.  */
13949   saved_num_template_parameter_lists 
13950     = parser->num_template_parameter_lists; 
13951   parser->num_template_parameter_lists = 0;
13952   /* If the next token is `try', then we are looking at a
13953      function-try-block.  */
13954   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13955     ctor_initializer_p = cp_parser_function_try_block (parser);
13956   /* A function-try-block includes the function-body, so we only do
13957      this next part if we're not processing a function-try-block.  */
13958   else
13959     ctor_initializer_p 
13960       = cp_parser_ctor_initializer_opt_and_function_body (parser);
13961
13962   /* Finish the function.  */
13963   fn = finish_function ((ctor_initializer_p ? 1 : 0) | 
13964                         (inline_p ? 2 : 0));
13965   /* Generate code for it, if necessary.  */
13966   expand_body (fn);
13967   /* Restore the saved values.  */
13968   parser->in_unbraced_linkage_specification_p 
13969     = saved_in_unbraced_linkage_specification_p;
13970   parser->num_template_parameter_lists 
13971     = saved_num_template_parameter_lists;
13972
13973   return fn;
13974 }
13975
13976 /* Parse a template-declaration, assuming that the `export' (and
13977    `extern') keywords, if present, has already been scanned.  MEMBER_P
13978    is as for cp_parser_template_declaration.  */
13979
13980 static void
13981 cp_parser_template_declaration_after_export (parser, member_p)
13982      cp_parser *parser;
13983      bool member_p;
13984 {
13985   tree decl = NULL_TREE;
13986   tree parameter_list;
13987   bool friend_p = false;
13988
13989   /* Look for the `template' keyword.  */
13990   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13991     return;
13992       
13993   /* And the `<'.  */
13994   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13995     return;
13996       
13997   /* Parse the template parameters.  */
13998   begin_template_parm_list ();
13999   /* If the next token is `>', then we have an invalid
14000      specialization.  Rather than complain about an invalid template
14001      parameter, issue an error message here.  */
14002   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14003     {
14004       cp_parser_error (parser, "invalid explicit specialization");
14005       parameter_list = NULL_TREE;
14006     }
14007   else
14008     parameter_list = cp_parser_template_parameter_list (parser);
14009   parameter_list = end_template_parm_list (parameter_list);
14010   /* Look for the `>'.  */
14011   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14012   /* We just processed one more parameter list.  */
14013   ++parser->num_template_parameter_lists;
14014   /* If the next token is `template', there are more template
14015      parameters.  */
14016   if (cp_lexer_next_token_is_keyword (parser->lexer, 
14017                                       RID_TEMPLATE))
14018     cp_parser_template_declaration_after_export (parser, member_p);
14019   else
14020     {
14021       decl = cp_parser_single_declaration (parser,
14022                                            member_p,
14023                                            &friend_p);
14024
14025       /* If this is a member template declaration, let the front
14026          end know.  */
14027       if (member_p && !friend_p && decl)
14028         decl = finish_member_template_decl (decl);
14029       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14030         make_friend_class (current_class_type, TREE_TYPE (decl));
14031     }
14032   /* We are done with the current parameter list.  */
14033   --parser->num_template_parameter_lists;
14034
14035   /* Finish up.  */
14036   finish_template_decl (parameter_list);
14037
14038   /* Register member declarations.  */
14039   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14040     finish_member_declaration (decl);
14041
14042   /* If DECL is a function template, we must return to parse it later.
14043      (Even though there is no definition, there might be default
14044      arguments that need handling.)  */
14045   if (member_p && decl 
14046       && (TREE_CODE (decl) == FUNCTION_DECL
14047           || DECL_FUNCTION_TEMPLATE_P (decl)))
14048     TREE_VALUE (parser->unparsed_functions_queues)
14049       = tree_cons (current_class_type, decl, 
14050                    TREE_VALUE (parser->unparsed_functions_queues));
14051 }
14052
14053 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14054    `function-definition' sequence.  MEMBER_P is true, this declaration
14055    appears in a class scope.
14056
14057    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14058    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14059
14060 static tree
14061 cp_parser_single_declaration (parser, 
14062                               member_p,
14063                               friend_p)
14064      cp_parser *parser;
14065      bool member_p;
14066      bool *friend_p;
14067 {
14068   bool declares_class_or_enum;
14069   tree decl = NULL_TREE;
14070   tree decl_specifiers;
14071   tree attributes;
14072   tree access_checks;
14073
14074   /* Parse the dependent declaration.  We don't know yet
14075      whether it will be a function-definition.  */
14076   cp_parser_parse_tentatively (parser);
14077   /* Defer access checks until we know what is being declared.  */
14078   cp_parser_start_deferring_access_checks (parser);
14079   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14080      alternative.  */
14081   decl_specifiers 
14082     = cp_parser_decl_specifier_seq (parser,
14083                                     CP_PARSER_FLAGS_OPTIONAL,
14084                                     &attributes,
14085                                     &declares_class_or_enum);
14086   /* Gather up the access checks that occurred the
14087      decl-specifier-seq.  */
14088   access_checks = cp_parser_stop_deferring_access_checks (parser);
14089   /* Check for the declaration of a template class.  */
14090   if (declares_class_or_enum)
14091     {
14092       if (cp_parser_declares_only_class_p (parser))
14093         {
14094           decl = shadow_tag (decl_specifiers);
14095           if (decl)
14096             decl = TYPE_NAME (decl);
14097           else
14098             decl = error_mark_node;
14099         }
14100     }
14101   else
14102     decl = NULL_TREE;
14103   /* If it's not a template class, try for a template function.  If
14104      the next token is a `;', then this declaration does not declare
14105      anything.  But, if there were errors in the decl-specifiers, then
14106      the error might well have come from an attempted class-specifier.
14107      In that case, there's no need to warn about a missing declarator.  */
14108   if (!decl
14109       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14110           || !value_member (error_mark_node, decl_specifiers)))
14111     decl = cp_parser_init_declarator (parser, 
14112                                       decl_specifiers,
14113                                       attributes,
14114                                       access_checks,
14115                                       /*function_definition_allowed_p=*/false,
14116                                       member_p,
14117                                       /*function_definition_p=*/NULL);
14118   /* Clear any current qualification; whatever comes next is the start
14119      of something new.  */
14120   parser->scope = NULL_TREE;
14121   parser->qualifying_scope = NULL_TREE;
14122   parser->object_scope = NULL_TREE;
14123   /* Look for a trailing `;' after the declaration.  */
14124   if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14125       && cp_parser_committed_to_tentative_parse (parser))
14126     cp_parser_skip_to_end_of_block_or_statement (parser);
14127   /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS.  */
14128   if (cp_parser_parse_definitely (parser))
14129     {
14130       if (friend_p)
14131         *friend_p = cp_parser_friend_p (decl_specifiers);
14132     }
14133   /* Otherwise, try a function-definition.  */
14134   else
14135     decl = cp_parser_function_definition (parser, friend_p);
14136
14137   return decl;
14138 }
14139
14140 /* Parse a functional cast to TYPE.  Returns an expression
14141    representing the cast.  */
14142
14143 static tree
14144 cp_parser_functional_cast (parser, type)
14145      cp_parser *parser;
14146      tree type;
14147 {
14148   tree expression_list;
14149
14150   /* Look for the opening `('.  */
14151   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14152     return error_mark_node;
14153   /* If the next token is not an `)', there are arguments to the
14154      cast.  */
14155   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14156     expression_list = cp_parser_expression_list (parser);
14157   else
14158     expression_list = NULL_TREE;
14159   /* Look for the closing `)'.  */
14160   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14161
14162   return build_functional_cast (type, expression_list);
14163 }
14164
14165 /* MEMBER_FUNCTION is a member function, or a friend.  If default
14166    arguments, or the body of the function have not yet been parsed,
14167    parse them now.  */
14168
14169 static void
14170 cp_parser_late_parsing_for_member (parser, member_function)
14171      cp_parser *parser;
14172      tree member_function;
14173 {
14174   cp_lexer *saved_lexer;
14175
14176   /* If this member is a template, get the underlying
14177      FUNCTION_DECL.  */
14178   if (DECL_FUNCTION_TEMPLATE_P (member_function))
14179     member_function = DECL_TEMPLATE_RESULT (member_function);
14180
14181   /* There should not be any class definitions in progress at this
14182      point; the bodies of members are only parsed outside of all class
14183      definitions.  */
14184   my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14185   /* While we're parsing the member functions we might encounter more
14186      classes.  We want to handle them right away, but we don't want
14187      them getting mixed up with functions that are currently in the
14188      queue.  */
14189   parser->unparsed_functions_queues
14190     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14191
14192   /* Make sure that any template parameters are in scope.  */
14193   maybe_begin_member_template_processing (member_function);
14194
14195   /* If there are default arguments that have not yet been processed,
14196      take care of them now.  */
14197   if (DECL_FUNCTION_MEMBER_P (member_function))
14198     push_nested_class (DECL_CONTEXT (member_function), 1);
14199   cp_parser_late_parsing_default_args (parser, TREE_TYPE (member_function));
14200   if (DECL_FUNCTION_MEMBER_P (member_function))
14201     pop_nested_class ();
14202
14203   /* If the body of the function has not yet been parsed, parse it
14204      now.  */
14205   if (DECL_PENDING_INLINE_P (member_function))
14206     {
14207       tree function_scope;
14208       cp_token_cache *tokens;
14209
14210       /* The function is no longer pending; we are processing it.  */
14211       tokens = DECL_PENDING_INLINE_INFO (member_function);
14212       DECL_PENDING_INLINE_INFO (member_function) = NULL;
14213       DECL_PENDING_INLINE_P (member_function) = 0;
14214       /* If this was an inline function in a local class, enter the scope
14215          of the containing function.  */
14216       function_scope = decl_function_context (member_function);
14217       if (function_scope)
14218         push_function_context_to (function_scope);
14219       
14220       /* Save away the current lexer.  */
14221       saved_lexer = parser->lexer;
14222       /* Make a new lexer to feed us the tokens saved for this function.  */
14223       parser->lexer = cp_lexer_new_from_tokens (tokens);
14224       parser->lexer->next = saved_lexer;
14225       
14226       /* Set the current source position to be the location of the first
14227          token in the saved inline body.  */
14228       cp_lexer_set_source_position_from_token 
14229         (parser->lexer,
14230          cp_lexer_peek_token (parser->lexer));
14231       
14232       /* Let the front end know that we going to be defining this
14233          function.  */
14234       start_function (NULL_TREE, member_function, NULL_TREE,
14235                       SF_PRE_PARSED | SF_INCLASS_INLINE);
14236       
14237       /* Now, parse the body of the function.  */
14238       cp_parser_function_definition_after_declarator (parser,
14239                                                       /*inline_p=*/true);
14240       
14241       /* Leave the scope of the containing function.  */
14242       if (function_scope)
14243         pop_function_context_from (function_scope);
14244       /* Restore the lexer.  */
14245       parser->lexer = saved_lexer;
14246     }
14247
14248   /* Remove any template parameters from the symbol table.  */
14249   maybe_end_member_template_processing ();
14250
14251   /* Restore the queue.  */
14252   parser->unparsed_functions_queues 
14253     = TREE_CHAIN (parser->unparsed_functions_queues);
14254 }
14255
14256 /* TYPE is a FUNCTION_TYPE or METHOD_TYPE which contains a parameter
14257    with an unparsed DEFAULT_ARG.  Parse those default args now.  */
14258
14259 static void
14260 cp_parser_late_parsing_default_args (parser, type)
14261      cp_parser *parser;
14262      tree type;
14263 {
14264   cp_lexer *saved_lexer;
14265   cp_token_cache *tokens;
14266   bool saved_local_variables_forbidden_p;
14267   tree parameters;
14268   
14269   for (parameters = TYPE_ARG_TYPES (type);
14270        parameters;
14271        parameters = TREE_CHAIN (parameters))
14272     {
14273       if (!TREE_PURPOSE (parameters)
14274           || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14275         continue;
14276   
14277        /* Save away the current lexer.  */
14278       saved_lexer = parser->lexer;
14279        /* Create a new one, using the tokens we have saved.  */
14280       tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
14281       parser->lexer = cp_lexer_new_from_tokens (tokens);
14282
14283        /* Set the current source position to be the location of the
14284           first token in the default argument.  */
14285       cp_lexer_set_source_position_from_token 
14286         (parser->lexer, cp_lexer_peek_token (parser->lexer));
14287
14288        /* Local variable names (and the `this' keyword) may not appear
14289           in a default argument.  */
14290       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14291       parser->local_variables_forbidden_p = true;
14292        /* Parse the assignment-expression.  */
14293       TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14294
14295        /* Restore saved state.  */
14296       parser->lexer = saved_lexer;
14297       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14298     }
14299 }
14300
14301 /* Parse the operand of `sizeof' (or a similar operator).  Returns
14302    either a TYPE or an expression, depending on the form of the
14303    input.  The KEYWORD indicates which kind of expression we have
14304    encountered.  */
14305
14306 static tree
14307 cp_parser_sizeof_operand (parser, keyword)
14308      cp_parser *parser;
14309      enum rid keyword;
14310 {
14311   static const char *format;
14312   tree expr = NULL_TREE;
14313   const char *saved_message;
14314   bool saved_constant_expression_p;
14315
14316   /* Initialize FORMAT the first time we get here.  */
14317   if (!format)
14318     format = "types may not be defined in `%s' expressions";
14319
14320   /* Types cannot be defined in a `sizeof' expression.  Save away the
14321      old message.  */
14322   saved_message = parser->type_definition_forbidden_message;
14323   /* And create the new one.  */
14324   parser->type_definition_forbidden_message 
14325     = ((const char *) 
14326        xmalloc (strlen (format) 
14327                 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14328                 + 1 /* `\0' */));
14329   sprintf ((char *) parser->type_definition_forbidden_message,
14330            format, IDENTIFIER_POINTER (ridpointers[keyword]));
14331
14332   /* The restrictions on constant-expressions do not apply inside
14333      sizeof expressions.  */
14334   saved_constant_expression_p = parser->constant_expression_p;
14335   parser->constant_expression_p = false;
14336
14337   /* If it's a `(', then we might be looking at the type-id
14338      construction.  */
14339   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14340     {
14341       tree type;
14342
14343       /* We can't be sure yet whether we're looking at a type-id or an
14344          expression.  */
14345       cp_parser_parse_tentatively (parser);
14346       /* Consume the `('.  */
14347       cp_lexer_consume_token (parser->lexer);
14348       /* Parse the type-id.  */
14349       type = cp_parser_type_id (parser);
14350       /* Now, look for the trailing `)'.  */
14351       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14352       /* If all went well, then we're done.  */
14353       if (cp_parser_parse_definitely (parser))
14354         {
14355           /* Build a list of decl-specifiers; right now, we have only
14356              a single type-specifier.  */
14357           type = build_tree_list (NULL_TREE,
14358                                   type);
14359
14360           /* Call grokdeclarator to figure out what type this is.  */
14361           expr = grokdeclarator (NULL_TREE,
14362                                  type,
14363                                  TYPENAME,
14364                                  /*initialized=*/0,
14365                                  /*attrlist=*/NULL);
14366         }
14367     }
14368
14369   /* If the type-id production did not work out, then we must be
14370      looking at the unary-expression production.  */
14371   if (!expr)
14372     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14373
14374   /* Free the message we created.  */
14375   free ((char *) parser->type_definition_forbidden_message);
14376   /* And restore the old one.  */
14377   parser->type_definition_forbidden_message = saved_message;
14378   parser->constant_expression_p = saved_constant_expression_p;
14379
14380   return expr;
14381 }
14382
14383 /* If the current declaration has no declarator, return true.  */
14384
14385 static bool
14386 cp_parser_declares_only_class_p (cp_parser *parser)
14387 {
14388   /* If the next token is a `;' or a `,' then there is no 
14389      declarator.  */
14390   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14391           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14392 }
14393
14394 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14395    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
14396
14397 static bool
14398 cp_parser_friend_p (decl_specifiers)
14399      tree decl_specifiers;
14400 {
14401   while (decl_specifiers)
14402     {
14403       /* See if this decl-specifier is `friend'.  */
14404       if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14405           && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14406         return true;
14407
14408       /* Go on to the next decl-specifier.  */
14409       decl_specifiers = TREE_CHAIN (decl_specifiers);
14410     }
14411
14412   return false;
14413 }
14414
14415 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
14416    issue an error message indicating that TOKEN_DESC was expected.
14417    
14418    Returns the token consumed, if the token had the appropriate type.
14419    Otherwise, returns NULL.  */
14420
14421 static cp_token *
14422 cp_parser_require (parser, type, token_desc)
14423      cp_parser *parser;
14424      enum cpp_ttype type;
14425      const char *token_desc;
14426 {
14427   if (cp_lexer_next_token_is (parser->lexer, type))
14428     return cp_lexer_consume_token (parser->lexer);
14429   else
14430     {
14431       dyn_string_t error_msg;
14432
14433       /* Format the error message.  */
14434       error_msg = dyn_string_new (0);
14435       dyn_string_append_cstr (error_msg, "expected ");
14436       dyn_string_append_cstr (error_msg, token_desc);
14437       cp_parser_error (parser, error_msg->s);
14438       dyn_string_delete (error_msg);
14439       return NULL;
14440     }
14441 }
14442
14443 /* Like cp_parser_require, except that tokens will be skipped until
14444    the desired token is found.  An error message is still produced if
14445    the next token is not as expected.  */
14446
14447 static void
14448 cp_parser_skip_until_found (parser, type, token_desc)
14449      cp_parser *parser;
14450      enum cpp_ttype type;
14451      const char *token_desc;
14452 {
14453   cp_token *token;
14454   unsigned nesting_depth = 0;
14455
14456   if (cp_parser_require (parser, type, token_desc))
14457     return;
14458
14459   /* Skip tokens until the desired token is found.  */
14460   while (true)
14461     {
14462       /* Peek at the next token.  */
14463       token = cp_lexer_peek_token (parser->lexer);
14464       /* If we've reached the token we want, consume it and 
14465          stop.  */
14466       if (token->type == type && !nesting_depth)
14467         {
14468           cp_lexer_consume_token (parser->lexer);
14469           return;
14470         }
14471       /* If we've run out of tokens, stop.  */
14472       if (token->type == CPP_EOF)
14473         return;
14474       if (token->type == CPP_OPEN_BRACE 
14475           || token->type == CPP_OPEN_PAREN
14476           || token->type == CPP_OPEN_SQUARE)
14477         ++nesting_depth;
14478       else if (token->type == CPP_CLOSE_BRACE 
14479                || token->type == CPP_CLOSE_PAREN
14480                || token->type == CPP_CLOSE_SQUARE)
14481         {
14482           if (nesting_depth-- == 0)
14483             return;
14484         }
14485       /* Consume this token.  */
14486       cp_lexer_consume_token (parser->lexer);
14487     }
14488 }
14489
14490 /* If the next token is the indicated keyword, consume it.  Otherwise,
14491    issue an error message indicating that TOKEN_DESC was expected.
14492    
14493    Returns the token consumed, if the token had the appropriate type.
14494    Otherwise, returns NULL.  */
14495
14496 static cp_token *
14497 cp_parser_require_keyword (parser, keyword, token_desc)
14498      cp_parser *parser;
14499      enum rid keyword;
14500      const char *token_desc;
14501 {
14502   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14503
14504   if (token && token->keyword != keyword)
14505     {
14506       dyn_string_t error_msg;
14507
14508       /* Format the error message.  */
14509       error_msg = dyn_string_new (0);
14510       dyn_string_append_cstr (error_msg, "expected ");
14511       dyn_string_append_cstr (error_msg, token_desc);
14512       cp_parser_error (parser, error_msg->s);
14513       dyn_string_delete (error_msg);
14514       return NULL;
14515     }
14516
14517   return token;
14518 }
14519
14520 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14521    function-definition.  */
14522
14523 static bool 
14524 cp_parser_token_starts_function_definition_p (token)
14525      cp_token *token;
14526 {
14527   return (/* An ordinary function-body begins with an `{'.  */
14528           token->type == CPP_OPEN_BRACE
14529           /* A ctor-initializer begins with a `:'.  */
14530           || token->type == CPP_COLON
14531           /* A function-try-block begins with `try'.  */
14532           || token->keyword == RID_TRY
14533           /* The named return value extension begins with `return'.  */
14534           || token->keyword == RID_RETURN);
14535 }
14536
14537 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14538    definition.  */
14539
14540 static bool
14541 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14542 {
14543   cp_token *token;
14544
14545   token = cp_lexer_peek_token (parser->lexer);
14546   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14547 }
14548
14549 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14550    or none_type otherwise.  */
14551
14552 static enum tag_types
14553 cp_parser_token_is_class_key (token)
14554      cp_token *token;
14555 {
14556   switch (token->keyword)
14557     {
14558     case RID_CLASS:
14559       return class_type;
14560     case RID_STRUCT:
14561       return record_type;
14562     case RID_UNION:
14563       return union_type;
14564       
14565     default:
14566       return none_type;
14567     }
14568 }
14569
14570 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
14571
14572 static void
14573 cp_parser_check_class_key (enum tag_types class_key, tree type)
14574 {
14575   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14576     pedwarn ("`%s' tag used in naming `%#T'",
14577             class_key == union_type ? "union"
14578              : class_key == record_type ? "struct" : "class", 
14579              type);
14580 }
14581                            
14582 /* Look for the `template' keyword, as a syntactic disambiguator.
14583    Return TRUE iff it is present, in which case it will be 
14584    consumed.  */
14585
14586 static bool
14587 cp_parser_optional_template_keyword (cp_parser *parser)
14588 {
14589   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14590     {
14591       /* The `template' keyword can only be used within templates;
14592          outside templates the parser can always figure out what is a
14593          template and what is not.  */
14594       if (!processing_template_decl)
14595         {
14596           error ("`template' (as a disambiguator) is only allowed "
14597                  "within templates");
14598           /* If this part of the token stream is rescanned, the same
14599              error message would be generated.  So, we purge the token
14600              from the stream.  */
14601           cp_lexer_purge_token (parser->lexer);
14602           return false;
14603         }
14604       else
14605         {
14606           /* Consume the `template' keyword.  */
14607           cp_lexer_consume_token (parser->lexer);
14608           return true;
14609         }
14610     }
14611
14612   return false;
14613 }
14614
14615 /* Add tokens to CACHE until an non-nested END token appears.  */
14616
14617 static void
14618 cp_parser_cache_group (cp_parser *parser, 
14619                        cp_token_cache *cache,
14620                        enum cpp_ttype end,
14621                        unsigned depth)
14622 {
14623   while (true)
14624     {
14625       cp_token *token;
14626
14627       /* Abort a parenthesized expression if we encounter a brace.  */
14628       if ((end == CPP_CLOSE_PAREN || depth == 0)
14629           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14630         return;
14631       /* Consume the next token.  */
14632       token = cp_lexer_consume_token (parser->lexer);
14633       /* If we've reached the end of the file, stop.  */
14634       if (token->type == CPP_EOF)
14635         return;
14636       /* Add this token to the tokens we are saving.  */
14637       cp_token_cache_push_token (cache, token);
14638       /* See if it starts a new group.  */
14639       if (token->type == CPP_OPEN_BRACE)
14640         {
14641           cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14642           if (depth == 0)
14643             return;
14644         }
14645       else if (token->type == CPP_OPEN_PAREN)
14646         cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14647       else if (token->type == end)
14648         return;
14649     }
14650 }
14651
14652 /* Begin parsing tentatively.  We always save tokens while parsing
14653    tentatively so that if the tentative parsing fails we can restore the
14654    tokens.  */
14655
14656 static void
14657 cp_parser_parse_tentatively (parser)
14658      cp_parser *parser;
14659 {
14660   /* Enter a new parsing context.  */
14661   parser->context = cp_parser_context_new (parser->context);
14662   /* Begin saving tokens.  */
14663   cp_lexer_save_tokens (parser->lexer);
14664   /* In order to avoid repetitive access control error messages,
14665      access checks are queued up until we are no longer parsing
14666      tentatively.  */
14667   cp_parser_start_deferring_access_checks (parser);
14668 }
14669
14670 /* Commit to the currently active tentative parse.  */
14671
14672 static void
14673 cp_parser_commit_to_tentative_parse (parser)
14674      cp_parser *parser;
14675 {
14676   cp_parser_context *context;
14677   cp_lexer *lexer;
14678
14679   /* Mark all of the levels as committed.  */
14680   lexer = parser->lexer;
14681   for (context = parser->context; context->next; context = context->next)
14682     {
14683       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14684         break;
14685       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14686       while (!cp_lexer_saving_tokens (lexer))
14687         lexer = lexer->next;
14688       cp_lexer_commit_tokens (lexer);
14689     }
14690 }
14691
14692 /* Abort the currently active tentative parse.  All consumed tokens
14693    will be rolled back, and no diagnostics will be issued.  */
14694
14695 static void
14696 cp_parser_abort_tentative_parse (parser)
14697      cp_parser *parser;
14698 {
14699   cp_parser_simulate_error (parser);
14700   /* Now, pretend that we want to see if the construct was
14701      successfully parsed.  */
14702   cp_parser_parse_definitely (parser);
14703 }
14704
14705 /* Stop parsing tentatively.  If a parse error has ocurred, restore the
14706    token stream.  Otherwise, commit to the tokens we have consumed.
14707    Returns true if no error occurred; false otherwise.  */
14708
14709 static bool
14710 cp_parser_parse_definitely (parser)
14711      cp_parser *parser;
14712 {
14713   bool error_occurred;
14714   cp_parser_context *context;
14715
14716   /* Remember whether or not an error ocurred, since we are about to
14717      destroy that information.  */
14718   error_occurred = cp_parser_error_occurred (parser);
14719   /* Remove the topmost context from the stack.  */
14720   context = parser->context;
14721   parser->context = context->next;
14722   /* If no parse errors occurred, commit to the tentative parse.  */
14723   if (!error_occurred)
14724     {
14725       /* Commit to the tokens read tentatively, unless that was
14726          already done.  */
14727       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14728         cp_lexer_commit_tokens (parser->lexer);
14729       if (!parser->context->deferring_access_checks_p)
14730         /* If in the parent context we are not deferring checks, then
14731            these perform these checks now.  */
14732         (cp_parser_perform_deferred_access_checks 
14733          (context->deferred_access_checks));
14734       else
14735         /* Any lookups that were deferred during the tentative parse are
14736            still deferred.  */
14737         parser->context->deferred_access_checks 
14738           = chainon (parser->context->deferred_access_checks,
14739                      context->deferred_access_checks);
14740       return true;
14741     }
14742   /* Otherwise, if errors occurred, roll back our state so that things
14743      are just as they were before we began the tentative parse.  */
14744   else
14745     {
14746       cp_lexer_rollback_tokens (parser->lexer);
14747       return false;
14748     }
14749 }
14750
14751 /* Returns non-zero if we are parsing tentatively.  */
14752
14753 static bool
14754 cp_parser_parsing_tentatively (parser)
14755      cp_parser *parser;
14756 {
14757   return parser->context->next != NULL;
14758 }
14759
14760 /* Returns true if we are parsing tentatively -- but have decided that
14761    we will stick with this tentative parse, even if errors occur.  */
14762
14763 static bool
14764 cp_parser_committed_to_tentative_parse (parser)
14765      cp_parser *parser;
14766 {
14767   return (cp_parser_parsing_tentatively (parser)
14768           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14769 }
14770
14771 /* Returns non-zero iff an error has occurred during the most recent
14772    tentative parse.  */
14773    
14774 static bool
14775 cp_parser_error_occurred (parser)
14776      cp_parser *parser;
14777 {
14778   return (cp_parser_parsing_tentatively (parser)
14779           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14780 }
14781
14782 /* Returns non-zero if GNU extensions are allowed.  */
14783
14784 static bool
14785 cp_parser_allow_gnu_extensions_p (parser)
14786      cp_parser *parser;
14787 {
14788   return parser->allow_gnu_extensions_p;
14789 }
14790
14791 \f
14792
14793 /* The parser.  */
14794
14795 static GTY (()) cp_parser *the_parser;
14796
14797 /* External interface.  */
14798
14799 /* Parse the entire translation unit.  */
14800
14801 int
14802 yyparse ()
14803 {
14804   bool error_occurred;
14805
14806   the_parser = cp_parser_new ();
14807   error_occurred = cp_parser_translation_unit (the_parser);
14808   the_parser = NULL;
14809
14810   return error_occurred;
14811 }
14812
14813 /* Clean up after parsing the entire translation unit.  */
14814
14815 void
14816 free_parser_stacks ()
14817 {
14818   /* Nothing to do.  */
14819 }
14820
14821 /* This variable must be provided by every front end.  */
14822
14823 int yydebug;
14824
14825 #include "gt-cp-parser.h"