OSDN Git Service

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